Skip to content

Commit c05ca37

Browse files
committed
fix: improve directory handling and test execution in clean command
- Updated the `clean.sh` script to enhance the logic for finding empty directories by using `grep -Fxv` for better accuracy. - Modified tests in `cmd_clean.bats` and `hooks.bats` to use `run` for executing commands, improving consistency in test status checks and output handling. - Ensured that hooks in `hooks.bats` reference the correct variable for repository paths, enhancing portability and reliability of hook execution.
1 parent 4e3f724 commit c05ca37

File tree

3 files changed

+12
-12
lines changed

3 files changed

+12
-12
lines changed

lib/commands/clean.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ cmd_clean() {
159159
# Find and remove empty directories
160160
local cleaned=0
161161
local empty_dirs
162-
empty_dirs=$(find "$base_dir" -maxdepth 1 -type d -empty 2>/dev/null | grep -v "^${base_dir}$" || true)
162+
empty_dirs=$(find "$base_dir" -maxdepth 1 -type d -empty 2>/dev/null | grep -Fxv "$base_dir" || true)
163163

164164
if [ -n "$empty_dirs" ]; then
165165
while IFS= read -r dir; do

tests/cmd_clean.bats

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,28 +47,28 @@ teardown() {
4747
# ── _clean_should_skip ──────────────────────────────────────────────────────
4848

4949
@test "_clean_should_skip skips detached HEAD" {
50-
_clean_should_skip "/some/dir" "(detached)"
51-
# Returns 0 = should skip
50+
run _clean_should_skip "/some/dir" "(detached)"
51+
[ "$status" -eq 0 ]
5252
}
5353

5454
@test "_clean_should_skip skips empty branch" {
55-
_clean_should_skip "/some/dir" ""
56-
# Returns 0 = should skip
55+
run _clean_should_skip "/some/dir" ""
56+
[ "$status" -eq 0 ]
5757
}
5858

5959
@test "_clean_should_skip skips dirty worktree" {
6060
create_test_worktree "dirty-test"
6161
echo "dirty" > "$TEST_WORKTREES_DIR/dirty-test/untracked.txt"
6262
git -C "$TEST_WORKTREES_DIR/dirty-test" add untracked.txt
63-
_clean_should_skip "$TEST_WORKTREES_DIR/dirty-test" "dirty-test"
64-
# Returns 0 = should skip (staged changes)
63+
run _clean_should_skip "$TEST_WORKTREES_DIR/dirty-test" "dirty-test"
64+
[ "$status" -eq 0 ]
6565
}
6666

6767
@test "_clean_should_skip skips worktree with untracked files" {
6868
create_test_worktree "untracked-test"
6969
echo "new" > "$TEST_WORKTREES_DIR/untracked-test/newfile.txt"
70-
_clean_should_skip "$TEST_WORKTREES_DIR/untracked-test" "untracked-test"
71-
# Returns 0 = should skip
70+
run _clean_should_skip "$TEST_WORKTREES_DIR/untracked-test" "untracked-test"
71+
[ "$status" -eq 0 ]
7272
}
7373

7474
@test "_clean_should_skip does not skip clean worktree" {

tests/hooks.bats

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ teardown() {
1818
}
1919

2020
@test "run_hooks executes single hook" {
21-
git config --add gtr.hook.postCreate "touch $TEST_REPO/hook-ran"
21+
git config --add gtr.hook.postCreate 'touch "$REPO_ROOT/hook-ran"'
2222
run_hooks postCreate REPO_ROOT="$TEST_REPO"
2323
[ -f "$TEST_REPO/hook-ran" ]
2424
}
@@ -36,8 +36,8 @@ teardown() {
3636
}
3737

3838
@test "run_hooks executes multiple hooks in order" {
39-
git config --add gtr.hook.postCreate "echo first >> $TEST_REPO/order"
40-
git config --add gtr.hook.postCreate "echo second >> $TEST_REPO/order"
39+
git config --add gtr.hook.postCreate 'echo first >> "$REPO_ROOT/order"'
40+
git config --add gtr.hook.postCreate 'echo second >> "$REPO_ROOT/order"'
4141
run_hooks postCreate REPO_ROOT="$TEST_REPO"
4242
[ "$(head -1 "$TEST_REPO/order")" = "first" ]
4343
[ "$(tail -1 "$TEST_REPO/order")" = "second" ]

0 commit comments

Comments
 (0)