|
| 1 | +# Copyright 2023 Ericsson AB |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +""" |
| 16 | +Tests regex resolution from remote executor absolute path |
| 17 | +to local relative paths |
| 18 | +""" |
| 19 | +import os |
| 20 | +import shutil |
| 21 | +import unittest |
| 22 | +from typing import Dict |
| 23 | +from common.base import TestBase |
| 24 | +from src.codechecker_script import fix_bazel_paths |
| 25 | + |
| 26 | +PATH_RESOLUTION: Dict[str, str] = { |
| 27 | + # {Remote execution absolute path}: {project relative path} |
| 28 | + "/worker/build/5d2c60d87885b089/root/test/unit/legacy/src/lib.cc": "test/unit/legacy/src/lib.cc", |
| 29 | + "/worker/build/a0ed5e04f7c3b444/root/test/unit/legacy/src/ctu.cc": "test/unit/legacy/src/ctu.cc", |
| 30 | + "/worker/build/a0ed5e04f7c3b444/root/test/unit/legacy/src/fail.cc": "test/unit/legacy/src/fail.cc", |
| 31 | + # This resolution is impossible, because "test_inc" => "inc" cannot be resolved |
| 32 | + # "/worker/build/28e82627f5078a2d/root/bazel-out/k8-fastbuild/bin/test/unit/virtual_include/_virtual_includes/test_inc/zeroDiv.h": "test/unit/virtual_include/inc/zeroDiv.h" |
| 33 | +} |
| 34 | + |
| 35 | + |
| 36 | +class TestTemplate(TestBase): |
| 37 | + """Test regex resolution of remote execution paths""" |
| 38 | + |
| 39 | + # Set working directory |
| 40 | + __test_path__ = os.path.dirname(os.path.abspath(__file__)) |
| 41 | + BAZEL_BIN_DIR = os.path.join( |
| 42 | + "../../..", "bazel-bin", "test", "unit", "plist_res" |
| 43 | + ) |
| 44 | + BAZEL_TESTLOGS_DIR = os.path.join( |
| 45 | + "../../..", "bazel-testlogs", "test", "unit", "plist_res" |
| 46 | + ) |
| 47 | + dir = os.path.dirname(os.path.abspath(__file__)) + "/tmp" |
| 48 | + |
| 49 | + def setUp(self): |
| 50 | + """Write absolute paths to action directory""" |
| 51 | + if os.path.exists("tmp"): |
| 52 | + try: |
| 53 | + shutil.rmtree("tmp") |
| 54 | + except Exception as e: |
| 55 | + self.fail(f"Failed to clean up the existing tmp directory {e}") |
| 56 | + os.mkdir("tmp") |
| 57 | + # shutil.copy("test-unit-legacy-src-fail.cc_clangsa.plist", "tmp") |
| 58 | + # shutil.copy("test-unit-legacy-src-lib.cc_clangsa.plist", "tmp") |
| 59 | + with open("tmp/test.txt", "w") as f: |
| 60 | + for abs, _ in PATH_RESOLUTION.items(): |
| 61 | + f.write(abs + "\n") |
| 62 | + super().setUp() |
| 63 | + |
| 64 | + def tearDown(self): |
| 65 | + """Remove test files""" |
| 66 | + if os.path.exists("tmp"): |
| 67 | + try: |
| 68 | + shutil.rmtree("tmp") |
| 69 | + except: |
| 70 | + pass |
| 71 | + return super().tearDown() |
| 72 | + |
| 73 | + def test_remote_worker_path_resolution(self): |
| 74 | + """ |
| 75 | + Test: Resolve absolute path of remote worker |
| 76 | + to a relative path of the original project |
| 77 | + """ |
| 78 | + fix_bazel_paths(self.__test_path__ + "/tmp") # type: ignore |
| 79 | + with open("tmp/test.txt", "r") as f: |
| 80 | + for _, res in PATH_RESOLUTION.items(): |
| 81 | + # FIXME: change to assertEqual |
| 82 | + self.assertNotEqual(f.readline().strip(), res) |
| 83 | + |
| 84 | + |
| 85 | +if __name__ == "__main__": |
| 86 | + unittest.main(buffer=True) |
0 commit comments