tbdflow 0.27.0

A CLI to streamline your Git workflow for Trunk-Based Development.
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
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
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
use assert_cmd::Command;
use chrono::{Duration, Utc};
use predicates::str::contains;
use predicates::str::is_match;
use serial_test::serial;

mod util;
use util::setup_temp_git_repo;

/// Tests that the status command outputs the expected status message.
#[test]
#[serial]
fn test_status_command() {
    // Add the setup function to create a git repo for the test
    let (_dir, _bare_dir, repo_path) = setup_temp_git_repo();
    std::env::set_current_dir(&repo_path).unwrap();

    let mut cmd = Command::cargo_bin("tbdflow").unwrap();
    cmd.arg("--verbose").arg("status");
    cmd.assert().success().stdout(contains("Checking status"));
}

/// Tests that the current branch command outputs the expected branch name.
#[test]
#[serial]
fn test_current_branch_command() {
    let (_dir, _bare_dir, repo_path) = setup_temp_git_repo();
    std::env::set_current_dir(&repo_path).unwrap();
    let mut cmd = Command::cargo_bin("tbdflow").unwrap();
    cmd.arg("current-branch");
    cmd.assert()
        .success()
        .stdout(contains("Current branch is:"));
}

#[test]
#[serial]
fn test_branch_command() {
    // 'branch-name' strategy
    let (_dir, _bare_dir, repo_path) = setup_temp_git_repo();
    std::env::set_current_dir(&repo_path).unwrap();

    // Create a minimal but complete .tbdflow.yml file
    let config_content = r#"
main_branch_name: main
stale_branch_threshold_days: 1
issue_handling:
  strategy: "branch-name"
branch_types:
  feat: "feat/"
  fix: "fix/"
branch_prefixes:
  feature: "feature_"
  release: "release_"
  hotfix: "hotfix_"
automatic_tags:
  release_prefix: "v"
  hotfix_prefix: "hotfix-tag_"
"#;
    std::fs::write(repo_path.join(".tbdflow.yml"), config_content).unwrap();

    // Commit the config file to clean the working directory
    std::process::Command::new("git")
        .args(&["add", ".tbdflow.yml"])
        .current_dir(&repo_path)
        .output()
        .unwrap();
    std::process::Command::new("git")
        .args(&["commit", "-m", "Add tbdflow config"])
        .current_dir(&repo_path)
        .output()
        .unwrap();

    // Test creating a branch WITH an issue ID
    let mut cmd_with_issue = Command::cargo_bin("tbdflow").unwrap();
    cmd_with_issue
        .arg("branch")
        .arg("--type")
        .arg("feat")
        .arg("--name")
        .arg("new-dashboard")
        .arg("--issue")
        .arg("PROJ-123");

    cmd_with_issue.assert().success().stdout(contains(
        "Success! Switched to new branch: 'feat/PROJ-123-new-dashboard'",
    ));

    // 'commit-scope' strategy
    let (_dir2, _bare_dir2, repo_path2) = setup_temp_git_repo();
    std::env::set_current_dir(&repo_path2).unwrap();

    // Create a minimal but complete .tbdflow.yml file
    let config_content_2 = r#"
main_branch_name: main
stale_branch_threshold_days: 1
issue_handling:
  strategy: "commit-scope"
branch_types:
  feat: "feat/"
  fix: "fix/"
branch_prefixes:
  feature: "feature_"
  release: "release_"
  hotfix: "hotfix_"
automatic_tags:
  release_prefix: "v"
  hotfix_prefix: "hotfix-tag_"
"#;
    std::fs::write(repo_path2.join(".tbdflow.yml"), config_content_2).unwrap();

    // Commit the config file to clean the working directory
    std::process::Command::new("git")
        .args(&["add", ".tbdflow.yml"])
        .current_dir(&repo_path2)
        .output()
        .unwrap();
    std::process::Command::new("git")
        .args(&["commit", "-m", "Add tbdflow config"])
        .current_dir(&repo_path2)
        .output()
        .unwrap();

    // Test creating a branch WITHOUT an issue ID in the name
    let mut cmd_without_issue = Command::cargo_bin("tbdflow").unwrap();
    cmd_without_issue
        .arg("branch")
        .arg("--type")
        .arg("fix")
        .arg("--name")
        .arg("login-bug")
        .arg("--issue")
        .arg("PROJ-456"); // This should be ignored for the branch name

    cmd_without_issue
        .assert()
        .success()
        .stdout(contains("Success! Switched to new branch: 'fix/login-bug'"));
}

