forked from EPNW/opus_flutter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunit_tests.sh
More file actions
executable file
·199 lines (170 loc) · 6.21 KB
/
unit_tests.sh
File metadata and controls
executable file
·199 lines (170 loc) · 6.21 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#!/usr/bin/env bash
#
# unit_tests.sh
#
# Runs all unit tests across every package in the monorepo and collects
# per-package lcov coverage reports. When `lcov` is available on the PATH
# all individual reports are merged into a single coverage/lcov.info at the
# repository root.
#
# Usage:
# ./scripts/unit_tests.sh
#
# Requirements:
# - flutter (stable channel)
# - dart (bundled with flutter)
# - lcov (optional, for merging reports) → brew install lcov
# - dart pub global activate coverage (for opus_dart dart-only coverage)
set -uo pipefail
# shellcheck source=scripts/common.sh
source "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/common.sh"
# Where per-package lcov files and the merged report are written
COVERAGE_DIR="$ROOT_DIR/coverage"
MERGED_LCOV="$COVERAGE_DIR/lcov.info"
# ---------------------------------------------------------------------------
# Setup
# ---------------------------------------------------------------------------
failed_packages=()
passed_packages=()
rm -rf "$COVERAGE_DIR"
mkdir -p "$COVERAGE_DIR"
# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------
# copy_lcov <package> <source_lcov>
# Normalises SF: paths so they are relative to the repo root.
# Absolute: SF:/full/repo/path/<package>/lib/… → SF:<package>/lib/…
# Relative: SF:lib/… → SF:<package>/lib/…
copy_lcov() {
local package="$1"
local source="$2"
if [ -f "$source" ]; then
sed \
-e "s|^SF:${ROOT_DIR}/${package}/|SF:${package}/|" \
-e "s|^SF:lib/|SF:${package}/lib/|" \
"$source" > "$COVERAGE_DIR/${package}.lcov.info"
log_info "Coverage saved → coverage/${package}.lcov.info"
else
log_warning "No lcov.info generated for $package"
fi
}
# run_flutter_tests <package>
run_flutter_tests() {
local package="$1"
local package_dir="$ROOT_DIR/$package"
log_header "▸ $package"
if [ ! -d "$package_dir" ]; then
log_warning "Directory not found, skipping: $package_dir"
return 0
fi
if (
cd "$package_dir"
flutter pub get
flutter test --coverage
); then
log_success "$package"
copy_lcov "$package" "$package_dir/coverage/lcov.info"
passed_packages+=("$package")
else
log_error "$package"
failed_packages+=("$package")
fi
}
# run_dart_tests <package>
run_dart_tests() {
local package="$1"
local package_dir="$ROOT_DIR/$package"
log_header "▸ $package (dart)"
if [ ! -d "$package_dir" ]; then
log_warning "Directory not found, skipping: $package_dir"
return 0
fi
# Test pass/fail is determined solely by `dart test`.
if (cd "$package_dir" && dart pub get && dart run build_runner build --delete-conflicting-outputs && dart test --coverage=coverage); then
log_success "$package"
passed_packages+=("$package")
# Coverage formatting is best-effort: activate the package if needed, then
# convert the raw coverage data to lcov. A failure here does not mark the
# package as failed.
dart pub global activate coverage --no-executables 2>/dev/null || true
(
cd "$package_dir"
dart pub global run coverage:format_coverage \
--lcov \
--check-ignore \
--in=coverage \
--out=coverage/lcov.info \
--packages=.dart_tool/package_config.json \
--report-on=lib/src
) && copy_lcov "$package" "$package_dir/coverage/lcov.info" \
|| log_warning "Coverage formatting skipped for $package (run: dart pub global activate coverage)"
else
log_error "$package"
failed_packages+=("$package")
fi
}
# ---------------------------------------------------------------------------
# Run all tests
# ---------------------------------------------------------------------------
log_header "Running unit tests for all packages"
echo "Root: $ROOT_DIR"
for package in "${FLUTTER_PACKAGES[@]}"; do
run_flutter_tests "$package"
done
run_dart_tests "$DART_PACKAGE"
# ---------------------------------------------------------------------------
# Merge coverage reports
# ---------------------------------------------------------------------------
log_header "Merging coverage reports"
lcov_args=()
for f in "$COVERAGE_DIR"/*.lcov.info; do
[ -f "$f" ] && lcov_args+=("-a" "$f")
done
HTML_REPORT_DIR="$COVERAGE_DIR/html"
if [ ${#lcov_args[@]} -eq 0 ]; then
log_warning "No coverage files found to merge."
elif command -v lcov &>/dev/null && command -v genhtml &>/dev/null; then
lcov "${lcov_args[@]}" -o "$MERGED_LCOV" 2>/dev/null
lcov --remove "$MERGED_LCOV" '*/wrappers/*' -o "$MERGED_LCOV" 2>/dev/null
log_success "Merged lcov report → coverage/lcov.info (wrappers excluded)"
if genhtml "$MERGED_LCOV" \
--output-directory "$HTML_REPORT_DIR" \
--prefix "$ROOT_DIR" \
--title "opus_flutter – Unit Test Coverage" \
--legend \
--ignore-errors source; then
log_success "HTML report → coverage/html/index.html"
else
log_warning "genhtml exited with errors — HTML report may be incomplete."
fi
echo ""
lcov --summary "$MERGED_LCOV" 2>/dev/null || true
else
log_warning "'lcov' / 'genhtml' not found on PATH — skipping HTML report."
log_warning "Install them with: brew install lcov"
log_warning "Individual lcov files are available in coverage/"
fi
# ---------------------------------------------------------------------------
# Summary
# ---------------------------------------------------------------------------
echo ""
echo -e "${BOLD}════════════════════════════════════════${NC}"
echo -e "${BOLD}Results${NC}"
echo -e "${BOLD}════════════════════════════════════════${NC}"
if [ ${#passed_packages[@]} -gt 0 ]; then
echo -e "${GREEN}Passed (${#passed_packages[@]}):${NC}"
for pkg in "${passed_packages[@]}"; do
echo -e " ${GREEN}✓${NC} $pkg"
done
fi
if [ ${#failed_packages[@]} -gt 0 ]; then
echo ""
echo -e "${RED}Failed (${#failed_packages[@]}):${NC}"
for pkg in "${failed_packages[@]}"; do
echo -e " ${RED}✗${NC} $pkg"
done
echo ""
exit 1
fi
echo ""
echo -e "${GREEN}${BOLD}All tests passed!${NC}"