-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtest_dependency_parser.py
More file actions
196 lines (159 loc) · 7.15 KB
/
Copy pathtest_dependency_parser.py
File metadata and controls
196 lines (159 loc) · 7.15 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
from pathlib import Path
from unittest.mock import Mock, patch
import pytest
from twyn.dependency_parser import (
DockerComposeParser,
PackageLockJsonParser,
PnpmLockParser,
PoetryLockParser,
RequirementsTxtParser,
UvLockParser,
)
from twyn.dependency_parser.parsers.abstract_parser import AbstractParser
from twyn.dependency_parser.parsers.dockerfile_parser import DockerfileParser
from twyn.dependency_parser.parsers.yarn_lock_parser import YarnLockParser
class TestAbstractParser:
class TemporaryParser(AbstractParser):
"""Subclass of AbstractParser to test methods."""
def parse(self) -> set[str]:
self._read()
return set()
@patch("pathlib.Path.stat")
@patch("pathlib.Path.exists")
@patch("pathlib.Path.is_file")
def test_file_exists(self, mock_is_file: Mock, mock_exists: Mock, mock_stat: Mock) -> None:
mock_exists.return_value = True
mock_is_file.return_value = True
mock_stat_result = Mock()
mock_stat_result.st_size = 100
mock_stat.return_value = mock_stat_result
parser = self.TemporaryParser("fake_path.txt")
assert parser.file_exists() is True
@patch("pathlib.Path.stat")
@patch("pathlib.Path.exists")
@patch("pathlib.Path.is_file")
@pytest.mark.parametrize(
("file_exists", "is_file", "file_size"),
[
(False, False, 100),
(True, False, 100),
(True, True, 0),
],
)
def test_raise_for_valid_file(
self,
mock_is_file: Mock,
mock_exists: Mock,
mock_stat: Mock,
file_exists: bool,
is_file: bool,
file_size: int,
) -> None:
mock_exists.return_value = file_exists
mock_is_file.return_value = is_file
mock_stat_result = Mock()
mock_stat_result.st_size = file_size
mock_stat.return_value = mock_stat_result
parser = self.TemporaryParser("fake_path.txt")
assert parser.file_exists() is False
class TestRequirementsTxtParser:
def test_parse_requirements_txt_file(self, requirements_txt_file: Path) -> None:
parser = RequirementsTxtParser(file_path=requirements_txt_file)
assert parser.parse() == {"South", "pycrypto", "Flask", "django", "requests", "urllib3"}
class TestLockParser:
def test_parse_poetry_lock_file_lt_1_5(self, poetry_lock_file_lt_1_5: Path) -> None:
parser = PoetryLockParser(file_path=poetry_lock_file_lt_1_5)
assert parser.parse() == {"charset-normalizer", "flake8", "mccabe"}
def test_parse_poetry_lock_file_ge_1_5(self, poetry_lock_file_ge_1_5: Path) -> None:
parser = PoetryLockParser(file_path=poetry_lock_file_ge_1_5)
assert parser.parse() == {"charset-normalizer", "flake8", "mccabe"}
def test_uv_lock_parser(self, uv_lock_file: Path) -> None:
parser = UvLockParser(file_path=uv_lock_file)
assert parser.parse() == {"annotated-types", "anyio", "argcomplete"}
class TestPackageLockJsonParser:
def test_parse_package_lock_json_file_v1(self, package_lock_json_file_v1: Path) -> None:
parser = PackageLockJsonParser(file_path=package_lock_json_file_v1)
result = parser.parse()
# Should find all dependencies, including nested ones, from the v1 fixture
assert "express" in result
assert "body-parser" in result
assert "debug" in result
assert "test-project" not in result
def test_parse_package_lock_json_file_v2(self, package_lock_json_file_v2: Path) -> None:
parser = PackageLockJsonParser(file_path=package_lock_json_file_v2)
result = parser.parse()
# Should find all dependencies, including nested ones, from the fixture
# Top-level: express
# Nested: body-parser (under express), debug (under body-parser)
assert "express" in result
assert "body-parser" in result
assert "debug" in result
# Should not include the project name itself
assert "test-project" not in result
def test_parse_package_lock_json_file_v3(self, package_lock_json_file_v3: Path) -> None:
parser = PackageLockJsonParser(file_path=package_lock_json_file_v3)
result = parser.parse()
# Should find all dependencies, including nested ones, from the v3 fixture
assert "express" in result
assert "body-parser" in result
assert "debug" in result
assert "test-project" not in result
class TestYarnLockParser:
def test_parse_yarn_lock_v1(self, yarn_lock_file_v1: Path) -> None:
parser = YarnLockParser(file_path=str(yarn_lock_file_v1))
assert parser.parse() == {"lodash", "react", "react-dom", "@babel/helper-plugin-utils"}
def test_parse_yarn_lock_v2(self, yarn_lock_file_v2: Path) -> None:
parser = YarnLockParser(file_path=str(yarn_lock_file_v2))
assert parser.parse() == {"dependencies", "react-dom", "lodash", "version", "cacheKey", "resolution", "react"}
class TestPnpmLockParser:
def test_parse_pnpm_lock_v9(self, pnpm_lock_file_v9: Path) -> None:
parser = PnpmLockParser(file_path=str(pnpm_lock_file_v9))
result = parser.parse()
# Should find all dependencies from the v9 fixture
expected_packages = {
"react",
"react-dom",
"lodash",
"@babel/core",
"@babel/helper-plugin-utils",
"express",
"debug",
"typescript",
"@types/node",
}
assert expected_packages == result
# Should not include workspace projects
assert "test-project" not in result
assert "my-workspace" not in result
class TestDockerfileParser:
def test_dockefile_parser(self, dockerfile: Path) -> None:
parser = DockerfileParser(file_path=str(dockerfile))
assert parser.parse() == {
"internal-registry.company.com/backend-team/alpine",
"my-registry.io/my-app",
"my-registry/node",
"prod-alpine",
"internal-repo.loc/python",
"my-registry/nginx",
"nginx",
}
class TestDockerComposeParser:
def test_docker_compose_parser(self, docker_compose_file: Path) -> None:
parser = DockerComposeParser(file_path=str(docker_compose_file))
result = parser.parse()
# Should find all services with explicit images
assert "nginx" in result
assert "my-registry.io/backend/python" in result
assert "default-image" in result # Variable with default resolved
assert "alpine" in result # Image with digest
assert "internal-registry.company.com:5000/team/service" in result
# Should NOT include services with only build context (no image)
# Should NOT include services with unresolved variables
assert len(result) == 5
def test_docker_compose_parser_legacy_format(self, docker_compose_file_legacy: Path) -> None:
parser = DockerComposeParser(file_path=str(docker_compose_file_legacy))
result = parser.parse()
# Should find services in v1 format (without services key)
assert "nginx" in result
assert "mysql" in result
assert len(result) == 2