forked from arkq/flake8-requirements
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_pep621.py
87 lines (70 loc) · 2.76 KB
/
test_pep621.py
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
import unittest
from flake8_requirements.checker import Flake8Checker
from flake8_requirements.checker import ModuleSet
from flake8_requirements.checker import memoize
try:
from unittest import mock
builtins_open = 'builtins.open'
except ImportError:
import mock
builtins_open = '__builtin__.open'
class Flake8Options:
known_modules = ""
requirements_file = None
requirements_max_depth = 1
scan_host_site_packages = False
class Pep621TestCase(unittest.TestCase):
content = """
[project]
name="test"
dependencies=["tools==1.0"]
[project.optional-dependencies]
dev = ["dev-tools==1.0"]
"""
def setUp(self):
memoize.mem = {}
def tearDown(self):
Flake8Checker.root_dir = ""
def test_pyproject_custom_mapping_parser(self):
class Options(Flake8Options):
known_modules = {"mylib": ["mylib.drm", "mylib.ex"]}
Flake8Checker.parse_options(Options)
self.assertEqual(
Flake8Checker.known_modules,
{"mylib": ["mylib.drm", "mylib.ex"]},
)
def test_get_pyproject_toml_pep621(self):
with mock.patch(builtins_open, mock.mock_open(read_data=self.content)):
pep621 = Flake8Checker.get_pyproject_toml_pep621()
expected = {
"name": "test",
"dependencies": ["tools==1.0"],
"optional-dependencies": {
"dev": ["dev-tools==1.0"]
},
}
self.assertDictEqual(pep621, expected)
def test_get_pyproject_toml_invalid(self):
content = self.content + "invalid"
with mock.patch(builtins_open, mock.mock_open(read_data=content)):
self.assertDictEqual(Flake8Checker.get_pyproject_toml_pep621(), {})
def test_1st_party(self):
with mock.patch(builtins_open, mock.mock_open()) as m:
m.side_effect = (
IOError("No such file or directory: 'setup.py'"),
IOError("No such file or directory: 'setup.cfg'"),
mock.mock_open(read_data=self.content).return_value,
)
checker = Flake8Checker(None, None)
mods = checker.get_mods_1st_party()
self.assertEqual(mods, ModuleSet({"test": {}}))
def test_3rd_party(self):
with mock.patch(builtins_open, mock.mock_open()) as m:
m.side_effect = (
IOError("No such file or directory: 'setup.py'"),
IOError("No such file or directory: 'setup.cfg'"),
mock.mock_open(read_data=self.content).return_value,
)
checker = Flake8Checker(None, None)
mods = checker.get_mods_3rd_party(False)
self.assertEqual(mods, ModuleSet({"tools": {}, "dev_tools": {}}))