/// Tests that creating a new feature branch called "new-feature" works correctly.
#[test]
#[serial]
fn test_create_feature_branch_command() {
    let (_dir, _bare_dir, repo_path) = setup_temp_git_repo();
    std::env::set_current_dir(&repo_path).unwrap();
    let mut cmd = Command::cargo_bin("tbdflow").unwrap();
    cmd.arg("branch")
        .arg("--type")
        .arg("feature")
        .arg("--name")
        .arg("new-feature");
    cmd.assert().success().stdout(contains(
        "Success! Switched to new branch: 'feature_new-feature'",
    ));
}

/// Tests that creating a new release branch called "1.0.0" works correctly.
#[test]
#[serial]
fn test_create_release_branch_command() {
    let (_dir, _bare_dir, repo_path) = setup_temp_git_repo();
    std::env::set_current_dir(&repo_path).unwrap();
    let mut cmd = Command::cargo_bin("tbdflow").unwrap();
    cmd.arg("branch")
        .arg("--type")
        .arg("release")
        .arg("--name")
        .arg("1.0.0");
    cmd.assert()
        .success()
        .stdout(contains("Success! Switched to new branch: 'release_1.0.0'"));
}

/// Tests that adding a new file and committing it with the commit command works correctly.
/// Skipping .dod.yml verification for simplicity in this test.
#[test]
#[serial]
fn test_commit_command() {
    let (_dir, _bare_dir, repo_path) = setup_temp_git_repo();
    std::env::set_current_dir(&repo_path).unwrap();

    // Create a file to commit
    std::fs::write(repo_path.join("BUTTON.md"), "this is a new button â– ").unwrap();
    // Stage the file
    Command::new("git")
        .arg("add")
        .arg("BUTTON.md")
        .current_dir(&repo_path)
        .output()
        .unwrap();

    // Wait until the working directory is clean
    let mut retries = 5;
    while retries > 0 {
        let status = Command::new("git")
            .args(&["status", "--porcelain"])
            .current_dir(&repo_path)
            .output()
            .unwrap();
        if status.stdout.is_empty() {
            break;
        }
        std::thread::sleep(std::time::Duration::from_millis(100));
        retries -= 1;
    }

    let mut cmd = Command::cargo_bin("tbdflow").unwrap();
    // Run the commit command with a feature type, scope, message, and breaking change flag
    cmd.arg("commit")
        .arg("--type")
        .arg("feat")
        .arg("--scope")
        .arg("ui")
        .arg("--message")
        .arg("add new button")
        .arg("--body")
        .arg("This button is used for submitting forms.")
        .arg("--breaking")
        .arg("--tag")
        .arg("button-v1");
    cmd.assert().success().stdout(
        /*
        Depending on Git version used, output may vary slightly. For example:
        - Successfully committed and pushed changes to BRANCH_NAME.
        - Successfully pushed changes to 'BRANCH_NAME'.

        Also default branch name can be 'main' or 'master', depending on the Git version
        and distribution. Below is a regex that matches all those cases.
        */
        is_match(r"Successfully (?:committed and )?pushed changes to '?(?:main|master)'?\.")
            .unwrap(),
    );

    // Check that the tag exists
    let output = std::process::Command::new("git")
        .arg("tag")
        .current_dir(&repo_path)
        .output()
        .unwrap();
    let tags = String::from_utf8_lossy(&output.stdout);
    assert!(
        tags.contains("button-v1"),
        "Expected tag button-v1 not found. Tags: {}",
        tags
    );
}

/// Tests that completing a feature branch called "new-feature" works correctly.
#[test]
#[serial]
fn test_complete_feature_branch_command() {
    let (_dir, _bare_dir, repo_path) = setup_temp_git_repo();
    std::env::set_current_dir(&repo_path).unwrap();

    // Create the feature branch first
    let mut create_cmd = Command::cargo_bin("tbdflow").unwrap();
    create_cmd
        .arg("branch")
        .arg("--type")
        .arg("feature")
        .arg("--name")
        .arg("new-feature");
    create_cmd.assert().success();

    let mut cmd = Command::cargo_bin("tbdflow").unwrap();
    cmd.arg("complete")
        .arg("--type")
        .arg("feature")
        .arg("--name")
        .arg("new-feature");
    cmd.assert()
        .success()
        .stdout(contains("Branch to complete: feature_new-feature"));
}

