-
Notifications
You must be signed in to change notification settings - Fork 179
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Explain why test deps may not be removed using go mod tidy. (#1378)
* Explain why test deps may not be removed using go mod tidy. * Apply suggestions from code review Co-authored-by: Jessica Black <[email protected]> * Add `rg` as an option. * Add a script to try to find out if a go dep is likely a test only dep. * Mention vendor code in test-only deps explainer. --------- Co-authored-by: Jessica Black <[email protected]>
- Loading branch information
Showing
2 changed files
with
71 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#!/bin/sh | ||
|
||
# This script returns 0 if it thinks the dep is a test-only dep, 1 otherwise. | ||
|
||
USAGE="usage: ./go_test_only_dep.sh <project directory> <Go Package>" | ||
|
||
if [ $# -lt 2 ]; then | ||
echo "$USAGE" | ||
exit 1 | ||
fi | ||
|
||
PROJ_DIR=$1 | ||
PACKAGE=$2 | ||
|
||
# Here we exclude any directories that look like vendored code. | ||
# If they have vendored code in other directories, this could mean there's a false positive. | ||
IMPORTS=$(find "$PROJ_DIR" -not -path '*vendor*' -name \*.go -exec grep -Hn "$PACKAGE" {} \;) | ||
|
||
if [ "$IMPORTS" = '' ]; then | ||
echo "Found no references to package \"$PACKAGE\" in $PROJ_DIR" | ||
exit 1 | ||
fi | ||
|
||
# -v inverts the match, printing any lines that DON'T match and returning 0 on matches found, 1 otherwise. | ||
# Any line match for the package in a file that DOES NOT have a '_test.go' string in the name is printed. | ||
# It's a convoluted way to figure out if any of the files found referencing the package are not _test.go files. | ||
# SH is the pits. | ||
TEST_LINES=$(echo "$IMPORTS" | grep -v "_test.go") | ||
HAS_TEST_LINES="$?" | ||
|
||
# Edge case to be aware of: | ||
# This doesn't actually parse Go source files, so even a commented reference to a package counts as a "hit". | ||
if [ "$HAS_TEST_LINES" = "0" ] ; then | ||
echo "$TEST_LINES" | while read -r i; do | ||
FILE=$(echo "$i" | awk 'BEGIN { FS=":" }; {print $1}') | ||
echo "Found package \"$PACKAGE\" in non-test file: $FILE" | ||
exit 1 | ||
done | ||
else | ||
echo "Package \"$PACKAGE\" not found in any non-*_test.go file." | ||
echo "It is likely a test-only package." | ||
exit 0 | ||
fi |