-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathchanged-test-pkgs.sh
More file actions
executable file
·65 lines (56 loc) · 2.16 KB
/
Copy pathchanged-test-pkgs.sh
File metadata and controls
executable file
·65 lines (56 loc) · 2.16 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
#!/usr/bin/env bash
#
# Outputs Go packages that need testing based on changes vs origin/main.
# Includes both directly changed packages and their reverse dependencies
# (packages that import a changed package).
#
# Usage:
# ./scripts/changed-test-pkgs.sh # outputs space-separated package list
# make test-changed # runs tests via this script
#
set -euo pipefail
cd "$(git rev-parse --show-toplevel)"
MODULE=$(go list -m)
# Get changed .go files: committed branch changes (three-dot merge-base)
# plus any uncommitted changes in the working tree.
COMMITTED=$(git diff --name-only origin/main...HEAD -- '*.go' 2>/dev/null || true)
UNCOMMITTED=$(git diff --name-only HEAD -- '*.go' 2>/dev/null || true)
CHANGED_FILES=$(printf '%s\n%s' "$COMMITTED" "$UNCOMMITTED" | sort -u | grep -v '^$' || true)
if [ -z "$CHANGED_FILES" ]; then
exit 0
fi
# Convert file paths to package import paths, skipping repo-root files
# (root .go files like main.go belong to the root module package, but
# testing "./" would run everything — handle explicitly). Drop directories
# that no longer exist on disk (deleted packages) since `go list` cannot
# resolve them and would fail the run.
CHANGED_PKGS=$(echo "$CHANGED_FILES" \
| xargs -I{} dirname {} \
| sort -u \
| grep -v '^\.$' \
| while IFS= read -r dir; do [ -d "$dir" ] && echo "$dir"; done \
| sed "s|^|${MODULE}/|")
# If there are root-level .go changes, add the module root package
if echo "$CHANGED_FILES" | xargs -I{} dirname {} | grep -q '^\.$'; then
CHANGED_PKGS=$(printf '%s\n%s' "$MODULE" "$CHANGED_PKGS")
fi
if [ -z "$CHANGED_PKGS" ]; then
exit 0
fi
# Find reverse dependencies: packages in this module that import any changed package
REVERSE_DEPS=$(go list -f '{{.ImportPath}} {{join .Deps " "}}' ./... 2>/dev/null \
| while IFS= read -r line; do
pkg="${line%% *}"
deps=" ${line#* } "
for cp in $CHANGED_PKGS; do
if echo "$deps" | grep -q " $cp "; then
echo "$pkg"
break
fi
done
done)
# Combine changed packages + reverse deps, deduplicate
printf '%s\n%s' "$CHANGED_PKGS" "$REVERSE_DEPS" \
| sort -u \
| grep -v '^$' \
| tr '\n' ' '