Skip to content

Commit afe4970

Browse files
committed
Add python test
1 parent 055f358 commit afe4970

1 file changed

Lines changed: 121 additions & 0 deletions

File tree

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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+
Unit and functional tests
17+
"""
18+
import logging
19+
import os
20+
import re
21+
import shlex
22+
import subprocess
23+
import sys
24+
import unittest
25+
import glob
26+
27+
28+
class TestBase(unittest.TestCase):
29+
"""Unittest base abstract class"""
30+
31+
BAZEL_BIN_DIR = os.path.join("../../..", "bazel-bin", "test",
32+
"unit", "virtual_include")
33+
BAZEL_TESTLOGS_DIR = os.path.join("../../..", "bazel-testlogs", "test",
34+
"unit", "virtual_include")
35+
36+
@classmethod
37+
def setUpClass(cls):
38+
"""Load module, save environment"""
39+
# Save environment and location
40+
cls.save_env = os.environ
41+
cls.save_cwd = os.getcwd()
42+
# Move to test dir
43+
cls.test_dir = os.path.abspath(os.path.dirname(__file__))
44+
os.chdir(cls.test_dir)
45+
46+
@classmethod
47+
def tearDownClass(cls):
48+
"""Restore environment"""
49+
os.chdir(cls.save_cwd)
50+
os.environ = cls.save_env
51+
52+
def setUp(self):
53+
"""Before every test"""
54+
logging.debug("\n%s", "-" * 70)
55+
56+
def check_command(self, cmd, exit_code=0):
57+
"""Run shell command and check status"""
58+
logging.debug("Running: %s", cmd)
59+
commands = shlex.split(cmd)
60+
with subprocess.Popen(commands,
61+
stdin=subprocess.PIPE,
62+
stdout=subprocess.PIPE,
63+
stderr=subprocess.PIPE) as process:
64+
stdout, stderr = process.communicate()
65+
self.assertEqual(
66+
process.returncode,
67+
exit_code, "\n" + "\n".join([
68+
f"command: {cmd}",
69+
f"stdout: {stdout.decode('utf-8')}",
70+
f"stderr: {stderr.decode('utf-8')}"]))
71+
72+
def grep_file(self, filename, regex):
73+
"""Grep given filename"""
74+
pattern = re.compile(regex)
75+
logging.debug("RegEx = r'%s'", regex)
76+
with open(filename, "r", encoding="utf-8") as fileobj:
77+
for line in fileobj:
78+
if pattern.search(line):
79+
logging.debug(line)
80+
return line
81+
self.fail(f"Could not find r'{regex}' in '{filename}'")
82+
return ""
83+
84+
class TestBasic(TestBase):
85+
"""Basic tests"""
86+
87+
def setUp(self):
88+
"""Before every test: clean Bazel cache"""
89+
super().setUp()
90+
self.check_command("bazel clean")
91+
92+
def test_bazel_plist_path_resolved(self):
93+
"""Test: bazel build :codechecker_virtual_include"""
94+
self.check_command("bazel build //test/unit/virtual_include:codechecker_virtual_include", exit_code=0)
95+
self.check_command("bazel build //test/unit/virtual_include:code_checker_virtual_include", exit_code=0)
96+
plist_files = glob.glob(os.path.join(self.BAZEL_BIN_DIR, "**", "*.plist"), recursive=True)
97+
for plist_file in plist_files:
98+
logging.debug(f"Checking file: {plist_file}")
99+
with open(plist_file, "r") as f:
100+
content = f.read()
101+
if re.search(r"/_virtual_includes/", content):
102+
self.fail(f"Found unresolved symlink within CodeChecker report: {plist_file}")
103+
104+
def setup_logging():
105+
"""Setup logging level for test execution"""
106+
# Enable debug logs for tests if "super verbose" flag is provided
107+
if "-vvv" in sys.argv:
108+
logging.basicConfig(
109+
level=logging.DEBUG,
110+
format="[TEST] %(levelname)5s: %(message)s")
111+
112+
113+
def main():
114+
"""Run unittest"""
115+
setup_logging()
116+
logging.debug("Start testing...")
117+
unittest.main(buffer=True)
118+
119+
120+
if __name__ == "__main__":
121+
main()

0 commit comments

Comments
 (0)