-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_cli.py
More file actions
532 lines (396 loc) · 18.4 KB
/
Copy pathtest_cli.py
File metadata and controls
532 lines (396 loc) · 18.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
"""Tests for CLI commands."""
import os
import pytest
from click.testing import CliRunner
from pathlib import Path
from wt.cli import cli
from wt import git
@pytest.fixture
def runner():
"""Create a Click test runner."""
return CliRunner()
@pytest.fixture
def initialized_repo(git_repo, tmp_path, monkeypatch):
"""Create a git repo with wt config in temp directory."""
from wt.config import Config
# Use temp directory for config
monkeypatch.setenv("WT_CONFIG", str(tmp_path))
# Initialize config with test values
config = Config(git_repo)
config.set("default_base", "main") # Use main instead of origin/main for tests
config.save()
# Change to repo directory for CLI commands
try:
original_dir = os.getcwd()
except (OSError, FileNotFoundError):
# Current directory was deleted, use /tmp
original_dir = "/tmp"
os.chdir(git_repo)
yield git_repo
try:
os.chdir(original_dir)
except (OSError, FileNotFoundError):
# Directory might have been deleted, go to /tmp
os.chdir("/tmp")
def test_cli_help(runner):
"""Test CLI help command."""
result = runner.invoke(cli, ["--help"])
assert result.exit_code == 0
assert "Git worktree manager" in result.output
def test_init_command(runner, tmp_path, monkeypatch):
"""Test wt init command."""
# Use temp directory for config
monkeypatch.setenv("WT_CONFIG", str(tmp_path))
result = runner.invoke(cli, ["init"])
assert result.exit_code == 0
assert "Configuration saved" in result.output
assert (tmp_path / ".wt.toml").exists()
def test_init_with_custom_options(runner, tmp_path, monkeypatch):
"""Test init with custom prefix and path."""
# Use temp directory for config
monkeypatch.setenv("WT_CONFIG", str(tmp_path))
result = runner.invoke(cli, ["init", "--prefix", "dev", "--path", "../{name}"])
assert result.exit_code == 0
assert "prefix: dev" in result.output
assert (tmp_path / ".wt.toml").exists()
def test_list_command(runner, initialized_repo):
"""Test wt list command."""
result = runner.invoke(cli, ["list"])
assert result.exit_code == 0
assert "main" in result.output
def test_switch_create(runner, initialized_repo, no_prompt):
"""Test wt switch -c command."""
result = runner.invoke(cli, ["switch", "-c", "feat"])
assert result.exit_code == 0
assert git.branch_exists("feature/feat", initialized_repo)
def test_switch_to_existing(runner, initialized_repo, no_prompt):
"""Test switching to existing worktree."""
# Create worktree first
runner.invoke(cli, ["switch", "-c", "feat"])
# Switch to it (this won't actually cd in tests, but should succeed)
result = runner.invoke(cli, ["switch", "feat"])
# In test environment, this might fail because we can't actually cd
# but the logic should work
def test_switch_nonexistent(runner, initialized_repo):
"""Test switching to non-existent worktree."""
result = runner.invoke(cli, ["switch", "nonexistent"])
assert result.exit_code == 4
assert "not found" in result.output
def test_config_list(runner, initialized_repo):
"""Test wt config --list."""
result = runner.invoke(cli, ["config", "--list"])
assert result.exit_code == 0
assert "prefix" in result.output
assert "feature" in result.output
def test_config_get(runner, initialized_repo):
"""Test wt config <key>."""
result = runner.invoke(cli, ["config", "prefix"])
assert result.exit_code == 0
assert "feature" in result.output
def test_config_set(runner, initialized_repo):
"""Test wt config <key> <value>."""
result = runner.invoke(cli, ["config", "prefix", "custom"])
assert result.exit_code == 0
assert "Set config" in result.output
def test_shell_init_bash(runner):
"""Test wt shell-init bash."""
result = runner.invoke(cli, ["shell-init", "bash"])
assert result.exit_code == 0
assert "function" in result.output or "wt()" in result.output
assert "bash" in result.output
def test_shell_init_fish(runner):
"""Test wt shell-init fish."""
result = runner.invoke(cli, ["shell-init", "fish"])
assert result.exit_code == 0
assert "function wt" in result.output
def test_status_command(runner, initialized_repo):
"""Test wt status command."""
result = runner.invoke(cli, ["status"])
assert result.exit_code == 0
assert "main" in result.output
def test_delete_command(runner, initialized_repo, no_prompt):
"""Test wt delete command."""
# Create worktree first
runner.invoke(cli, ["switch", "-c", "feat"])
# Delete it
result = runner.invoke(cli, ["delete", "feat", "--force"])
assert result.exit_code == 0
def test_clean_command(runner, initialized_repo, no_prompt):
"""Test wt clean command."""
result = runner.invoke(cli, ["clean", "--dry-run"])
assert result.exit_code == 0
def test_commands_from_secondary_worktree(runner, initialized_repo, no_prompt):
"""Test that wt commands work from secondary worktrees."""
# Create a secondary worktree
result = runner.invoke(cli, ["switch", "-c", "feat"])
assert result.exit_code == 0
# Find the worktree path
from wt import git
worktrees = git.list_worktrees(initialized_repo)
feat_worktree = None
for wt in worktrees:
if wt.get("branch") == "feature/feat":
feat_worktree = wt["path"]
break
assert feat_worktree is not None
# Change to the secondary worktree directory
original_dir = os.getcwd()
try:
os.chdir(feat_worktree)
# Run wt list from the secondary worktree - should still work
result = runner.invoke(cli, ["list"])
assert result.exit_code == 0
assert "main" in result.output
assert "feat" in result.output
# Run wt status from the secondary worktree
result = runner.invoke(cli, ["status"])
assert result.exit_code == 0
assert "main" in result.output
# Config should also work (reads from main worktree)
result = runner.invoke(cli, ["config", "--list"])
assert result.exit_code == 0
assert "prefix" in result.output
finally:
try:
os.chdir(original_dir)
except (OSError, FileNotFoundError):
os.chdir("/tmp")
def test_run_command(runner, initialized_repo):
"""Test wt run command."""
# Run a simple command in main worktree
result = runner.invoke(cli, ["run", "main", "echo hello"])
assert result.exit_code == 0
def test_run_command_with_default_symbol(runner, initialized_repo):
"""Test wt run command with ^ symbol for default worktree."""
# Run a command in default worktree using ^
result = runner.invoke(cli, ["run", "^", "echo hello"])
assert result.exit_code == 0
def test_run_command_with_previous_symbol(runner, initialized_repo, no_prompt):
"""Test wt run command with - symbol for previous worktree."""
# Create a worktree and switch to it to establish a previous worktree
runner.invoke(cli, ["switch", "-c", "feat"])
runner.invoke(cli, ["switch", "main"])
# Now run a command in previous worktree using -
result = runner.invoke(cli, ["run", "-", "echo hello"])
assert result.exit_code == 0
def test_run_command_no_previous_worktree(runner, initialized_repo):
"""Test wt run command with - symbol when there's no previous worktree."""
result = runner.invoke(cli, ["run", "-", "echo hello"])
assert result.exit_code == 1
assert "No previous worktree" in result.output
def test_run_command_nonexistent_worktree(runner, initialized_repo):
"""Test wt run command with non-existent worktree."""
result = runner.invoke(cli, ["run", "nonexistent", "echo hello"])
assert result.exit_code == 4
assert "not found" in result.output
def test_sync_command_no_upstream(runner, initialized_repo, no_prompt):
"""Test wt sync command when current worktree has no upstream."""
result = runner.invoke(cli, ["sync"])
# Should show error about no upstream since initialized_repo doesn't have remote
assert "no upstream" in result.output.lower() or "error" in result.output.lower()
def test_sync_command_all_worktrees(runner, initialized_repo, no_prompt):
"""Test wt sync --all command."""
# Create feature worktrees
runner.invoke(cli, ["switch", "-c", "feat"])
# Run sync on all worktrees - will have no upstream but shouldn't crash
result = runner.invoke(cli, ["sync", "--all"])
assert result.exit_code == 0 or result.exit_code == 3
assert "Syncing" in result.output
def test_sync_command_include(runner, initialized_repo, no_prompt):
"""Test wt sync --include command."""
# Create worktrees
runner.invoke(cli, ["switch", "-c", "feat1"])
runner.invoke(cli, ["switch", "-c", "feat2"])
# Sync only feat1
result = runner.invoke(cli, ["sync", "--include", "feat1"])
assert result.exit_code == 0 or result.exit_code == 3
def test_sync_command_exclude(runner, initialized_repo, no_prompt):
"""Test wt sync --all --exclude command."""
# Create worktrees
runner.invoke(cli, ["switch", "-c", "feat1"])
runner.invoke(cli, ["switch", "-c", "feat2"])
# Sync all except feat1
result = runner.invoke(cli, ["sync", "--all", "--exclude", "feat1"])
assert result.exit_code == 0 or result.exit_code == 3
def test_sync_command_with_rebase(runner, initialized_repo, no_prompt):
"""Test wt sync --rebase command."""
# Run sync with rebase - will have no upstream but shouldn't crash
result = runner.invoke(cli, ["sync", "--rebase"])
assert result.exit_code == 0 or result.exit_code == 3
def test_sync_command_with_rebase_shortcut(runner, initialized_repo, no_prompt):
"""Test wt sync -r command (shortcut for --rebase)."""
# Run sync with -r shortcut - will have no upstream but shouldn't crash
result = runner.invoke(cli, ["sync", "-r"])
assert result.exit_code == 0 or result.exit_code == 3
def test_sync_command_invalid_args(runner, initialized_repo):
"""Test wt sync with invalid argument combinations."""
# Both include and exclude
result = runner.invoke(cli, ["sync", "--include", "feat1", "--exclude", "feat2"])
assert result.exit_code == 2
assert "Cannot use both" in result.output
# Exclude without all
result = runner.invoke(cli, ["sync", "--exclude", "feat1"])
assert result.exit_code == 2
assert "requires --all" in result.output
def test_detached_worktree_create(runner, initialized_repo, no_prompt):
"""Test creating a detached worktree."""
result = runner.invoke(cli, ["switch", "-c", "mydetached", "--detached"])
assert result.exit_code == 0
# Worktree should be created
from wt.config import Config
from wt.worktree import WorktreeManager
config = Config(initialized_repo)
manager = WorktreeManager(config)
wt = manager.find_worktree_by_name("mydetached")
assert wt is not None
assert wt["name"] == "mydetached"
assert wt.get("branch") is None # detached worktrees have no branch
def test_detached_worktree_list(runner, initialized_repo, no_prompt):
"""Test listing detached worktrees shows custom names."""
# Create two detached worktrees
runner.invoke(cli, ["switch", "-c", "detached1", "--detached"])
runner.invoke(cli, ["switch", "-c", "detached2", "--detached"])
result = runner.invoke(cli, ["list"])
assert result.exit_code == 0
# Both should show with their custom names, not "(detached)"
assert "detached1" in result.output
assert "detached2" in result.output
# Should not show generic "(detached)" for named worktrees
lines = result.output.split('\n')
detached_lines = [l for l in lines if "detached1" in l or "detached2" in l]
assert len(detached_lines) == 2
def test_detached_worktree_switch(runner, initialized_repo, no_prompt):
"""Test switching to a detached worktree by its name."""
# Create a detached worktree
runner.invoke(cli, ["switch", "-c", "mydetached", "--detached"])
# Switch to it by name
result = runner.invoke(cli, ["switch", "mydetached"])
assert result.exit_code == 0
assert "mydetached" in result.output
def test_detached_worktree_run(runner, initialized_repo, no_prompt):
"""Test running commands in a detached worktree."""
# Create a detached worktree
runner.invoke(cli, ["switch", "-c", "mydetached", "--detached"])
# Run a command in it
result = runner.invoke(cli, ["run", "mydetached", "echo hello"])
assert result.exit_code == 0
def test_multiple_detached_worktrees_unique_names(runner, initialized_repo, no_prompt):
"""Test that multiple detached worktrees can coexist with unique names."""
# Create multiple detached worktrees
runner.invoke(cli, ["switch", "-c", "det1", "--detached"])
runner.invoke(cli, ["switch", "-c", "det2", "--detached"])
runner.invoke(cli, ["switch", "-c", "det3", "--detached"])
# List should show all three with their unique names
result = runner.invoke(cli, ["list"])
assert result.exit_code == 0
assert "det1" in result.output
assert "det2" in result.output
assert "det3" in result.output
# Each should be findable by name
from wt.config import Config
from wt.worktree import WorktreeManager
config = Config(initialized_repo)
manager = WorktreeManager(config)
for name in ["det1", "det2", "det3"]:
wt = manager.find_worktree_by_name(name)
assert wt is not None
assert wt["name"] == name
def test_detached_worktree_delete(runner, initialized_repo, no_prompt):
"""Test deleting a detached worktree by its name."""
# Create a detached worktree
runner.invoke(cli, ["switch", "-c", "mydetached", "--detached"])
# Delete it by name
result = runner.invoke(cli, ["delete", "mydetached", "--force"])
assert result.exit_code == 0
# Verify it's gone
from wt.config import Config
from wt.worktree import WorktreeManager
config = Config(initialized_repo)
manager = WorktreeManager(config)
wt = manager.find_worktree_by_name("mydetached")
assert wt is None
def test_detached_worktree_backward_compatibility(runner, initialized_repo, no_prompt):
"""Test that detached worktrees created without stored name still work."""
# Create a detached worktree using raw git (simulates old behavior)
from wt import git
from wt.config import Config
from wt.worktree import WorktreeManager
config = Config(initialized_repo)
wt_path = config.resolve_path_pattern("legacy", "feature/legacy")
git.add_worktree(wt_path, "legacy", create_branch=False, base="HEAD",
detached=True, repo_path=initialized_repo)
# Note: Not calling set_worktree_name - simulates old behavior
# List should infer name from path
manager = WorktreeManager(config)
worktrees = manager.list_worktrees()
legacy_wt = None
for wt in worktrees:
if "legacy" in wt["name"]:
legacy_wt = wt
break
assert legacy_wt is not None
assert legacy_wt["name"] == "legacy" # Inferred from path
# Should be able to find it by inferred name
found_wt = manager.find_worktree_by_name("legacy")
assert found_wt is not None
# --- checkout branch (switch -c -B) tests ---
def test_switch_checkout_branch(runner, initialized_repo, no_prompt):
"""Test wt switch -c -B with existing branch."""
git.create_branch("fix/login-bug", "HEAD", initialized_repo)
result = runner.invoke(cli, ["switch", "-c", "-B", "fix/login-bug"])
assert result.exit_code == 0
assert "fix/login-bug" in result.output
assert "login-bug" in result.output
def test_switch_checkout_branch_custom_name(runner, initialized_repo, no_prompt):
"""Test wt switch -c <name> -B <branch>."""
git.create_branch("fix/login-bug", "HEAD", initialized_repo)
result = runner.invoke(cli, ["switch", "-c", "review", "-B", "fix/login-bug"])
assert result.exit_code == 0
assert "review" in result.output
def test_switch_checkout_branch_nonexistent(runner, initialized_repo):
"""Test wt switch -c -B with nonexistent branch."""
result = runner.invoke(cli, ["switch", "-c", "-B", "nonexistent/branch"])
assert result.exit_code == 3 # EXIT_GIT_ERROR
assert "does not exist" in result.output
def test_switch_checkout_branch_requires_create(runner, initialized_repo):
"""Test that -B requires -c flag."""
result = runner.invoke(cli, ["switch", "-B", "fix/login-bug"])
assert result.exit_code == 2
assert "requires" in result.output
def test_switch_checkout_branch_no_detached(runner, initialized_repo):
"""Test that -B cannot be used with -d."""
result = runner.invoke(cli, ["switch", "-c", "-B", "fix/login-bug", "-d"])
assert result.exit_code == 2
assert "cannot be used" in result.output.lower()
def test_switch_checkout_branch_no_base(runner, initialized_repo):
"""Test that -B cannot be used with -b."""
result = runner.invoke(cli, ["switch", "-c", "-B", "fix/login-bug", "-b", "main"])
assert result.exit_code == 2
assert "cannot be used" in result.output.lower()
def test_switch_checkout_branch_shell_helper(runner, initialized_repo, no_prompt):
"""Test wt switch -c -B with --shell-helper."""
git.create_branch("fix/login-bug", "HEAD", initialized_repo)
result = runner.invoke(cli, ["switch", "-c", "-B", "fix/login-bug", "--shell-helper"])
assert result.exit_code == 0
# Output should be just the path
output = result.output.strip()
assert "/" in output
def test_switch_checkout_then_switch(runner, initialized_repo, no_prompt):
"""Test that wt switch works after checkout."""
git.create_branch("fix/login-bug", "HEAD", initialized_repo)
runner.invoke(cli, ["switch", "-c", "-B", "fix/login-bug"])
# Should be able to switch to it by derived name
result = runner.invoke(cli, ["switch", "login-bug"])
assert result.exit_code == 0
def test_switch_checkout_then_delete(runner, initialized_repo, no_prompt):
"""Test that wt delete works after checkout."""
git.create_branch("fix/login-bug", "HEAD", initialized_repo)
runner.invoke(cli, ["switch", "-c", "-B", "fix/login-bug"])
# Should be able to delete by derived name
result = runner.invoke(cli, ["delete", "login-bug", "--force", "--keep-branch"])
assert result.exit_code == 0
def test_switch_checkout_fetch_requires_branch(runner, initialized_repo):
"""Test that --fetch requires -B."""
result = runner.invoke(cli, ["switch", "-c", "feat", "--fetch"])
assert result.exit_code == 2
assert "requires" in result.output