-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-lib.sh
More file actions
162 lines (123 loc) · 6.4 KB
/
test-lib.sh
File metadata and controls
162 lines (123 loc) · 6.4 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
156
157
158
159
160
161
162
#!/usr/bin/env bash
# Android Plugin - lib.sh Unit Tests
#
# Tests for core utility functions in lib.sh
set -euo pipefail
script_dir="$(cd "$(dirname "$0")" && pwd)"
. "$script_dir/../test-framework.sh"
setup_logging
# ============================================================================
# Setup
# ============================================================================
lib_path="$script_dir/../../android/virtenv/scripts/lib/lib.sh"
if [ ! -f "$lib_path" ]; then
echo "ERROR: lib.sh not found at: $lib_path"
exit 1
fi
# Source lib.sh
# shellcheck source=../../android/virtenv/scripts/lib/lib.sh
. "$lib_path"
echo "========================================"
echo "Android lib.sh Unit Tests"
echo "========================================"
echo "Testing: $lib_path"
# ============================================================================
# Tests: String Normalization
# ============================================================================
start_test "android_normalize_name - lowercase conversion"
result="$(android_normalize_name "Pixel")"
assert_equal "pixel" "$result" "Should convert to lowercase"
start_test "android_normalize_name - removes special chars"
result="$(android_normalize_name "Pixel-8_Pro")"
assert_equal "pixel8pro" "$result" "Should remove dashes and underscores"
start_test "android_normalize_name - removes spaces"
result="$(android_normalize_name "Nexus 5X")"
assert_equal "nexus5x" "$result" "Should remove spaces"
start_test "android_sanitize_avd_name - preserves allowed chars"
result="$(android_sanitize_avd_name "Pixel_8-Pro.v2")"
assert_equal "Pixel_8-Pro.v2" "$result" "Should preserve ._- characters"
start_test "android_sanitize_avd_name - converts spaces"
result="$(android_sanitize_avd_name "Pixel 8 Pro")"
assert_equal "Pixel_8_Pro" "$result" "Should convert spaces to underscores"
start_test "android_sanitize_avd_name - removes invalid chars"
result="$(android_sanitize_avd_name "Pixel@#8!")"
assert_equal "Pixel8" "$result" "Should remove @#! characters"
start_test "android_sanitize_avd_name - fails on empty input"
assert_failure "android_sanitize_avd_name ''" "Should fail on empty string"
# ============================================================================
# Tests: Checksum Functions
# ============================================================================
# Read-only checksum tests use example project fixtures
example_devices="$(fixture_android_devices_dir)"
start_test "android_compute_devices_checksum - generates checksum"
result="$(android_compute_devices_checksum "$example_devices")"
assert_success "[ -n '$result' ]" "Should return non-empty checksum"
start_test "android_compute_devices_checksum - stable checksum"
checksum1="$(android_compute_devices_checksum "$example_devices")"
checksum2="$(android_compute_devices_checksum "$example_devices")"
assert_equal "$checksum1" "$checksum2" "Should return same checksum for same files"
# Write test needs a temp dir
start_test "android_compute_devices_checksum - different content = different checksum"
test_dir="$(make_temp_dir "android-checksum")"
echo '{"name":"test1","api":28}' > "$test_dir/test1.json"
checksum_before="$(android_compute_devices_checksum "$test_dir")"
echo '{"name":"test2","api":36}' > "$test_dir/test2.json"
checksum_after="$(android_compute_devices_checksum "$test_dir")"
assert_success "[ '$checksum_before' != '$checksum_after' ]" "Should change when files change"
rm -rf "$test_dir"
start_test "android_compute_devices_checksum - fails on non-existent dir"
assert_failure "android_compute_devices_checksum '/nonexistent/path'" "Should fail on missing directory"
# ============================================================================
# Tests: Path Resolution
# ============================================================================
# Use example android project for path resolution
example_android_dir="$REPO_ROOT/examples/android"
# Save original and set test root
SAVED_PROJECT_ROOT="${DEVBOX_PROJECT_ROOT:-}"
unset ANDROID_CONFIG_DIR
export DEVBOX_PROJECT_ROOT="$example_android_dir"
start_test "android_resolve_project_path - finds existing file"
result="$(android_resolve_project_path "devices" 2>/dev/null || true)"
if [ -n "$result" ] && [ -d "$result" ]; then
assert_success "true" "Should resolve to existing directory"
else
assert_failure "false" "Should have found devices directory"
fi
start_test "android_resolve_project_path - finds directory"
result="$(android_resolve_project_path "devices" 2>/dev/null || true)"
expected="${example_android_dir}/devbox.d/android/devices"
assert_equal "$expected" "$result" "Should resolve devices directory"
start_test "android_resolve_project_path - fails on missing path"
assert_failure "android_resolve_project_path 'nonexistent.json'" "Should fail when path doesn't exist"
start_test "android_resolve_config_dir - finds config directory"
result="$(android_resolve_config_dir 2>/dev/null || true)"
expected="${example_android_dir}/devbox.d/android"
assert_equal "$expected" "$result" "Should find android config directory"
# Restore
if [ -n "$SAVED_PROJECT_ROOT" ]; then
export DEVBOX_PROJECT_ROOT="$SAVED_PROJECT_ROOT"
else
unset DEVBOX_PROJECT_ROOT
fi
# ============================================================================
# Tests: Requirement Functions
# ============================================================================
start_test "android_require_jq - succeeds when jq available"
assert_success "android_require_jq" "Should succeed if jq is installed"
start_test "android_require_tool - succeeds for existing tool"
assert_success "android_require_tool 'sh'" "Should succeed for sh"
start_test "android_require_tool - fails for missing tool"
assert_failure "android_require_tool 'nonexistent_tool_xyz'" "Should fail for missing tool"
# Create test directory for dir_contains test
test_sdk="$(make_temp_dir "android-sdk")"
mkdir -p "$test_sdk/platform-tools"
start_test "android_require_dir_contains - succeeds when path exists"
assert_success "android_require_dir_contains '$test_sdk' 'platform-tools'" "Should succeed when subpath exists"
start_test "android_require_dir_contains - fails when path missing"
assert_failure "android_require_dir_contains '$test_sdk' 'nonexistent'" "Should fail when subpath missing"
# Cleanup
rm -rf "$test_sdk"
# ============================================================================
# Test Summary
# ============================================================================
test_summary "android-lib"