Skip to content

Commit

Permalink
Make it possible to run more init-script tests on M1 Mac
Browse files Browse the repository at this point in the history
Older TestKit on M1 Mac doesn't allow env vars to be passed, so we need to use
jvmArgs instead.
  • Loading branch information
bigdaz committed Apr 8, 2024
1 parent 59cb762 commit 6f92769
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ abstract class BuildResultsRecorder implements BuildService<BuildResultsRecorder
buildScanFailed: false
]

def runnerTempDir = System.getenv("RUNNER_TEMP")
def githubActionStep = System.getenv("GITHUB_ACTION")
def runnerTempDir = System.getProperty("RUNNER_TEMP") ?: System.getenv("RUNNER_TEMP")
def githubActionStep = System.getProperty("GITHUB_ACTION") ?: System.getenv("GITHUB_ACTION")
if (!runnerTempDir || !githubActionStep) {
return
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ class BuildResults {
}

def writeToResultsFile(boolean overwrite) {
def runnerTempDir = System.getenv("RUNNER_TEMP")
def githubActionStep = System.getenv("GITHUB_ACTION")
def runnerTempDir = System.getProperty("RUNNER_TEMP") ?: System.getenv("RUNNER_TEMP")
def githubActionStep = System.getProperty("GITHUB_ACTION") ?: System.getenv("GITHUB_ACTION")
if (!runnerTempDir || !githubActionStep) {
return
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,20 +211,31 @@ task expectFailure {
File initScriptsDir = new File(testProjectDir, "initScripts")
args << '-I' << new File(initScriptsDir, initScript).absolutePath

envVars.putIfAbsent('RUNNER_TEMP', testProjectDir.absolutePath)
envVars.putIfAbsent('GITHUB_ACTION', 'github-step-id')

def runner = ((DefaultGradleRunner) GradleRunner.create())
.withJvmArguments(jvmArgs)
.withGradleVersion(gradleVersion.version)
.withProjectDir(testProjectDir)
.withArguments(args)
.withEnvironment(envVars)
.forwardOutput()

if (testKitSupportsEnvVars(gradleVersion)) {
runner.withEnvironment(envVars)
} else {
(runner as DefaultGradleRunner).withJvmArguments(jvmArgs)
}

runner
}

private boolean testKitSupportsEnvVars(GradleVersion gradleVersion) {
// TestKit supports env vars for Gradle 3.5+, except on M1 Mac where only 6.9+ is supported
def isM1Mac = System.getProperty("os.arch") == "aarch64"
if (isM1Mac) {
return gradleVersion >= GRADLE_6_X.gradleVersion
} else {
return gradleVersion >= GRADLE_3_X.gradleVersion
}
}

static final class TestGradleVersion {

final GradleVersion gradleVersion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class TestBuildResultRecorder extends BaseInitScriptTest {
assumeTrue testGradleVersion.compatibleWithCurrentJvm

when:
run(['help'], initScript, testGradleVersion.gradleVersion)
run(testGradleVersion.gradleVersion)

then:
assertResults('help', testGradleVersion, false, false)
Expand All @@ -25,7 +25,7 @@ class TestBuildResultRecorder extends BaseInitScriptTest {

when:
addFailingTaskToBuild()
runAndFail(['expectFailure'], initScript, testGradleVersion.gradleVersion)
runAndFail(testGradleVersion.gradleVersion)

then:
assertResults('expectFailure', testGradleVersion, true, false)
Expand All @@ -38,14 +38,14 @@ class TestBuildResultRecorder extends BaseInitScriptTest {
assumeTrue testGradleVersion.compatibleWithCurrentJvm

when:
run(['help', '--configuration-cache'], initScript, testGradleVersion.gradleVersion)
run(['help', '--configuration-cache'], testGradleVersion.gradleVersion)

then:
assertResults('help', testGradleVersion, false, false)
assert buildResultFile.delete()

when:
run(['help', '--configuration-cache'], initScript, testGradleVersion.gradleVersion)
run(['help', '--configuration-cache'], testGradleVersion.gradleVersion)

then:
assertResults('help', testGradleVersion, false, false)
Expand All @@ -59,7 +59,7 @@ class TestBuildResultRecorder extends BaseInitScriptTest {

when:
declareDevelocityPluginApplication(testGradleVersion.gradleVersion)
run(['help'], initScript, testGradleVersion.gradleVersion)
run(testGradleVersion.gradleVersion)

then:
assertResults('help', testGradleVersion, false, true)
Expand All @@ -73,7 +73,7 @@ class TestBuildResultRecorder extends BaseInitScriptTest {

when:
declareLegacyGradleEnterprisePluginApplication(testGradleVersion.gradleVersion)
run(['help'], initScript, testGradleVersion.gradleVersion)
run(testGradleVersion.gradleVersion)

then:
assertResults('help', testGradleVersion, false, true)
Expand All @@ -87,7 +87,7 @@ class TestBuildResultRecorder extends BaseInitScriptTest {

when:
declareDevelocityPluginApplication(testGradleVersion.gradleVersion)
run(['help', '--no-scan'], initScript, testGradleVersion.gradleVersion)
run(['help', '--no-scan'], testGradleVersion.gradleVersion)

then:
assertResults('help', testGradleVersion, false, false)
Expand All @@ -102,7 +102,7 @@ class TestBuildResultRecorder extends BaseInitScriptTest {
when:
declareDevelocityPluginApplication(testGradleVersion.gradleVersion)
addFailingTaskToBuild()
runAndFail(['expectFailure'], initScript, testGradleVersion.gradleVersion)
runAndFail(testGradleVersion.gradleVersion)

then:
assertResults('expectFailure', testGradleVersion, true, true)
Expand All @@ -116,14 +116,14 @@ class TestBuildResultRecorder extends BaseInitScriptTest {

when:
declareDevelocityPluginApplication(testGradleVersion.gradleVersion)
run(['help', '--configuration-cache'], initScript, testGradleVersion.gradleVersion)
run(['help', '--configuration-cache'], testGradleVersion.gradleVersion)

then:
assertResults('help', testGradleVersion, false, true)
assert buildResultFile.delete()

when:
run(['help', '--configuration-cache'], initScript, testGradleVersion.gradleVersion)
run(['help', '--configuration-cache'], testGradleVersion.gradleVersion)

then:
assertResults('help', testGradleVersion, false, true)
Expand All @@ -139,7 +139,7 @@ class TestBuildResultRecorder extends BaseInitScriptTest {
declareDevelocityPluginApplication(testGradleVersion.gradleVersion)
addFailingTaskToBuild()
failScanUpload = true
runAndFail(['expectFailure'], initScript, testGradleVersion.gradleVersion)
runAndFail(testGradleVersion.gradleVersion)

then:
assertResults('expectFailure', testGradleVersion, true, false, true)
Expand All @@ -152,7 +152,9 @@ class TestBuildResultRecorder extends BaseInitScriptTest {
assumeTrue testGradleVersion.compatibleWithCurrentJvm

when:
run(['help'], initScript, testGradleVersion.gradleVersion, [], [RUNNER_TEMP: '', GITHUB_ACTION: ''])
run(['help'], initScript, testGradleVersion.gradleVersion,
["-DRUNNER_TEMP=", "-DGITHUB_ACTION="],
[RUNNER_TEMP: '', GITHUB_ACTION: ''])

then:
def buildResultsDir = new File(testProjectDir, '.build-results')
Expand All @@ -169,7 +171,9 @@ class TestBuildResultRecorder extends BaseInitScriptTest {
def invalidDir = new File(testProjectDir, 'invalid-runner-temp')
invalidDir.createNewFile()

run(['help'], initScript, testGradleVersion.gradleVersion, [], [RUNNER_TEMP: invalidDir.absolutePath])
run(['help'], initScript, testGradleVersion.gradleVersion,
["-DRUNNER_TEMP=${invalidDir.absolutePath}".toString()],
[RUNNER_TEMP: invalidDir.absolutePath])

then:
def buildResultsDir = new File(testProjectDir, '.build-results')
Expand All @@ -195,7 +199,7 @@ class TestBuildResultRecorder extends BaseInitScriptTest {
}
""" + settingsFile.text

run(['help'], initScript, testGradleVersion.gradleVersion)
run(testGradleVersion.gradleVersion)

then:
assertResults('help', testGradleVersion, false, true)
Expand All @@ -204,6 +208,28 @@ class TestBuildResultRecorder extends BaseInitScriptTest {
testGradleVersion << SETTINGS_PLUGIN_VERSIONS
}

def run(def args = ['help'], def gradleVersion) {
return run(args, initScript, gradleVersion, jvmArgs, envVars)
}

def runAndFail(def gradleVersion) {
return runAndFail(['expectFailure'], initScript, gradleVersion, jvmArgs, envVars)
}

def getJvmArgs() {
[
"-DRUNNER_TEMP=${testProjectDir.absolutePath}".toString(),
"-DGITHUB_ACTION=github-step-id".toString()
]
}

def getEnvVars() {
[
RUNNER_TEMP: testProjectDir.absolutePath,
GITHUB_ACTION: 'github-step-id'
]
}

void assertResults(String task, TestGradleVersion testGradleVersion, boolean hasFailure, boolean hasBuildScan, boolean scanUploadFailed = false) {
def results = new JsonSlurper().parse(buildResultFile)
assert results['rootProjectName'] == ROOT_PROJECT_NAME
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class TestDependencyGraph extends BaseInitScriptTest {
assumeTrue testGradleVersion.compatibleWithCurrentJvm

when:
run(['help'], initScript, testGradleVersion.gradleVersion, [], envVars)
run(['help'], initScript, testGradleVersion.gradleVersion, jvmArgs, envVars)

then:
assert reportFile.exists()
Expand All @@ -43,7 +43,7 @@ class TestDependencyGraph extends BaseInitScriptTest {
assumeTrue testGradleVersion.compatibleWithCurrentJvm

when:
run(['help', '--configuration-cache'], initScript, testGradleVersion.gradleVersion, [], envVars)
run(['help', '--configuration-cache'], initScript, testGradleVersion.gradleVersion, jvmArgs, envVars)

then:
assert reportFile.exists()
Expand All @@ -57,7 +57,7 @@ class TestDependencyGraph extends BaseInitScriptTest {
assumeTrue testGradleVersion.compatibleWithCurrentJvm

when:
def result = run(['help'], initScript, testGradleVersion.gradleVersion, [], envVars)
def result = run(['help'], initScript, testGradleVersion.gradleVersion, jvmArgs, envVars)

then:
assert !reportsDir.exists()
Expand All @@ -73,7 +73,12 @@ class TestDependencyGraph extends BaseInitScriptTest {
when:
def vars = envVars
vars.put('GITHUB_DEPENDENCY_GRAPH_CONTINUE_ON_FAILURE', 'false')
def result = runAndFail(['help'], initScript, testGradleVersion.gradleVersion, [], vars)
def args = jvmArgs
Collections.replaceAll(args,
'-DGITHUB_DEPENDENCY_GRAPH_CONTINUE_ON_FAILURE=true',
'-DGITHUB_DEPENDENCY_GRAPH_CONTINUE_ON_FAILURE=false'
)
def result = runAndFail(['help'], initScript, testGradleVersion.gradleVersion, args, vars)

then:
assert !reportsDir.exists()
Expand Down Expand Up @@ -103,20 +108,20 @@ class TestDependencyGraph extends BaseInitScriptTest {
"""

when:
run(['help'], initScript, testGradleVersion.gradleVersion, [], envVars)
run(['help'], initScript, testGradleVersion.gradleVersion, jvmArgs, envVars)

then:
assert reportFile.exists()

when:
run(['first'], initScript, testGradleVersion.gradleVersion, [], envVars)
run(['first'], initScript, testGradleVersion.gradleVersion, jvmArgs, envVars)

then:
assert reportFile.exists()
assert reportFile1.exists()

when:
run(['second'], initScript, testGradleVersion.gradleVersion, [], envVars)
run(['second'], initScript, testGradleVersion.gradleVersion, jvmArgs, envVars)

then:
assert reportFile.exists()
Expand All @@ -133,7 +138,11 @@ class TestDependencyGraph extends BaseInitScriptTest {
when:
def vars = envVars
vars.put('GRADLE_PLUGIN_REPOSITORY_URL', 'https://plugins.grdev.net/m2')
def result = run(['help', '--info'], initScript, testGradleVersion.gradleVersion, [], vars)
// TODO:DAZ This props are set too late to control init-script plugin resolution
// This makes the tests fail on Mac with Gradle < 6
def args = jvmArgs
args.add('-DGRADLE_PLUGIN_REPOSITORY_URL=https://plugins.grdev.net/m2')
def result = run(['help', '--info'], initScript, testGradleVersion.gradleVersion, args, vars)

then:
assert reportFile.exists()
Expand All @@ -151,7 +160,13 @@ class TestDependencyGraph extends BaseInitScriptTest {
vars.put('GRADLE_PLUGIN_REPOSITORY_URL', 'https://plugins.grdev.net/m2')
vars.put('GRADLE_PLUGIN_REPOSITORY_USERNAME', 'REPO_USER')
vars.put('GRADLE_PLUGIN_REPOSITORY_PASSWORD', 'REPO_PASSWORD')
def result = run(['help', '--info'], initScript, testGradleVersion.gradleVersion, [], vars)
// TODO:DAZ This props are set too late to control init-script plugin resolution
// This makes the tests fail on Mac with Gradle < 6
def args = jvmArgs
args.add('-DGRADLE_PLUGIN_REPOSITORY_URL=https://plugins.grdev.net/m2')
args.add('-DGRADLE_PLUGIN_REPOSITORY_USERNAME=REPO_USER')
args.add('-DGRADLE_PLUGIN_REPOSITORY_PASSWORD=REPO_PASSWORD')
def result = run(['help', '--info'], initScript, testGradleVersion.gradleVersion, args, vars)

then:
assert reportFile.exists()
Expand All @@ -175,6 +190,19 @@ class TestDependencyGraph extends BaseInitScriptTest {
]
}

def getJvmArgs() {
return [
"-DGITHUB_DEPENDENCY_GRAPH_ENABLED=true",
"-DGITHUB_DEPENDENCY_GRAPH_CONTINUE_ON_FAILURE=true",
"-DGITHUB_DEPENDENCY_GRAPH_JOB_CORRELATOR=CORRELATOR",
"-DGITHUB_DEPENDENCY_GRAPH_JOB_ID=1",
"-DGITHUB_DEPENDENCY_GRAPH_REF=main",
"-DGITHUB_DEPENDENCY_GRAPH_SHA=123456",
"-DGITHUB_DEPENDENCY_GRAPH_WORKSPACE=" + testProjectDir.absolutePath,
"-DDEPENDENCY_GRAPH_REPORT_DIR=" + reportsDir.absolutePath,
].collect {it.toString() } // Convert from GString to String
}

def getReportsDir() {
return new File(testProjectDir, 'build/reports/github-dependency-graph-snapshots')
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -425,21 +425,7 @@ class TestDevelocityInjection extends BaseInitScriptTest {
}

private BuildResult run(TestGradleVersion testGradleVersion, TestConfig config, List<String> args = ["help"]) {
if (testKitSupportsEnvVars(testGradleVersion.gradleVersion)) {
return run(args, initScript, testGradleVersion.gradleVersion, [], config.envVars)
} else {
return run(args, initScript, testGradleVersion.gradleVersion, config.jvmArgs, [:])
}
}

private boolean testKitSupportsEnvVars(GradleVersion gradleVersion) {
// TestKit supports env vars for Gradle 3.5+, except on M1 Mac where only 6.9+ is supported
def isM1Mac = System.getProperty("os.arch") == "aarch64"
if (isM1Mac) {
return gradleVersion >= GRADLE_6_X.gradleVersion
} else {
return gradleVersion >= GRADLE_3_X.gradleVersion
}
return run(args, initScript, testGradleVersion.gradleVersion, config.jvmArgs, config.envVars)
}

private TestConfig testConfig(String develocityPluginVersion = DEVELOCITY_PLUGIN_VERSION) {
Expand Down

0 comments on commit 6f92769

Please sign in to comment.