Skip to content

Commit

Permalink
Allow sysprops to override env var settings
Browse files Browse the repository at this point in the history
  • Loading branch information
bigdaz committed Jul 16, 2023
1 parent 3449082 commit 8843b43
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ abstract class BaseExtractorTest extends Specification {
}

@CompileStatic
private static class JsonRepositorySnapshotLoader {
protected static class JsonRepositorySnapshotLoader {
private static final String SCHEMA = "schema/github-repository-snapshot-schema.json"
private final File manifestFile

Expand All @@ -214,14 +214,14 @@ abstract class BaseExtractorTest extends Specification {
}

@Memoized
protected Object jsonRepositorySnapshot() {
protected Map jsonRepositorySnapshot() {
def jsonSlurper = new JsonSlurper()
println(manifestFile.text)
JsonSchema schema = createSchemaValidator()
ObjectMapper mapper = new ObjectMapper()
JsonNode node = mapper.readTree(manifestFile)
validateAgainstJsonSchema(schema, node)
return jsonSlurper.parse(manifestFile)
return jsonSlurper.parse(manifestFile) as Map
}

private static void validateAgainstJsonSchema(JsonSchema schema, JsonNode json) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package org.gradle.github.dependencygraph

import org.gradle.test.fixtures.maven.MavenModule

class DependencyExtractorConfigTest extends BaseExtractorTest {
private MavenModule foo
private File settingsFile
private File buildFile

def setup() {
applyDependencyGraphPlugin()
establishEnvironmentVariables()

foo = mavenRepo.module("org.test", "foo", "1.0").publish()

settingsFile = file("settings.gradle") << """
rootProject.name = 'a'
"""

buildFile = file("build.gradle") << """
apply plugin: 'java'
repositories {
maven { url "${mavenRepo.uri}" }
}
"""
}

def "can override env vars with system properties"() {
given:
buildFile << """
dependencies {
implementation "org.test:foo:1.0"
}
"""

when:
executer
.withArgument("-Dorg.gradle.github.env.GITHUB_DEPENDENCY_GRAPH_JOB_CORRELATOR=TEST_CORRELATOR")
.withArgument("-Dorg.gradle.github.env.GITHUB_REF=refs/my-branch/foo")
run()

then:
def manifestFile = reportDir.file("TEST_CORRELATOR.json")
def snapshot = new JsonRepositorySnapshotLoader(manifestFile).jsonRepositorySnapshot()
assert snapshot.sha == environmentVars.sha
assert snapshot.ref == "refs/my-branch/foo"
def job = snapshot.job as Map
assert job.correlator == "TEST_CORRELATOR"
assert job.id == environmentVars.jobId
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ internal interface EnvironmentVariableLoader {
interface Default {
fun Gradle.loadEnvironmentVariable(envName: String, default: String? = null): Provider<String> =
service<ProviderFactory>().run {
environmentVariable(envName)
.orElse(systemProperty(ENV_VIA_SYS_PROP_PREFIX + envName))
systemProperty(ENV_VIA_SYS_PROP_PREFIX + envName)
.orElse(environmentVariable(envName))
.orElse(provider {
default ?: throwEnvironmentVariableMissingException(envName)
})
Expand All @@ -19,8 +19,8 @@ internal interface EnvironmentVariableLoader {

interface Legacy {
fun Gradle.loadEnvironmentVariable(envName: String, default: String? = null): String {
return System.getenv()[envName]
?: System.getProperty(ENV_VIA_SYS_PROP_PREFIX + envName)
return System.getProperty(ENV_VIA_SYS_PROP_PREFIX + envName)
?: System.getenv()[envName]
?: default
?: throwEnvironmentVariableMissingException(envName)
}
Expand Down

0 comments on commit 8843b43

Please sign in to comment.