Debian Codegraph @

test/test_setup.py

raw
1 # test/test_setup.py
2 # Part of ‘dput’, a Debian package upload toolkit.
3 #
4 # This is free software, and you are welcome to redistribute it under
5 # certain conditions; see the end of this file for copyright
6 # information, grant of license, and disclaimer of warranty.
7
8 """ Unit tests for Python Setuptools configuration module ‘setup’. """
9
10 import debian.debian_support
11 import packaging.version
12 import testscenarios
13 import testtools
14
15 import setup
16
17 from .helper import make_expected_error_context
18
19
20 class packaging_version_from_debian_version_TestCase(
21 testscenarios.WithScenarios,
22 testtools.TestCase):
23 """ Test cases for function `packaging_version_from_debian_version`. """
24
25 function_to_test = staticmethod(
26 setup.packaging_version_from_debian_version)
27
28 epoch_scenarios = [
29 ('no-epoch', {
30 'test_version_prefix': "",
31 }),
32 ('with-epoch', {
33 'test_version_prefix': "2:",
34 }),
35 ('epoch-invalid', {
36 'test_version_prefix': "beans:",
37 'expected_error': ValueError,
38 }),
39 ('prefix-invalid', {
40 'test_version_prefix': "b0gUs",
41 'expected_error': ValueError,
42 }),
43 ]
44
45 release_version_scenarios = [
46 ('simple-one', {
47 'test_version_release': "3",
48 'expected_packaging_version_main': "3",
49 }),
50 ('simple-two', {
51 'test_version_release': "1.3",
52 'expected_packaging_version_main': "1.3",
53 }),
54 ('simple-three', {
55 'test_version_release': "1.3.5",
56 'expected_packaging_version_main': "1.3.5",
57 }),
58 ('release-invalid', {
59 'test_version_release': "b@gU$",
60 'expected_error': ValueError,
61 }),
62 ]
63
64 prerelease_scenarios = [
65 ('no-prerelease', {
66 'test_version_prerelease': "",
67 'expected_packaging_version_prerelease': "",
68 }),
69 ('prerelease-alpha1', {
70 'test_version_prerelease': "~alpha1",
71 'expected_packaging_version_prerelease': "alpha1",
72 }),
73 ('prerelease-a1', {
74 'test_version_prerelease': "~a1",
75 'expected_packaging_version_prerelease': "a1",
76 }),
77 ('prerelease-rc1', {
78 'test_version_prerelease': "~rc1",
79 'expected_packaging_version_prerelease': "rc1",
80 }),
81 ('prerelease-lorem1', {
82 'test_version_prerelease': "~lorem1",
83 'expected_error': ValueError,
84 }),
85 ('prerelease-invalid', {
86 'test_version_prerelease': "~eggs",
87 'expected_error': ValueError,
88 }),
89 ('suffix-invalid', {
90 'test_version_prerelease': "b0gUs",
91 'expected_error': ValueError,
92 }),
93 ]
94
95 annotation_scenarios = [
96 ('no-annotation', {
97 'test_version_annotation': "",
98 }),
99 ('annotation-real-version', {
100 'test_version_annotation': "+really1.2",
101 }),
102 ('annotation-nmu', {
103 'test_version_annotation': "+nmu2",
104 }),
105 ('annotation-dfsg', {
106 'test_version_annotation': "+dfsg2",
107 }),
108 ('annotation-binary', {
109 'test_version_annotation': "+b2",
110 }),
111 ('annotation-ci-chatter', {
112 'test_version_annotation': "+salsaci+20250216+16",
113 }),
114 ('annotation-invalid', {
115 'test_version_annotation': "+b@gU$",
116 'expected_error': ValueError,
117 }),
118 ]
119
120 scenarios = testscenarios.multiply_scenarios(
121 epoch_scenarios,
122 release_version_scenarios,
123 prerelease_scenarios,
124 annotation_scenarios,
125 )
126
127 for (scenario_name, scenario) in scenarios:
128 try:
129 test_version = (
130 debian.debian_support.Version(
131 "{prefix}{release}{prerelease}{annotation}".format(
132 prefix=scenario['test_version_prefix'],
133 release=scenario['test_version_release'],
134 prerelease=scenario['test_version_prerelease'],
135 annotation=scenario['test_version_annotation'],
136 ))
137 )
138 except ValueError:
139 test_version = object()
140 scenario['test_version'] = test_version
141 scenario['test_args'] = [scenario['test_version']]
142 if 'expected_error' not in scenario:
143 scenario['expected_result'] = (
144 "{main}{suffix}".format(
145 main=scenario['expected_packaging_version_main'],
146 suffix=scenario['expected_packaging_version_prerelease'],
147 ))
148
149 def test_returns_expected_result_or_raises_exception(self):
150 """ Should return expected result or raise expected exception. """
151 with make_expected_error_context(self):
152 result = self.function_to_test(*self.test_args)
153 if not hasattr(self, 'expected_error'):
154 self.assertEqual(self.expected_result, result)
155
156 def test_result_is_valid_python_packaging_version(self):
157 """ Should return a valid Python packaging version. """
158 if hasattr(self, 'expected_error'):
159 self.skipTest("no expected result in this scenario")
160 result = self.function_to_test(*self.test_args)
161 __ = packaging.version.Version(result)
162
163
164 # Copyright © 2015–2026 Ben Finney <[email protected]>
165 #
166 # This is free software: you may copy, modify, and/or distribute this work
167 # under the terms of the GNU General Public License as published by the
168 # Free Software Foundation; version 3 of that license or any later version.
169 # No warranty expressed or implied. See the file ‘LICENSE.GPL-3’ for details.
170
171
172 # Local variables:
173 # coding: utf-8
174 # mode: python
175 # End:
176 # vim: fileencoding=utf-8 filetype=python :