Skip to content

Commit de34886

Browse files
committed
chore: add code formatting, linting, and coverage pre-commit hooks
1 parent 18221b6 commit de34886

58 files changed

Lines changed: 8369 additions & 6922 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.githooks/pre-commit

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,32 @@
11
#!/bin/sh
22

3+
set -e
4+
35
echo "Running pre-commit hooks..."
46

5-
# 1. Run tests
7+
# 1. Format
8+
echo "Running code formatting..."
9+
mvn spotless:apply
10+
mvn spotless:check
11+
12+
# 2. Build
13+
echo "Building..."
14+
make build
15+
16+
# 3. Test
17+
echo "Running tests..."
618
make test
7-
if [ $? -ne 0 ]; then
8-
echo "Tests failed! Aborting commit."
9-
exit 1
10-
fi
1119

12-
# 2. Update README.md with 100% coverage badges (mocking calculation for now since TestRunner outputs 100%)
13-
sed -i -E 's/\[\!\[Test Coverage\]\(.*\)\]\(.*\)/[![Test Coverage](https:\/\/img.shields.io\/badge\/Coverage-100%25-success.svg)](#)/g' README.md
14-
sed -i -E 's/\[\!\[Doc Coverage\]\(.*\)\]\(.*\)/[![Doc Coverage](https:\/\/img.shields.io\/badge\/Doc%20Coverage-100%25-success.svg)](#)/g' README.md
20+
# 4. Lint
21+
echo "Running linters..."
22+
mvn checkstyle:check
23+
24+
# 5. Update Badges
25+
echo "Updating badges..."
26+
python3 scripts/update_badges.py
1527

16-
# Add README.md back to the commit if it was modified
17-
git add README.md
28+
# Add README.md and formatting changes back to the commit
29+
git add README.md src/ pom.xml scripts/update_badges.py
1830

1931
echo "Pre-commit hooks passed!"
2032
exit 0

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,6 @@ node_modules/
110110
/out/
111111
/public/
112112
.tools/
113+
114+
# --- Code Formatting Settings ---
115+
.spotless/

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ cdd-java
22
========
33
[![License](https://img.shields.io/badge/license-Apache--2.0%20OR%20MIT-blue.svg)](https://opensource.org/licenses/Apache-2.0)
44
[![CI](https://github.com/SamuelMarks/cdd-java/actions/workflows/ci.yml/badge.svg)](https://github.com/SamuelMarks/cdd-java/actions)
5-
[![Test Coverage](https://img.shields.io/badge/test_coverage-100%25-brightgreen.svg)](#)
5+
[![Test Coverage](https://img.shields.io/badge/test_coverage-13%25-red.svg)](#)
66
[![Doc Coverage](https://img.shields.io/badge/doc_coverage-100%25-brightgreen.svg)](#)
77

88
----

pom.xml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,27 @@
9494
</execution>
9595
</executions>
9696
</plugin>
97+
<plugin>
98+
<groupId>com.diffplug.spotless</groupId>
99+
<artifactId>spotless-maven-plugin</artifactId>
100+
<version>2.43.0</version>
101+
<configuration>
102+
<java>
103+
<eclipse><version>4.30</version></eclipse>
104+
</java>
105+
</configuration>
106+
</plugin>
107+
<plugin>
108+
<groupId>org.apache.maven.plugins</groupId>
109+
<artifactId>maven-checkstyle-plugin</artifactId>
110+
<version>3.3.1</version>
111+
<configuration>
112+
<configLocation>google_checks.xml</configLocation>
113+
<consoleOutput>true</consoleOutput>
114+
<failsOnError>true</failsOnError>
115+
<linkXRef>false</linkXRef>
116+
</configuration>
117+
</plugin>
97118
</plugins>
98119
</build>
99120

@@ -128,6 +149,27 @@
128149
<fallback>false</fallback>
129150
</configuration>
130151
</plugin>
152+
<plugin>
153+
<groupId>com.diffplug.spotless</groupId>
154+
<artifactId>spotless-maven-plugin</artifactId>
155+
<version>2.43.0</version>
156+
<configuration>
157+
<java>
158+
<eclipse><version>4.30</version></eclipse>
159+
</java>
160+
</configuration>
161+
</plugin>
162+
<plugin>
163+
<groupId>org.apache.maven.plugins</groupId>
164+
<artifactId>maven-checkstyle-plugin</artifactId>
165+
<version>3.3.1</version>
166+
<configuration>
167+
<configLocation>google_checks.xml</configLocation>
168+
<consoleOutput>true</consoleOutput>
169+
<failsOnError>true</failsOnError>
170+
<linkXRef>false</linkXRef>
171+
</configuration>
172+
</plugin>
131173
</plugins>
132174
</build>
133175
</profile>

scripts/update_badges.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ def main():
2020
jacoco_csv = os.path.join("target", "site", "jacoco", "jacoco.csv")
2121
test_cov = 0
2222
if os.path.exists(jacoco_csv):
23-
with open(jacoco_csv, "r") as f:
24-
lines = f.readlines()[1:]
25-
missed = sum(int(l.split(',')[3]) for l in lines)
26-
covered = sum(int(l.split(',')[4]) for l in lines)
27-
if missed + covered > 0:
28-
test_cov = int((covered / (missed + covered)) * 100)
23+
with open(jacoco_csv, "r") as f:
24+
lines = f.readlines()[1:]
25+
missed = sum(int(l.split(',')[3]) for l in lines)
26+
covered = sum(int(l.split(',')[4]) for l in lines)
27+
if missed + covered > 0:
28+
test_cov = int((covered / (missed + covered)) * 100)
2929
except Exception as e:
3030
print(f'Coverage calculation failed: {e}')
3131
test_cov = 0

0 commit comments

Comments
 (0)