Skip to content

Commit d7e33b1

Browse files
authored
Build: Run checkWorkingCopyClean as a Test-task finalizer (#4417)
Use checkWorkingCopyClean as a test taming mechanism which will fail the build if a test writes changes that it should not. Presently the Java SecurityManager enforces this comprehensively but that's going away.
1 parent b6abc8a commit d7e33b1

1 file changed

Lines changed: 23 additions & 4 deletions

File tree

gradle/validation/git-status.gradle

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,9 @@ configure(rootProject) {
4949
def ref = repository.findRef("HEAD").getObjectId()
5050
project.ext.gitRev = ref.name()
5151
project.ext.gitRevShort = ref.abbreviate(8).name()
52-
project.ext.gitStatus = new Git(repository).status().call()
5352
} catch (RepositoryNotFoundException | NoWorkTreeException e) {
5453
project.ext.gitRev = "(not a git checkout)"
5554
project.ext.gitRevShort = "(not a git checkout)"
56-
project.ext.gitStatus = null
5755
} catch (NotSupportedException e) {
5856
throw new GradleException("jgit does not support git repository version at this location: ${dir}",
5957
e)
@@ -62,10 +60,19 @@ configure(rootProject) {
6260
}
6361

6462
// Verify git working copy does not have any unstaged modified files.
63+
// Status is captured here (not in gitStatus) so it reflects changes from tests that finalize with this task.
6564
task checkWorkingCopyClean() {
66-
dependsOn gitStatus
6765
doFirst {
68-
def status = rootProject.ext.gitStatus
66+
def status
67+
try {
68+
def repository = new FileRepositoryBuilder()
69+
.setWorkTree(rootProject.projectDir)
70+
.setMustExist(true)
71+
.build()
72+
status = new Git(repository).status().call()
73+
} catch (RepositoryNotFoundException | NoWorkTreeException e) {
74+
status = null
75+
}
6976
if (status == null) {
7077
if (file("${rootProject.projectDir}/.git").exists()) {
7178
// Ignore git worktree branches until jgit supports them.
@@ -121,4 +128,16 @@ configure(rootProject) {
121128
}
122129
}
123130
}
131+
132+
// Catch tests that write into the working tree.
133+
// On CI: finalize every Test task with the clean check (runs even on test failure).
134+
// Locally: only enforce ordering when both are already scheduled (e.g. via `check`).
135+
allprojects {
136+
tasks.withType(Test).configureEach { testTask ->
137+
rootProject.checkWorkingCopyClean.mustRunAfter(testTask)
138+
if (isCIBuild) {
139+
testTask.finalizedBy(rootProject.checkWorkingCopyClean)
140+
}
141+
}
142+
}
124143
}

0 commit comments

Comments
 (0)