1#!/usr/bin/env python3 2# Copyright 2022 The Pigweed Authors 3# 4# Licensed under the Apache License, Version 2.0 (the "License"); you may not 5# use this file except in compliance with the License. You may obtain a copy of 6# the License at 7# 8# https://www.apache.org/licenses/LICENSE-2.0 9# 10# Unless required by applicable law or agreed to in writing, software 11# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13# License for the specific language governing permissions and limitations under 14# the License. 15"""Tests for bazel_parser.""" 16 17from pathlib import Path 18import tempfile 19import unittest 20 21from pw_presubmit import bazel_parser 22 23# This is a real Bazel failure, trimmed slightly. 24_REAL_TEST_INPUT = """ 25Starting local Bazel server and connecting to it... 26WARNING: --verbose_explanations has no effect when --explain=<file> is not enabled 27Loading: 28Loading: 0 packages loaded 29Analyzing: 1362 targets (197 packages loaded) 30Analyzing: 1362 targets (197 packages loaded, 0 targets configured) 31INFO: Analyzed 1362 targets (304 packages loaded, 15546 targets configured). 32 33INFO: Found 1362 targets... 34[6 / 124] [Prepa] BazelWorkspaceStatusAction stable-status.txt 35[747 / 1,548] Compiling pw_kvs/entry.cc; 0s linux-sandbox ... (3 actions, ... 36ERROR: /usr/local/google/home/mohrr/pigweed/pigweed/pw_kvs/BUILD.bazel:25:14: Compiling pw_kvs/entry.cc failed: (Exit 1): gcc failed: error executing command 37 (cd /usr/local/google/home/mohrr/.cache/bazel/_bazel_mohrr/7e133e1f95b61... \ 38 exec env - \ 39 CPP_TOOL_PATH=external/clang_llvm_12_00_x86_64_linux_gnu_ubuntu_16_04/... \ 40 GCC_TOOL_PATH=external/clang_llvm_12_00_x86_64_linux_gnu_ubuntu_16_04/... \ 41 LD_TOOL_PATH=external/clang_llvm_12_00_x86_64_linux_gnu_ubuntu_16_04/... \ 42 NM_TOOL_PATH=external/clang_llvm_12_00_x86_64_linux_gnu_ubuntu_16_04/... \ 43 OBJDUMP_TOOL_PATH=external/clang_llvm_12_00_x86_64_linux_gnu_ubuntu_16_... \ 44 PATH=/usr/local/google/home/mohrr/pigweed/pigweed/out/host/host_tools:... \ 45 PWD=/proc/self/cwd \ 46 external/rules_cc_toolchain/cc_toolchain/wrappers/posix/gcc -MD -MF bazel-out/k8-fastbuild/bin/pw_kvs/_objs/pw_kvs/entry.pic.d '-frandom-seed=bazel-out/k8-fastbuild/bin/pw_kvs/_objs/pw_kvs/entry.pic.o' -fPIC -iquote . -iquote bazel-out/k8-fastbuild/bin -isystem pw_kvs/public -isystem bazel-out/k8-fastbuild/bin/pw_kvs/public -isystem pw_assert/assert_compatibility_public_overrides -isystem bazel-out/k8-fastbuild/bin/pw_assert/assert_compatibility_public_overrides -isystem pw_assert/public -isystem bazel-out/k8-fastbuild/bin/pw_assert/public -isystem pw_preprocessor/public -isystem bazel-out/k8-fastbuild/bin/pw_preprocessor/public -isystem pw_assert_basic/public -isystem bazel-out/k8-fastbuild/bin/pw_assert_basic/public -isystem pw_assert_basic/public_overrides -isystem bazel-out/k8-fastbuild/bin/pw_assert_basic/public_overrides -isystem pw_string/public -isystem bazel-out/k8-fastbuild/bin/pw_string/public -isystem pw_span/public -isystem bazel-out/k8-fastbuild/bin/pw_span/public -isystem pw_polyfill/public -isystem bazel-out/k8-fastbuild/bin/pw_polyfill/public -isystem pw_status/public -isystem bazel-out/k8-fastbuild/bin/pw_status/public -isystem pw_result/public -isystem bazel-out/k8-fastbuild/bin/pw_result/public -isystem pw_sys_io/public -isystem bazel-out/k8-fastbuild/bin/pw_sys_io/public -isystem pw_bytes/public -isystem bazel-out/k8-fastbuild/bin/pw_bytes/public -isystem pw_containers/public -isystem bazel-out/k8-fastbuild/bin/pw_containers/public -isystem pw_checksum/public -isystem bazel-out/k8-fastbuild/bin/pw_checksum/public -isystem pw_compilation_testing/public -isystem bazel-out/k8-fastbuild/bin/pw_compilation_testing/public -isystem pw_log/public -isystem bazel-out/k8-fastbuild/bin/pw_log/public -isystem pw_log_basic/public -isystem bazel-out/k8-fastbuild/bin/pw_log_basic/public -isystem pw_log_basic/public_overrides -isystem bazel-out/k8-fastbuild/bin/pw_log_basic/public_overrides -isystem pw_stream/public -isystem bazel-out/k8-fastbuild/bin/pw_stream/public -nostdinc++ -nostdinc -isystemexternal/clang_llvm_12_00_x86_64_linux_gnu_ubuntu_16_04/include/c++/v1 -isystemexternal/debian_stretch_amd64_sysroot/usr/local/include -isystemexternal/debian_stretch_amd64_sysroot/usr/include/x86_64-linux-gnu -isystemexternal/debian_stretch_amd64_sysroot/usr/include -isystemexternal/clang_llvm_12_00_x86_64_linux_gnu_ubuntu_16_04/lib/clang/12.0.0 -isystemexternal/clang_llvm_12_00_x86_64_linux_gnu_ubuntu_16_04/lib/clang/12.0.0/include -fdata-sections -ffunction-sections -no-canonical-prefixes -Wno-builtin-macro-redefined '-D__DATE__="redacted"' '-D__TIMESTAMP__="redacted"' '-D__TIME__="redacted"' -xc++ --sysroot external/debian_stretch_amd64_sysroot -O0 -fPIC -g -fno-common -fno-exceptions -ffunction-sections -fdata-sections -Wall -Wextra -Werror '-Wno-error=cpp' '-Wno-error=deprecated-declarations' -Wno-private-header '-std=c++17' -fno-rtti -Wnon-virtual-dtor -Wno-register -c pw_kvs/entry.cc -o bazel-out/k8-fastbuild/bin/pw_kvs/_objs/pw_kvs/entry.pic.o) 47# Configuration: 752863e407a197a5b9da05cfc572e7013efd6958e856cee61d2fa474ed... 48# Execution platform: @local_config_platform//:host 49 50Use --sandbox_debug to see verbose messages from the sandbox and retain the sandbox build root for debugging 51pw_kvs/entry.cc:49:20: error: no member named 'Dat' in 'pw::Status' 52 return Status::Dat aLoss(); 53 ~~~~~~~~^ 54pw_kvs/entry.cc:49:23: error: expected ';' after return statement 55 return Status::Dat aLoss(); 56 ^ 57 ; 582 errors generated. 59INFO: Elapsed time: 5.662s, Critical Path: 1.01s 60INFO: 12 processes: 12 internal. 61FAILED: Build did NOT complete successfully 62FAILED: Build did NOT complete successfully 63""" 64 65_REAL_TEST_SUMMARY = """ 66ERROR: /usr/local/google/home/mohrr/pigweed/pigweed/pw_kvs/BUILD.bazel:25:14: Compiling pw_kvs/entry.cc failed: (Exit 1): gcc failed: error executing command 67# Execution platform: @local_config_platform//:host 68pw_kvs/entry.cc:49:20: error: no member named 'Dat' in 'pw::Status' 69 return Status::Dat aLoss(); 70 ~~~~~~~~^ 71pw_kvs/entry.cc:49:23: error: expected ';' after return statement 72 return Status::Dat aLoss(); 73 ^ 74 ; 752 errors generated. 76""" 77 78_STOP = 'INFO:\n' 79 80# pylint: disable=attribute-defined-outside-init 81 82 83class TestBazelParser(unittest.TestCase): 84 """Test bazel_parser.""" 85 86 def _run(self, contents: str) -> None: 87 with tempfile.TemporaryDirectory() as tempdir: 88 path = Path(tempdir) / 'foo' 89 90 with path.open('w') as outs: 91 outs.write(contents) 92 93 self.output = bazel_parser.parse_bazel_stdout(path) 94 95 def test_simple(self) -> None: 96 error = 'ERROR: abc\nerror 1\nerror2\n' 97 self._run('[0/10] foo\n[1/10] bar\n' + error + _STOP) 98 self.assertEqual(error.strip(), self.output.strip()) 99 100 def test_path(self) -> None: 101 error_in = 'ERROR: abc\n PATH=... \\\nerror 1\nerror2\n' 102 error_out = 'ERROR: abc\nerror 1\nerror2\n' 103 self._run('[0/10] foo\n[1/10] bar\n' + error_in + _STOP) 104 self.assertEqual(error_out.strip(), self.output.strip()) 105 106 def test_unterminated(self) -> None: 107 error = 'ERROR: abc\nerror 1\nerror 2\n' 108 self._run('[0/10] foo\n[1/10] bar\n' + error) 109 self.assertEqual(error.strip(), self.output.strip()) 110 111 def test_failure(self) -> None: 112 self._run(_REAL_TEST_INPUT) 113 self.assertEqual(_REAL_TEST_SUMMARY.strip(), self.output.strip()) 114 115 116if __name__ == '__main__': 117 unittest.main() 118