Debian Codegraph @

tests/cmdoptions_test.py

raw
1 #!/usr/bin/env python3
2 # Copyright: 2026, Maximiliano Curia <[email protected]>
3 #
4 # License: ISC
5 # Permission to use, copy, modify, and/or distribute this software for any
6 # purpose with or without fee is hereby granted, provided that the above
7 # copyright notice and this permission notice appear in all copies.
8 # .
9 # THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10 # REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11 # AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12 # INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13 # LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14 # OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15 # PERFORMANCE OF THIS SOFTWARE.
16
17 """Tests for cmdoptions CLI flags."""
18
19 import dataclasses
20 import tempfile
21 import typing
22 import unittest
23 from pathlib import Path
24
25 from decopy import cmdoptions
26 from decopy.common.core_types import Options
27
28 # The Options-owned defaults the CLI must reuse rather than re-declare.
29 _SHARED_DEFAULT_FIELDS = (
30 "spdx",
31 "explain",
32 "since",
33 "expand_archives",
34 "max_archive_depth",
35 "max_archive_member_size",
36 "use_gitignore",
37 "fill_licenses",
38 )
39
40
41 def _option_default(name):
42 field = next(f for f in dataclasses.fields(Options) if f.name == name)
43 return field.default
44
45
46 class FillLicensesOptionTest(unittest.TestCase):
47 """Tests for the --fill-licenses / --no-fill-licenses option."""
48
49 _BASE: typing.ClassVar[list[str]] = ["--no-progress", "--root", "."]
50
51 def test_defaults_to_true(self):
52 opts = cmdoptions.process_options(self._BASE)
53 self.assertTrue(opts.fill_licenses)
54
55 def test_no_flag_disables(self):
56 opts = cmdoptions.process_options([*self._BASE, "--no-fill-licenses"])
57 self.assertFalse(opts.fill_licenses)
58
59 def test_flag_enables(self):
60 opts = cmdoptions.process_options([*self._BASE, "--fill-licenses"])
61 self.assertTrue(opts.fill_licenses)
62
63 def test_last_flag_wins(self):
64 opts = cmdoptions.process_options(
65 [*self._BASE, "--fill-licenses", "--no-fill-licenses"]
66 )
67 self.assertFalse(opts.fill_licenses)
68
69 def test_field_is_bool(self):
70 opts = cmdoptions.process_options(self._BASE)
71 self.assertIsInstance(opts, Options)
72 self.assertIs(type(opts.fill_licenses), bool)
73
74
75 class TestProcessOptionsDefaults(unittest.TestCase):
76 """The CLI defaults come from the Options dataclass (single source)."""
77
78 def test_defaults_match_options_dataclass(self):
79 with tempfile.TemporaryDirectory() as tmp:
80 options = cmdoptions.process_options(["--no-progress", "--root", tmp])
81 for name in _SHARED_DEFAULT_FIELDS:
82 self.assertEqual(
83 getattr(options, name),
84 _option_default(name),
85 f"{name}: CLI default diverged from Options default",
86 )
87
88
89 class TestProcessOptionsAssembly(unittest.TestCase):
90 """Characterisation snapshot: a representative argv -> Options field values.
91
92 Pins the parse/assemble behaviour so the process_options decomposition
93 cannot change any produced value.
94 """
95
96 def test_flag_matrix_produces_expected_options(self):
97 with tempfile.TemporaryDirectory() as tmp:
98 options = cmdoptions.process_options(
99 [
100 "--no-progress",
101 "--spdx",
102 "--no-glob",
103 "--group-by",
104 "copyright",
105 "--mode",
106 "partial",
107 "--jobs",
108 "4",
109 "--since",
110 "HEAD~1",
111 "--max-archive-depth",
112 "3",
113 "--no-fill-licenses",
114 "--copyright-file",
115 "debian/copyright",
116 "--output",
117 "-",
118 "--root",
119 tmp,
120 ]
121 )
122
123 self.assertEqual(options.mode, "partial")
124 self.assertEqual(options.group_by, "copyright")
125 self.assertEqual(options.jobs, 4)
126 self.assertEqual(options.since, "HEAD~1")
127 self.assertEqual(options.max_archive_depth, 3)
128 self.assertEqual(options.copyright_file, "debian/copyright")
129 self.assertTrue(options.spdx)
130 self.assertFalse(options.glob)
131 self.assertFalse(options.progress)
132 self.assertFalse(options.fill_licenses)
133 # "-" is the stdout sentinel, normalised to the empty string.
134 self.assertEqual(options.output, "")
135 # root is absolutised.
136 self.assertEqual(options.root, str(Path(tmp).absolute()))
137 # The four exclude regexes are compiled onto the result.
138 for name in (
139 "exclude_fullname_re",
140 "exclude_special_re",
141 "exclude_file_re",
142 "exclude_directory_re",
143 ):
144 self.assertIsNotNone(getattr(options, name))
145
146 def test_changed_shorthand_sets_mode(self):
147 with tempfile.TemporaryDirectory() as tmp:
148 options = cmdoptions.process_options(
149 ["--no-progress", "--changed", "--root", tmp]
150 )
151 self.assertEqual(options.mode, "changed")
152
153 def test_files_are_relative_to_root(self):
154 with tempfile.TemporaryDirectory() as tmp:
155 (Path(tmp) / "a.c").write_text("", encoding="utf-8")
156 options = cmdoptions.process_options(
157 ["--no-progress", "--root", tmp, str(Path(tmp) / "a.c")]
158 )
159 self.assertEqual(options.files, ["a.c"])
160
161 def test_every_required_option_field_is_produced(self):
162 """A required Options field with no CLI wiring would fail construction.
163
164 Guards the namespace->Options assembly: a new required field on Options
165 without a matching argument makes process_options raise.
166 """
167 with tempfile.TemporaryDirectory() as tmp:
168 options = cmdoptions.process_options(["--no-progress", "--root", tmp])
169 required = [
170 f.name
171 for f in dataclasses.fields(Options)
172 if f.default is dataclasses.MISSING
173 and f.default_factory is dataclasses.MISSING
174 ]
175 for name in required:
176 self.assertTrue(hasattr(options, name))
177
178
179 if __name__ == "__main__":
180 unittest.main()