Repository navigation
Expand file tree
/
Copy pathfind-unused-mocks.sh
More file actions
executable file
·68 lines (54 loc) · 1.63 KB
/
Copy pathfind-unused-mocks.sh
File metadata and controls
executable file
·68 lines (54 loc) · 1.63 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
#!/usr/bin/env bash
set -euo pipefail
# Parse flags
VERBOSE=false
while getopts "v" opt; do
case $opt in
v) VERBOSE=true ;;
*) echo "Usage: $0 [-v] [root_dir]" >&2; exit 1 ;;
esac
done
shift $((OPTIND - 1))
# Root of the repo to search
ROOT="${1:-.}"
# Go module import prefix
MODULE_PREFIX="github.com/onflow/flow-go"
# Mockery header we are looking for
MOCKERY_HEADER='^// Code generated by mockery; DO NOT EDIT\.'
# Temporary storage for package paths
packages=()
# Step 1: Find all mockery-generated files
while IFS= read -r -d '' file; do
# Remove leading ./ if present
relative_path="${file#./}"
# Get directory containing the file
package_dir="${relative_path%/*}"
# Build full Go import path
package_path="${MODULE_PREFIX}/${package_dir}"
packages+=("$package_path")
done < <(
grep -rlZ "$MOCKERY_HEADER" "$ROOT"
)
# Step 2: Deduplicate packages
IFS=$'\n' read -r -d '' -a unique_packages < <(printf '%s\n' "${packages[@]}" | sort -u && printf '\0') || true
# 3) Find unused (not referenced in *.go files)
unused_packages=()
used_packages=()
for pkg in "${unique_packages[@]}"; do
if ! grep -R --include="*.go" -Fq -- "$pkg" "$ROOT"; then
unused_packages+=("$pkg")
else
used_packages+=("$pkg")
fi
done
# 4) Output used packages if verbose
if $VERBOSE && ((${#used_packages[@]} > 0)); then
printf 'Used mock packages:\n'
printf ' %s\n' "${used_packages[@]}"
fi
# 5) Output unused packages, then fail if any exist
if ((${#unused_packages[@]} > 0)); then
printf 'Generated mock packages are unused. Please exclude them from `.mockery.yaml` and remove them: %s\n' "${unused_packages[@]}"
exit 1
fi
exit 0