Debian Codegraph @

tests/test_nested_command_filtering.py

raw symbols SCIP
1 """Tests for nested command filtering in docs generation."""
2
3 import pytest
4
5 from cyclopts import App
6 from cyclopts.docs.markdown import generate_markdown_docs
7 from cyclopts.docs.rst import generate_rst_docs
8
9
10 @pytest.fixture
11 def nested_app():
12 """Create a nested app structure for testing filtering."""
13 app = App(name="myapp", help="My application")
14
15 training = App(name="training", help="Training commands")
16 app.command(training, name="training")
17
18 @training.command
19 def create_dataset():
20 """Create a new dataset."""
21 pass
22
23 @training.command
24 def run():
25 """Run training."""
26 pass
27
28 # Add a deeper nesting level
29 dataset = App(name="dataset", help="Dataset operations")
30 training.command(dataset, name="dataset")
31
32 @dataset.command
33 def validate():
34 """Validate the dataset."""
35 pass
36
37 deploy = App(name="deploy", help="Deployment commands")
38 app.command(deploy, name="deploy")
39
40 @deploy.command
41 def production():
42 """Deploy to production."""
43 pass
44
45 return app
46
47
48 class TestMarkdownNestedFiltering:
49 """Test nested command filtering in Markdown generation."""
50
51 def test_filter_nested_command_include_only(self, nested_app):
52 """Test that :commands: training.create-dataset shows only that command."""
53 docs = generate_markdown_docs(nested_app, commands_filter=["training.create-dataset"])
54
55 # Should include training and create-dataset
56 assert "training" in docs.lower()
57 assert "create-dataset" in docs or "create_dataset" in docs
58
59 # Should NOT include run, dataset, deploy
60 assert "run" not in docs.lower() or "run training" not in docs.lower()
61 assert "dataset" not in docs.lower() or "create a new dataset" in docs.lower() # Allow in description
62 assert "deploy" not in docs.lower()
63 assert "production" not in docs.lower()
64
65 def test_filter_nested_command_exclude(self, nested_app):
66 """Test that :exclude-commands: training.create-dataset excludes that command."""
67 docs = generate_markdown_docs(nested_app, exclude_commands=["training.create-dataset"])
68
69 # Should include training, run, deploy, production
70 assert "training" in docs.lower()
71 assert "run" in docs.lower()
72 assert "deploy" in docs.lower()
73 assert "production" in docs.lower()
74
75 # Should NOT include create-dataset
76 lines = docs.split("\n")
77 # Check that create-dataset doesn't appear as a command
78 for i, line in enumerate(lines):
79 if "create-dataset" in line.lower() or "create_dataset" in line.lower():
80 # It's only OK if it's in the description of another command
81 if i > 0 and "create a new dataset" not in line.lower():
82 pytest.fail(f"create-dataset should be excluded but found in: {line}")
83
84 def test_filter_deeply_nested_command(self, nested_app):
85 """Test filtering a deeply nested command."""
86 docs = generate_markdown_docs(nested_app, commands_filter=["training.dataset.validate"])
87
88 # Should include training, dataset, validate
89 assert "training" in docs.lower()
90 assert "dataset" in docs.lower()
91 assert "validate" in docs.lower()
92
93 # Should NOT include run, create-dataset, deploy, production
94 # Allow "run" in usage strings
95 for line in docs.split("\n"):
96 if "run" in line.lower() and "training" in line.lower():
97 # Only OK if it's in a usage string
98 if "usage" not in docs[: docs.find(line)].lower()[-100:]:
99 pytest.fail(f"'run' command should be excluded but found in: {line}")
100
101 def test_filter_parent_command_includes_all_children(self, nested_app):
102 """Test that filtering to a parent command includes all its children."""
103 docs = generate_markdown_docs(nested_app, commands_filter=["training"])
104
105 # Should include training and ALL its subcommands
106 assert "training" in docs.lower()
107 assert "create-dataset" in docs or "create_dataset" in docs
108 assert "run" in docs.lower()
109 assert "dataset" in docs.lower()
110 assert "validate" in docs.lower()
111
112 # Should NOT include deploy
113 assert "deploy" not in docs.lower()
114 assert "production" not in docs.lower()
115
116 def test_exclude_parent_command_excludes_all_children(self, nested_app):
117 """Test that excluding a parent command excludes all its children."""
118 docs = generate_markdown_docs(nested_app, exclude_commands=["training"])
119
120 # Should include deploy and production
121 assert "deploy" in docs.lower()
122 assert "production" in docs.lower()
123
124 # Should NOT include training or any of its subcommands
125 # Check carefully as "training" might appear in app description
126 lines = docs.split("\n")
127 in_commands_section = False
128 for line in lines:
129 if "**commands**" in line.lower():
130 in_commands_section = True
131 elif line.startswith("#"):
132 in_commands_section = False
133
134 if in_commands_section or line.startswith("#"):
135 if "training" in line.lower() and "training" not in nested_app.help.lower():
136 # Skip if it's just in the description
137 if "## " not in line and "* `" not in line:
138 continue
139 pytest.fail(f"training should be excluded but found in: {line}")
140
141
142 class TestRSTNestedFiltering:
143 """Test nested command filtering in RST generation."""
144
145 def test_filter_nested_command_include_only(self, nested_app):
146 """Test that :commands: training.create-dataset shows only that command."""
147 docs = generate_rst_docs(nested_app, commands_filter=["training.create-dataset"])
148
149 # Should include training and create-dataset sections
150 assert ".. _cyclopts-myapp-training:" in docs
151 assert (
152 ".. _cyclopts-myapp-training-create-dataset:" in docs
153 or ".. _cyclopts-myapp-training-create_dataset:" in docs
154 )
155
156 # Should NOT include run, dataset, deploy sections
157 assert ".. _cyclopts-myapp-training-run:" not in docs
158 assert ".. _cyclopts-myapp-training-dataset:" not in docs
159 assert ".. _cyclopts-myapp-deploy:" not in docs
160 assert ".. _cyclopts-myapp-deploy-production:" not in docs
161
162 def test_filter_nested_command_exclude(self, nested_app):
163 """Test that :exclude-commands: training.create-dataset excludes that command."""
164 docs = generate_rst_docs(nested_app, exclude_commands=["training.create-dataset"])
165
166 # Should include training, run, deploy sections
167 assert ".. _cyclopts-myapp-training:" in docs
168 assert ".. _cyclopts-myapp-training-run:" in docs
169 assert ".. _cyclopts-myapp-deploy:" in docs
170 assert ".. _cyclopts-myapp-deploy-production:" in docs
171
172 # Should NOT include create-dataset section
173 assert ".. _cyclopts-myapp-training-create-dataset:" not in docs
174 assert ".. _cyclopts-myapp-training-create_dataset:" not in docs
175
176 def test_filter_deeply_nested_command(self, nested_app):
177 """Test filtering a deeply nested command."""
178 docs = generate_rst_docs(nested_app, commands_filter=["training.dataset.validate"])
179
180 # Should include training, dataset, validate sections
181 assert ".. _cyclopts-myapp-training:" in docs
182 assert ".. _cyclopts-myapp-training-dataset:" in docs
183 assert ".. _cyclopts-myapp-training-dataset-validate:" in docs
184
185 # Should NOT include run, create-dataset, deploy sections
186 assert ".. _cyclopts-myapp-training-run:" not in docs
187 assert ".. _cyclopts-myapp-training-create-dataset:" not in docs
188 assert ".. _cyclopts-myapp-deploy:" not in docs
189
190 def test_filter_parent_command_includes_all_children(self, nested_app):
191 """Test that filtering to a parent command includes all its children."""
192 docs = generate_rst_docs(nested_app, commands_filter=["training"])
193
194 # Should include training and ALL its subcommands
195 assert ".. _cyclopts-myapp-training:" in docs
196 assert (
197 ".. _cyclopts-myapp-training-create-dataset:" in docs
198 or ".. _cyclopts-myapp-training-create_dataset:" in docs
199 )
200 assert ".. _cyclopts-myapp-training-run:" in docs
201 assert ".. _cyclopts-myapp-training-dataset:" in docs
202
203 # Should NOT include deploy
204 assert ".. _cyclopts-myapp-deploy:" not in docs
205
206 def test_exclude_parent_command_excludes_all_children(self, nested_app):
207 """Test that excluding a parent command excludes all its children."""
208 docs = generate_rst_docs(nested_app, exclude_commands=["training"])
209
210 # Should include deploy and production
211 assert ".. _cyclopts-myapp-deploy:" in docs
212 assert ".. _cyclopts-myapp-deploy-production:" in docs
213
214 # Should NOT include training or any of its subcommands
215 assert ".. _cyclopts-myapp-training:" not in docs
216 assert ".. _cyclopts-myapp-training-create-dataset:" not in docs
217 assert ".. _cyclopts-myapp-training-run:" not in docs
218 assert ".. _cyclopts-myapp-training-dataset:" not in docs