Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
Joaquimmnetto committed Dec 13, 2023
1 parent ec864e8 commit 46d6261
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 33 deletions.
56 changes: 56 additions & 0 deletions src/net/wooga/jenkins/pipeline/check/Nodes.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package net.wooga.jenkins.pipeline.check

import net.wooga.jenkins.pipeline.check.steps.PackedStep
import net.wooga.jenkins.pipeline.config.Platform
import net.wooga.jenkins.pipeline.model.Docker

class Nodes {

private Object jenkins
private Docker docker
private EnclosureCreator enclosureCreator

Nodes(Object jenkins, Docker docker, EnclosureCreator enclosureCreator) {
this.jenkins = jenkins
this.docker = docker
this.enclosureCreator = enclosureCreator
}

def withDocker(Platform platform, PackedStep mainCls, Closure catchCls = {throw it}, PackedStep finallyCls = {}) {
return enclosureCreator.atlasNode(platform.name,
platform.generateTestLabelsString(),
platform.testEnvironment,
withCheckout(platform.checkoutDirectory, { docker.runOnImage(mainCls) }),
catchCls,
withCleanup(platform.clearWs, finallyCls)
)
}

def simple(Platform platform, PackedStep mainClosure, Closure catchCls = {throw it}, PackedStep finallyCls = {}) {
return enclosureCreator.atlasNode(platform.name,
platform.generateTestLabelsString(),
platform.testEnvironment,
withCheckout(platform.checkoutDirectory, mainClosure),
catchCls,
withCleanup(platform.clearWs, finallyCls)
)
}

private PackedStep withCheckout(String checkoutDir, PackedStep step) {
return {
jenkins.dir(checkoutDir) {
jenkins.checkout(jenkins.scm)
}
step()
}
}

private PackedStep withCleanup(boolean hasCleanup, PackedStep step) {
return {
step()
if(hasCleanup) {
jenkins.cleanWs()
}
}
}
}
2 changes: 1 addition & 1 deletion src/net/wooga/jenkins/pipeline/config/GradleArgs.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class GradleArgs {


static GradleArgs fromConfigMap(Map config) {
def environment = new EnvVars((config.gradleEnvironment ?: [:]) as Map<String, ?>)
def environment = EnvVars.fromList((config.gradleEnvironment ?: []) as List<?>)
def showStackTraces = (config.showStackTrace ?: false) as boolean
def refreshDependencies = (config.refreshDependencies ?: false) as boolean
return new GradleArgs(environment, config.logLevel as String, showStackTraces, refreshDependencies)
Expand Down
43 changes: 19 additions & 24 deletions src/net/wooga/jenkins/pipeline/model/EnvVars.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,34 @@ package net.wooga.jenkins.pipeline.model

class EnvVars {

final Map<String, Closure<String>> environment
final List<?> environment

EnvVars(Map<String, ?> environment) {
this.environment = lazifyMap(environment)
static EnvVars fromList(List<?> env) {
return new EnvVars(env)
}

static Map<String, Closure<String>> lazifyMap(Map<String, ?> env) {
Map<String, Closure<String>> lazyMap = [:]
for(String key : env.keySet()) {
if(env[key] instanceof Closure) {
lazyMap.put(key, env[key])
} else {
def value = env.get(key)
lazyMap.put(key, { -> value })
}
}
return lazyMap
EnvVars(List<?> environment) {
this.environment = environment
}


List<String> resolveAsStrings() {
List<String> result = []
for(String key : environment.keySet()) {
Closure<String> lazyValue = environment[key]
def value = lazyValue?.call()
if(value != null) {
result += ["$key=$value"]
}
return environment.collect {
resolveString(it)
}
}

def resolveString(Object obj) {
if(obj instanceof Closure) {
return obj.call().toString()
}
return result
return obj.toString()
}

Closure<String> getAt(String key) {
environment[key]
Object getAt(String key) {
environment.collect{resolveString(it).split("=")}.find{
it[0] == key
}
}

}
2 changes: 2 additions & 0 deletions src/net/wooga/jenkins/pipeline/model/Gradle.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ class Gradle {
*/
def wrapper(String command, Boolean returnStatus = false,
Boolean returnStdout = false) {
jenkins.echo "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
jenkins.withEnv(environment.resolveAsStrings()) {
jenkins.echo "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
if (jenkins.isUnix()) {
return jenkins.sh(script: "./gradlew ${formatCommand(command)}", returnStdout: returnStdout, returnStatus: returnStatus)
} else {
Expand Down
10 changes: 5 additions & 5 deletions test/groovy/scripts/GradleWrapperSpec.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ class GradleWrapperSpec extends DeclarativeJenkinsSpec {
usedEnvironments.last() == expectedEnv

where:
givenEnv | expectedEnv
[:] | [:]
[ENV: "eager", EAGER: "eager"] | ["ENV": "eager", "EAGER": "eager"]
[EAGER: "eager", LAZY: { "lazy" }] | ["EAGER": "eager", "LAZY": "lazy"]
[ENV: { "lazy" }, LAZY: { "lazy" }] | ["ENV": "lazy", "LAZY": "lazy"]
givenEnv | expectedEnv
[:] | [:]
["ENV=eager", "EAGER=eager"] | ["ENV": "eager", "EAGER": "eager"]
["EAGER=eager", { "LAZY=lazy" }] | ["EAGER": "eager", "LAZY": "lazy"]
[{ "ENV=lazy" }, { "LAZY=lazy" }] | ["ENV": "lazy", "LAZY": "lazy"]
}

@Unroll
Expand Down
6 changes: 3 additions & 3 deletions vars/gradleWrapper.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import net.wooga.jenkins.pipeline.model.Gradle
/**
* execute gradlew or gradlew.bat based on current os
*/
def call(String command, Boolean returnStatus = false, Boolean returnStdout = false, Map<String, ?> environment=[:]) {
def call(String command, Boolean returnStatus = false, Boolean returnStdout = false, List<?> environment=[]) {
def gradle = Gradle.fromJenkins(this,
new EnvVars(environment),
EnvVars.fromList(environment),
params.LOG_LEVEL?: env.LOG_LEVEL as String,
params.STACK_TRACE? params.STACK_TRACE as Boolean : false,
params.REFRESH_DEPENDENCIES? params.REFRESH_DEPENDENCIES as Boolean : false)
Expand All @@ -18,7 +18,7 @@ def call(Map args) {
call(args.command?.toString(),
(args.returnStatus?: false) as Boolean,
(args.returnStdout?: false) as Boolean,
(args.environment?: [:]) as Map<String, ?>
(args.environment?: []) as List<?>
)
}

Expand Down

0 comments on commit 46d6261

Please sign in to comment.