Skip to content

Commit 6fda4f4

Browse files
Merge pull request #472 from MateSaary/unit-test-pr-check
SREP-702: Add PR check for unit tests
2 parents 3fe5abd + 805a465 commit 6fda4f4

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-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: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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=$(echo "$CLONEREFS_OPTIONS" | grep -o '"base_sha":"[^"]*"' | cut -d':' -f2 | tr -d '"' )
10+
11+
if [[ -z "$base_sha" ]]; then
12+
echo -e "${RED}Could not obtain base_sha"
13+
exit 1
14+
fi
15+
16+
# Obtaining instances of files added in the PR
17+
diff_files=$(git diff --name-status "$base_sha" HEAD | grep '^A' | awk '{print $2}')
18+
19+
# Filter to relevant directory (investigations folder)
20+
investigations=$(echo "$diff_files" | grep '^pkg/investigations/' || true)
21+
if [ -z "$investigations" ]; then
22+
echo -e "${GREEN}[PASS] PR does not contain any new files within the \`pkg/investigations\` directory${NC}"
23+
exit 0
24+
fi
25+
26+
for file in $investigations; do
27+
if [[ "$file" =~ ^pkg/investigations/([^/]+)/([^/]+)\.go$ ]]; then
28+
29+
if echo "$file" | grep -q "test"; then
30+
break
31+
fi
32+
33+
inv_name="${BASH_REMATCH[1]}"
34+
inv_dir=$(dirname "$file")
35+
expected_test_file="${inv_dir}/${inv_name}_test.go"
36+
37+
echo "Found new investigation file: $file, expecting unit test: $expected_test_file"
38+
39+
if echo "$diff_files" | grep -xq "$expected_test_file"; then
40+
echo "Successfully found test file."
41+
else
42+
echo "Failed to locate test file"
43+
echo -e "${RED}[FAIL] Added investigation '$inv_name' is missing a \`${inv_name}_test.go\` file.${NC}"
44+
exit 1
45+
fi
46+
fi
47+
done
48+
49+
echo -e "${GREEN}[PASS] Added investigation(s) have a corresponding test file${NC}"
50+
exit 0

0 commit comments

Comments
 (0)