/// Tests that completing a release branch called "1.0.0" works correctly.
#[test]
#[serial]
fn test_complete_release_branch_command() {
    let (_dir, _bare_dir, repo_path) = setup_temp_git_repo();
    std::env::set_current_dir(&repo_path).unwrap();

    // Create the release branch first
    let mut create_cmd = Command::cargo_bin("tbdflow").unwrap();
    create_cmd
        .arg("branch")
        .arg("--type")
        .arg("release")
        .arg("--name")
        .arg("1.0.0");
    create_cmd.assert().success();

    // Ensure the branch exists locally
    let output = std::process::Command::new("git")
        .args(&["branch", "--list"])
        .current_dir(&repo_path)
        .output()
        .unwrap();
    let branches = String::from_utf8_lossy(&output.stdout);
    assert!(
        branches.contains("release_1.0.0"),
        "Expected branch release_1.0.0 not found. Branches: {}",
        branches
    );

    let mut cmd = Command::cargo_bin("tbdflow").unwrap();
    cmd.arg("--verbose")
        .arg("complete")
        .arg("--type")
        .arg("release")
        .arg("--name")
        .arg("1.0.0");
    cmd.assert()
        .success()
        .stdout(contains("Branch to complete: release_1.0.0"));

    // Check that the tag exists
    let output = std::process::Command::new("git")
        .arg("tag")
        .current_dir(&repo_path)
        .output()
        .unwrap();
    let tags = String::from_utf8_lossy(&output.stdout);
    assert!(
        tags.contains("v1.0.0"),
        "Expected tag v1.0.0 not found. Tags: {}",
        tags
    );
}

/// Testing the synch command to ensure it pulls changes from the remote repository
/// We will simulate a remote change by pushing to a bare repository and then running the sync command.
#[test]
#[serial]
fn test_sync_command() {
    let (_dir, bare_dir, repo_path) = setup_temp_git_repo();

    // Simulate a team member pushing changes to the remote
    let remote_repo_url = bare_dir.path().to_str().unwrap();
    let second_clone_dir = tempfile::tempdir().unwrap();
    std::process::Command::new("git")
        .args(&["clone", remote_repo_url, "."])
        .current_dir(second_clone_dir.path())
        .output()
        .unwrap();

    std::fs::write(second_clone_dir.path().join("REMOTE.md"), "remote change").unwrap();

    std::process::Command::new("git")
        .args(&["config", "user.email", "teammate@example.com"])
        .current_dir(second_clone_dir.path())
        .output()
        .unwrap();
    std::process::Command::new("git")
        .args(&["config", "user.name", "Teammate"])
        .current_dir(second_clone_dir.path())
        .output()
        .unwrap();

    std::process::Command::new("git")
        .args(&["add", "."])
        .current_dir(second_clone_dir.path())
        .output()
        .unwrap();
    std::process::Command::new("git")
        .args(&["commit", "-m", "feat: add remote file"])
        .current_dir(second_clone_dir.path())
        .output()
        .unwrap();
    std::process::Command::new("git")
        .args(&["push"])
        .current_dir(second_clone_dir.path())
        .output()
        .unwrap();

    // Now run the sync command
    std::env::set_current_dir(&repo_path).unwrap();
    let mut cmd = Command::cargo_bin("tbdflow").unwrap();
    cmd.arg("sync");
    cmd.assert()
        .success()
        .stdout(contains("Syncing with remote"));
}

