twin-cli 0.1.0

Git worktree wrapper with side effects (symlinks and hooks)
Documentation
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
/// Git Worktreeラッパーとしての機能をテストするモジュール
///
/// このテストモジュールは、twinがgit worktreeの純粋なラッパーとして
/// 正しく動作することを確認します。
use std::fs;
use std::process::Command;
use tempfile::TempDir;

/// テスト用のGitリポジトリをセットアップ
fn setup_test_repo() -> TempDir {
    let dir = TempDir::new().unwrap();

    // Gitリポジトリを初期化
    Command::new("git")
        .args(["init"])
        .current_dir(dir.path())
        .output()
        .expect("Failed to init git repo");

    // Git設定(ローカルリポジトリのみ)
    Command::new("git")
        .args(["config", "user.name", "Test User"])
        .current_dir(dir.path())
        .output()
        .expect("Failed to set git user name");

    Command::new("git")
        .args(["config", "user.email", "test@example.com"])
        .current_dir(dir.path())
        .output()
        .expect("Failed to set git user email");

    // 初期コミットを作成
    fs::write(dir.path().join("README.md"), "# Test Repo").unwrap();
    Command::new("git")
        .args(["add", "."])
        .current_dir(dir.path())
        .output()
        .expect("Failed to add files");
    Command::new("git")
        .args(["commit", "-m", "Initial commit"])
        .current_dir(dir.path())
        .output()
        .expect("Failed to commit");

    dir
}

/// twinバイナリのパスを取得
fn get_twin_binary() -> String {
    let exe_path = std::env::current_exe().unwrap();
    let target_dir = exe_path.parent().unwrap().parent().unwrap();
    let twin_path = target_dir.join("twin");

    twin_path.to_string_lossy().to_string()
}

/// ユニークなワークツリーパスを生成(リポジトリ内に作成)
fn unique_worktree_path(name: &str) -> String {
    let id = uuid::Uuid::new_v4().to_string()[0..8].to_string();
    format!("wt-{name}-{id}")
}

// =============================================================================
// 1. addコマンドの基本テスト
// =============================================================================

#[test]
fn test_add_command_basic() {
    let repo = setup_test_repo();
    let twin = get_twin_binary();
    let worktree_path = unique_worktree_path("add");

    // twin add コマンドを実行
    let output = Command::new(&twin)
        .args(["add", &worktree_path, "-b", "test-branch"])
        .current_dir(repo.path())
        .output()
        .expect("Failed to execute twin add");

    let stderr = String::from_utf8_lossy(&output.stderr);
    let stdout = String::from_utf8_lossy(&output.stdout);

    eprintln!("STDOUT: {stdout}");
    eprintln!("STDERR: {stderr}");

    assert!(
        output.status.success(),
        "twin add should succeed. stderr: {stderr}"
    );

    // worktreeが作成されたことを確認
    let list_output = Command::new("git")
        .args(["worktree", "list"])
        .current_dir(repo.path())
        .output()
        .expect("Failed to list worktrees");

    let list_stdout = String::from_utf8_lossy(&list_output.stdout);
    assert!(list_stdout.contains(&worktree_path));
    assert!(list_stdout.contains("test-branch"));
}

#[test]
fn test_add_without_branch_option() {
    let repo = setup_test_repo();
    let twin = get_twin_binary();
    let worktree_path = unique_worktree_path("nobranch");

    // ブランチオプションなしでaddを実行
    // git worktreeと同様、HEADの状態でworktreeを作成する
    let output = Command::new(&twin)
        .args(["add", &worktree_path])
        .current_dir(repo.path())
        .output()
        .expect("Failed to execute twin add");

    // git worktreeと同様の動作:ブランチ指定がない場合もworktreeは作成される
    // (detached HEADまたはHEADのブランチを使用)
    assert!(
        output.status.success(),
        "twin add without branch should succeed like git worktree: {}",
        String::from_utf8_lossy(&output.stderr)
    );
}

#[test]
fn test_force_branch_option() {
    let repo = setup_test_repo();
    let twin = get_twin_binary();
    let worktree_path1 = unique_worktree_path("force1");
    let worktree_path2 = unique_worktree_path("force2");

    // 既存のブランチを作成
    Command::new("git")
        .args(["branch", "existing-branch"])
        .current_dir(repo.path())
        .output()
        .expect("Failed to create branch");

    // 最初のworktreeを作成
    Command::new(&twin)
        .args(["add", &worktree_path1, "-b", "existing-branch"])
        .current_dir(repo.path())
        .output()
        .expect("Failed to execute twin add");

    // -Bオプションで同じブランチ名を強制作成
    let output = Command::new(&twin)
        .args(["add", &worktree_path2, "-B", "existing-branch"])
        .current_dir(repo.path())
        .output()
        .expect("Failed to execute twin add");

    assert!(output.status.success(), "twin add with -B should succeed");
}

