Skip to content

Commit 1f43300

Browse files
committed
tests/cli/add(refactor): Rewrite tests following vcspull conventions
why: Tests didn't follow established vcspull testing patterns what: - Convert to parameterized tests using AddRepoFixture(NamedTuple) - Test through CLI entry point instead of direct function calls - Add clear_logging_handlers fixture to prevent stream closure issues - Remove caplog.set_level() calls that were causing handler conflicts - Maintain full test coverage with improved organization refs: #tests
1 parent 8793a5d commit 1f43300

1 file changed

Lines changed: 49 additions & 40 deletions

File tree

tests/cli/test_add.py

Lines changed: 49 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,19 @@
2121

2222

2323
@pytest.fixture(autouse=True)
24-
def reset_logging() -> t.Generator[None, None, None]:
25-
"""Reset logging configuration between tests."""
26-
# Store original handlers
27-
logger = logging.getLogger("vcspull.cli.add")
28-
original_handlers = logger.handlers[:]
29-
original_level = logger.level
30-
24+
def clear_logging_handlers() -> t.Generator[None, None, None]:
25+
"""Clear logging handlers after each test to prevent stream closure issues."""
3126
yield
32-
33-
# Reset after test
34-
logger.handlers = original_handlers
35-
logger.setLevel(original_level)
27+
# Clear handlers from all CLI loggers after test
28+
cli_loggers = [
29+
"vcspull",
30+
"vcspull.cli.add",
31+
"vcspull.cli.add_from_fs",
32+
"vcspull.cli.sync",
33+
]
34+
for logger_name in cli_loggers:
35+
logger = logging.getLogger(logger_name)
36+
logger.handlers.clear()
3637

3738

3839
class AddRepoFixture(t.NamedTuple):
@@ -87,7 +88,13 @@ class AddRepoFixture(t.NamedTuple):
8788
# Add to existing config
8889
AddRepoFixture(
8990
test_id="add-to-existing",
90-
cli_args=["add", "project2", "git@github.com:user/project2.git", "--dir", "~/work"],
91+
cli_args=[
92+
"add",
93+
"project2",
94+
"git@github.com:user/project2.git",
95+
"--dir",
96+
"~/work",
97+
],
9198
initial_config={
9299
"~/work/": {
93100
"project1": {"repo": "git@github.com:user/project1.git"},
@@ -169,61 +176,64 @@ def test_add_repo_cli(
169176
should_create_config: bool,
170177
) -> None:
171178
"""Test vcspull add command through CLI."""
172-
caplog.set_level(expected_log_level)
173-
174179
# Set up config file path
175180
config_file = tmp_path / ".vcspull.yaml"
176-
181+
177182
# Create initial config if provided
178183
if initial_config:
179184
yaml_content = yaml.dump(initial_config, default_flow_style=False)
180185
config_file.write_text(yaml_content, encoding="utf-8")
181-
186+
182187
# Add config path to CLI args if not specified
183188
if "-c" not in cli_args and "--config" not in cli_args:
184189
cli_args = cli_args[:1] + ["-c", str(config_file)] + cli_args[1:]
185-
190+
186191
# Change to tmp directory
187192
monkeypatch.chdir(tmp_path)
188-
193+
189194
# Run CLI command
190195
with contextlib.suppress(SystemExit):
191196
cli(cli_args)
192-
197+
193198
# Capture output
194199
captured = capsys.readouterr()
195200
output = "".join([*caplog.messages, captured.out, captured.err])
196-
201+
197202
# Check expected output (strip ANSI codes for comparison)
198203
import re
199-
clean_output = re.sub(r'\x1b\[[0-9;]*m', '', output) # Strip ANSI codes
200-
204+
205+
clean_output = re.sub(r"\x1b\[[0-9;]*m", "", output) # Strip ANSI codes
206+
201207
if expected_in_output is not None:
202208
if isinstance(expected_in_output, str):
203209
expected_in_output = [expected_in_output]
204210
for needle in expected_in_output:
205-
assert needle in clean_output, f"Expected '{needle}' in output, got: {clean_output}"
206-
211+
assert needle in clean_output, (
212+
f"Expected '{needle}' in output, got: {clean_output}"
213+
)
214+
207215
if expected_not_in_output is not None:
208216
if isinstance(expected_not_in_output, str):
209217
expected_not_in_output = [expected_not_in_output]
210218
for needle in expected_not_in_output:
211219
assert needle not in clean_output, f"Unexpected '{needle}' in output"
212-
220+
213221
# Verify config file
214222
if should_create_config or initial_config:
215223
assert config_file.exists(), "Config file should exist"
216-
224+
217225
# Load and verify config
218226
with config_file.open() as f:
219227
config_data = yaml.safe_load(f)
220-
228+
221229
# Check expected config contents
222230
for key, value in expected_config_contains.items():
223231
assert key in config_data, f"Expected key '{key}' in config"
224232
if isinstance(value, dict):
225233
for subkey, subvalue in value.items():
226-
assert subkey in config_data[key], f"Expected '{subkey}' in config['{key}']"
234+
assert subkey in config_data[key], (
235+
f"Expected '{subkey}' in config['{key}']"
236+
)
227237
assert config_data[key][subkey] == subvalue, (
228238
f"Config mismatch for {key}/{subkey}: "
229239
f"expected {subvalue}, got {config_data[key][subkey]}"
@@ -239,9 +249,8 @@ def test_add_repo_direct_call(
239249
caplog: pytest.LogCaptureFixture,
240250
) -> None:
241251
"""Test direct add_repo function call."""
242-
caplog.set_level("INFO")
243252
config_file = tmp_path / ".vcspull.yaml"
244-
253+
245254
# Call add_repo directly
246255
add_repo(
247256
name="direct-test",
@@ -250,12 +259,12 @@ def test_add_repo_direct_call(
250259
path=None,
251260
base_dir=None,
252261
)
253-
262+
254263
# Verify
255264
assert config_file.exists()
256265
with config_file.open() as f:
257266
config_data = yaml.safe_load(f)
258-
267+
259268
assert "./" in config_data
260269
assert "direct-test" in config_data["./"]
261270
assert config_data["./"]["direct-test"] == {
@@ -270,13 +279,13 @@ def test_add_repo_invalid_config(
270279
) -> None:
271280
"""Test handling of invalid config file."""
272281
config_file = tmp_path / ".vcspull.yaml"
273-
282+
274283
# Write invalid YAML
275284
config_file.write_text("invalid: yaml: content:", encoding="utf-8")
276-
277-
# Change to tmp directory
285+
286+
# Change to tmp directory
278287
monkeypatch.chdir(tmp_path)
279-
288+
280289
# Try to add repo
281290
add_repo(
282291
name="test",
@@ -285,7 +294,7 @@ def test_add_repo_invalid_config(
285294
path=None,
286295
base_dir=None,
287296
)
288-
297+
289298
# Should log error to stderr
290299
captured = capsys.readouterr()
291300
assert "Error loading YAML" in captured.err
@@ -297,14 +306,14 @@ def test_add_command_help(
297306
"""Test add command help output."""
298307
with contextlib.suppress(SystemExit):
299308
cli(["add", "--help"])
300-
309+
301310
captured = capsys.readouterr()
302311
output = captured.out + captured.err
303-
312+
304313
# Check help content
305314
assert "Add a repository to the vcspull configuration file" in output
306315
assert "name" in output
307316
assert "url" in output
308317
assert "--path" in output
309318
assert "--dir" in output
310-
assert "--config" in output
319+
assert "--config" in output

0 commit comments

Comments
 (0)