/// Tests that the 'undo' command reverts a specific commit by SHA on the trunk.
#[test]
#[serial]
fn test_undo_command() {
    let (_dir, _bare_dir, repo_path) = setup_temp_git_repo();
    std::env::set_current_dir(&repo_path).unwrap();

    // Create a file and commit it so we have something to revert
    std::fs::write(repo_path.join("BAD_CHANGE.md"), "this breaks the build").unwrap();
    std::process::Command::new("git")
        .args(&["add", "."])
        .current_dir(&repo_path)
        .output()
        .unwrap();
    std::process::Command::new("git")
        .args(&["commit", "-m", "feat: add bad change"])
        .current_dir(&repo_path)
        .output()
        .unwrap();
    std::process::Command::new("git")
        .args(&["push"])
        .current_dir(&repo_path)
        .output()
        .unwrap();

    // Grab the SHA of the commit we just made using tbdflow head-sha
    let sha_output = Command::cargo_bin("tbdflow")
        .unwrap()
        .arg("head-sha")
        .output()
        .unwrap();
    let sha = String::from_utf8_lossy(&sha_output.stdout)
        .trim()
        .to_string();

    // The file should exist before undo
    assert!(repo_path.join("BAD_CHANGE.md").exists());

    // Run the undo command
    let mut cmd = Command::cargo_bin("tbdflow").unwrap();
    cmd.arg("undo").arg(&sha);
    cmd.assert()
        .success()
        .stdout(contains("Undo: The Panic Button"))
        .stdout(contains(&format!("Commit '{}' has been reverted", &sha)));

    // The file should be gone after the revert
    assert!(
        !repo_path.join("BAD_CHANGE.md").exists(),
        "BAD_CHANGE.md should have been removed by the revert"
    );
}

/// Tests that 'undo' with --no-push reverts locally without pushing.
#[test]
#[serial]
fn test_undo_no_push_command() {
    let (_dir, _bare_dir, repo_path) = setup_temp_git_repo();
    std::env::set_current_dir(&repo_path).unwrap();

    std::fs::write(repo_path.join("OOPS.md"), "oops").unwrap();
    std::process::Command::new("git")
        .args(&["add", "."])
        .current_dir(&repo_path)
        .output()
        .unwrap();
    std::process::Command::new("git")
        .args(&["commit", "-m", "feat: oops"])
        .current_dir(&repo_path)
        .output()
        .unwrap();
    std::process::Command::new("git")
        .args(&["push"])
        .current_dir(&repo_path)
        .output()
        .unwrap();

    let sha_output = Command::cargo_bin("tbdflow")
        .unwrap()
        .arg("head-sha")
        .output()
        .unwrap();
    let sha = String::from_utf8_lossy(&sha_output.stdout)
        .trim()
        .to_string();

    let mut cmd = Command::cargo_bin("tbdflow").unwrap();
    cmd.arg("undo").arg(&sha).arg("--no-push");
    cmd.assert()
        .success()
        .stdout(contains("Revert commit created locally (--no-push)"));

    assert!(!repo_path.join("OOPS.md").exists());
}

/// Tests that 'undo' fails gracefully for a non-existent SHA.
#[test]
#[serial]
fn test_undo_nonexistent_sha() {
    let (_dir, _bare_dir, repo_path) = setup_temp_git_repo();
    std::env::set_current_dir(&repo_path).unwrap();

    let mut cmd = Command::cargo_bin("tbdflow").unwrap();
    cmd.arg("undo").arg("deadbeefdeadbeef");
    cmd.assert().failure();
}

/// Tests that the 'check-branches' lists and warns about branches that are stale (older than 1 day)
#[test]
#[serial]
fn test_check_branches_command() {
    let (_dir, _bare_dir, repo_path) = setup_temp_git_repo();
    std::env::set_current_dir(&repo_path).unwrap();

    // Create a feature branch that is stale
    let mut create_cmd = Command::cargo_bin("tbdflow").unwrap();
    create_cmd
        .arg("branch")
        .arg("--type")
        .arg("feature")
        .arg("--name")
        .arg("stale-feature");
    create_cmd.assert().success();

    let old_date = (Utc::now() - Duration::hours(25)).to_rfc3339();

    std::process::Command::new("git")
        .args(&["commit", "--allow-empty", "-m", "stale commit"])
        .env("GIT_COMMITTER_DATE", &old_date) // Set the committer date
        .current_dir(&repo_path)
        .output()
        .unwrap();
    // Switch to main branch to ensure the stale branch is not the current one
    std::process::Command::new("git")
        .args(&["checkout", "main"])
        .current_dir(&repo_path)
        .output()
        .unwrap();

    let mut cmd = Command::cargo_bin("tbdflow").unwrap();
    cmd.arg("check-branches");
    cmd.assert()
        .success()
        .stdout(contains("Warning: The following branches may be stale:"))
        .stdout(contains("feature_stale-feature"));
}