#[test]
fn test_detach_option() {
    let repo = setup_test_repo();
    let twin = get_twin_binary();
    let worktree_path = unique_worktree_path("detached");

    // HEADのコミットハッシュを取得
    let head_output = Command::new("git")
        .args(["rev-parse", "HEAD"])
        .current_dir(repo.path())
        .output()
        .expect("Failed to get HEAD");
    let _head_commit = String::from_utf8_lossy(&head_output.stdout)
        .trim()
        .to_string();

    // --detachオプションでworktreeを作成
    let output = Command::new(&twin)
        .args(["add", &worktree_path, "--detach"])
        .current_dir(repo.path())
        .output()
        .expect("Failed to execute twin add");

    assert!(
        output.status.success(),
        "Detach should succeed: {}",
        String::from_utf8_lossy(&output.stderr)
    );

    // worktreeがdetached状態であることを確認
    let list_output = Command::new("git")
        .args(["worktree", "list", "--porcelain"])
        .current_dir(repo.path())
        .output()
        .expect("Failed to list worktrees");

    let list_stdout = String::from_utf8_lossy(&list_output.stdout);
    assert!(list_stdout.contains("detached"));
}

#[test]
fn test_lock_option() {
    let repo = setup_test_repo();
    let twin = get_twin_binary();
    let worktree_path = unique_worktree_path("locked");

    // --lockオプションでworktreeを作成
    let output = Command::new(&twin)
        .args(["add", &worktree_path, "-b", "locked-branch", "--lock"])
        .current_dir(repo.path())
        .output()
        .expect("Failed to execute twin add");

    assert!(output.status.success());

    // worktreeがロックされていることを確認
    let list_output = Command::new("git")
        .args(["worktree", "list", "--porcelain"])
        .current_dir(repo.path())
        .output()
        .expect("Failed to list worktrees");

    let list_stdout = String::from_utf8_lossy(&list_output.stdout);
    assert!(list_stdout.contains("locked"));
}

#[test]
fn test_no_checkout_option() {
    let repo = setup_test_repo();
    let twin = get_twin_binary();
    let worktree_path = unique_worktree_path("nocheckout");

    // --no-checkoutオプションでworktreeを作成
    let output = Command::new(&twin)
        .args(["add", &worktree_path, "-b", "empty-branch", "--no-checkout"])
        .current_dir(repo.path())
        .output()
        .expect("Failed to execute twin add");

    assert!(
        output.status.success(),
        "No-checkout should succeed: {}",
        String::from_utf8_lossy(&output.stderr)
    );

    // worktreeディレクトリが空であることを確認
    let worktree_full_path = repo.path().join(&worktree_path);
    let entries: Vec<_> = fs::read_dir(&worktree_full_path)
        .expect("Failed to read worktree dir")
        .collect();
    // .gitディレクトリのみが存在するはず
    assert!(entries.len() <= 1, "Worktree should be nearly empty");
}

#[test]
fn test_quiet_option() {
    let repo = setup_test_repo();
    let twin = get_twin_binary();
    let worktree_path = unique_worktree_path("quiet");

    // --quietオプションでworktreeを作成
    let output = Command::new(&twin)
        .args(["add", &worktree_path, "-b", "quiet-branch", "--quiet"])
        .current_dir(repo.path())
        .output()
        .expect("Failed to execute twin add");

    assert!(output.status.success());

    // 出力が抑制されていることを確認
    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(
        stdout.is_empty() || stdout.trim().is_empty(),
        "Quiet mode should suppress output"
    );
}

#[test]
fn test_git_only_mode() {
    let repo = setup_test_repo();
    let twin = get_twin_binary();
    let worktree_path = unique_worktree_path("gitonly");

    // 設定ファイルを作成
    let config = r#"
[[files]]
path = "test.txt"
mapping_type = "symlink"
"#;
    fs::write(repo.path().join(".twin.toml"), config).unwrap();
    fs::write(repo.path().join("test.txt"), "test content").unwrap();

    // --git-onlyオプションでworktreeを作成
    let output = Command::new(&twin)
        .args([
            "add",
            &worktree_path,
            "-b",
            "git-only-branch",
            "--config",
            ".twin.toml",
            "--git-only",
        ])
        .current_dir(repo.path())
        .output()
        .expect("Failed to execute twin add");

    assert!(output.status.success());

    // シンボリックリンクが作成されていないことを確認
    let worktree_full_path = repo.path().join(&worktree_path);
    assert!(!worktree_full_path.join("test.txt").exists());
}

