File: test_detection.py

package info (click to toggle)
python-requirements-detector 1.3.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 472 kB
  • sloc: python: 2,096; makefile: 13; sh: 1
file content (124 lines) | stat: -rw-r--r-- 4,467 bytes parent folder | download
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
from pathlib import Path
from unittest import TestCase

from requirements_detector.detect import (
    CouldNotParseRequirements,
    from_pyproject_toml,
    from_requirements_blob,
    from_requirements_dir,
    from_requirements_txt,
    from_setup_py,
)
from requirements_detector.requirement import DetectedRequirement

_TEST_DIR = Path(__file__).parent / 'detection'


class DependencyDetectionTest(TestCase):
    def _expected(self, *requirements):
        return sorted(DetectedRequirement.parse(req) for req in requirements)

    def test_requirements_txt_parsing(self):
        filepath = _TEST_DIR / 'test1/requirements.txt'
        dependencies = from_requirements_txt(filepath)

        expected = self._expected(
            "amqp!=1.0.13",
            "Django>=1.5.0",
            "six<1.4,>=1.3.0",
            "South==0.8.2",
        )

        self.assertEqual(expected, sorted(dependencies))

    def test_requirements_dir_parsing(self):
        filepath = _TEST_DIR / "test2/requirements"
        dependencies = from_requirements_dir(filepath)

        expected = self._expected(
            "amqp==1.0.13",
            "anyjson==0.3.3",
            "Django==1.5.2",
            "South==0.8.2",
        )

        self.assertEqual(expected, sorted(dependencies))

    def test_requirements_blob_parsing(self):
        filepath = _TEST_DIR / "test3"
        dependencies = from_requirements_blob(filepath)

        expected = self._expected(
            "amqp==1.0.13",
            "anyjson==0.3.3",
            "django-gubbins==1.1.2",
        )

        self.assertEqual(expected, sorted(dependencies))

    def test_invalid_requirements_txt(self):
        filepath = _TEST_DIR / "test5/invalid_requirements.txt"
        dependencies = from_requirements_txt(filepath)
        expected = self._expected("django<1.6", "django")
        self.assertEqual(expected, sorted(dependencies))

    def test_requirements_txt(self):
        filepath = _TEST_DIR / "test6/requirements.txt"
        reqs = from_requirements_txt(filepath)
        self.assertTrue(len(reqs) > 0)

    def test_poetry_requirements_txt(self):
        # tests parsing a requirements format generated by 'poetry export'
        filepath = _TEST_DIR / "test7/poetry-format-requirements.txt"
        reqs = from_requirements_txt(str(filepath))
        self.assertTrue(len(reqs) > 0)

    def test_pyproject_toml(self):
        # tests parsing a pyproject.toml file
        filepath = _TEST_DIR / "test8/pyproject.toml"
        reqs = from_pyproject_toml(str(filepath))
        self.assertTrue(len(reqs) > 0)

        names = [req.name for req in reqs]
        self.assertIn('click', names)
        self.assertIn('twine', names)
        self.assertIn('requests', names)
        self.assertIn('pytest', names)

        pyroma = [req for req in reqs if req.name == 'pyroma'][0]
        self.assertEqual(2, len(pyroma.version_specs[0]))
        self.assertEqual('pyroma>=2.4', str(pyroma))

    def _test_setup_py(self, setup_py_file, *expected):
        filepath = _TEST_DIR / 'test4' / setup_py_file
        dependencies = from_setup_py(str(filepath))
        expected = self._expected(*expected)
        self.assertEqual(expected, sorted(dependencies))

    def _test_setup_py_not_parseable(self, setup_py_file):
        filepath = _TEST_DIR / 'test4' / setup_py_file
        self.assertRaises(CouldNotParseRequirements, from_setup_py, filepath)

    def test_simple_setup_py_parsing(self):
        self._test_setup_py("simple.py", "Django==1.5.0", "django-gubbins==1.1.2")

    def test_setup_py_reqs_defined_in_file_parsing(self):
        self._test_setup_py("in_file.py", "Django==1.5.0", "django-gubbins==1.1.2")

    def test_setup_py_tuple(self):
        self._test_setup_py("tuple.py", "Django==1.5.0", "django-gubbins==1.1.2")

    def test_subscript_assign(self):
        self._test_setup_py("subscript_assign.py", "Django==1.5.0", "django-gubbins==1.1.2")

    def test_utf8_setup_py(self):
        self._test_setup_py("utf8.py", "Django==1.5.0", "django-gubbins==1.1.2")

    def test_requires_setup_py(self):
        self._test_setup_py("uses_requires.py", "Django==1.5.0", "django-gubbins==1.1.2")

    def test_requires_and_install_requires_setup_py(self):
        self._test_setup_py("uses_requires_and_install_requires.py", "Django==1.5.0", "django-gubbins==1.1.2")

    def test_callable_install_requires(self):
        self._test_setup_py_not_parseable("callable.py")