Skip to content

Commit af1055b

Browse files
committed
Add test script
1 parent 9945f64 commit af1055b

File tree

1 file changed

+253
-0
lines changed

1 file changed

+253
-0
lines changed

test.sh

Lines changed: 253 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,253 @@
1+
#!/bin/bash
2+
3+
set -euo pipefail
4+
5+
# Colors for output
6+
RED='\033[0;31m'
7+
GREEN='\033[0;32m'
8+
YELLOW='\033[1;33m'
9+
NC='\033[0m' # No Color
10+
11+
# Test counters
12+
TESTS_RUN=0
13+
TESTS_PASSED=0
14+
TESTS_FAILED=0
15+
16+
# Test helper functions
17+
test_start() {
18+
echo -e "${YELLOW}Testing: $1${NC}"
19+
TESTS_RUN=$((TESTS_RUN + 1))
20+
}
21+
22+
test_pass() {
23+
echo -e "${GREEN}✓ PASS${NC}"
24+
TESTS_PASSED=$((TESTS_PASSED + 1))
25+
echo
26+
}
27+
28+
test_fail() {
29+
echo -e "${RED}✗ FAIL: $1${NC}"
30+
TESTS_FAILED=$((TESTS_FAILED + 1))
31+
echo
32+
}
33+
34+
# Create temporary directory for testing
35+
TEST_DIR=$(mktemp -d)
36+
echo "Using test directory: $TEST_DIR"
37+
38+
# Create a mock git command that doesn't actually clone
39+
MOCK_GIT_DIR="$TEST_DIR/mock-bin"
40+
mkdir -p "$MOCK_GIT_DIR"
41+
42+
cat > "$MOCK_GIT_DIR/git" << 'EOF'
43+
#!/bin/bash
44+
# Mock git command for testing
45+
if [ "$1" = "config" ]; then
46+
# Handle git config calls
47+
if [ "$2" = "get.location" ]; then
48+
# Return test config if set
49+
if [ -f "$TEST_CONFIG_FILE" ]; then
50+
cat "$TEST_CONFIG_FILE"
51+
else
52+
exit 1 # Config not set
53+
fi
54+
fi
55+
elif [ "$1" = "clone" ]; then
56+
# Mock git clone - just create the target directory
57+
TARGET_DIR="${@: -1}" # Last argument is the target directory
58+
echo "Cloning into '$TARGET_DIR'..."
59+
mkdir -p "$TARGET_DIR"
60+
echo "Mock clone completed successfully"
61+
else
62+
# Pass through other git commands to real git
63+
exec /usr/bin/git "$@"
64+
fi
65+
EOF
66+
67+
chmod +x "$MOCK_GIT_DIR/git"
68+
69+
# Set up test environment
70+
export PATH="$MOCK_GIT_DIR:$PATH"
71+
export TEST_CONFIG_FILE="$TEST_DIR/git-config"
72+
export HOME="$TEST_DIR/fake-home"
73+
mkdir -p "$HOME"
74+
75+
# Copy git-get to test directory for isolated testing
76+
cp git-get "$TEST_DIR/git-get-test"
77+
78+
echo "Starting git-get tests..."
79+
echo "=========================="
80+
echo
81+
82+
# Test 1: Help output
83+
test_start "Help output contains expected content"
84+
HELP_OUTPUT=$("$TEST_DIR/git-get-test" --help 2>&1)
85+
if echo "$HELP_OUTPUT" | grep -q "Usage: git get" && \
86+
echo "$HELP_OUTPUT" | grep -q "\-\-print\-path" && \
87+
echo "$HELP_OUTPUT" | grep -q "\-\-location"; then
88+
test_pass
89+
else
90+
test_fail "Help output missing expected content"
91+
fi
92+
93+
# Test 2: Print path with default location
94+
test_start "Print path with default location"
95+
EXPECTED_PATH="$HOME/code/github.com/stilvoid/git-get"
96+
ACTUAL_PATH=$("$TEST_DIR/git-get-test" --print-path https://github.com/stilvoid/git-get.git 2>&1)
97+
if [ "$ACTUAL_PATH" = "$EXPECTED_PATH" ]; then
98+
test_pass
99+
else
100+
test_fail "Expected '$EXPECTED_PATH', got '$ACTUAL_PATH'"
101+
fi
102+
103+
# Test 3: Print path with custom config location
104+
test_start "Print path with custom config location"
105+
echo "$TEST_DIR/custom-code" > "$TEST_CONFIG_FILE"
106+
EXPECTED_PATH="$TEST_DIR/custom-code/github.com/stilvoid/git-get"
107+
ACTUAL_PATH=$("$TEST_DIR/git-get-test" --print-path https://github.com/stilvoid/git-get.git 2>&1)
108+
if [ "$ACTUAL_PATH" = "$EXPECTED_PATH" ]; then
109+
test_pass
110+
else
111+
test_fail "Expected '$EXPECTED_PATH', got '$ACTUAL_PATH'"
112+
fi
113+
114+
# Test 4: Print path with --location override
115+
test_start "Print path with --location override"
116+
EXPECTED_PATH="$TEST_DIR/override-location/github.com/stilvoid/git-get"
117+
ACTUAL_PATH=$("$TEST_DIR/git-get-test" --location "$TEST_DIR/override-location" --print-path https://github.com/stilvoid/git-get.git 2>&1)
118+
if [ "$ACTUAL_PATH" = "$EXPECTED_PATH" ]; then
119+
test_pass
120+
else
121+
test_fail "Expected '$EXPECTED_PATH', got '$ACTUAL_PATH'"
122+
fi
123+
124+
# Test 5: URL parsing - SSH format
125+
test_start "URL parsing - SSH format"
126+
rm -f "$TEST_CONFIG_FILE" # Use default location
127+
EXPECTED_PATH="$HOME/code/github.com/user/repo"
128+
ACTUAL_PATH=$("$TEST_DIR/git-get-test" --print-path [email protected]:user/repo.git 2>&1)
129+
if [ "$ACTUAL_PATH" = "$EXPECTED_PATH" ]; then
130+
test_pass
131+
else
132+
test_fail "Expected '$EXPECTED_PATH', got '$ACTUAL_PATH'"
133+
fi
134+
135+
# Test 6: URL parsing - HTTPS format
136+
test_start "URL parsing - HTTPS format"
137+
EXPECTED_PATH="$HOME/code/github.com/user/repo"
138+
ACTUAL_PATH=$("$TEST_DIR/git-get-test" --print-path https://github.com/user/repo.git 2>&1)
139+
if [ "$ACTUAL_PATH" = "$EXPECTED_PATH" ]; then
140+
test_pass
141+
else
142+
test_fail "Expected '$EXPECTED_PATH', got '$ACTUAL_PATH'"
143+
fi
144+
145+
# Test 7: URL parsing - Git protocol
146+
test_start "URL parsing - Git protocol"
147+
EXPECTED_PATH="$HOME/code/github.com/user/repo"
148+
ACTUAL_PATH=$("$TEST_DIR/git-get-test" --print-path git://github.com/user/repo.git 2>&1)
149+
if [ "$ACTUAL_PATH" = "$EXPECTED_PATH" ]; then
150+
test_pass
151+
else
152+
test_fail "Expected '$EXPECTED_PATH', got '$ACTUAL_PATH'"
153+
fi
154+
155+
# Test 8: URL without .git suffix
156+
test_start "URL parsing - without .git suffix"
157+
EXPECTED_PATH="$HOME/code/github.com/user/repo"
158+
ACTUAL_PATH=$("$TEST_DIR/git-get-test" --print-path https://github.com/user/repo 2>&1)
159+
if [ "$ACTUAL_PATH" = "$EXPECTED_PATH" ]; then
160+
test_pass
161+
else
162+
test_fail "Expected '$EXPECTED_PATH', got '$ACTUAL_PATH'"
163+
fi
164+
165+
# Test 9: Tilde expansion in --location
166+
test_start "Tilde expansion in --location"
167+
EXPECTED_PATH="$HOME/temp/github.com/user/repo"
168+
ACTUAL_PATH=$("$TEST_DIR/git-get-test" --location ~/temp --print-path https://github.com/user/repo.git 2>&1)
169+
if [ "$ACTUAL_PATH" = "$EXPECTED_PATH" ]; then
170+
test_pass
171+
else
172+
test_fail "Expected '$EXPECTED_PATH', got '$ACTUAL_PATH'"
173+
fi
174+
175+
# Test 10: Trailing slash handling in config
176+
test_start "Trailing slash handling in config"
177+
echo "$TEST_DIR/custom-code/" > "$TEST_CONFIG_FILE" # Note trailing slash
178+
EXPECTED_PATH="$TEST_DIR/custom-code/github.com/user/repo"
179+
ACTUAL_PATH=$("$TEST_DIR/git-get-test" --print-path https://github.com/user/repo.git 2>&1)
180+
if [ "$ACTUAL_PATH" = "$EXPECTED_PATH" ]; then
181+
test_pass
182+
else
183+
test_fail "Expected '$EXPECTED_PATH', got '$ACTUAL_PATH'"
184+
fi
185+
186+
# Test 11: Error handling - missing repository argument
187+
test_start "Error handling - missing repository argument"
188+
OUTPUT=$("$TEST_DIR/git-get-test" 2>&1 || true)
189+
if echo "$OUTPUT" | grep -q "Usage: git get"; then
190+
test_pass
191+
else
192+
test_fail "Should show usage when repository argument is missing"
193+
fi
194+
195+
# Test 12: Error handling - --location without argument
196+
test_start "Error handling - --location without argument"
197+
OUTPUT=$("$TEST_DIR/git-get-test" --location 2>&1 || true)
198+
if echo "$OUTPUT" | grep -q "Error: --location requires a directory path"; then
199+
test_pass
200+
else
201+
test_fail "Should show error when --location has no argument"
202+
fi
203+
204+
# Test 13: Actual clone operation (mock)
205+
test_start "Actual clone operation (mocked)"
206+
rm -f "$TEST_CONFIG_FILE" # Use default location
207+
TARGET_DIR="$HOME/code/github.com/test/repo"
208+
OUTPUT=$("$TEST_DIR/git-get-test" https://github.com/test/repo.git 2>&1)
209+
if [ -d "$TARGET_DIR" ] && echo "$OUTPUT" | grep -q "Successfully cloned to: $TARGET_DIR"; then
210+
test_pass
211+
else
212+
test_fail "Clone operation failed or directory not created"
213+
fi
214+
215+
# Test 14: Clone with additional git arguments
216+
test_start "Clone with additional git arguments"
217+
rm -rf "$HOME/code/github.com/test/repo-with-args"
218+
OUTPUT=$("$TEST_DIR/git-get-test" https://github.com/test/repo-with-args.git --depth 1 2>&1)
219+
TARGET_DIR="$HOME/code/github.com/test/repo-with-args"
220+
if [ -d "$TARGET_DIR" ] && echo "$OUTPUT" | grep -q "Successfully cloned to: $TARGET_DIR"; then
221+
test_pass
222+
else
223+
test_fail "Clone with additional arguments failed"
224+
fi
225+
226+
# Test 15: Clone with --location override
227+
test_start "Clone with --location override"
228+
OVERRIDE_DIR="$TEST_DIR/override-test"
229+
TARGET_DIR="$OVERRIDE_DIR/github.com/test/override-repo"
230+
OUTPUT=$("$TEST_DIR/git-get-test" --location "$OVERRIDE_DIR" https://github.com/test/override-repo.git 2>&1)
231+
if [ -d "$TARGET_DIR" ] && echo "$OUTPUT" | grep -q "Successfully cloned to: $TARGET_DIR"; then
232+
test_pass
233+
else
234+
test_fail "Clone with --location override failed"
235+
fi
236+
237+
# Cleanup
238+
echo "Cleaning up test directory: $TEST_DIR"
239+
rm -rf "$TEST_DIR"
240+
241+
# Summary
242+
echo "=========================="
243+
echo "Test Results:"
244+
echo " Total tests: $TESTS_RUN"
245+
echo -e " Passed: ${GREEN}$TESTS_PASSED${NC}"
246+
if [ $TESTS_FAILED -gt 0 ]; then
247+
echo -e " Failed: ${RED}$TESTS_FAILED${NC}"
248+
exit 1
249+
else
250+
echo -e " Failed: $TESTS_FAILED"
251+
echo -e "${GREEN}All tests passed!${NC}"
252+
exit 0
253+
fi

0 commit comments

Comments
 (0)