-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_install.sh
executable file
·181 lines (151 loc) · 4.54 KB
/
test_install.sh
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
#!/bin/bash
# shellcheck source=./download_cli.sh
source "$(dirname "$0")/download_cli.sh"
# Colors for test output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'
total_tests=0
failed_tests=0
# Test helper functions
assert_equals() {
local expected="$1"
local actual="$2"
local message="$3"
((total_tests++))
if [ "$expected" = "$actual" ]; then
echo -e "${GREEN}✓${NC} $message"
return 0
else
echo -e "${RED}✗${NC} $message"
echo " Expected: $expected"
echo " Got: $actual"
((failed_tests++))
return 1
fi
}
assert_success() {
local message="$1"
shift
((total_tests++))
if "$@"; then
echo -e "${GREEN}✓${NC} $message"
return 0
else
echo -e "${RED}✗${NC} $message"
((failed_tests++))
return 1
fi
}
assert_failure() {
local message="$1"
shift
((total_tests++))
if ! "$@"; then
echo -e "${GREEN}✓${NC} $message"
return 0
else
echo -e "${RED}✗${NC} $message"
((failed_tests++))
return 1
fi
}
# Unit Tests
echo -e "\n${YELLOW}Running unit tests...${NC}"
# Test platform detection
test_platform_detection() {
echo "Testing platform detection..."
local platform os arch expected_platform result=0
os="$(uname -s)"
arch="$(uname -m)"
echo "Debug - OS: $os, Arch: $arch"
case "$os" in
"Darwin")
case "$arch" in
"arm64")
expected_platform="aarch64-apple-darwin"
;;
"x86_64")
expected_platform="x86_64-apple-darwin"
;;
esac
;;
"Linux")
case "$arch" in
"aarch64"|"arm64")
expected_platform="aarch64-unknown-linux-gnu"
;;
"x86_64")
expected_platform="x86_64-unknown-linux-gnu"
;;
esac
;;
esac
platform=$(detect_platform)
echo "Debug - Detected platform: $platform"
echo "Debug - Expected platform: $expected_platform"
assert_equals "$expected_platform" "$platform" "Should detect correct platform"
result=$?
return $result
}
# Test version fetching
test_version_fetching() {
echo "Testing version fetching..."
local version
version=$(get_latest_version)
assert_success "Version should be retrieved successfully" test -n "$version"
assert_success "Version should start with 'v'" echo "$version" | grep -q "^v"
}
# Test checksum verification
test_checksum_verification() {
echo "Testing checksum verification..."
local temp_dir
temp_dir=$(mktemp -d)
cd "$temp_dir" || exit 1
# Create a test file with known content
echo "test" > test.txt
# Create checksum file (f2ca1bb6c7e907d06dafe4687e579fce76b37e4e93b7605022da52e6ccc26fd2 is SHA256 of "test\n")
echo "f2ca1bb6c7e907d06dafe4687e579fce76b37e4e93b7605022da52e6ccc26fd2 test.txt" > test.txt.sha256
# Test verification
assert_success "Checksum verification should pass for valid file" verify_checksum test.txt
# Test with invalid checksum
echo "invalid_sha test.txt" > test.txt.sha256
assert_failure "Checksum verification should fail for invalid SHA" verify_checksum test.txt
# Clean up
cd - > /dev/null || exit 1
rm -rf "$temp_dir"
}
# Integration Tests
test_full_installation() {
echo -e "\n${YELLOW}Running integration tests...${NC}"
echo "Testing full installation process..."
local test_dir
test_dir=$(mktemp -d)
export HOME="$test_dir"
export INSTALL_DIR="${test_dir}/.local/bin"
# Run installation
assert_success "Installation should complete successfully" main
# Verify binary exists and is executable
assert_success "Binary should exist" test -f "${INSTALL_DIR}/essex"
assert_success "Binary should be executable" test -x "${INSTALL_DIR}/essex"
# Clean up
rm -rf "$test_dir"
}
# Run all tests
run_tests() {
local exit_code=0
# Run all test functions
test_platform_detection || exit_code=$?
test_version_fetching || exit_code=$?
test_checksum_verification || exit_code=$?
test_full_installation || exit_code=$?
# Print test summary
echo -e "\n${YELLOW}Test Summary${NC}"
echo "Total tests: $total_tests"
echo "Failed tests: $failed_tests"
# Return overall test result
return $exit_code
}
# Run tests
run_tests