Skip to content

Commit

Permalink
chore: Make signing tests more robust
Browse files Browse the repository at this point in the history
Old tests didn't verify the signatures were produced and also assumed
that signing config wasn't set up wherever you ran tests. Since I do
use signed commits now for my own work, this was a bad assumption and
made the tests fail locally.

Now we check for either the signature to be present or that signing
failed because no secret key was found.

For disabling signing, we ensure that no signature was added to the commit.
  • Loading branch information
ajoberstar committed Nov 23, 2024
1 parent e9b625b commit 34df457
Showing 1 changed file with 23 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import org.gradle.testkit.runner.UnexpectedBuildFailure
import spock.lang.Specification
import spock.lang.TempDir

import java.rmi.UnexpectedException

class BaseCompatTest extends Specification {
@TempDir File tempDir
File projectDir
Expand Down Expand Up @@ -420,10 +422,15 @@ gitPublish {
}
"""
when:
def result = buildAndFail()
def result = buildOrFail()

then:
result.output.contains("gpg: signing failed: No secret key")
def newCommit = remote.resolve.toCommit("gh-pages").id
def remoteGitDir = "${remote.repository.rootDir}/.git"
def proc = "git --git-dir ${remoteGitDir} cat-file -p ${newCommit}".execute()
def latestCommit = proc.in.text
// either it will work and sign or the key will be missing and it won't be able to
latestCommit.contains("gpgsig") || result.output.contains("gpg: signing failed: No secret key")
}

def 'can deactivate signing'() {
Expand Down Expand Up @@ -451,6 +458,12 @@ gitPublish {

then:
result.task(':gitPublishPush').outcome == TaskOutcome.SUCCESS

def newCommit = remote.resolve.toCommit("gh-pages").id
def remoteGitDir = "${remote.repository.rootDir}/.git"
def proc = "git --git-dir ${remoteGitDir} cat-file -p ${newCommit}".execute()
def latestCommit = proc.in.text
!latestCommit.contains('gpgsign')
}

private BuildResult build(String... args = ['gitPublishPush', '--stacktrace', '--configuration-cache']) {
Expand All @@ -461,6 +474,14 @@ gitPublish {
return runner(args).buildAndFail()
}

private BuildResult buildOrFail(String... args = ['gitPublishPush', '--stacktrace', '--configuration-cache']) {
try {
return runner(args).build()
} catch (UnexpectedBuildFailure e) {
return e.buildResult
}
}

private GradleRunner runner(String... args) {
return GradleRunner.create()
.withGradleVersion(System.properties['compat.gradle.version'])
Expand Down

0 comments on commit 34df457

Please sign in to comment.