Skip to content

Commit

Permalink
Adds environment property to UnityTask (#98)
Browse files Browse the repository at this point in the history
  • Loading branch information
Azurelol authored Jun 16, 2021
1 parent 6209bab commit e8c6687
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -137,14 +137,20 @@ abstract class UnityIntegrationSpec extends IntegrationSpec {
if (windows) {
mockUnityFile << """
@echo off
echo ${mockUnityStartupMessage}
echo [ENVIRONMENT]:
set
echo [ARGUMENTS]:
echo %*
echo ${mockUnityStartupMessage}
""".stripIndent()
} else {
mockUnityFile << """
#!/usr/bin/env bash
echo ${mockUnityStartupMessage}
echo [ENVIRONMENT]:
env
echo [ARGUMENTS]:
echo \$@
echo ${mockUnityStartupMessage}
""".stripIndent()
}
addUnityPathToExtension(mockUnityFile.path)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,48 @@ abstract class UnityTaskIntegrationSpec<T extends UnityTask> extends UnityIntegr
method = (useSetter) ? "setLogCategory" : "logCategory.set"
}

def "set environment for task exec"() {
given:
appendToSubjectTask("$method($value)")
addProviderQueryTask("custom", "${subjectUnderTestName}.environment", ".get()")

when:
def result = runTasksSuccessfully(subjectUnderTestName, "custom")

then:
result.standardOutput.contains("${subjectUnderTestName}.environment: ${rawValue.toString()}")

where:
property | useSetter | rawValue
"environment" | true | ["A": "7"]
"environment" | false | ["A": "7"]

method = (useSetter) ? "set${property.capitalize()}" : "${property}.set"
value = wrapValueBasedOnType(rawValue, Map)
}

def "adds environment for task exec"() {
given:
appendToSubjectTask("environment.set(${wrapValueBasedOnType(initialValue, Map)})")
appendToSubjectTask("$method(${wrapValueBasedOnType(rawValue, Map)})")
addProviderQueryTask("custom", "${subjectUnderTestName}.environment", ".get().sort()")

when:
def result = runTasksSuccessfully(subjectUnderTestName, "custom")

then:
def expectedValue = new HashMap<String, ?>()
expectedValue.putAll(initialValue)
expectedValue.putAll(rawValue)
result.standardOutput.contains("${subjectUnderTestName}.environment: ${expectedValue}")

where:
method | rawValue | initialValue
"environment.putAll" | ["A": "7"] | ["B": "5"]

value = wrapValueBasedOnType(rawValue, Map)
}

def "task executes without output when set to quiet"() {
given:
logLevel = LogLevel.QUIET
Expand Down
6 changes: 4 additions & 2 deletions src/main/groovy/wooga/gradle/unity/UnityTask.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ abstract class UnityTask extends DefaultTask
// and also an additional one for our custom use
wooga_gradle_unity_traits_ArgumentsSpec__arguments = project.provider({ getUnityCommandLineOptions() })
wooga_gradle_unity_traits_ArgumentsSpec__additionalArguments = project.objects.listProperty(String)
wooga_gradle_unity_traits_ArgumentsSpec__environment = project.objects.mapProperty(String, String)
}

@TaskAction
Expand All @@ -63,14 +64,15 @@ abstract class UnityTask extends DefaultTask

def unityPath = unityPath.get().asFile.absolutePath
def unityArgs = getAllArguments()

OutputStream outputStream = getOutputStream()
def unityEnvironment = environment.get()
def outputStream = getOutputStream()

exec.with {
executable unityPath
args = unityArgs
standardOutput = outputStream
ignoreExitValue = true
environment = unityEnvironment
}
}
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package wooga.gradle.unity.traits

import org.gradle.api.provider.ListProperty
import org.gradle.api.provider.MapProperty
import org.gradle.api.provider.Provider
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.Internal
Expand Down Expand Up @@ -88,4 +89,12 @@ trait ArgumentsSpec {
result
}

private final MapProperty<String, ?> environment
/**
* @return Used for populating the system environment
*/
@Internal
MapProperty<String, ?> getEnvironment() {
environment
}
}

0 comments on commit e8c6687

Please sign in to comment.