-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_flash.sh
More file actions
executable file
·155 lines (135 loc) · 4.99 KB
/
test_flash.sh
File metadata and controls
executable file
·155 lines (135 loc) · 4.99 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
#!/usr/bin/env bash
# Integration test for flash CLI.
# Creates a temp repo with a worktree, runs all commands, and verifies results.
set -euo pipefail
RED='\033[0;31m'
GREEN='\033[0;32m'
NC='\033[0m'
PASS=0
FAIL=0
assert_eq() {
local desc="$1" expected="$2" actual="$3"
if [ "$expected" = "$actual" ]; then
echo -e "${GREEN}PASS${NC}: $desc"
PASS=$((PASS + 1))
else
echo -e "${RED}FAIL${NC}: $desc"
echo " expected: $expected"
echo " actual: $actual"
FAIL=$((FAIL + 1))
fi
}
assert_contains() {
local desc="$1" needle="$2" haystack="$3"
if echo "$haystack" | grep -q "$needle"; then
echo -e "${GREEN}PASS${NC}: $desc"
PASS=$((PASS + 1))
else
echo -e "${RED}FAIL${NC}: $desc"
echo " expected to contain: $needle"
echo " actual: $haystack"
FAIL=$((FAIL + 1))
fi
}
assert_file_content() {
local desc="$1" file="$2" expected="$3"
local actual
actual=$(cat "$file")
assert_eq "$desc" "$expected" "$actual"
}
# --- Setup ---
TMPDIR=$(mktemp -d)
REPO="$TMPDIR/repo"
WT="$TMPDIR/worktree"
trap "rm -rf $TMPDIR" EXIT
mkdir "$REPO"
cd "$REPO"
git init -q
echo "original" > file.txt
git add . && git commit -q -m "initial"
git worktree add -q "$WT" -b test-branch
echo "committed-change" > "$WT/new-file.txt"
(cd "$WT" && git add . && git commit -q -m "worktree commit")
# Add uncommitted change in worktree
echo "uncommitted-change" > "$WT/uncommitted.txt"
echo "committed-change
extra-line" > "$WT/new-file.txt"
# Dirty the main checkout
echo "dirty" > "$REPO/dirty.txt"
echo ""
echo "=== Test 1: flash into ==="
cd "$REPO"
output=$(flash into test-branch 2>&1)
assert_contains "stash message" "Stashing" "$output"
assert_contains "flash message" "Flashed into" "$output"
echo ""
echo "=== Test 2: flash status ==="
output=$(flash status 2>&1)
assert_contains "shows branch" "test-branch" "$output"
assert_contains "shows original" "main" "$output"
echo ""
echo "=== Test 3: uncommitted changes copied in ==="
assert_file_content "uncommitted file present" "$REPO/uncommitted.txt" "uncommitted-change"
assert_file_content "modified file has worktree state" "$REPO/new-file.txt" "committed-change
extra-line"
echo ""
echo "=== Test 4: double flash refused ==="
output=$(flash into test-branch 2>&1 || true)
assert_contains "refuses double flash" "Already flashed" "$output"
echo ""
echo "=== Test 5: flash apply (unstaged files) ==="
echo "flash-fix" >> "$REPO/new-file.txt"
output=$(flash apply 2>&1)
assert_contains "apply syncs files" "Synced" "$output"
assert_file_content "worktree gets unstaged fix" "$WT/new-file.txt" "committed-change
extra-line
flash-fix"
echo ""
echo "=== Test 6: flash apply (commits) ==="
echo "committed-fix" > "$REPO/committed-file.txt"
(cd "$REPO" && git add . && git commit -q -m "fix during flash")
output=$(flash apply 2>&1)
assert_contains "cherry-pick message" "Cherry-picked 1 commit" "$output"
# The committed file should now be in the worktree's git history
wt_has_commit=$(cd "$WT" && git log --oneline | grep -c "fix during flash" || true)
assert_eq "commit landed in worktree" "1" "$wt_has_commit"
echo ""
echo "=== Test 7: flash out --apply (commits + unstaged) ==="
echo "second-committed" > "$REPO/second.txt"
(cd "$REPO" && git add . && git commit -q -m "second fix")
echo "loose-change" > "$REPO/loose.txt"
output=$(flash out --apply 2>&1)
assert_contains "cherry-pick on exit" "Cherry-picked 1 commit" "$output"
assert_contains "sync on exit" "Synced" "$output"
assert_contains "back on main" "Back on" "$output"
# Verify commit landed in worktree
wt_has_second=$(cd "$WT" && git log --oneline | grep -c "second fix" || true)
assert_eq "second commit in worktree" "1" "$wt_has_second"
# Verify unstaged file synced
assert_file_content "loose file in worktree" "$WT/loose.txt" "loose-change"
# Verify restoration
assert_eq "back on main" "main" "$(git branch --show-current)"
assert_file_content "stash restored" "$REPO/dirty.txt" "dirty"
assert_eq "no .flash dir" "false" "$([ -d "$REPO/.flash" ] && echo true || echo false)"
assert_eq "temp branch deleted" "" "$(git branch --list 'flash/*')"
echo ""
echo "=== Test 8: flash out --discard ==="
flash into test-branch >/dev/null 2>&1
echo "throwaway" >> "$REPO/new-file.txt"
wt_before=$(cd "$WT" && git log --oneline | wc -l | tr -d ' ')
flash out --discard >/dev/null 2>&1
wt_after=$(cd "$WT" && git log --oneline | wc -l | tr -d ' ')
assert_eq "discard doesn't touch worktree" "$wt_before" "$wt_after"
echo ""
echo "=== Test 9: flash from worktree directory ==="
cd "$WT"
flash into test-branch >/dev/null 2>&1
output=$(flash status 2>&1)
assert_contains "works from worktree" "Flashed into: test-branch" "$output"
canonical_branch=$(cd "$REPO" && git branch --show-current)
assert_eq "canonical checkout switched" "flash/test-branch" "$canonical_branch"
flash out --discard >/dev/null 2>&1
echo ""
echo "================================"
echo -e "Results: ${GREEN}$PASS passed${NC}, ${RED}$FAIL failed${NC}"
[ "$FAIL" -eq 0 ] && exit 0 || exit 1