Skip to content

Commit c0fdb6b

Browse files
committed
refactor(tests)!: update tests to the new docker environment
1 parent 4e89638 commit c0fdb6b

File tree

3 files changed

+3
-79
lines changed

3 files changed

+3
-79
lines changed

tests/conftest.py

Lines changed: 2 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
"""Defines shared fixtures and hooks for pytest."""
22

3-
import hashlib
4-
import json
53
import logging
64
import os
75
import shutil
@@ -10,79 +8,17 @@
108

119
import pytest
1210

13-
from codesectools.utils import USER_CACHE_DIR
14-
1511
# Fix: I/O operation on closed (https://github.com/pallets/click/issues/824)
1612
logging.getLogger("matplotlib").setLevel(logging.ERROR)
1713

18-
test_type = os.environ.get("TEST_TYPE")
19-
state_file = Path(f".pytest_cache/state_{test_type}.json")
20-
21-
22-
def gen_state() -> dict[str, str]:
23-
"""Generate a state dictionary of source file paths and their SHA256 hashes.
24-
25-
Monitors .py files in 'codesectools' and 'tests' directories.
26-
"""
27-
state = {}
28-
for directory in ["codesectools", "tests"]:
29-
for code_path in Path(directory).rglob("*.py"):
30-
path = str(code_path)
31-
file_hash = hashlib.sha256(code_path.read_bytes()).hexdigest()
32-
state[path] = file_hash
33-
34-
return state
35-
36-
37-
def source_code_changed() -> bool:
38-
"""Check if monitored source code has changed since the last successful test run.
39-
40-
Compares the current state with a saved state in '.pytest_cache/state.json'.
41-
"""
42-
if not state_file.is_file():
43-
return True
44-
45-
with state_file.open("r") as f:
46-
try:
47-
old_state = json.load(f)
48-
except json.JSONDecodeError:
49-
return True
50-
51-
new_state = gen_state()
52-
53-
return new_state != old_state
54-
5514

5615
def pytest_sessionstart(session: pytest.Session) -> None:
57-
"""Pytest hook that runs at the beginning of a test session.
58-
59-
Skips the entire test session if no source files have changed.
60-
"""
61-
if USER_CACHE_DIR.is_dir():
62-
for child in USER_CACHE_DIR.iterdir():
63-
if child.is_file():
64-
child.unlink()
65-
elif child.is_dir():
66-
shutil.rmtree(child)
67-
68-
if not source_code_changed():
69-
pytest.exit("No changes in source code, skipping test session.", returncode=0)
16+
"""Initialize the test session by copying test codes to a temporary directory."""
17+
shutil.copytree(Path("tests/testcodes"), Path("/tmp/tests/testcodes"))
7018

7119

7220
@pytest.fixture(autouse=True, scope="session")
7321
def constant_random() -> GeneratorType:
7422
"""Set a constant random seed for reproducible tests."""
7523
os.environ["CONSTANT_RANDOM"] = os.urandom(16).hex()
7624
yield
77-
78-
79-
def pytest_sessionfinish(session: pytest.Session) -> None:
80-
"""Pytest hook that runs at the end of a test session.
81-
82-
Saves the current source code state if the test session was successful.
83-
"""
84-
if session.testscollected > 0 and session.testsfailed == 0:
85-
new_state = gen_state()
86-
state_file.parent.mkdir(exist_ok=True, parents=True)
87-
with state_file.open("w") as f:
88-
json.dump(new_state, f, indent=2)

tests/test_all_sasts.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
"""Test the 'allsast' command integration."""
22

33
import logging
4-
import os
54
from pathlib import Path
65
from types import GeneratorType
76

@@ -14,11 +13,6 @@
1413
from codesectools.sasts.all.sast import AllSAST
1514
from codesectools.utils import run_command
1615

17-
if os.environ.get("TEST_TYPE") == "no-sast":
18-
pytest.skip(
19-
"Skipping SAST tools testing in no-sast environment", allow_module_level=True
20-
)
21-
2216
all_sast = AllSAST()
2317

2418

tests/test_sasts.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
"""Test SAST integration functionalities."""
22

33
import logging
4-
import os
54
import tempfile
65
from pathlib import Path
76
from types import GeneratorType
@@ -19,11 +18,6 @@
1918
from codesectools.sasts import SASTS_ALL
2019
from codesectools.utils import run_command
2120

22-
if os.environ.get("TEST_TYPE") == "no-sast":
23-
pytest.skip(
24-
"Skipping SAST tools testing in no-sast environment", allow_module_level=True
25-
)
26-
2721

2822
@pytest.fixture(autouse=True, scope="module")
2923
def update_sast_module_state() -> GeneratorType:
@@ -44,7 +38,7 @@ def update_sast_module_state() -> GeneratorType:
4438

4539
runner = CliRunner(env={"COLUMNS": "200"})
4640

47-
TEST_CODES_DIR = Path("tests/testcodes").resolve()
41+
TEST_CODES_DIR = Path("/tmp/tests/testcodes").resolve()
4842
TEST_CODES = {
4943
"java": {"build_command": "javac {filename}"},
5044
"c": {"build_command": "bear -- g++ {filename}"},

0 commit comments

Comments
 (0)