Do you usually run your tests before you commit, to check your work?
Do your tests also get run by a pre-commit hook?
This is helpful in the worst case -- the pre-commit hook has your back when you forget -- but in the typical case it's harmful:
- Disrupts flow
- Doubles cost
- Adds no marginal benefit
- Discourages frequent small well-tested commits
- Engenders learned helplessness
We can fix this.
- Update
build.gradle.kts
:
dependencies {
testRuntimeOnly("com.schmonz:junit-greencently:CHECK_ABOVE_FOR_VERSION")
}
tasks.withType<Test> {
maxParallelForks = 1 // see issue #4
}
- Run all tests in project
- If green, observe top-level timestamp file
.when-all-tests-were-green-junit5
- Append to top-level
.gitignore
:*when-all-tests-were-green*
- Observe
git status
not showing timestamp file - In pre-commit hook, inspect file modification time. Example:
#!/bin/sh
all_tests_were_recently_green() {
too_many_seconds_ago=$1
thenstamp=$(date -r .when-all-tests-were-green-junit5 '+%s' 2>/dev/null || echo 0)
nowstamp=$(date '+%s')
secondsago=$(expr ${nowstamp} - ${thenstamp})
[ ${secondsago} -lt ${too_many_seconds_ago} ]
}
if all_tests_were_recently_green 30; then
./gradlew clean build -x test
else
./gradlew clean build
fi
See the Greencently webpage.