From b674f6a3bdf5de358dabd585bcd5e231bdc8f9d9 Mon Sep 17 00:00:00 2001 From: "Philip K. Warren" Date: Tue, 26 Oct 2021 20:50:13 -0500 Subject: [PATCH] fix error when using empty agent label Update AgentDeclaration to only fail if label is null (and not found via other conditionals) instead of when it is falsy. Fixes #412 --- .../unit/declarative/AgentDeclaration.groovy | 2 +- .../declarative/TestDeclarativePipeline.groovy | 8 ++++++++ .../jenkinsfiles/AgentEmptyLabel_Jenkinsfile | 15 +++++++++++++++ 3 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 src/test/jenkins/jenkinsfiles/AgentEmptyLabel_Jenkinsfile diff --git a/src/main/groovy/com/lesfurets/jenkins/unit/declarative/AgentDeclaration.groovy b/src/main/groovy/com/lesfurets/jenkins/unit/declarative/AgentDeclaration.groovy index 23c6137b..db370f43 100644 --- a/src/main/groovy/com/lesfurets/jenkins/unit/declarative/AgentDeclaration.groovy +++ b/src/main/groovy/com/lesfurets/jenkins/unit/declarative/AgentDeclaration.groovy @@ -77,7 +77,7 @@ class AgentDeclaration extends GenericPipelineDeclaration { def execute(Object delegate) { def agentDesc = null - if (label) { + if (label != null) { agentDesc = '[label:' + label.toString() + ']' } else if (docker) { diff --git a/src/test/groovy/com/lesfurets/jenkins/unit/declarative/TestDeclarativePipeline.groovy b/src/test/groovy/com/lesfurets/jenkins/unit/declarative/TestDeclarativePipeline.groovy index 56a5a98a..ecdfeca3 100644 --- a/src/test/groovy/com/lesfurets/jenkins/unit/declarative/TestDeclarativePipeline.groovy +++ b/src/test/groovy/com/lesfurets/jenkins/unit/declarative/TestDeclarativePipeline.groovy @@ -647,4 +647,12 @@ class TestDeclarativePipeline extends DeclarativePipelineTest { assertCallStack().contains('echo(Deploy to someLabel)') assertJobStatusSuccess() } + + @Test void should_agent_with_empty_label() throws Exception { + runScript('AgentEmptyLabel_Jenkinsfile') + printCallStack() + assertCallStack().contains('[label:]') + assertCallStack().contains('echo(Hello using custom workspace and empty label)') + assertJobStatusSuccess() + } } diff --git a/src/test/jenkins/jenkinsfiles/AgentEmptyLabel_Jenkinsfile b/src/test/jenkins/jenkinsfiles/AgentEmptyLabel_Jenkinsfile new file mode 100644 index 00000000..71d1802c --- /dev/null +++ b/src/test/jenkins/jenkinsfiles/AgentEmptyLabel_Jenkinsfile @@ -0,0 +1,15 @@ +pipeline { + agent { + node { + label '' + customWorkspace "myworkspace" + } + } + stages { + stage('Example Build') { + steps { + echo "Hello using custom workspace and empty label" + } + } + } +}