-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathtest_code_directory.py
More file actions
97 lines (77 loc) · 3.96 KB
/
test_code_directory.py
File metadata and controls
97 lines (77 loc) · 3.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# pylint: disable=redefined-outer-name
from pathlib import Path
import pytest
from codemodder.code_directory import file_line_patterns, match_files
@pytest.fixture(scope="module")
def dir_structure(tmp_path_factory):
tests_dir = tmp_path_factory.mktemp("tests")
samples_dir = tests_dir / "samples"
samples_dir.mkdir()
(samples_dir / "make_request.py").touch()
(samples_dir / "insecure_random.py").touch()
more_samples_dir = samples_dir / "more_samples"
more_samples_dir.mkdir()
(more_samples_dir / "empty_for_testing.py").touch()
(more_samples_dir / "empty_for_testing.txt").touch()
assert len(list(tests_dir.rglob("*"))) == 6
return tests_dir
class TestMatchFiles:
def _assert_expected(self, result_files, expected_files):
assert len(result_files) == len(expected_files)
file_names = [file.name for file in result_files]
file_names.sort()
assert file_names == expected_files
def test_all_py_files_match(self, dir_structure):
expected = ["empty_for_testing.py", "insecure_random.py", "make_request.py"]
files = match_files(dir_structure)
self._assert_expected(files, expected)
def test_match_excluded(self, dir_structure):
expected = ["empty_for_testing.py", "insecure_random.py"]
files = match_files(dir_structure, ["*request.py"])
self._assert_expected(files, expected)
def test_match_included_file_with_line(self, dir_structure):
expected = ["insecure_random.py"]
files = match_files(dir_structure, include_paths=["insecure_random.py:2"])
self._assert_expected(files, expected)
def test_match_excluded_line(self, dir_structure):
expected = ["empty_for_testing.py", "insecure_random.py", "make_request.py"]
files = match_files(dir_structure, exclude_paths=["insecure_random.py:2"])
self._assert_expected(files, expected)
def test_match_included_line_and_glob(self, dir_structure):
expected = ["insecure_random.py"]
files = match_files(dir_structure, include_paths=["insecure*.py:3"])
self._assert_expected(files, expected)
def test_match_excluded_line_and_glob(self, dir_structure):
expected = ["empty_for_testing.py", "insecure_random.py", "make_request.py"]
files = match_files(dir_structure, exclude_paths=["insecure*.py:3"])
self._assert_expected(files, expected)
def test_match_excluded_dir_incorrect_glob(self, dir_structure):
incorrect_glob = "more_samples"
expected = ["empty_for_testing.py", "insecure_random.py", "make_request.py"]
files = match_files(dir_structure, [incorrect_glob])
self._assert_expected(files, expected)
def test_match_excluded_dir_correct_glob(self, dir_structure):
correct_globs = ["**/more_samples/**", "*/more_samples/*"]
for correct_glob in correct_globs:
expected = ["insecure_random.py", "make_request.py"]
files = match_files(dir_structure, [correct_glob])
self._assert_expected(files, expected)
def test_match_excluded_multiple(self, dir_structure):
expected = ["insecure_random.py"]
files = match_files(dir_structure, ["*request.py", "*empty*"])
self._assert_expected(files, expected)
def test_match_included(self, dir_structure):
expected = ["make_request.py"]
files = match_files(dir_structure, include_paths=["*request.py"])
self._assert_expected(files, expected)
def test_match_excluded_precedence_over_included(self, dir_structure):
expected = ["empty_for_testing.py", "insecure_random.py"]
files = match_files(
dir_structure,
exclude_paths=["*request.py"],
include_paths=["*request.py", "*empty*.py", "*random.py"],
)
self._assert_expected(files, expected)
def test_extract_line_from_pattern(self):
lines = file_line_patterns(Path("insecure_random.py"), ["insecure_*.py:3"])
assert lines == [3]