// =============================================================================
// 2. エラーハンドリングのテスト
// =============================================================================

#[test]
fn test_error_message_passthrough() {
    let repo = setup_test_repo();
    let twin = get_twin_binary();

    // 既に存在するブランチで同じパスにworktreeを作成しようとする(エラーになる)
    let worktree_path = unique_worktree_path("error");

    // 最初のworktreeを作成
    Command::new(&twin)
        .args(["add", &worktree_path, "-b", "test-branch"])
        .current_dir(repo.path())
        .output()
        .expect("Failed to execute first twin add");

    // 同じパスで別のworktreeを作成しようとする
    let output = Command::new(&twin)
        .args(["add", &worktree_path, "-b", "another-branch"])
        .current_dir(repo.path())
        .output()
        .expect("Failed to execute twin add");

    assert!(!output.status.success());

    // gitのエラーメッセージが表示されることを確認
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        stderr.contains("Error") || stderr.contains("fatal") || stderr.contains("already exists")
    );
}

#[test]
fn test_invalid_branch_name_error() {
    let repo = setup_test_repo();
    let twin = get_twin_binary();
    let worktree_path = unique_worktree_path("invalid");

    // 無効なブランチ名でaddを実行
    let output = Command::new(&twin)
        .args(["add", &worktree_path, "-b", "invalid..branch"])
        .current_dir(repo.path())
        .output()
        .expect("Failed to execute twin add");

    assert!(!output.status.success());

    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        stderr.contains("invalid") || stderr.contains("fatal"),
        "Should show branch name error"
    );
}

// =============================================================================
// 3. listコマンドのテスト
// =============================================================================

#[test]
fn test_list_includes_manual_worktree() {
    let repo = setup_test_repo();
    let twin = get_twin_binary();
    let worktree_path = unique_worktree_path("manual");

    // 手動でgit worktreeを作成
    Command::new("git")
        .args(["worktree", "add", &worktree_path, "-b", "manual-branch"])
        .current_dir(repo.path())
        .output()
        .expect("Failed to create manual worktree");

    // twin listを実行
    let output = Command::new(&twin)
        .args(["list"])
        .current_dir(repo.path())
        .output()
        .expect("Failed to execute twin list");

    assert!(output.status.success());

    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(stdout.contains("manual-branch"));
}

// =============================================================================
// 4. removeコマンドのテスト
// =============================================================================

#[test]
fn test_remove_manual_worktree() {
    let repo = setup_test_repo();
    let twin = get_twin_binary();
    let worktree_path = unique_worktree_path("remove");

    // 手動でgit worktreeを作成
    Command::new("git")
        .args(["worktree", "add", &worktree_path, "-b", "to-remove"])
        .current_dir(repo.path())
        .output()
        .expect("Failed to create manual worktree");

    // twin removeを実行
    let output = Command::new(&twin)
        .args(["remove", &worktree_path, "--force"])
        .current_dir(repo.path())
        .output()
        .expect("Failed to execute twin remove");

    assert!(output.status.success());

    // worktreeが削除されたことを確認
    let list_output = Command::new("git")
        .args(["worktree", "list"])
        .current_dir(repo.path())
        .output()
        .expect("Failed to list worktrees");

    let list_stdout = String::from_utf8_lossy(&list_output.stdout);
    assert!(!list_stdout.contains("to-remove"));
}

// =============================================================================
// 5. git worktreeとの一致性テスト
// =============================================================================

#[test]
fn test_output_matches_git_worktree() {
    let repo = setup_test_repo();
    let twin = get_twin_binary();
    let worktree_path1 = unique_worktree_path("git1");
    let worktree_path2 = unique_worktree_path("twin1");

    // git worktree addを直接実行
    let git_output = Command::new("git")
        .args(["worktree", "add", &worktree_path1, "-b", "git-branch"])
        .current_dir(repo.path())
        .output()
        .expect("Failed to execute git worktree add");

    // twin addを実行
    let twin_output = Command::new(&twin)
        .args(["add", &worktree_path2, "-b", "twin-branch"])
        .current_dir(repo.path())
        .output()
        .expect("Failed to execute twin add");

    // 両方とも成功することを確認
    assert_eq!(git_output.status.success(), twin_output.status.success());

    // worktree listで両方が表示されることを確認
    let list_output = Command::new("git")
        .args(["worktree", "list"])
        .current_dir(repo.path())
        .output()
        .expect("Failed to list worktrees");

    let list_stdout = String::from_utf8_lossy(&list_output.stdout);
    assert!(list_stdout.contains("git-branch"));
    assert!(list_stdout.contains("twin-branch"));
}