Debian Codegraph @

tests/test_app_attribute_inheritance.py

raw symbols SCIP
1 import pytest
2
3 from cyclopts import App
4 from cyclopts.exceptions import UnknownOptionError
5
6
7 def test_app_has_extra_attributes_as_attributes():
8 """Test that extra attributes are App attributes with defaults None."""
9 app = App()
10
11 assert app.print_error is None
12 assert app.exit_on_error is None
13 assert app.help_on_error is None
14 assert app.verbose is None
15 assert app.end_of_options_delimiter is None
16 assert app.result_action is None
17
18
19 def test_app_attributes_can_be_set():
20 """Test that extra attributes can be set as App attributes."""
21 app = App(
22 print_error=False,
23 exit_on_error=False,
24 help_on_error=True,
25 verbose=True,
26 end_of_options_delimiter="--",
27 )
28
29 assert app.print_error is False
30 assert app.exit_on_error is False
31 assert app.help_on_error is True
32 assert app.verbose is True
33 assert app.end_of_options_delimiter == "--"
34
35
36 def test_call_overrides_app_attributes():
37 """Test that parameters in __call__ override the stored values in attributes."""
38 app = App(exit_on_error=False)
39
40 @app.default
41 def main():
42 return "success"
43
44 with pytest.raises(UnknownOptionError):
45 app(["--unknown-flag"])
46
47 with pytest.raises(SystemExit):
48 app(["--unknown-flag"], exit_on_error=True)
49
50
51 def test_parse_args_overrides_app_attributes():
52 """Test that parameters in parse_args override the stored values in attributes."""
53 app = App(
54 exit_on_error=True,
55 verbose=False,
56 )
57
58 @app.default
59 def main():
60 return "success"
61
62 with pytest.raises(UnknownOptionError):
63 app.parse_args(["--unknown-flag"], exit_on_error=False)
64
65 with pytest.raises(SystemExit):
66 app.parse_args(["--unknown-flag"])
67
68
69 def test_parse_known_args_overrides_app_attributes():
70 """Test that parameters in parse_known_args override the stored values in attributes."""
71 app = App(end_of_options_delimiter="===")
72
73 @app.default
74 def main(arg: str = "default"):
75 return arg
76
77 # Override the end_of_options_delimiter
78 command, bound, unused_tokens, ignored = app.parse_known_args(
79 ["((", "after_delimiter"], end_of_options_delimiter="(("
80 )
81
82 # With the override, the standard "--" should work, not "==="
83 assert command == main
84 assert bound.arguments["arg"] == "after_delimiter"
85 assert unused_tokens == []
86
87
88 def test_app_stack_inheritance_simple():
89 """Test that child apps inherit extra attributes from parent apps via AppStack."""
90 parent_app = App(
91 print_error=False,
92 exit_on_error=False,
93 help_on_error=True,
94 verbose=True,
95 )
96
97 parent_app.command(child_app := App(name="child"))
98
99 @child_app.default
100 def child_command():
101 return "child_success"
102
103 # Child should inherit parent's settings
104 with pytest.raises(UnknownOptionError):
105 # This will raise UnknownOptionError due to unknown flag, but should not exit due to inherited exit_on_error=False
106 parent_app("child --unknown-flag", exit_on_error=None) # None means use inherited
107
108
109 def test_app_stack_inheritance_override():
110 """Test that child apps can override parent app extra attributes."""
111 parent_app = App(
112 print_error=True,
113 exit_on_error=True,
114 verbose=False,
115 )
116
117 child_app = App(
118 name="child",
119 print_error=False,
120 exit_on_error=False,
121 verbose=True,
122 result_action="return_value",
123 )
124 parent_app.command(child_app)
125
126 @child_app.default
127 def child_command():
128 return "child_success"
129
130 # Child's settings should override parent's
131 child_app([], exit_on_error=False)
132
133
134 def test_app_stack_resolution_none_values():
135 """Test that None values in child apps allow parent values to be used."""
136 parent_app = App(
137 print_error=False,
138 exit_on_error=False,
139 help_on_error=True,
140 verbose=True,
141 end_of_options_delimiter="--parent--",
142 )
143
144 # Child has all None values, should inherit from parent
145 child_app = App(name="child")
146 parent_app.command(child_app)
147
148 @child_app.default
149 def child_command():
150 return "child_success"
151
152 # Test that child inherits parent's end_of_options_delimiter through AppStack
153 command, bound, unused_tokens, ignored = parent_app.parse_known_args(
154 ["child", "--parent--", "after_delimiter"],
155 end_of_options_delimiter=None, # Should use app's setting
156 )
157
158 # The child should have inherited the parent's delimiter
159 assert command == child_command
160 assert unused_tokens == ["after_delimiter"]
161
162
163 def test_meta_app_inheritance():
164 """Test that meta apps also participate in extra attribute inheritance."""
165 parent_app = App(
166 print_error=False,
167 exit_on_error=False,
168 verbose=True,
169 )
170
171 # Set up meta app with different settings
172 parent_app.meta.print_error = True
173 parent_app.meta.verbose = False
174
175 child_app = App(name="child", result_action="return_value")
176 parent_app.command(child_app)
177
178 @child_app.default
179 def child_command():
180 return "child_success"
181
182 # Child should inherit from the closest parent in the stack
183 child_app([])
184
185
186 def test_signature_parameter_override_precedence():
187 """Test that signature parameters have highest precedence over app attributes."""
188 app = App(
189 print_error=True,
190 exit_on_error=True,
191 help_on_error=False,
192 verbose=False,
193 end_of_options_delimiter="--app--",
194 )
195
196 @app.default
197 def main(args, /):
198 return args
199
200 # All signature parameters should override app attributes
201 command, bound, ignored = app.parse_args(
202 ["--signature--", "foo"],
203 end_of_options_delimiter="--signature--",
204 )
205 assert command == main
206 assert bound.args == ("foo",)
207 assert not ignored
208
209
210 def test_true_inheritance_without_fallback_override():
211 """Test that parent app attributes are inherited without being overridden by method defaults."""
212 parent_app = App(
213 exit_on_error=False,
214 )
215
216 parent_app.command(child_app := App(name="child"))
217
218 @child_app.default
219 def child_command():
220 return "child_success"
221
222 with pytest.raises(UnknownOptionError):
223 # This should not raise a SystemExit due to the root parent_app exit_on_error=False
224 parent_app("child --unknown-flag")
225
226 with pytest.raises(SystemExit):
227 # The immediately supplied argument should have highest priority.
228 parent_app("child --unknown-flag", exit_on_error=True)