Skip to content

Commit

Permalink
Warn about unused rules (currently breaks the build!)
Browse files Browse the repository at this point in the history
  • Loading branch information
zjs committed Sep 21, 2018
1 parent a624c1d commit e3bcb38
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
1 change: 1 addition & 0 deletions cmd/vic-machine/inspect/.godeps_rules
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@
^cmd/vic-machine/converter/*
^cmd/vic-machine/create/*

^UNUSED
48 changes: 47 additions & 1 deletion infra/scripts/go-deps-enforcement.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,17 @@ find-packages () {
find "${@}" -type f -name '*.go' -exec dirname {} \; | sort --unique
}

# Returns a list of all directories containing a rule file
# Arguments
# *: directories to search under
#
# Returns:
# directories with rule files
find-rule-dirs () {
find "${@}" -type f -name "$RULES_FILE" -exec dirname {} \; | sort --unique
}


# Returns the path to the "nearest" rule file for a given package
# Arguments
# 1: package path
Expand Down Expand Up @@ -102,7 +113,42 @@ for package in $(find-packages "${ENFORCE[@]}"); do
echo "${invalid//^/ /}"
echo "See $rules for details."
echo ""
rc=1
rc=$(( rc | 1 ))
fi
done

export VIC_CACHE_DEPS=true #TODO: verify this doesn't leak!
# for each rule file
for directory in $(find-rule-dirs "${ENFORCE[@]}"); do
deps=""
# for each package under that rule file
for package in $(find-packages "$directory"); do
# find-rule for the package
rules="$(find-rule "$package")"
# if not the rule file from the outer loop (i.e, the directory is covered by a more specific file)s, continue
if [ "$directory" != "$(dirname "$rules")" ]; then
continue
fi
# accumulate deps
deps+="$(get-deps "$package")"
done

# use grep to make sure each pattern matches at least one dep
unused=()
for rule in $(get-rules "$rules"); do
matches=$(echo "$deps" | grep -c -e "$rule" || true)

# if not, error with the rule file and unmatched rule
if [ "$matches" = "0" ]; then
unused+=("$rule")
fi
done

if [ ${#unused[@]} -ne 0 ]; then
echo "Unused rules in $rules:"
printf ' %s\n' "${unused[@]}"
echo ""
rc=$(( rc | 2 ))
fi
done

Expand Down

0 comments on commit e3bcb38

Please sign in to comment.