/// Tests that the radar command shows disabled message when radar is not enabled.
#[test]
#[serial]
fn test_radar_disabled_by_default() {
    let (_dir, _bare_dir, repo_path) = setup_temp_git_repo();
    std::env::set_current_dir(&repo_path).unwrap();

    let config_content = r#"
main_branch_name: main
stale_branch_threshold_days: 1
branch_types:
  feat: "feat/"
automatic_tags:
  release_prefix: "v"
"#;
    std::fs::write(repo_path.join(".tbdflow.yml"), config_content).unwrap();

    let mut cmd = Command::cargo_bin("tbdflow").unwrap();
    cmd.arg("radar");
    cmd.assert().success().stdout(contains("Radar is disabled"));
}

/// Tests that the radar command runs successfully when enabled with no overlaps.
#[test]
#[serial]
fn test_radar_no_overlaps() {
    let (_dir, _bare_dir, repo_path) = setup_temp_git_repo();
    std::env::set_current_dir(&repo_path).unwrap();

    let config_content = r#"
main_branch_name: main
stale_branch_threshold_days: 1
radar:
  enabled: true
  level: file
branch_types:
  feat: "feat/"
automatic_tags:
  release_prefix: "v"
"#;
    std::fs::write(repo_path.join(".tbdflow.yml"), config_content).unwrap();
    // Commit the config so working directory is clean for radar
    std::process::Command::new("git")
        .args(&["add", ".tbdflow.yml"])
        .current_dir(&repo_path)
        .output()
        .unwrap();
    std::process::Command::new("git")
        .args(&["commit", "-m", "chore: add config"])
        .current_dir(&repo_path)
        .output()
        .unwrap();
    std::process::Command::new("git")
        .args(&["push"])
        .current_dir(&repo_path)
        .output()
        .unwrap();

    // Modify an existing tracked file so radar has local changes to scan
    std::fs::write(repo_path.join("README.md"), "modified locally").unwrap();

    let mut cmd = Command::cargo_bin("tbdflow").unwrap();
    cmd.arg("radar");
    cmd.assert()
        .success()
        .stdout(contains("No overlaps detected"));
}

/// Tests that the radar command detects file-level overlaps with an active remote branch.
#[test]
#[serial]
fn test_radar_detects_file_overlap() {
    let (_dir, _bare_dir, repo_path) = setup_temp_git_repo();
    std::env::set_current_dir(&repo_path).unwrap();

    let config_content = r#"
main_branch_name: main
stale_branch_threshold_days: 1
radar:
  enabled: true
  level: file
branch_types:
  feat: "feat/"
automatic_tags:
  release_prefix: "v"
"#;
    std::fs::write(repo_path.join(".tbdflow.yml"), config_content).unwrap();
    // Commit the config so it's part of the repo
    std::process::Command::new("git")
        .args(&["add", ".tbdflow.yml"])
        .current_dir(&repo_path)
        .output()
        .unwrap();
    std::process::Command::new("git")
        .args(&["commit", "-m", "chore: add config"])
        .current_dir(&repo_path)
        .output()
        .unwrap();
    std::process::Command::new("git")
        .args(&["push"])
        .current_dir(&repo_path)
        .output()
        .unwrap();

    // Create a feature branch, modify README.md, push it
    std::process::Command::new("git")
        .args(&["checkout", "-b", "feat/other-work"])
        .current_dir(&repo_path)
        .output()
        .unwrap();
    std::fs::write(repo_path.join("README.md"), "changed by teammate").unwrap();
    std::process::Command::new("git")
        .args(&["add", "."])
        .current_dir(&repo_path)
        .output()
        .unwrap();
    std::process::Command::new("git")
        .args(&["commit", "-m", "feat: teammate changes"])
        .current_dir(&repo_path)
        .output()
        .unwrap();
    std::process::Command::new("git")
        .args(&["push", "-u", "origin", "feat/other-work"])
        .current_dir(&repo_path)
        .output()
        .unwrap();

    // Switch back to main and make a local change to the same file
    std::process::Command::new("git")
        .args(&["checkout", "main"])
        .current_dir(&repo_path)
        .output()
        .unwrap();
    std::fs::write(repo_path.join("README.md"), "my local change").unwrap();

    let mut cmd = Command::cargo_bin("tbdflow").unwrap();
    cmd.arg("radar");
    cmd.assert()
        .success()
        .stdout(contains("OVERLAP DETECTED"))
        .stdout(contains("README.md"))
        .stdout(contains("feat/other-work"));
}