From 53368ebb9ba2d92e64700364091a250581206c14 Mon Sep 17 00:00:00 2001 From: Victor Martinez Date: Wed, 6 Jul 2022 18:05:02 +0100 Subject: [PATCH] Add support for go packages with explicit versions (#1764) --- src/test/groovy/WithGoEnvUnixStepTests.groovy | 14 ++++++++++++++ src/test/groovy/WithGoEnvWindowsStepTests.groovy | 13 +++++++++++++ vars/withGoEnvUnix.groovy | 7 ++++++- vars/withGoEnvWindows.groovy | 7 ++++++- 4 files changed, 39 insertions(+), 2 deletions(-) diff --git a/src/test/groovy/WithGoEnvUnixStepTests.groovy b/src/test/groovy/WithGoEnvUnixStepTests.groovy index c10b849e8..0d43c3295 100644 --- a/src/test/groovy/WithGoEnvUnixStepTests.groovy +++ b/src/test/groovy/WithGoEnvUnixStepTests.groovy @@ -171,6 +171,20 @@ class WithGoEnvUnixStepTests extends ApmBasePipelineTest { assertJobStatusSuccess() } + @Test + void testPkgs_with_version() throws Exception { + helper.registerAllowedMethod('nodeOS', [], { "linux" }) + def isOK = false + script.call(version: "1.16.1", pkgs: [ "P1@1", "P2@2" ]){ + isOK = true + } + printCallStack() + assertTrue(isOK) + assertTrue(assertMethodCallContainsPattern('sh', 'go install P1@1')) + assertTrue(assertMethodCallContainsPattern('sh', 'go install P2@2')) + assertJobStatusSuccess() + } + @Test void testDefaultGoVersion() throws Exception { helper.registerAllowedMethod('nodeOS', [], { "linux" }) diff --git a/src/test/groovy/WithGoEnvWindowsStepTests.groovy b/src/test/groovy/WithGoEnvWindowsStepTests.groovy index db72d0a63..d972a4764 100644 --- a/src/test/groovy/WithGoEnvWindowsStepTests.groovy +++ b/src/test/groovy/WithGoEnvWindowsStepTests.groovy @@ -119,6 +119,19 @@ void testOSArg() throws Exception { assertJobStatusSuccess() } + @Test + void testPkgs_with_version() throws Exception { + def isOK = false + script.call(version: "1.16.1", pkgs: [ "P1@1", "P2@2" ]){ + isOK = definedVariables('1.16.1', 'windows') + } + printCallStack() + assertTrue(isOK) + assertTrue(assertMethodCallContainsPattern('bat', 'go install P1@1')) + assertTrue(assertMethodCallContainsPattern('bat', 'go install P2@2')) + assertJobStatusSuccess() + } + @Test void testDefaultGoVersion() throws Exception { helper.registerAllowedMethod('nodeOS', [], { "windows" }) diff --git a/vars/withGoEnvUnix.groovy b/vars/withGoEnvUnix.groovy index d23f9afdf..1a807ce24 100644 --- a/vars/withGoEnvUnix.groovy +++ b/vars/withGoEnvUnix.groovy @@ -73,8 +73,13 @@ def installPackages(Map args = [:]) { def atVersion = isBeforeGo1_16(version: version) ? '' : "@latest" withEnv(["GOARCH=${arch}"]){ args.pkgs?.each{ p -> + // If the package is already with the given version then use it + def packageWithVersion = "${p}${atVersion}" + if (p.contains('@')) { + packageWithVersion = "${p}" + } retryWithSleep(retries: 3, seconds: 5, backoff: true){ - sh(label: "Installing ${p}", script: "go ${method} ${p}${atVersion}") + sh(label: "Installing ${packageWithVersion}", script: "go ${method} ${packageWithVersion}") } } } diff --git a/vars/withGoEnvWindows.groovy b/vars/withGoEnvWindows.groovy index 016b496b9..153c91e22 100644 --- a/vars/withGoEnvWindows.groovy +++ b/vars/withGoEnvWindows.groovy @@ -63,8 +63,13 @@ def call(Map args = [:], Closure body) { bat(label: "Installing Go ${version}", script: content) } pkgs?.each{ p -> + // If the package is already with the given version then use it + def packageWithVersion = "${p}${atVersion}" + if (p.contains('@')) { + packageWithVersion = "${p}" + } retryWithSleep(retries: 3, seconds: 5, backoff: true){ - bat(label: "Installing ${p}", script: "go ${method} ${p}${atVersion}") + bat(label: "Installing ${packageWithVersion}", script: "go ${method} ${packageWithVersion}") } } body()