Skip to content

Commit 1f7b29d

Browse files
committed
Fix PR check for unit tests
1 parent 88c6e7f commit 1f7b29d

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

Makefile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,12 @@ boilerplate-update: ## Update boilerplate version
101101
coverage:
102102
hack/codecov.sh
103103

104+
.PHONY: investigation-unit-tested
105+
investigation-unit-tested:
106+
hack/investigation-test-coverage.sh
107+
104108
.PHONY: validate
105-
validate: isclean
109+
validate: isclean investigation-unit-tested
106110

107111
### Prerequisites
108112
### It is assumed that 'make' is already installed

hack/investigation-test-coverage.sh

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
# Text colors for output
5+
GREEN='\033[0;32m'
6+
RED='\033[0;31m'
7+
NC='\033[0m'
8+
9+
BASE_SHA=$(git ls-remote "https://github.com/openshift/configuration-anomaly-detection.git" "refs/heads/main" | awk '{print $1}')
10+
11+
PR_SHA=$(git rev-parse HEAD)
12+
13+
# Obtaining instances of files added in the PR
14+
diff_files=$(git diff --name-status "$BASE_SHA" "$PR_SHA" | grep '^A' | awk '{print $2}')
15+
16+
# Filter to relevant directory (investigations folder)
17+
investigations=$(echo "$diff_files" | grep '^pkg/investigations/' || true)
18+
if [ -z "$investigations" ]; then
19+
echo -e "${GREEN}[PASS] PR does not contain any new files within the \`pkg/investigations\` directory${NC}"
20+
exit 0
21+
fi
22+
23+
for file in $investigations; do
24+
if [[ "$file" =~ ^pkg/investigations/([^/]+)/([^/]+)\.go$ ]]; then
25+
26+
if echo "$file" | grep -q "test"; then
27+
break
28+
fi
29+
30+
inv_name="${BASH_REMATCH[1]}"
31+
inv_dir=$(dirname "$file")
32+
expected_test_file="${inv_dir}/${inv_name}_test.go"
33+
34+
echo "Found new investigation file: $file, expecting unit test: $expected_test_file"
35+
36+
if echo "$diff_files" | grep -xq "$expected_test_file"; then
37+
echo "Successfully found test file."
38+
else
39+
echo "Failed to locate test file"
40+
echo -e "${RED}[FAIL] Added investigation '$inv_name' is missing a \`${inv_name}_test.go\` file.${NC}"
41+
exit 1
42+
fi
43+
fi
44+
done
45+
46+
echo -e "${GREEN}[PASS] Added investigation(s) have a corresponding test file${NC}"
47+
exit 0

0 commit comments

Comments
 (0)