From a38ce9ac7301c282eddf11138e7447c1ec2438b5 Mon Sep 17 00:00:00 2001 From: Keith Manning Date: Wed, 22 Jan 2020 14:15:32 -0500 Subject: [PATCH 01/33] Add basic travis config --- .travis.yml | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 .travis.yml diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 00000000..cd0d1aa2 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,4 @@ +language: groovy +jdk: + - openjdk9 +script: ./gradlew check --info From 12b0c4c402ca5682051b7a89760aa12fa577cd12 Mon Sep 17 00:00:00 2001 From: Keith Manning Date: Wed, 22 Jan 2020 14:15:40 -0500 Subject: [PATCH 02/33] Lets avoid mucking with Jenkinsfile.instance all together --- src/FileParametersPlugin.groovy | 6 +++++- test/FileParametersPluginTest.groovy | 16 ++++------------ 2 files changed, 9 insertions(+), 13 deletions(-) diff --git a/src/FileParametersPlugin.groovy b/src/FileParametersPlugin.groovy index 56bd4ef6..7c47fae6 100644 --- a/src/FileParametersPlugin.groovy +++ b/src/FileParametersPlugin.groovy @@ -38,6 +38,10 @@ class FileParametersPlugin implements TerraformEnvironmentStagePlugin { } public String interpolate(String value) { - return new StreamingTemplateEngine().createTemplate(value).make([env: Jenkinsfile.instance.getEnv()]).toString() + return new StreamingTemplateEngine().createTemplate(value).make([env: getEnv()]).toString() + } + + public getEnv() { + return Jenkinsfile.instance.getEnv() } } diff --git a/test/FileParametersPluginTest.groovy b/test/FileParametersPluginTest.groovy index ee58ec61..634b1a6e 100644 --- a/test/FileParametersPluginTest.groovy +++ b/test/FileParametersPluginTest.groovy @@ -5,6 +5,7 @@ import org.junit.runner.RunWith import de.bechte.junit.runners.context.HierarchicalContextRunner import static org.mockito.Mockito.when; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.spy; import static org.hamcrest.Matchers.* @RunWith(HierarchicalContextRunner.class) @@ -25,16 +26,6 @@ class FileParametersPluginTest { } public class GetVariables { - @After - public void resetJenkinsfile() { - Jenkinsfile.instance = mock(Jenkinsfile.class) - } - - private configureJenkinsfile(Map config = [:]) { - Jenkinsfile.instance = mock(Jenkinsfile.class) - when(Jenkinsfile.instance.getEnv()).thenReturn(config.env ?: [:]) - } - @Test void returnsAValueForEachLine() { List expectedValues = [ "VAR1=VALUE1", "VAR2=VALUE2" ] @@ -70,10 +61,11 @@ class FileParametersPluginTest { @Test void interpolatesReferencesToOtherEnvironmentVariables() { - configureJenkinsfile(env: [ OTHER_VARIABLE: 'VALUE1' ]) String fileContents = 'SOME_VARIABLE=${env.OTHER_VARIABLE}' - FileParametersPlugin plugin = new FileParametersPlugin() + FileParametersPlugin plugin = spy(new FileParametersPlugin()) + when(plugin.getEnv()).thenReturn([ OTHER_VARIABLE: 'VALUE1']) + List actualValues = plugin.getVariables(fileContents) assertEquals(["SOME_VARIABLE=VALUE1"], actualValues) From 6e1b1a80ca7c2fb599c1e3f7ac40beb83b63ff16 Mon Sep 17 00:00:00 2001 From: Keith Manning Date: Tue, 21 Jan 2020 14:33:20 -0500 Subject: [PATCH 03/33] Guard against empty Jenkinsfile.instance --- src/FileParametersPlugin.groovy | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/FileParametersPlugin.groovy b/src/FileParametersPlugin.groovy index 7c47fae6..f9641637 100644 --- a/src/FileParametersPlugin.groovy +++ b/src/FileParametersPlugin.groovy @@ -42,6 +42,6 @@ class FileParametersPlugin implements TerraformEnvironmentStagePlugin { } public getEnv() { - return Jenkinsfile.instance.getEnv() + return (Jenkinsfile.instance != null) ? Jenkinsfile.instance.getEnv() : [:] } } From 6d6c128f834203c75d04282cc1a8b9ec598fa7ab Mon Sep 17 00:00:00 2001 From: Keith Manning Date: Tue, 21 Jan 2020 16:12:10 -0500 Subject: [PATCH 04/33] Avoid mucking with Jenkinsfile.instance --- src/S3BackendPlugin.groovy | 49 ++++++---- test/S3BackendPluginTest.groovy | 168 ++++++++++++-------------------- 2 files changed, 96 insertions(+), 121 deletions(-) diff --git a/src/S3BackendPlugin.groovy b/src/S3BackendPlugin.groovy index da6d045e..e273d3e9 100644 --- a/src/S3BackendPlugin.groovy +++ b/src/S3BackendPlugin.groovy @@ -34,7 +34,7 @@ class S3BackendPlugin implements TerraformInitCommandPlugin { Closure backendKeyPattern = keyPattern if (backendKeyPattern == null) { - String repoSlug = Jenkinsfile.instance.getStandardizedRepoSlug() + String repoSlug = getStandardizedRepoSlug() backendKeyPattern = { String env -> "terraform/${repoSlug}/${env}" } } @@ -42,15 +42,16 @@ class S3BackendPlugin implements TerraformInitCommandPlugin { } public String getBucket(String environment) { - String bucket = Jenkinsfile.instance.getEnv()["S3_BACKEND_BUCKET"] + def env = getEnv() + String bucket = env["S3_BACKEND_BUCKET"] if (bucket == null) { println("No S3_BACKEND_BUCKET found - checking for environment-specific bucket") - bucket = Jenkinsfile.instance.getEnv()["${environment.toUpperCase()}_S3_BACKEND_BUCKET"] + bucket = env["${environment.toUpperCase()}_S3_BACKEND_BUCKET"] } if (bucket == null) { - bucket = Jenkinsfile.instance.getEnv()["${environment}_S3_BACKEND_BUCKET"] + bucket = env["${environment}_S3_BACKEND_BUCKET"] } if (bucket == null) { @@ -61,19 +62,20 @@ class S3BackendPlugin implements TerraformInitCommandPlugin { } public String getRegion(String environment) { - String region = Jenkinsfile.instance.getEnv()['S3_BACKEND_REGION'] + def env = getEnv() + String region = env['S3_BACKEND_REGION'] if (region == null) { println("No S3_BACKEND_REGION found - checking for environment-specific region") - region = Jenkinsfile.instance.getEnv()["${environment.toUpperCase()}_S3_BACKEND_REGION"] + region = env["${environment.toUpperCase()}_S3_BACKEND_REGION"] } if (region == null) { - region = Jenkinsfile.instance.getEnv()["${environment}_S3_BACKEND_REGION"] + region = env["${environment}_S3_BACKEND_REGION"] } if (region == null) { - region = Jenkinsfile.instance.getEnv()['DEFAULT_S3_BACKEND_REGION'] + region = env['DEFAULT_S3_BACKEND_REGION'] if (region != null) { println("WARNING: DEFAULT_S3_BACKEND_REGION is deprecated, please use S3_BACKEND_REGION or ${environment.toUpperCase()}_S3_BACKEND_REGION") } @@ -83,18 +85,19 @@ class S3BackendPlugin implements TerraformInitCommandPlugin { } public String getDynamodbTable(String environment) { - String table = Jenkinsfile.instance.getEnv()["S3_BACKEND_DYNAMODB_TABLE"] + def env = getEnv() + String table = env["S3_BACKEND_DYNAMODB_TABLE"] if (table == null) { - table = Jenkinsfile.instance.getEnv()["${environment.toUpperCase()}_S3_BACKEND_DYNAMODB_TABLE"] + table = env["${environment.toUpperCase()}_S3_BACKEND_DYNAMODB_TABLE"] } if (table == null) { - table = Jenkinsfile.instance.getEnv()["${environment}_S3_BACKEND_DYNAMODB_TABLE"] + table = env["${environment}_S3_BACKEND_DYNAMODB_TABLE"] } if (table == null) { - table = Jenkinsfile.instance.getEnv()["${environment.toUpperCase()}_S3_BACKEND_DYNAMO_TABLE_LOCK"] + table = env["${environment.toUpperCase()}_S3_BACKEND_DYNAMO_TABLE_LOCK"] if (table != null) { println("${environment.toUpperCase()}_S3_BACKEND_DYNAMO_TABLE_LOCK is deprecated - please use ${environment.toUpperCase()}_S3_BACKEND_DYNAMODB_TABLE instead") } @@ -104,30 +107,40 @@ class S3BackendPlugin implements TerraformInitCommandPlugin { } public String getEncrypt(String environment) { - String encrypt = Jenkinsfile.instance.getEnv()["S3_BACKEND_ENCRYPT"] + def env = getEnv() + String encrypt = env["S3_BACKEND_ENCRYPT"] if (encrypt == null) { - encrypt = Jenkinsfile.instance.getEnv()["${environment.toUpperCase()}_S3_BACKEND_ENCRYPT"] + encrypt = env["${environment.toUpperCase()}_S3_BACKEND_ENCRYPT"] } if (encrypt == null) { - encrypt = Jenkinsfile.instance.getEnv()["${environment}_S3_BACKEND_ENCRYPT"] + encrypt = env["${environment}_S3_BACKEND_ENCRYPT"] } return encrypt } public String getKmsKeyId(String environment) { - String arn = Jenkinsfile.instance.getEnv()["S3_BACKEND_KMS_KEY_ID"] + def env = getEnv() + String arn = env["S3_BACKEND_KMS_KEY_ID"] if (arn == null) { - arn = Jenkinsfile.instance.getEnv()["${environment.toUpperCase()}_S3_BACKEND_KMS_KEY_ID"] + arn = env["${environment.toUpperCase()}_S3_BACKEND_KMS_KEY_ID"] } if (arn == null) { - arn = Jenkinsfile.instance.getEnv()["${environment}_S3_BACKEND_KMS_KEY_ID"] + arn = env["${environment}_S3_BACKEND_KMS_KEY_ID"] } return arn } + + public getEnv() { + return (Jenkinsfile.instance != null) ? Jenkinsfile.instance.getEnv() : [:] + } + + public getStandardizedRepoSlug() { + return Jenkinsfile.instance.getStandardizedRepoSlug() + } } diff --git a/test/S3BackendPluginTest.groovy b/test/S3BackendPluginTest.groovy index c8982021..e32ffcf9 100644 --- a/test/S3BackendPluginTest.groovy +++ b/test/S3BackendPluginTest.groovy @@ -5,21 +5,11 @@ import org.junit.runner.RunWith import de.bechte.junit.runners.context.HierarchicalContextRunner import static org.mockito.Mockito.when; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.spy; import static org.hamcrest.Matchers.* @RunWith(HierarchicalContextRunner.class) class S3BackendPluginTest { - @After - void resetJenkins() { - when(Jenkinsfile.instance.getEnv()).thenReturn([:]) - } - - private configureJenkins(Map config = [:]) { - Jenkinsfile.instance = mock(Jenkinsfile.class) - when(Jenkinsfile.instance.getStandardizedRepoSlug()).thenReturn(config.repoSlug) - when(Jenkinsfile.instance.getEnv()).thenReturn(config.env ?: [:]) - } - public class Init { @After void resetPlugins() { @@ -44,10 +34,9 @@ class S3BackendPluginTest { @Test void addsEnvironmentSpecificKeyAsBackendParameter() { String repoSlug = 'myOrg/myRepo' - configureJenkins(repoSlug: repoSlug) - String environment = "myEnv" - S3BackendPlugin plugin = new S3BackendPlugin() + S3BackendPlugin plugin = spy(new S3BackendPlugin()) + when(plugin.getStandardizedRepoSlug()).thenReturn(repoSlug) TerraformInitCommand command = new TerraformInitCommand(environment) plugin.apply(command) @@ -59,10 +48,9 @@ class S3BackendPluginTest { @Test void addsBucketPreDefinedByEnvironmentAsBackendParameter() { String expectedBucket = 'bucket' - configureJenkins(env: [MYENV_S3_BACKEND_BUCKET: expectedBucket]) - String environment = "myEnv" - S3BackendPlugin plugin = new S3BackendPlugin() + S3BackendPlugin plugin = spy(new S3BackendPlugin()) + when(plugin.getEnv()).thenReturn([MYENV_S3_BACKEND_BUCKET: expectedBucket]) TerraformInitCommand command = new TerraformInitCommand(environment) plugin.apply(command) @@ -74,10 +62,10 @@ class S3BackendPluginTest { @Test void addsBucketRegionUsingPreDefinedEnvironmentVariableAsBackendParameter() { String expectedRegion = 'theFarEast' - configureJenkins(env: [ 'DEFAULT_S3_BACKEND_REGION': expectedRegion ]) String environment = "myEnv" - S3BackendPlugin plugin = new S3BackendPlugin() + S3BackendPlugin plugin = spy(new S3BackendPlugin()) + when(plugin.getEnv()).thenReturn([ 'DEFAULT_S3_BACKEND_REGION': expectedRegion]) TerraformInitCommand command = new TerraformInitCommand(environment) plugin.apply(command) @@ -89,10 +77,10 @@ class S3BackendPluginTest { @Test void addsDynamoDbTableAsBackendParameter() { String dynamodb_table = 'terraform-state-lock-dynamo' - configureJenkins(env: [ 'MYENV_S3_BACKEND_DYNAMO_TABLE_LOCK': dynamodb_table ]) String environment = "myEnv" - S3BackendPlugin plugin = new S3BackendPlugin() + S3BackendPlugin plugin = spy(new S3BackendPlugin()) + when(plugin.getEnv()).thenReturn([ 'MYENV_S3_BACKEND_DYNAMO_TABLE_LOCK': dynamodb_table ]) TerraformInitCommand command = new TerraformInitCommand(environment) plugin.apply(command) @@ -102,11 +90,10 @@ class S3BackendPluginTest { } @Test - void skipsDynamoDbTableAsBackendParameter() { - configureJenkins() - + void skipsDynamoDbTableAsBackendParameterWhenNoneSpecified() { String environment = "myEnv" - S3BackendPlugin plugin = new S3BackendPlugin() + S3BackendPlugin plugin = spy(new S3BackendPlugin()) + when(plugin.getEnv()).thenReturn([:]) TerraformInitCommand command = new TerraformInitCommand(environment) plugin.apply(command) @@ -118,11 +105,10 @@ class S3BackendPluginTest { @Test void isAddedAndUsesCustomizedPatternFolderKeyAsBackendParameter() { String repoSlug = 'myOrg/myRepo' - configureJenkins(repoSlug: repoSlug) - S3BackendPlugin.keyPattern = { String env -> "customPatternFor/${repoSlug}/entropy/${env}" } - S3BackendPlugin plugin = new S3BackendPlugin() - + S3BackendPlugin plugin = spy(new S3BackendPlugin()) + when(plugin.getStandardizedRepoSlug()).thenReturn(repoSlug) + String environment = "myEnv" TerraformInitCommand command = new TerraformInitCommand(environment) plugin.apply(command) @@ -133,10 +119,9 @@ class S3BackendPluginTest { public class GetBackend { @Test void shouldReturnTheValueOfS3BackendBucket() { - def plugin = new S3BackendPlugin() String expectedBucket = 'defaultBucket' - - configureJenkins(env: ['S3_BACKEND_BUCKET': expectedBucket]) + def plugin = spy(new S3BackendPlugin()) + when(plugin.getEnv()).thenReturn(['S3_BACKEND_BUCKET': expectedBucket]) String actualBucket = plugin.getBucket('myenv') @@ -145,10 +130,9 @@ class S3BackendPluginTest { @Test void shouldReturnTheValueOfTheEnvironmentSpecificS3BackendBucket() { - def plugin = new S3BackendPlugin() String expectedBucket = 'myBucket' - - configureJenkins(env: ['MYENV_S3_BACKEND_BUCKET': expectedBucket]) + def plugin = spy(new S3BackendPlugin()) + when(plugin.getEnv()).thenReturn(['MYENV_S3_BACKEND_BUCKET': expectedBucket]) String actualBucket = plugin.getBucket('myenv') @@ -157,10 +141,9 @@ class S3BackendPluginTest { @Test void shouldReturnTheValueOfTheEnvironmentSpecificS3BackendBucketCaseInsensitive() { - def plugin = new S3BackendPlugin() String expectedBucket = 'myBucket' - - configureJenkins(env: ['myenv_S3_BACKEND_BUCKET': expectedBucket]) + def plugin = spy(new S3BackendPlugin()) + when(plugin.getEnv()).thenReturn(['myenv_S3_BACKEND_BUCKET': expectedBucket]) String actualBucket = plugin.getBucket('myenv') @@ -169,9 +152,9 @@ class S3BackendPluginTest { @Test void shouldPreferS3BackendBucketOverEnvironmentSpecificBucket() { - def plugin = new S3BackendPlugin() String expectedBucket = "thisBucket" - configureJenkins(env: ['S3_BACKEND_BUCKET': expectedBucket, + def plugin = spy(new S3BackendPlugin()) + when(plugin.getEnv()).thenReturn(['S3_BACKEND_BUCKET': expectedBucket, 'MYENV_S3_BACKEND_BUCKET': 'notThisBucket', 'myenv_S3_BACKEND_BUCKET': 'notThisBucketEither']) @@ -184,10 +167,9 @@ class S3BackendPluginTest { public class GetRegion { @Test void shouldReturnTheValueOfDefaultS3BackendRegion() { - def plugin = new S3BackendPlugin() String expectedRegion = 'defaultRegion' - - configureJenkins(env: ['DEFAULT_S3_BACKEND_REGION': expectedRegion]) + def plugin = spy(new S3BackendPlugin()) + when(plugin.getEnv()).thenReturn(['DEFAULT_S3_BACKEND_REGION': expectedRegion]) String actualRegion = plugin.getRegion('myenv') @@ -196,10 +178,9 @@ class S3BackendPluginTest { @Test void shouldReturnTheValueOfS3BackendRegion() { - def plugin = new S3BackendPlugin() String expectedRegion = 'region' - - configureJenkins(env: ['S3_BACKEND_REGION': expectedRegion]) + def plugin = spy(new S3BackendPlugin()) + when(plugin.getEnv()).thenReturn(['S3_BACKEND_REGION': expectedRegion]) String actualRegion = plugin.getRegion('myenv') @@ -208,10 +189,9 @@ class S3BackendPluginTest { @Test void shouldReturnTheValueOfEnvironmentSpecificS3BackendRegion() { - def plugin = new S3BackendPlugin() String expectedRegion = 'environmentSpecificRegion' - - configureJenkins(env: ['MYENV_S3_BACKEND_REGION': expectedRegion]) + def plugin = spy(new S3BackendPlugin()) + when(plugin.getEnv()).thenReturn(['MYENV_S3_BACKEND_REGION': expectedRegion]) String actualRegion = plugin.getRegion('myenv') @@ -220,10 +200,9 @@ class S3BackendPluginTest { @Test void shouldReturnTheValueOfEnvironmentSpecificS3BackendRegionCaseInsensitive() { - def plugin = new S3BackendPlugin() String expectedRegion = 'environmentSpecificRegion' - - configureJenkins(env: ['myenv_S3_BACKEND_REGION': expectedRegion]) + def plugin = spy(new S3BackendPlugin()) + when(plugin.getEnv()).thenReturn(['myenv_S3_BACKEND_REGION': expectedRegion]) String actualRegion = plugin.getRegion('myenv') @@ -232,10 +211,9 @@ class S3BackendPluginTest { @Test void shouldPreferS3BackendRegionOverEnvironmentSpecificRegion() { - def plugin = new S3BackendPlugin() String expectedRegion = 'thisRegion' - - configureJenkins(env: [ + def plugin = spy(new S3BackendPlugin()) + when(plugin.getEnv()).thenReturn([ 'S3_BACKEND_REGION': expectedRegion, 'myenv_S3_BACKEND_REGION': 'notThisRegion' ]) @@ -247,10 +225,9 @@ class S3BackendPluginTest { @Test void shouldPreferEnvironmentSpecificRegionOverDeprecatedDefaultS3BackendRegion() { - def plugin = new S3BackendPlugin() String expectedRegion = 'thisRegion' - - configureJenkins(env: [ + def plugin = spy(new S3BackendPlugin()) + when(plugin.getEnv()).thenReturn([ 'MYENV_S3_BACKEND_REGION': expectedRegion, 'DEFAULT_S3_BACKEND_REGION': "notThisRegion" ]) @@ -269,9 +246,8 @@ class S3BackendPluginTest { @Test void shouldBeGeneratedFromRepoSlugAndEnvironment() { - def plugin = new S3BackendPlugin() - - configureJenkins(repoSlug: 'Org/App') + def plugin = spy(new S3BackendPlugin()) + when(plugin.getStandardizedRepoSlug()).thenReturn('Org/App') String actualKey = plugin.getKey('myenv') @@ -292,10 +268,9 @@ class S3BackendPluginTest { public class GetDynamoTable { @Test void shouldReturnDeprecatedS3BackendDynamoTableLockValue() { - def plugin = new S3BackendPlugin() String expectedTable = 'myDeprecatedDynamoTable' - - configureJenkins(env: [MYENV_S3_BACKEND_DYNAMO_TABLE_LOCK: expectedTable]) + def plugin = spy(new S3BackendPlugin()) + when(plugin.getEnv()).thenReturn([MYENV_S3_BACKEND_DYNAMO_TABLE_LOCK: expectedTable]) String actualTable = plugin.getDynamodbTable('myenv') @@ -304,10 +279,9 @@ class S3BackendPluginTest { @Test void shouldReturnS3BackendDynamodbTableValue() { - def plugin = new S3BackendPlugin() String expectedTable = 'myDynamoTable' - - configureJenkins(env: [S3_BACKEND_DYNAMODB_TABLE: expectedTable]) + def plugin = spy(new S3BackendPlugin()) + when(plugin.getEnv()).thenReturn([S3_BACKEND_DYNAMODB_TABLE: expectedTable]) String actualTable = plugin.getDynamodbTable('myenv') @@ -316,10 +290,9 @@ class S3BackendPluginTest { @Test void shouldReturnEnvironmentSpecificS3BackendDynamodbTableValue() { - def plugin = new S3BackendPlugin() String expectedTable = 'myEnvDynamoTable' - - configureJenkins(env: [MYENV_S3_BACKEND_DYNAMODB_TABLE: expectedTable]) + def plugin = spy(new S3BackendPlugin()) + when(plugin.getEnv()).thenReturn([MYENV_S3_BACKEND_DYNAMODB_TABLE: expectedTable]) String actualTable = plugin.getDynamodbTable('myenv') @@ -328,10 +301,9 @@ class S3BackendPluginTest { @Test void shouldReturnEnvironmentSpecificS3BackendDynamodbTableValueCaseInsensitive() { - def plugin = new S3BackendPlugin() String expectedTable = 'myEnvDynamoTable' - - configureJenkins(env: [myenv_S3_BACKEND_DYNAMODB_TABLE: expectedTable]) + def plugin = spy(new S3BackendPlugin()) + when(plugin.getEnv()).thenReturn([myenv_S3_BACKEND_DYNAMODB_TABLE: expectedTable]) String actualTable = plugin.getDynamodbTable('myenv') @@ -340,10 +312,9 @@ class S3BackendPluginTest { @Test void shouldPreferS3BackendDynamodbTableOverEnvironmentSpecificValue() { - def plugin = new S3BackendPlugin() String expectedTable = 'thisTable' - - configureJenkins(env: [ + def plugin = spy(new S3BackendPlugin()) + when(plugin.getEnv()).thenReturn([ S3_BACKEND_DYNAMODB_TABLE: expectedTable, MYENV_S3_BACKEND_DYNAMODB_TABLE: 'notThisTable' ]) @@ -355,10 +326,9 @@ class S3BackendPluginTest { @Test void shouldPreferS3BackendDynamodbTableOverDeprecatedValue() { - def plugin = new S3BackendPlugin() String expectedTable = 'thisTable' - - configureJenkins(env: [ + def plugin = spy(new S3BackendPlugin()) + when(plugin.getEnv()).thenReturn([ S3_BACKEND_DYNAMODB_TABLE: expectedTable, MYENV_S3_BACKEND_DYNAMO_TABLE_LOCK: 'notThisTable' ]) @@ -372,10 +342,9 @@ class S3BackendPluginTest { public class GetEncrypt { @Test void shouldReturnS3BackendEncryptValue() { - def plugin = new S3BackendPlugin() String expectedValue = 'true' - - configureJenkins(env: [S3_BACKEND_ENCRYPT: expectedValue]) + def plugin = spy(new S3BackendPlugin()) + when(plugin.getEnv()).thenReturn([S3_BACKEND_ENCRYPT: expectedValue]) String actualValue = plugin.getEncrypt('myenv') @@ -384,10 +353,9 @@ class S3BackendPluginTest { @Test void shouldReturnEnvironmentSpecificS3BackendEncryptValue() { - def plugin = new S3BackendPlugin() String expectedValue = 'true' - - configureJenkins(env: [MYENV_S3_BACKEND_ENCRYPT: expectedValue]) + def plugin = spy(new S3BackendPlugin()) + when(plugin.getEnv()).thenReturn([MYENV_S3_BACKEND_ENCRYPT: expectedValue]) String actualValue = plugin.getEncrypt('myenv') @@ -396,10 +364,9 @@ class S3BackendPluginTest { @Test void shouldReturnEnvironmentSpecificS3BackendEncryptValueCaseInsensitive() { - def plugin = new S3BackendPlugin() String expectedValue = 'true' - - configureJenkins(env: [myenv_S3_BACKEND_ENCRYPT: expectedValue]) + def plugin = spy(new S3BackendPlugin()) + when(plugin.getEnv()).thenReturn([myenv_S3_BACKEND_ENCRYPT: expectedValue]) String actualValue = plugin.getEncrypt('myenv') @@ -408,10 +375,9 @@ class S3BackendPluginTest { @Test void shouldPreferS3BackendEncryptOverEnvironmentSpecificValue() { - def plugin = new S3BackendPlugin() String expectedValue = 'true' - - configureJenkins(env: [ + def plugin = spy(new S3BackendPlugin()) + when(plugin.getEnv()).thenReturn([ S3_BACKEND_ENCRYPT: expectedValue, MYENV_S3_BACKEND_ENCRYPT: 'false' ]) @@ -425,10 +391,9 @@ class S3BackendPluginTest { public class GetKmsKeyId { @Test void shouldReturnS3BackendKmsKeyIdValue() { - def plugin = new S3BackendPlugin() String expectedArn = 'arn:aws:kms:us-east-1:000000000000:key/eed43b74-c0ff-475f-abab-d0e31b85ee8d' - - configureJenkins(env: [S3_BACKEND_KMS_KEY_ID: expectedArn]) + def plugin = spy(new S3BackendPlugin()) + when(plugin.getEnv()).thenReturn([S3_BACKEND_KMS_KEY_ID: expectedArn]) String actualArn = plugin.getKmsKeyId('myenv') @@ -437,10 +402,9 @@ class S3BackendPluginTest { @Test void shouldReturnEnvironmentSpecificS3BackendKmsKeyIdValue() { - def plugin = new S3BackendPlugin() String expectedArn = 'arn:aws:kms:us-east-1:000000000000:key/eed43b74-c0ff-475f-abab-d0e31b85ee8d' - - configureJenkins(env: [MYENV_S3_BACKEND_KMS_KEY_ID: expectedArn]) + def plugin = spy(new S3BackendPlugin()) + when(plugin.getEnv()).thenReturn([MYENV_S3_BACKEND_KMS_KEY_ID: expectedArn]) String actualArn = plugin.getKmsKeyId('myenv') @@ -449,10 +413,9 @@ class S3BackendPluginTest { @Test void shouldReturnEnvironmentSpecificS3BackendKmsKeyIdValueCaseInsensitive() { - def plugin = new S3BackendPlugin() String expectedArn = 'arn:aws:kms:us-east-1:000000000000:key/eed43b74-c0ff-475f-abab-d0e31b85ee8d' - - configureJenkins(env: [myenv_S3_BACKEND_KMS_KEY_ID: expectedArn]) + def plugin = spy(new S3BackendPlugin()) + when(plugin.getEnv()).thenReturn([myenv_S3_BACKEND_KMS_KEY_ID: expectedArn]) String actualArn = plugin.getKmsKeyId('myenv') @@ -461,10 +424,9 @@ class S3BackendPluginTest { @Test void shouldPreferS3BackendKmsKeyIdOverEnvironmentSpecificValue() { - def plugin = new S3BackendPlugin() String expectedArn = 'arn:aws:kms:us-east-1:000000000000:key/eed43b74-c0ff-475f-abab-d0e31b85ee8d' - - configureJenkins(env: [ + def plugin = spy(new S3BackendPlugin()) + when(plugin.getEnv()).thenReturn([ S3_BACKEND_KMS_KEY_ID: expectedArn, MYENV_S3_BACKEND_KMS_KEY_ID: 'arn:aws:kms:us-east-1:000000000000:key/e665286a-12bc-471f-97fa-38c3eb412074' ]) From f07abb78f9ec266d6cbdd461a1af208fe11eef48 Mon Sep 17 00:00:00 2001 From: Keith Manning Date: Tue, 21 Jan 2020 16:24:46 -0500 Subject: [PATCH 05/33] Guard against empty Jenkinsfile.instance --- src/S3BackendPlugin.groovy | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/S3BackendPlugin.groovy b/src/S3BackendPlugin.groovy index e273d3e9..8f99dbca 100644 --- a/src/S3BackendPlugin.groovy +++ b/src/S3BackendPlugin.groovy @@ -141,6 +141,6 @@ class S3BackendPlugin implements TerraformInitCommandPlugin { } public getStandardizedRepoSlug() { - return Jenkinsfile.instance.getStandardizedRepoSlug() + return (Jenkinsfile.instance != null) ? Jenkinsfile.instance.getStandardizedRepoSlug() : null } } From 1e7fccb5bb012c46f838863443e508579b63eebf Mon Sep 17 00:00:00 2001 From: Keith Manning Date: Wed, 22 Jan 2020 14:02:55 -0500 Subject: [PATCH 06/33] Avoid mucking with Jenkinsfile.instance in tests --- src/CrqPlugin.groovy | 9 +++++++-- test/CrqPluginTest.groovy | 31 +++++++++---------------------- 2 files changed, 16 insertions(+), 24 deletions(-) diff --git a/src/CrqPlugin.groovy b/src/CrqPlugin.groovy index e6fb5fcf..ec86dbcc 100644 --- a/src/CrqPlugin.groovy +++ b/src/CrqPlugin.groovy @@ -16,10 +16,11 @@ class CrqPlugin implements TerraformEnvironmentStagePlugin { } public String getCrqEnvironment(String environment) { - String crqEnvironment = Jenkinsfile.instance.getEnv()['CRQ_ENVIRONMENT'] + def env = getEnv() + String crqEnvironment = env['CRQ_ENVIRONMENT'] if (crqEnvironment == null) { - crqEnvironment = Jenkinsfile.instance.getEnv()["${environment.toUpperCase()}_CRQ_ENVIRONMENT"] + crqEnvironment = env["${environment.toUpperCase()}_CRQ_ENVIRONMENT"] } return crqEnvironment @@ -78,4 +79,8 @@ class CrqPlugin implements TerraformEnvironmentStagePlugin { return "manheim_remedy error `cat ChangeID.txt | sed 's/ChangeID=//g'` \"${reason}\"" } + public getEnv() { + return Jenkinsfile.instance.getEnv() + } + } diff --git a/test/CrqPluginTest.groovy b/test/CrqPluginTest.groovy index 302c7398..3c16a15c 100644 --- a/test/CrqPluginTest.groovy +++ b/test/CrqPluginTest.groovy @@ -12,17 +12,6 @@ import static org.mockito.Mockito.*; @RunWith(HierarchicalContextRunner.class) class CrqPluginTest { - @After - void resetJenkins() { - when(Jenkinsfile.instance.getEnv()).thenReturn([:]) - } - - private configureJenkins(Map config = [:]) { - Jenkinsfile.instance = mock(Jenkinsfile.class) - when(Jenkinsfile.instance.getStandardizedRepoSlug()).thenReturn(config.repoSlug) - when(Jenkinsfile.instance.getEnv()).thenReturn(config.env ?: [:]) - } - public class Init { @After void resetPlugins() { @@ -42,9 +31,9 @@ class CrqPluginTest { public class withCrqEnvironment { @Test public void shouldExecutePipeline() { - configureJenkins(env: ['CRQ_ENVIRONMENT': 'MyCrqEnv'], repoSlug: 'Org/Repo') - - def plugin = new CrqPlugin() + def plugin = spy(new CrqPlugin()) + when(plugin.getEnv()).thenReturn(['CRQ_ENVIRONMENT': 'MyCrqEnv']) + //when(plugin.getrepoSlug: 'Org/Repo')) Closure crqClosure = plugin.addCrq('myEnv') Closure pipeline = mock(Closure.class) @@ -79,10 +68,9 @@ class CrqPluginTest { public class GetCrqEnviroment { @Test void returnsCrqEnvirommentIfPresent() { - def plugin = new CrqPlugin() String expectedCrqEnvironment = 'someEnvironment' - - configureJenkins(env: ['CRQ_ENVIRONMENT': expectedCrqEnvironment]) + CrqPlugin plugin = spy(new CrqPlugin()) + when(plugin.getEnv()).thenReturn(['CRQ_ENVIRONMENT': expectedCrqEnvironment]) String actualCrqEnvironment = plugin.getCrqEnvironment('myenv') @@ -91,10 +79,9 @@ class CrqPluginTest { @Test void returnsEnvironmentSpecificCrqEnvirommentIfPresent() { - def plugin = new CrqPlugin() String expectedCrqEnvironment = 'someEnvironment' - - configureJenkins(env: ['MYENV_CRQ_ENVIRONMENT': expectedCrqEnvironment]) + CrqPlugin plugin = spy(new CrqPlugin()) + when(plugin.getEnv()).thenReturn(['MYENV_CRQ_ENVIRONMENT': expectedCrqEnvironment]) String actualCrqEnvironment = plugin.getCrqEnvironment('myenv') @@ -106,11 +93,11 @@ class CrqPluginTest { // The UseCase: // Set PROD_CRQ_ENVIRONMENT as a global variable, to make multiple pipeline's prod behave the same way // Set CRQ_ENVIRONMENT in a specific app's prod environment (eg: parameterstore), to override the global value - def plugin = new CrqPlugin() + CrqPlugin plugin = spy(new CrqPlugin()) String nonPrefixedCrq = 'nonPrefixed' String prefixedCrq = 'prefixed' - configureJenkins(env: [ + when(plugin.getEnv()).thenReturn([ 'CRQ_ENVIRONMENT': nonPrefixedCrq, 'MYENV_CRQ_ENVIRONMENT': prefixedCrq ]) From c18058a71b73dda566143d8fe3b10ba432205daa Mon Sep 17 00:00:00 2001 From: Keith Manning Date: Wed, 22 Jan 2020 14:05:30 -0500 Subject: [PATCH 07/33] Guard against null Jenkinsfile.instance --- src/CrqPlugin.groovy | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/CrqPlugin.groovy b/src/CrqPlugin.groovy index ec86dbcc..46dfdb0f 100644 --- a/src/CrqPlugin.groovy +++ b/src/CrqPlugin.groovy @@ -80,7 +80,7 @@ class CrqPlugin implements TerraformEnvironmentStagePlugin { } public getEnv() { - return Jenkinsfile.instance.getEnv() + return (Jenkinsfile.instance != null) ? Jenkinsfile.instance.getEnv() : [:] } } From 9ed0ae3fe4bc4898abf5dc27edb15fbc9ce25f45 Mon Sep 17 00:00:00 2001 From: Keith Manning Date: Wed, 22 Jan 2020 14:09:38 -0500 Subject: [PATCH 08/33] Make getRepoName stubbable and guarded --- src/CrqPlugin.groovy | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/CrqPlugin.groovy b/src/CrqPlugin.groovy index 46dfdb0f..0227a527 100644 --- a/src/CrqPlugin.groovy +++ b/src/CrqPlugin.groovy @@ -33,7 +33,7 @@ class CrqPlugin implements TerraformEnvironmentStagePlugin { if (crqEnvironment) { def config = [ environment: environment, - app: Jenkinsfile.instance.getRepoName(), + app: getRepoName(), crqEnvironment: crqEnvironment ] @@ -83,4 +83,7 @@ class CrqPlugin implements TerraformEnvironmentStagePlugin { return (Jenkinsfile.instance != null) ? Jenkinsfile.instance.getEnv() : [:] } + public String getRepoName() { + return (Jenkinsfile.instance != null) ? Jenkinsfile.instance.getRepoName() : null + } } From f4886e4a6a258f58780ee79ea9cf424fea1908f2 Mon Sep 17 00:00:00 2001 From: Keith Manning Date: Wed, 22 Jan 2020 14:21:44 -0500 Subject: [PATCH 09/33] Add build status badge to README --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 44fdbf49..5e4f2004 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,5 @@ +[![Build Status](https://travis-ci.com/manheim/terraform-pipeline.svg?branch=master)](https://travis-ci.com/manheim/terraform-pipeline) + # terraform-pipeline A reusable pipeline template to apply terraform configuration serially across multiple environments. From 11250195155305f7ae4770edfa7ee19552b117de Mon Sep 17 00:00:00 2001 From: Keith Manning Date: Wed, 22 Jan 2020 15:40:22 -0500 Subject: [PATCH 10/33] Update ChangeLog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 65d51a1d..9c0953a7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,7 @@ # Unreleased +* [Issue #87](https://github.com/manheim/terraform-pipeline/issues/87) Add travis CI support to the project + # v5.3 * [Issue #178](https://github.com/manheim/terraform-pipeline/issues/178) Fix broken link to source code in AwssumePlugin docs From 048e1d2d5c4922a409cb398433fefec7ba57a223 Mon Sep 17 00:00:00 2001 From: Keith Manning Date: Thu, 23 Jan 2020 10:51:33 -0500 Subject: [PATCH 11/33] Run jacoco code coverage and send to codecov.io --- .travis.yml | 2 + build.gradle | 9 + codecov.sh | 1660 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 1671 insertions(+) create mode 100755 codecov.sh diff --git a/.travis.yml b/.travis.yml index cd0d1aa2..cbbed67d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,3 +2,5 @@ language: groovy jdk: - openjdk9 script: ./gradlew check --info +after_success: + - ./codecov.sh diff --git a/build.gradle b/build.gradle index e42f0108..c00331eb 100644 --- a/build.gradle +++ b/build.gradle @@ -30,3 +30,12 @@ sourceSets { } } + +jacocoTestReport { + reports { + xml.enabled true + html.enabled true + } +} + +check.dependsOn jacocoTestReport diff --git a/codecov.sh b/codecov.sh new file mode 100755 index 00000000..8648ee4d --- /dev/null +++ b/codecov.sh @@ -0,0 +1,1660 @@ +#!/usr/bin/env bash + +# Apache License Version 2.0, January 2004 +# https://github.com/codecov/codecov-bash/blob/master/LICENSE + + +set -e +o pipefail + +VERSION="20191211-b8db533" + +url="https://codecov.io" +env="$CODECOV_ENV" +service="" +token="" +search_in="" +flags="" +exit_with=0 +curlargs="" +curlawsargs="" +dump="0" +clean="0" +curl_s="-s" +name="$CODECOV_NAME" +include_cov="" +exclude_cov="" +ddp="$(echo ~)/Library/Developer/Xcode/DerivedData" +xp="" +files="" +cacert="$CODECOV_CA_BUNDLE" +gcov_ignore="-not -path './bower_components/**' -not -path './node_modules/**' -not -path './vendor/**'" +gcov_include="" + +ft_gcov="1" +ft_coveragepy="1" +ft_fix="1" +ft_search="1" +ft_s3="1" +ft_network="1" +ft_xcodellvm="1" +ft_xcodeplist="0" +ft_gcovout="1" + +_git_root=$(git rev-parse --show-toplevel 2>/dev/null || hg root 2>/dev/null || echo $PWD) +git_root="$_git_root" +codecov_yml="" +remote_addr="" +if [ "$git_root" = "$PWD" ]; +then + git_root="." +fi + +url_o="" +pr_o="" +build_o="" +commit_o="" +search_in_o="" +tag_o="" +branch_o="" +slug_o="" +prefix_o="" + +commit="$VCS_COMMIT_ID" +branch="$VCS_BRANCH_NAME" +pr="$VCS_PULL_REQUEST" +slug="$VCS_SLUG" +tag="$VCS_TAG" +build_url="$CI_BUILD_URL" +build="$CI_BUILD_ID" +job="$CI_JOB_ID" + +beta_xcode_partials="" + +proj_root="$git_root" +gcov_exe="gcov" +gcov_arg="" + +b="\033[0;36m" +g="\033[0;32m" +r="\033[0;31m" +e="\033[0;90m" +x="\033[0m" + +show_help() { +cat << EOF + + Codecov Bash $VERSION + + Global report uploading tool for Codecov + Documentation at https://docs.codecov.io/docs + Contribute at https://github.com/codecov/codecov-bash + + + -h Display this help and exit + -f FILE Target file(s) to upload + + -f "path/to/file" only upload this file + skips searching unless provided patterns below + + -f '!*.bar' ignore all files at pattern *.bar + -f '*.foo' include all files at pattern *.foo + Must use single quotes. + This is non-exclusive, use -s "*.foo" to match specific paths. + + -s DIR Directory to search for coverage reports. + Already searches project root and artifact folders. + -t TOKEN Set the private repository token + (option) set environment variable CODECOV_TOKEN=:uuid + + -t @/path/to/token_file + -t uuid + + -n NAME Custom defined name of the upload. Visible in Codecov UI + + -e ENV Specify environment variables to be included with this build + Also accepting environment variables: CODECOV_ENV=VAR,VAR2 + + -e VAR,VAR2 + + -X feature Toggle functionalities + + -X gcov Disable gcov + -X coveragepy Disable python coverage + -X fix Disable report fixing + -X search Disable searching for reports + -X xcode Disable xcode processing + -X network Disable uploading the file network + -X gcovout Disable gcov output + + -N The commit SHA of the parent for which you are uploading coverage. If not present, + the parent will be determined using the API of your repository provider. + When using the repository provider's API, the parent is determined via finding + the closest ancestor to the commit. + + -R root dir Used when not in git/hg project to identify project root directory + -y conf file Used to specify the location of the .codecov.yml config file + -F flag Flag the upload to group coverage metrics + + -F unittests This upload is only unittests + -F integration This upload is only integration tests + -F ui,chrome This upload is Chrome - UI tests + + -c Move discovered coverage reports to the trash + -Z Exit with 1 if not successful. Default will Exit with 0 + + -- xcode -- + -D Custom Derived Data Path for Coverage.profdata and gcov processing + Default '~/Library/Developer/Xcode/DerivedData' + -J Specify packages to build coverage. + This can significantly reduces time to build coverage reports. + + -J 'MyAppName' Will match "MyAppName" and "MyAppNameTests" + -J '^ExampleApp$' Will match only "ExampleApp" not "ExampleAppTests" + + -- gcov -- + -g GLOB Paths to ignore during gcov gathering + -G GLOB Paths to include during gcov gathering + -p dir Project root directory + Also used when preparing gcov + -k prefix Prefix filepaths to help resolve path fixing: https://github.com/codecov/support/issues/472 + -x gcovexe gcov executable to run. Defaults to 'gcov' + -a gcovargs extra arguments to pass to gcov + + -- Override CI Environment Variables -- + These variables are automatically detected by popular CI providers + + -B branch Specify the branch name + -C sha Specify the commit sha + -P pr Specify the pull request number + -b build Specify the build number + -T tag Specify the git tag + + -- Enterprise -- + -u URL Set the target url for Enterprise customers + Not required when retrieving the bash uploader from your CCE + (option) Set environment variable CODECOV_URL=https://my-hosted-codecov.com + -r SLUG owner/repo slug used instead of the private repo token in Enterprise + (option) set environment variable CODECOV_SLUG=:owner/:repo + (option) set in your codecov.yml "codecov.slug" + -S PATH File path to your cacert.pem file used to verify ssl with Codecov Enterprise (optional) + (option) Set environment variable: CODECOV_CA_BUNDLE="/path/to/ca.pem" + -U curlargs Extra curl arguments to communicate with Codecov. e.g., -U "--proxy http://http-proxy" + -A curlargs Extra curl arguments to communicate with AWS. + + -- Debugging -- + -d Don't upload, but dump upload file to stdout + -K Remove color from the output + -v Verbose mode + +EOF +} + + +say() { + echo -e "$1" +} + + +urlencode() { + echo "$1" | curl -Gso /dev/null -w %{url_effective} --data-urlencode @- "" | cut -c 3- | sed -e 's/%0A//' +} + + +swiftcov() { + _dir=$(dirname "$1" | sed 's/\(Build\).*/\1/g') + for _type in app framework xctest + do + find "$_dir" -name "*.$_type" | while read f + do + _proj=${f##*/} + _proj=${_proj%."$_type"} + if [ "$2" = "" ] || [ "$(echo "$_proj" | grep -i "$2")" != "" ]; + then + say " $g+$x Building reports for $_proj $_type" + dest=$([ -f "$f/$_proj" ] && echo "$f/$_proj" || echo "$f/Contents/MacOS/$_proj") + _proj_name=$(echo "$_proj" | sed -e 's/[[:space:]]//g') + xcrun llvm-cov show $beta_xcode_partials -instr-profile "$1" "$dest" > "$_proj_name.$_type.coverage.txt" \ + || say " ${r}x>${x} llvm-cov failed to produce results for $dest" + fi + done + done +} + + +# Credits to: https://gist.github.com/pkuczynski/8665367 +parse_yaml() { + local prefix=$2 + local s='[[:space:]]*' w='[a-zA-Z0-9_]*' fs=$(echo @|tr @ '\034') + sed -ne "s|^\($s\)\($w\)$s:$s\"\(.*\)\"$s\$|\1$fs\2$fs\3|p" \ + -e "s|^\($s\)\($w\)$s:$s\(.*\)$s\$|\1$fs\2$fs\3|p" $1 | + awk -F$fs '{ + indent = length($1)/2; + vname[indent] = $2; + for (i in vname) {if (i > indent) {delete vname[i]}} + if (length($3) > 0) { + vn=""; if (indent > 0) {vn=(vn)(vname[0])("_")} + printf("%s%s%s=\"%s\"\n", "'$prefix'",vn, $2, $3); + } + }' +} + + +if [ $# != 0 ]; +then + while getopts "a:A:b:B:cC:dD:e:f:F:g:G:hJ:k:Kn:p:P:r:R:y:s:S:t:T:u:U:vx:X:ZN:" o + do + case "$o" in + "N") + parent=$OPTARG + ;; + "a") + gcov_arg=$OPTARG + ;; + "A") + curlawsargs="$OPTARG" + ;; + "b") + build_o="$OPTARG" + ;; + "B") + branch_o="$OPTARG" + ;; + "c") + clean="1" + ;; + "C") + commit_o="$OPTARG" + ;; + "d") + dump="1" + ;; + "D") + ddp="$OPTARG" + ;; + "e") + env="$env,$OPTARG" + ;; + "f") + if [ "${OPTARG::1}" = "!" ]; + then + exclude_cov="$exclude_cov -not -path '${OPTARG:1}'" + + elif [[ "$OPTARG" = *"*"* ]]; + then + include_cov="$include_cov -or -name '$OPTARG'" + + else + ft_search=0 + if [ "$files" = "" ]; + then + files="$OPTARG" + else + files="$files +$OPTARG" + fi + fi + ;; + "F") + if [ "$flags" = "" ]; + then + flags="$OPTARG" + else + flags="$flags,$OPTARG" + fi + ;; + "g") + gcov_ignore="$gcov_ignore -not -path '$OPTARG'" + ;; + "G") + gcov_include="$gcov_include -path '$OPTARG'" + ;; + "h") + show_help + exit 0; + ;; + "J") + ft_xcodellvm="1" + ft_xcodeplist="0" + if [ "$xp" = "" ]; + then + xp="$OPTARG" + else + xp="$xp\|$OPTARG" + fi + ;; + "k") + prefix_o=$(echo "$OPTARG" | sed -e 's:^/*::' -e 's:/*$::') + ;; + "K") + b="" + g="" + r="" + e="" + x="" + ;; + "n") + name="$OPTARG" + ;; + "p") + proj_root="$OPTARG" + ;; + "P") + pr_o="$OPTARG" + ;; + "r") + slug_o="$OPTARG" + ;; + "R") + git_root="$OPTARG" + ;; + "s") + if [ "$search_in_o" = "" ]; + then + search_in_o="$OPTARG" + else + search_in_o="$search_in_o $OPTARG" + fi + ;; + "S") + cacert="--cacert \"$OPTARG\"" + ;; + "t") + if [ "${OPTARG::1}" = "@" ]; + then + token=$(cat "${OPTARG:1}" | tr -d ' \n') + else + token="$OPTARG" + fi + ;; + "T") + tag_o="$OPTARG" + ;; + "u") + url_o=$(echo "$OPTARG" | sed -e 's/\/$//') + ;; + "U") + curlargs="$OPTARG" + ;; + "v") + set -x + curl_s="" + ;; + "x") + gcov_exe=$OPTARG + ;; + "X") + if [ "$OPTARG" = "gcov" ]; + then + ft_gcov="0" + elif [ "$OPTARG" = "coveragepy" ] || [ "$OPTARG" = "py" ]; + then + ft_coveragepy="0" + elif [ "$OPTARG" = "gcovout" ]; + then + ft_gcovout="0" + elif [ "$OPTARG" = "xcodellvm" ]; + then + ft_xcodellvm="1" + ft_xcodeplist="0" + elif [ "$OPTARG" = "fix" ] || [ "$OPTARG" = "fixes" ]; + then + ft_fix="0" + elif [ "$OPTARG" = "xcode" ]; + then + ft_xcodellvm="0" + ft_xcodeplist="0" + elif [ "$OPTARG" = "search" ]; + then + ft_search="0" + elif [ "$OPTARG" = "xcodepartials" ]; + then + beta_xcode_partials="-use-color" + elif [ "$OPTARG" = "network" ]; + then + ft_network="0" + elif [ "$OPTARG" = "s3" ]; + then + ft_s3="0" + fi + ;; + "y") + codecov_yml="$OPTARG" + ;; + "Z") + exit_with=1 + ;; + esac + done +fi + +say " + _____ _ + / ____| | | +| | ___ __| | ___ ___ _____ __ +| | / _ \\ / _\` |/ _ \\/ __/ _ \\ \\ / / +| |___| (_) | (_| | __/ (_| (_) \\ V / + \\_____\\___/ \\__,_|\\___|\\___\\___/ \\_/ + Bash-$VERSION + +" + +search_in="$proj_root" + +if [ "$JENKINS_URL" != "" ]; +then + say "$e==>$x Jenkins CI detected." + # https://wiki.jenkins-ci.org/display/JENKINS/Building+a+software+project + # https://wiki.jenkins-ci.org/display/JENKINS/GitHub+pull+request+builder+plugin#GitHubpullrequestbuilderplugin-EnvironmentVariables + service="jenkins" + + if [ "$ghprbSourceBranch" != "" ]; + then + branch="$ghprbSourceBranch" + elif [ "$GIT_BRANCH" != "" ]; + then + branch="$GIT_BRANCH" + elif [ "$BRANCH_NAME" != "" ]; + then + branch="$BRANCH_NAME" + fi + + if [ "$ghprbActualCommit" != "" ]; + then + commit="$ghprbActualCommit" + elif [ "$GIT_COMMIT" != "" ]; + then + commit="$GIT_COMMIT" + fi + + if [ "$ghprbPullId" != "" ]; + then + pr="$ghprbPullId" + elif [ "$CHANGE_ID" != "" ]; + then + pr="$CHANGE_ID" + fi + + build="$BUILD_NUMBER" + build_url=$(urlencode "$BUILD_URL") + +elif [ "$CI" = "true" ] && [ "$TRAVIS" = "true" ] && [ "$SHIPPABLE" != "true" ]; +then + say "$e==>$x Travis CI detected." + # https://docs.travis-ci.com/user/environment-variables/ + service="travis" + commit="${TRAVIS_PULL_REQUEST_SHA:-$TRAVIS_COMMIT}" + build="$TRAVIS_JOB_NUMBER" + pr="$TRAVIS_PULL_REQUEST" + job="$TRAVIS_JOB_ID" + slug="$TRAVIS_REPO_SLUG" + env="$env,TRAVIS_OS_NAME" + tag="$TRAVIS_TAG" + if [ "$TRAVIS_BRANCH" != "$TRAVIS_TAG" ]; + then + branch="$TRAVIS_BRANCH" + fi + + language=$(compgen -A variable | grep "^TRAVIS_.*_VERSION$" | head -1) + if [ "$language" != "" ]; + then + env="$env,${!language}" + fi + +elif [ "$CODEBUILD_BUILD_ARN" != "" ]; +then + say "$e==>$x AWS Codebuild detected." + # https://docs.aws.amazon.com/codebuild/latest/userguide/build-env-ref-env-vars.html + service="codebuild" + commit="$CODEBUILD_RESOLVED_SOURCE_VERSION" + build="$CODEBUILD_BUILD_ID" + branch="$(echo $CODEBUILD_WEBHOOK_HEAD_REF | sed 's/^refs\/heads\///')" + if [ "${CODEBUILD_SOURCE_VERSION/pr}" = "$CODEBUILD_SOURCE_VERSION" ] ; then + pr="false" + else + pr="$(echo $CODEBUILD_SOURCE_VERSION | sed 's/^pr\///')" + fi + job="$CODEBUILD_BUILD_ID" + slug="$(echo $CODEBUILD_SOURCE_REPO_URL | sed 's/^.*github.com\///' | sed 's/\.git$//')" + +elif [ "$DOCKER_REPO" != "" ]; +then + say "$e==>$x Docker detected." + # https://docs.docker.com/docker-cloud/builds/advanced/ + service="docker" + branch="$SOURCE_BRANCH" + commit="$SOURCE_COMMIT" + slug="$DOCKER_REPO" + tag="$CACHE_TAG" + env="$env,IMAGE_NAME" + +elif [ "$CI" = "true" ] && [ "$CI_NAME" = "codeship" ]; +then + say "$e==>$x Codeship CI detected." + # https://www.codeship.io/documentation/continuous-integration/set-environment-variables/ + service="codeship" + branch="$CI_BRANCH" + build="$CI_BUILD_NUMBER" + build_url=$(urlencode "$CI_BUILD_URL") + commit="$CI_COMMIT_ID" + +elif [ ! -z "$CF_BUILD_URL" ] && [ ! -z "$CF_BUILD_ID" ]; +then + say "$e==>$x Codefresh CI detected." + # https://docs.codefresh.io/v1.0/docs/variables + service="codefresh" + branch="$CF_BRANCH" + build="$CF_BUILD_ID" + build_url=$(urlencode "$CF_BUILD_URL") + commit="$CF_REVISION" + +elif [ "$TEAMCITY_VERSION" != "" ]; +then + say "$e==>$x TeamCity CI detected." + # https://confluence.jetbrains.com/display/TCD8/Predefined+Build+Parameters + # https://confluence.jetbrains.com/plugins/servlet/mobile#content/view/74847298 + if [ "$TEAMCITY_BUILD_BRANCH" = '' ]; + then + echo " Teamcity does not automatically make build parameters available as environment variables." + echo " Add the following environment parameters to the build configuration" + echo " env.TEAMCITY_BUILD_BRANCH = %teamcity.build.branch%" + echo " env.TEAMCITY_BUILD_ID = %teamcity.build.id%" + echo " env.TEAMCITY_BUILD_URL = %teamcity.serverUrl%/viewLog.html?buildId=%teamcity.build.id%" + echo " env.TEAMCITY_BUILD_COMMIT = %system.build.vcs.number%" + echo " env.TEAMCITY_BUILD_REPOSITORY = %vcsroot..url%" + fi + service="teamcity" + branch="$TEAMCITY_BUILD_BRANCH" + build="$TEAMCITY_BUILD_ID" + build_url=$(urlencode "$TEAMCITY_BUILD_URL") + if [ "$TEAMCITY_BUILD_COMMIT" != "" ]; + then + commit="$TEAMCITY_BUILD_COMMIT" + else + commit="$BUILD_VCS_NUMBER" + fi + remote_addr="$TEAMCITY_BUILD_REPOSITORY" + +elif [ "$CI" = "true" ] && [ "$CIRCLECI" = "true" ]; +then + say "$e==>$x Circle CI detected." + # https://circleci.com/docs/environment-variables + service="circleci" + branch="$CIRCLE_BRANCH" + build="$CIRCLE_BUILD_NUM" + job="$CIRCLE_NODE_INDEX" + if [ "$CIRCLE_PROJECT_REPONAME" != "" ]; + then + slug="$CIRCLE_PROJECT_USERNAME/$CIRCLE_PROJECT_REPONAME" + else + # git@github.com:owner/repo.git + slug="${CIRCLE_REPOSITORY_URL##*:}" + # owner/repo.git + slug="${slug%%.git}" + fi + pr="$CIRCLE_PR_NUMBER" + commit="$CIRCLE_SHA1" + search_in="$search_in $CIRCLE_ARTIFACTS $CIRCLE_TEST_REPORTS" + +elif [ "$BUDDYBUILD_BRANCH" != "" ]; +then + say "$e==>$x buddybuild detected" + # http://docs.buddybuild.com/v6/docs/custom-prebuild-and-postbuild-steps + service="buddybuild" + branch="$BUDDYBUILD_BRANCH" + build="$BUDDYBUILD_BUILD_NUMBER" + build_url="https://dashboard.buddybuild.com/public/apps/$BUDDYBUILD_APP_ID/build/$BUDDYBUILD_BUILD_ID" + # BUDDYBUILD_TRIGGERED_BY + if [ "$ddp" = "$(echo ~)/Library/Developer/Xcode/DerivedData" ]; + then + ddp="/private/tmp/sandbox/${BUDDYBUILD_APP_ID}/bbtest" + fi + +elif [ "${bamboo_planRepository_revision}" != "" ]; +then + say "$e==>$x Bamboo detected" + # https://confluence.atlassian.com/bamboo/bamboo-variables-289277087.html#Bamboovariables-Build-specificvariables + service="bamboo" + commit="${bamboo_planRepository_revision}" + branch="${bamboo_planRepository_branch}" + build="${bamboo_buildNumber}" + build_url="${bamboo_buildResultsUrl}" + remote_addr="${bamboo_planRepository_repositoryUrl}" + +elif [ "$CI" = "true" ] && [ "$BITRISE_IO" = "true" ]; +then + # http://devcenter.bitrise.io/faq/available-environment-variables/ + say "$e==>$x Bitrise CI detected." + service="bitrise" + branch="$BITRISE_GIT_BRANCH" + build="$BITRISE_BUILD_NUMBER" + build_url=$(urlencode "$BITRISE_BUILD_URL") + pr="$BITRISE_PULL_REQUEST" + if [ "$GIT_CLONE_COMMIT_HASH" != "" ]; + then + commit="$GIT_CLONE_COMMIT_HASH" + fi + +elif [ "$CI" = "true" ] && [ "$SEMAPHORE" = "true" ]; +then + say "$e==>$x Semaphore CI detected." + # https://semaphoreapp.com/docs/available-environment-variables.html + service="semaphore" + branch="$BRANCH_NAME" + build="$SEMAPHORE_BUILD_NUMBER" + job="$SEMAPHORE_CURRENT_THREAD" + pr="$PULL_REQUEST_NUMBER" + slug="$SEMAPHORE_REPO_SLUG" + commit="$REVISION" + env="$env,SEMAPHORE_TRIGGER_SOURCE" + +elif [ "$CI" = "true" ] && [ "$BUILDKITE" = "true" ]; +then + say "$e==>$x Buildkite CI detected." + # https://buildkite.com/docs/guides/environment-variables + service="buildkite" + branch="$BUILDKITE_BRANCH" + build="$BUILDKITE_BUILD_NUMBER" + job="$BUILDKITE_JOB_ID" + build_url=$(urlencode "$BUILDKITE_BUILD_URL") + slug="$BUILDKITE_PROJECT_SLUG" + commit="$BUILDKITE_COMMIT" + if [[ "$BUILDKITE_PULL_REQUEST" != "false" ]]; then + pr="$BUILDKITE_PULL_REQUEST" + fi + tag="$BUILDKITE_TAG" + +elif [ "$CI" = "drone" ] || [ "$DRONE" = "true" ]; +then + say "$e==>$x Drone CI detected." + # http://docs.drone.io/env.html + # drone commits are not full shas + service="drone.io" + branch="$DRONE_BRANCH" + build="$DRONE_BUILD_NUMBER" + build_url=$(urlencode "${DRONE_BUILD_LINK}") + pr="$DRONE_PULL_REQUEST" + job="$DRONE_JOB_NUMBER" + tag="$DRONE_TAG" + +elif [ "$HEROKU_TEST_RUN_BRANCH" != "" ]; +then + say "$e==>$x Heroku CI detected." + # https://devcenter.heroku.com/articles/heroku-ci#environment-variables + service="heroku" + branch="$HEROKU_TEST_RUN_BRANCH" + build="$HEROKU_TEST_RUN_ID" + +elif [ "$CI" = "True" ] && [ "$APPVEYOR" = "True" ]; +then + say "$e==>$x Appveyor CI detected." + # http://www.appveyor.com/docs/environment-variables + service="appveyor" + branch="$APPVEYOR_REPO_BRANCH" + build=$(urlencode "$APPVEYOR_JOB_ID") + pr="$APPVEYOR_PULL_REQUEST_NUMBER" + job="$APPVEYOR_ACCOUNT_NAME%2F$APPVEYOR_PROJECT_SLUG%2F$APPVEYOR_BUILD_VERSION" + slug="$APPVEYOR_REPO_NAME" + commit="$APPVEYOR_REPO_COMMIT" + build_url=$(urlencode "${APPVEYOR_URL}/project/${APPVEYOR_REPO_NAME}/builds/$APPVEYOR_BUILD_ID/job/${APPVEYOR_JOB_ID}") +elif [ "$CI" = "true" ] && [ "$WERCKER_GIT_BRANCH" != "" ]; +then + say "$e==>$x Wercker CI detected." + # http://devcenter.wercker.com/articles/steps/variables.html + service="wercker" + branch="$WERCKER_GIT_BRANCH" + build="$WERCKER_MAIN_PIPELINE_STARTED" + slug="$WERCKER_GIT_OWNER/$WERCKER_GIT_REPOSITORY" + commit="$WERCKER_GIT_COMMIT" + +elif [ "$CI" = "true" ] && [ "$MAGNUM" = "true" ]; +then + say "$e==>$x Magnum CI detected." + # https://magnum-ci.com/docs/environment + service="magnum" + branch="$CI_BRANCH" + build="$CI_BUILD_NUMBER" + commit="$CI_COMMIT" + +elif [ "$SHIPPABLE" = "true" ]; +then + say "$e==>$x Shippable CI detected." + # http://docs.shippable.com/ci_configure/ + service="shippable" + branch=$([ "$HEAD_BRANCH" != "" ] && echo "$HEAD_BRANCH" || echo "$BRANCH") + build="$BUILD_NUMBER" + build_url=$(urlencode "$BUILD_URL") + pr="$PULL_REQUEST" + slug="$REPO_FULL_NAME" + commit="$COMMIT" + +elif [ "$TDDIUM" = "true" ]; +then + say "Solano CI detected." + # http://docs.solanolabs.com/Setup/tddium-set-environment-variables/ + service="solano" + commit="$TDDIUM_CURRENT_COMMIT" + branch="$TDDIUM_CURRENT_BRANCH" + build="$TDDIUM_TID" + pr="$TDDIUM_PR_ID" + +elif [ "$GREENHOUSE" = "true" ]; +then + say "$e==>$x Greenhouse CI detected." + # http://docs.greenhouseci.com/docs/environment-variables-files + service="greenhouse" + branch="$GREENHOUSE_BRANCH" + build="$GREENHOUSE_BUILD_NUMBER" + build_url=$(urlencode "$GREENHOUSE_BUILD_URL") + pr="$GREENHOUSE_PULL_REQUEST" + commit="$GREENHOUSE_COMMIT" + search_in="$search_in $GREENHOUSE_EXPORT_DIR" + +elif [ "$GITLAB_CI" != "" ]; +then + say "$e==>$x GitLab CI detected." + # http://doc.gitlab.com/ce/ci/variables/README.html + service="gitlab" + branch="${CI_BUILD_REF_NAME:-$CI_COMMIT_REF_NAME}" + build="${CI_BUILD_ID:-$CI_JOB_ID}" + remote_addr="${CI_BUILD_REPO:-$CI_REPOSITORY_URL}" + commit="${CI_BUILD_REF:-$CI_COMMIT_SHA}" + slug="${CI_PROJECT_PATH}" + +elif [ "$GITHUB_ACTION" != "" ]; +then + say "$e==>$x GitHub Actions detected." + + # https://github.com/features/actions + service="github-actions" + + # https://help.github.com/en/articles/virtual-environments-for-github-actions#environment-variables + branch="${GITHUB_REF#refs/heads/}" + commit="${GITHUB_SHA}" + slug="${GITHUB_REPOSITORY}" + +elif [ "$SYSTEM_TEAMFOUNDATIONSERVERURI" != "" ]; +then + say "$e==>$x Azure Pipelines detected." + # https://docs.microsoft.com/en-us/azure/devops/pipelines/build/variables?view=vsts + service="azure_pipelines" + commit="$BUILD_SOURCEVERSION" + build="$BUILD_BUILDNUMBER" + if [ -z "$PULL_REQUEST_NUMBER" ]; + then + pr="$PULL_REQUEST_ID" + else + pr="$PULL_REQUEST_NUMBER" + fi + project="${SYSTEM_TEAMPROJECT}" + server_uri="${SYSTEM_TEAMFOUNDATIONSERVERURI}" + job="${BUILD_BUILDID}" + branch="$BUILD_SOURCEBRANCHNAME" + build_url=$(urlencode "${SYSTEM_TEAMFOUNDATIONSERVERURI}${SYSTEM_TEAMPROJECT}/_build/results?buildId=${BUILD_BUILDID}") +elif [ "$CI" = "true" ] && [ "$BITBUCKET_BUILD_NUMBER" != "" ]; +then + say "$e==>$x Bitbucket detected." + # https://confluence.atlassian.com/bitbucket/variables-in-pipelines-794502608.html + service="bitbucket" + branch="$BITBUCKET_BRANCH" + build="$BITBUCKET_BUILD_NUMBER" + slug="$BITBUCKET_REPO_OWNER/$BITBUCKET_REPO_SLUG" + job="$BITBUCKET_BUILD_NUMBER" + pr="$BITBUCKET_PR_ID" + commit="$BITBUCKET_COMMIT" + # See https://jira.atlassian.com/browse/BCLOUD-19393 + if [ "${#commit}" = 12 ]; + then + commit=$(git rev-parse "$BITBUCKET_COMMIT") + fi +elif [ "$CIRRUS_CI" != "" ]; +then + say "$e==>$x Cirrus CI detected." + # https://cirrus-ci.org/guide/writing-tasks/#environment-variables + service="cirrus-ci" + slug="$CIRRUS_REPO_FULL_NAME" + branch="$CIRRUS_BRANCH" + pr="$CIRRUS_PR" + commit="$CIRRUS_CHANGE_IN_REPO" + build="$CIRRUS_TASK_ID" + job="$CIRRUS_TASK_NAME" +else + say "${r}x>${x} No CI provider detected." + say " Testing inside Docker? ${b}http://docs.codecov.io/docs/testing-with-docker${x}" + say " Testing with Tox? ${b}https://docs.codecov.io/docs/python#section-testing-with-tox${x}" + +fi + +say " ${e}project root:${x} $git_root" + +# find branch, commit, repo from git command +if [ "$GIT_BRANCH" != "" ]; +then + branch="$GIT_BRANCH" + +elif [ "$branch" = "" ]; +then + branch=$(git rev-parse --abbrev-ref HEAD 2>/dev/null || hg branch 2>/dev/null || echo "") + if [ "$branch" = "HEAD" ]; + then + branch="" + fi +fi + +if [ "$commit_o" = "" ]; +then + # merge commit -> actual commit + mc= + if [ -n "$pr" ] && [ "$pr" != false ]; + then + mc=$(git show --no-patch --format="%P" 2>/dev/null || echo "") + fi + if [[ "$mc" =~ ^[a-z0-9]{40}[[:space:]][a-z0-9]{40}$ ]]; + then + say " Fixing merge commit SHA" + commit=$(echo "$mc" | cut -d' ' -f2) + elif [ "$GIT_COMMIT" != "" ]; + then + commit="$GIT_COMMIT" + elif [ "$commit" = "" ]; + then + commit=$(git log -1 --format="%H" 2>/dev/null || hg id -i --debug 2>/dev/null | tr -d '+' || echo "") + fi +else + commit="$commit_o" +fi + +if [ "$CODECOV_TOKEN" != "" ] && [ "$token" = "" ]; +then + say "${e}-->${x} token set from env" + token="$CODECOV_TOKEN" +fi + +if [ "$CODECOV_URL" != "" ] && [ "$url_o" = "" ]; +then + say "${e}-->${x} url set from env" + url_o=$(echo "$CODECOV_URL" | sed -e 's/\/$//') +fi + +if [ "$CODECOV_SLUG" != "" ]; +then + say "${e}-->${x} slug set from env" + slug_o="$CODECOV_SLUG" + +elif [ "$slug" = "" ]; +then + if [ "$remote_addr" = "" ]; + then + remote_addr=$(git config --get remote.origin.url || hg paths default || echo '') + fi + if [ "$remote_addr" != "" ]; + then + if echo "$remote_addr" | grep -q "//"; then + # https + slug=$(echo "$remote_addr" | cut -d / -f 4,5 | sed -e 's/\.git$//') + else + # ssh + slug=$(echo "$remote_addr" | cut -d : -f 2 | sed -e 's/\.git$//') + fi + fi + if [ "$slug" = "/" ]; + then + slug="" + fi +fi + +yaml=$(test -n "$codecov_yml" && echo "$codecov_yml" \ + || cd "$git_root" && \ + git ls-files "*codecov.yml" "*codecov.yaml" 2>/dev/null \ + || hg locate "*codecov.yml" "*codecov.yaml" 2>/dev/null \ + || cd $proj_root && find . -type f -name '*codecov.y*ml' -depth 1 2>/dev/null \ + || echo '') +yaml=$(echo "$yaml" | head -1) + +if [ "$yaml" != "" ]; +then + say " ${e}Yaml found at:${x} $yaml" + config=$(parse_yaml "$git_root/$yaml" || echo '') + + # TODO validate the yaml here + + if [ "$(echo "$config" | grep 'codecov_token="')" != "" ] && [ "$token" = "" ]; + then + say "${e}-->${x} token set from yaml" + token="$(echo "$config" | grep 'codecov_token="' | sed -e 's/codecov_token="//' | sed -e 's/"\.*//')" + fi + + if [ "$(echo "$config" | grep 'codecov_url="')" != "" ] && [ "$url_o" = "" ]; + then + say "${e}-->${x} url set from yaml" + url_o="$(echo "$config" | grep 'codecov_url="' | sed -e 's/codecov_url="//' | sed -e 's/"\.*//')" + fi + + if [ "$(echo "$config" | grep 'codecov_slug="')" != "" ] && [ "$slug_o" = "" ]; + then + say "${e}-->${x} slug set from yaml" + slug_o="$(echo "$config" | grep 'codecov_slug="' | sed -e 's/codecov_slug="//' | sed -e 's/"\.*//')" + fi +else + say " ${g}Yaml not found, that's ok! Learn more at${x} ${b}http://docs.codecov.io/docs/codecov-yaml${x}" + +fi + +if [ "$branch_o" != "" ]; +then + branch=$(urlencode "$branch_o") +else + branch=$(urlencode "$branch") +fi + +query="branch=$branch\ + &commit=$commit\ + &build=$([ "$build_o" = "" ] && echo "$build" || echo "$build_o")\ + &build_url=$build_url\ + &name=$(urlencode "$name")\ + &tag=$([ "$tag_o" = "" ] && echo "$tag" || echo "$tag_o")\ + &slug=$([ "$slug_o" = "" ] && urlencode "$slug" || urlencode "$slug_o")\ + &service=$service\ + &flags=$flags\ + &pr=$([ "$pr_o" = "" ] && echo "${pr##\#}" || echo "${pr_o##\#}")\ + &job=$job" + +if [ ! -z "$project" ] && [ ! -z "$server_uri" ]; +then + query=$(echo "$query&project=$project&server_uri=$server_uri" | tr -d ' ') +fi + +if [ "$parent" != "" ]; +then + query=$(echo "parent=$parent&$query" | tr -d ' ') +fi + +if [ "$ft_search" = "1" ]; +then + # detect bower comoponents location + bower_components="bower_components" + bower_rc=$(cd "$git_root" && cat .bowerrc 2>/dev/null || echo "") + if [ "$bower_rc" != "" ]; + then + bower_components=$(echo "$bower_rc" | tr -d '\n' | grep '"directory"' | cut -d'"' -f4 | sed -e 's/\/$//') + if [ "$bower_components" = "" ]; + then + bower_components="bower_components" + fi + fi + + # Swift Coverage + if [ "$ft_xcodellvm" = "1" ] && [ -d "$ddp" ]; + then + say "${e}==>${x} Processing Xcode reports via llvm-cov" + say " DerivedData folder: $ddp" + profdata_files=$(find "$ddp" -name '*.profdata' 2>/dev/null || echo '') + if [ "$profdata_files" != "" ]; + then + # xcode via profdata + if [ "$xp" = "" ]; + then + # xp=$(xcodebuild -showBuildSettings 2>/dev/null | grep -i "^\s*PRODUCT_NAME" | sed -e 's/.*= \(.*\)/\1/') + # say " ${e}->${x} Speed up Xcode processing by adding ${e}-J '$xp'${x}" + say " ${g}hint${x} Speed up Swift processing by using use ${g}-J 'AppName'${x} (regexp accepted)" + say " ${g}hint${x} This will remove Pods/ from your report. Also ${b}https://docs.codecov.io/docs/ignoring-paths${x}" + fi + while read -r profdata; + do + if [ "$profdata" != "" ]; + then + swiftcov "$profdata" "$xp" + fi + done <<< "$profdata_files" + else + say " ${e}->${x} No Swift coverage found" + fi + + # Obj-C Gcov Coverage + if [ "$ft_gcov" = "1" ]; + then + say " ${e}->${x} Running $gcov_exe for Obj-C" + if [ "$ft_gcovout" = "1" ]; + then + # suppress gcov output + bash -c "find $ddp -type f -name '*.gcda' $gcov_include $gcov_ignore -exec $gcov_exe -p $gcov_arg {} +" || true 2>/dev/null + else + bash -c "find $ddp -type f -name '*.gcda' $gcov_include $gcov_ignore -exec $gcov_exe -p $gcov_arg {} +" || true + fi + fi + fi + + if [ "$ft_xcodeplist" = "1" ] && [ -d "$ddp" ]; + then + say "${e}==>${x} Processing Xcode plists" + plists_files=$(find "$ddp" -name '*.xccoverage' 2>/dev/null || echo '') + if [ "$plists_files" != "" ]; + then + while read -r plist; + do + if [ "$plist" != "" ]; + then + say " ${g}Found${x} plist file at $plist" + plutil -convert xml1 -o "$(basename "$plist").plist" -- $plist + fi + done <<< "$plists_files" + fi + fi + + # Gcov Coverage + if [ "$ft_gcov" = "1" ]; + then + say "${e}==>${x} Running gcov in $proj_root ${e}(disable via -X gcov)${x}" + bash -c "find $proj_root -type f -name '*.gcno' $gcov_include $gcov_ignore -execdir $gcov_exe -pb $gcov_arg {} +" || true + else + say "${e}==>${x} gcov disabled" + fi + + # Python Coverage + if [ "$ft_coveragepy" = "1" ]; + then + if [ ! -f coverage.xml ]; + then + if which coverage >/dev/null 2>&1; + then + say "${e}==>${x} Python coveragepy exists ${e}disable via -X coveragepy${x}" + + dotcoverage=$(find "$git_root" -name '.coverage' -or -name '.coverage.*' | head -1 || echo '') + if [ "$dotcoverage" != "" ]; + then + cd "$(dirname "$dotcoverage")" + if [ ! -f .coverage ]; + then + say " ${e}->${x} Running coverage combine" + coverage combine -a + fi + say " ${e}->${x} Running coverage xml" + if [ "$(coverage xml -i)" != "No data to report." ]; + then + files="$files +$PWD/coverage.xml" + else + say " ${r}No data to report.${x}" + fi + cd "$proj_root" + else + say " ${r}No .coverage file found.${x}" + fi + else + say "${e}==>${x} Python coveragepy not found" + fi + fi + else + say "${e}==>${x} Python coveragepy disabled" + fi + + if [ "$search_in_o" != "" ]; + then + # location override + search_in="$search_in_o" + fi + + say "$e==>$x Searching for coverage reports in:" + for _path in $search_in + do + say " ${g}+${x} $_path" + done + + patterns="find $search_in \( \ + -name vendor \ + -or -name htmlcov \ + -or -name virtualenv \ + -or -name js/generated/coverage \ + -or -name .virtualenv \ + -or -name virtualenvs \ + -or -name .virtualenvs \ + -or -name .env \ + -or -name .envs \ + -or -name env \ + -or -name .yarn-cache \ + -or -name envs \ + -or -name .venv \ + -or -name .venvs \ + -or -name venv \ + -or -name venvs \ + -or -name .git \ + -or -name .hg \ + -or -name .tox \ + -or -name __pycache__ \ + -or -name '.egg-info*' \ + -or -name '$bower_components' \ + -or -name node_modules \ + -or -name 'conftest_*.c.gcov' \ + \) -prune -or \ + -type f \( -name '*coverage*.*' \ + -or -name 'nosetests.xml' \ + -or -name 'jacoco*.xml' \ + -or -name 'clover.xml' \ + -or -name 'report.xml' \ + -or -name '*.codecov.*' \ + -or -name 'codecov.*' \ + -or -name 'cobertura.xml' \ + -or -name 'excoveralls.json' \ + -or -name 'luacov.report.out' \ + -or -name 'coverage-final.json' \ + -or -name 'naxsi.info' \ + -or -name 'lcov.info' \ + -or -name 'lcov.dat' \ + -or -name '*.lcov' \ + -or -name '*.clover' \ + -or -name 'cover.out' \ + -or -name 'gcov.info' \ + -or -name '*.gcov' \ + -or -name '*.lst' \ + $include_cov \) \ + $exclude_cov \ + -not -name '*.profdata' \ + -not -name 'coverage-summary.json' \ + -not -name 'phpunit-code-coverage.xml' \ + -not -name '*/classycle/report.xml' \ + -not -name 'remapInstanbul.coverage*.json' \ + -not -name 'phpunit-coverage.xml' \ + -not -name '*codecov.yml' \ + -not -name '*.serialized' \ + -not -name '.coverage*' \ + -not -name '.*coveragerc' \ + -not -name '*.sh' \ + -not -name '*.bat' \ + -not -name '*.ps1' \ + -not -name '*.env' \ + -not -name '*.cmake' \ + -not -name '*.dox' \ + -not -name '*.ec' \ + -not -name '*.rst' \ + -not -name '*.h' \ + -not -name '*.scss' \ + -not -name '*.o' \ + -not -name '*.proto' \ + -not -name '*.sbt' \ + -not -name '*.xcoverage.*' \ + -not -name '*.gz' \ + -not -name '*.conf' \ + -not -name '*.p12' \ + -not -name '*.csv' \ + -not -name '*.rsp' \ + -not -name '*.m4' \ + -not -name '*.pem' \ + -not -name '*~' \ + -not -name '*.exe' \ + -not -name '*.am' \ + -not -name '*.template' \ + -not -name '*.cp' \ + -not -name '*.bw' \ + -not -name '*.crt' \ + -not -name '*.log' \ + -not -name '*.cmake' \ + -not -name '*.pth' \ + -not -name '*.in' \ + -not -name '*.jar*' \ + -not -name '*.pom*' \ + -not -name '*.png' \ + -not -name '*.jpg' \ + -not -name '*.sql' \ + -not -name '*.jpeg' \ + -not -name '*.svg' \ + -not -name '*.gif' \ + -not -name '*.csv' \ + -not -name '*.snapshot' \ + -not -name '*.mak*' \ + -not -name '*.bash' \ + -not -name '*.data' \ + -not -name '*.py' \ + -not -name '*.class' \ + -not -name '*.xcconfig' \ + -not -name '*.ec' \ + -not -name '*.coverage' \ + -not -name '*.pyc' \ + -not -name '*.cfg' \ + -not -name '*.egg' \ + -not -name '*.ru' \ + -not -name '*.css' \ + -not -name '*.less' \ + -not -name '*.pyo' \ + -not -name '*.whl' \ + -not -name '*.html' \ + -not -name '*.ftl' \ + -not -name '*.erb' \ + -not -name '*.rb' \ + -not -name '*.js' \ + -not -name '*.jade' \ + -not -name '*.db' \ + -not -name '*.md' \ + -not -name '*.cpp' \ + -not -name '*.gradle' \ + -not -name '*.tar.tz' \ + -not -name '*.scss' \ + -not -name 'include.lst' \ + -not -name 'fullLocaleNames.lst' \ + -not -name 'inputFiles.lst' \ + -not -name 'createdFiles.lst' \ + -not -name 'scoverage.measurements.*' \ + -not -name 'test_*_coverage.txt' \ + -not -name 'testrunner-coverage*' \ + -print 2>/dev/null" + files=$(eval "$patterns" || echo '') + +elif [ "$include_cov" != "" ]; +then + files=$(eval "find $search_in -type f \( ${include_cov:5} \)$exclude_cov 2>/dev/null" || echo '') +fi + +num_of_files=$(echo "$files" | wc -l | tr -d ' ') +if [ "$num_of_files" != '' ] && [ "$files" != '' ]; +then + say " ${e}->${x} Found $num_of_files reports" +fi + +# no files found +if [ "$files" = "" ]; +then + say "${r}-->${x} No coverage report found." + say " Please visit ${b}http://docs.codecov.io/docs/supported-languages${x}" + exit ${exit_with}; +fi + +if [ "$ft_network" == "1" ]; +then + say "${e}==>${x} Detecting git/mercurial file structure" + network=$(cd "$git_root" && git ls-files 2>/dev/null || hg locate 2>/dev/null || echo "") + if [ "$network" = "" ]; + then + network=$(find "$git_root" \( \ + -name virtualenv \ + -name .virtualenv \ + -name virtualenvs \ + -name .virtualenvs \ + -name '*.png' \ + -name '*.gif' \ + -name '*.jpg' \ + -name '*.jpeg' \ + -name '*.md' \ + -name .env \ + -name .envs \ + -name env \ + -name envs \ + -name .venv \ + -name .venvs \ + -name venv \ + -name venvs \ + -name .git \ + -name .egg-info \ + -name shunit2-2.1.6 \ + -name vendor \ + -name __pycache__ \ + -name node_modules \ + -path '*/$bower_components/*' \ + -path '*/target/delombok/*' \ + -path '*/build/lib/*' \ + -path '*/js/generated/coverage/*' \ + \) -prune -or \ + -type f -print 2>/dev/null || echo '') + fi + + if [ "$prefix_o" != "" ]; + then + network=$(echo "$network" | awk "{print \"$prefix_o/\"\$0}") + fi +fi + +upload_file=`mktemp /tmp/codecov.XXXXXX` +adjustments_file=`mktemp /tmp/codecov.adjustments.XXXXXX` + +cleanup() { + rm -f $upload_file $adjustments_file $upload_file.gz +} + +trap cleanup INT ABRT TERM + +if [ "$env" != "" ]; +then + inc_env="" + say "${e}==>${x} Appending build variables" + for varname in $(echo "$env" | tr ',' ' ') + do + if [ "$varname" != "" ]; + then + say " ${g}+${x} $varname" + inc_env="${inc_env}${varname}=$(eval echo "\$${varname}") +" + fi + done + +echo "$inc_env<<<<<< ENV" >> $upload_file +fi + +# Append git file list +# write discovered yaml location +echo "$yaml" >> $upload_file +if [ "$ft_network" == "1" ]; +then + i="woff|eot|otf" # fonts + i="$i|gif|png|jpg|jpeg|psd" # images + i="$i|ptt|pptx|numbers|pages|md|txt|xlsx|docx|doc|pdf|html|csv" # docs + i="$i|yml|yaml|.gitignore" # supporting docs + echo "$network" | grep -vwE "($i)$" >> $upload_file +fi +echo "<<<<<< network" >> $upload_file + +fr=0 +say "${e}==>${x} Reading reports" +while IFS='' read -r file; +do + # read the coverage file + if [ "$(echo "$file" | tr -d ' ')" != '' ]; + then + if [ -f "$file" ]; + then + report_len=$(wc -c < "$file") + if [ "$report_len" -ne 0 ]; + then + say " ${g}+${x} $file ${e}bytes=$(echo "$report_len" | tr -d ' ')${x}" + # append to to upload + _filename=$(basename "$file") + if [ "${_filename##*.}" = 'gcov' ]; + then + echo "# path=$(echo "$file.reduced" | sed "s|^$git_root/||")" >> $upload_file + # get file name + head -1 $file >> $upload_file + # 1. remove source code + # 2. remove ending bracket lines + # 3. remove whitespace + # 4. remove contextual lines + # 5. remove function names + awk -F': *' '{print $1":"$2":"}' $file \ + | sed '\/: *} *$/d' \ + | sed 's/^ *//' \ + | sed '/^-/d' \ + | sed 's/^function.*/func/' >> $upload_file + else + echo "# path=$(echo "$file" | sed "s|^$git_root/||")" >> $upload_file + cat "$file" >> $upload_file + fi + echo "<<<<<< EOF" >> $upload_file + fr=1 + if [ "$clean" = "1" ]; + then + rm "$file" + fi + else + say " ${r}-${x} Skipping empty file $file" + fi + else + say " ${r}-${x} file not found at $file" + fi + fi +done <<< "$(echo -e "$files")" + +if [ "$fr" = "0" ]; +then + say "${r}-->${x} No coverage data found." + say " Please visit ${b}http://docs.codecov.io/docs/supported-languages${x}" + say " search for your projects language to learn how to collect reports." + exit ${exit_with}; +fi + +if [ "$ft_fix" = "1" ]; +then + say "${e}==>${x} Appending adjustments" + say " ${b}http://docs.codecov.io/docs/fixing-reports${x}" + + empty_line='^[[:space:]]*$' + # // + syntax_comment='^[[:space:]]*//.*' + # /* or */ + syntax_comment_block='^[[:space:]]*(\/\*|\*\/)[[:space:]]*$' + # { or } + syntax_bracket='^[[:space:]]*[\{\}][[:space:]]*(//.*)?$' + # [ or ] + syntax_list='^[[:space:]]*[][][[:space:]]*(//.*)?$' + + skip_dirs="-not -path '*/$bower_components/*' \ + -not -path '*/node_modules/*'" + + cut_and_join() { + awk 'BEGIN { FS=":" } + $3 ~ /\/\*/ || $3 ~ /\*\// { print $0 ; next } + $1!=key { if (key!="") print out ; key=$1 ; out=$1":"$2 ; next } + { out=out","$2 } + END { print out }' 2>/dev/null + } + + if echo "$network" | grep -m1 '.kt$' 1>/dev/null; + then + # skip brackets and comments + find "$git_root" -type f \ + -name '*.kt' \ + -exec \ + grep -nIHE -e $syntax_bracket \ + -e $syntax_comment_block {} \; \ + | cut_and_join \ + >> $adjustments_file \ + || echo '' + + # last line in file + find "$git_root" -type f \ + -name '*.kt' -exec \ + wc -l {} \; \ + | while read l; do echo "EOF: $l"; done \ + 2>/dev/null \ + >> $adjustments_file \ + || echo '' + + fi + + if echo "$network" | grep -m1 '.go$' 1>/dev/null; + then + # skip empty lines, comments, and brackets + find "$git_root" -not -path '*/vendor/*' \ + -type f \ + -name '*.go' \ + -exec \ + grep -nIHE \ + -e $empty_line \ + -e $syntax_comment \ + -e $syntax_comment_block \ + -e $syntax_bracket \ + {} \; \ + | cut_and_join \ + >> $adjustments_file \ + || echo '' + fi + + if echo "$network" | grep -m1 '.dart$' 1>/dev/null; + then + # skip brackets + find "$git_root" -type f \ + -name '*.dart' \ + -exec \ + grep -nIHE \ + -e $syntax_bracket \ + {} \; \ + | cut_and_join \ + >> $adjustments_file \ + || echo '' + fi + + if echo "$network" | grep -m1 '.php$' 1>/dev/null; + then + # skip empty lines, comments, and brackets + find "$git_root" -not -path "*/vendor/*" \ + -type f \ + -name '*.php' \ + -exec \ + grep -nIHE \ + -e $syntax_list \ + -e $syntax_bracket \ + -e '^[[:space:]]*\);[[:space:]]*(//.*)?$' \ + {} \; \ + | cut_and_join \ + >> $adjustments_file \ + || echo '' + fi + + if echo "$network" | grep -m1 '\(.cpp\|.h\|.cxx\|.c\|.hpp\|.m\)$' 1>/dev/null; + then + # skip brackets + find "$git_root" -type f \ + $skip_dirs \ + \( \ + -name '*.h' \ + -or -name '*.cpp' \ + -or -name '*.cxx' \ + -or -name '*.m' \ + -or -name '*.c' \ + -or -name '*.hpp' \ + \) -exec \ + grep -nIHE \ + -e $empty_line \ + -e $syntax_bracket \ + -e '// LCOV_EXCL' \ + {} \; \ + | cut_and_join \ + >> $adjustments_file \ + || echo '' + + # skip brackets + find "$git_root" -type f \ + $skip_dirs \ + \( \ + -name '*.h' \ + -or -name '*.cpp' \ + -or -name '*.cxx' \ + -or -name '*.m' \ + -or -name '*.c' \ + -or -name '*.hpp' \ + \) -exec \ + grep -nIH '// LCOV_EXCL' \ + {} \; \ + >> $adjustments_file \ + || echo '' + + fi + + found=$(cat $adjustments_file | tr -d ' ') + + if [ "$found" != "" ]; + then + say " ${g}+${x} Found adjustments" + echo "# path=fixes" >> $upload_file + cat $adjustments_file >> $upload_file + echo "<<<<<< EOF" >> $upload_file + rm -rf $adjustments_file + else + say " ${e}->${x} No adjustments found" + fi +fi + +if [ "$url_o" != "" ]; +then + url="$url_o" +fi + +if [ "$dump" != "0" ]; +then + # trim whitespace from query + say " ${e}->${x} Dumping upload file (no upload)" + echo "$url/upload/v4?$(echo "package=bash-$VERSION&token=$token&$query" | tr -d ' ')" + cat $upload_file +else + + say "${e}==>${x} Gzipping contents" + gzip -nf9 $upload_file + + query=$(echo "${query}" | tr -d ' ') + say "${e}==>${x} Uploading reports" + say " ${e}url:${x} $url" + say " ${e}query:${x} $query" + + # Full query without token (to display on terminal output) + queryNoToken=$(echo "package=bash-$VERSION&token=secret&$query" | tr -d ' ') + # now add token to query + query=$(echo "package=bash-$VERSION&token=$token&$query" | tr -d ' ') + + if [ "$ft_s3" = "1" ]; + then + i="0" + while [ $i -lt 4 ] + do + i=$[$i+1] + say " ${e}->${x} Pinging Codecov" + say "$url/upload/v4?$queryNoToken" + res=$(curl $curl_s -X POST $curlargs $cacert \ + -H 'X-Reduced-Redundancy: false' \ + -H 'X-Content-Type: application/x-gzip' \ + "$url/upload/v4?$query" || true) + # a good replay is "https://codecov.io" + "\n" + "https://codecov.s3.amazonaws.com/..." + status=$(echo "$res" | head -1 | grep 'HTTP ' | cut -d' ' -f2) + if [ "$status" = "" ]; + then + s3target=$(echo "$res" | sed -n 2p) + say " ${e}->${x} Uploading" + + + s3=$(curl $curl_s -fiX PUT $curlawsargs \ + --data-binary @$upload_file.gz \ + -H 'Content-Type: application/x-gzip' \ + -H 'Content-Encoding: gzip' \ + -H 'x-amz-acl: public-read' \ + "$s3target" || true) + + + if [ "$s3" != "" ]; + then + say " ${g}->${x} View reports at ${b}$(echo "$res" | sed -n 1p)${x}" + exit 0 + else + say " ${r}X>${x} Failed to upload" + fi + elif [ "$status" = "400" ]; + then + # 400 Error + say "${g}${res}${x}" + exit ${exit_with} + fi + say " ${e}->${x} Sleeping for 30s and trying again..." + sleep 30 + done + fi + + say " ${e}->${x} Uploading to Codecov" + i="0" + while [ $i -lt 4 ] + do + i=$[$i+1] + + res=$(curl $curl_s -X POST $curlargs $cacert \ + --data-binary @$upload_file.gz \ + -H 'Content-Type: text/plain' \ + -H 'Content-Encoding: gzip' \ + -H 'X-Content-Encoding: gzip' \ + -H 'Accept: text/plain' \ + "$url/upload/v2?$query" || echo 'HTTP 500') + # HTTP 200 + # http://.... + status=$(echo "$res" | head -1 | cut -d' ' -f2) + if [ "$status" = "" ]; + then + say " View reports at ${b}$(echo "$res" | head -2 | tail -1)${x}" + exit 0 + + elif [ "${status:0:1}" = "5" ]; + then + say " ${e}->${x} Sleeping for 30s and trying again..." + sleep 30 + + else + say " ${g}${res}${x}" + exit 0 + exit ${exit_with} + fi + + done + + say " ${r}X> Failed to upload coverage reports${x}" +fi + +exit ${exit_with} From 0e1489001fd44c2e010d9aed9c4a78bf67acf49f Mon Sep 17 00:00:00 2001 From: Keith Manning Date: Fri, 24 Jan 2020 16:02:53 -0500 Subject: [PATCH 12/33] Add codenarc, no rules enforced --- build.gradle | 1 + config/codenarc/codenarc.xml | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+) create mode 100644 config/codenarc/codenarc.xml diff --git a/build.gradle b/build.gradle index c00331eb..9ab48bfe 100644 --- a/build.gradle +++ b/build.gradle @@ -1,5 +1,6 @@ apply plugin: 'groovy' apply plugin: 'jacoco' +apply plugin: 'codenarc' repositories { mavenCentral() diff --git a/config/codenarc/codenarc.xml b/config/codenarc/codenarc.xml new file mode 100644 index 00000000..b6387056 --- /dev/null +++ b/config/codenarc/codenarc.xml @@ -0,0 +1,19 @@ + + + From f83c0e36a12af1189170f81f7f88382cd3c9360d Mon Sep 17 00:00:00 2001 From: Keith Manning Date: Fri, 24 Jan 2020 16:58:49 -0500 Subject: [PATCH 13/33] Apply groovyism rules and make corrections --- config/codenarc/codenarc.xml | 37 ++++++++++++++++++++++++++++ src/Jenkinsfile.groovy | 2 +- src/RegressionStage.groovy | 2 +- src/TerraformEnvironmentStage.groovy | 2 +- src/TerraformPlugin.groovy | 2 +- src/TerraformValidateStage.groovy | 2 +- test/BuildStageTest.groovy | 2 +- test/SemanticVersionTest.groovy | 10 +++++--- 8 files changed, 50 insertions(+), 9 deletions(-) diff --git a/config/codenarc/codenarc.xml b/config/codenarc/codenarc.xml index b6387056..0557378d 100644 --- a/config/codenarc/codenarc.xml +++ b/config/codenarc/codenarc.xml @@ -16,4 +16,41 @@ + + + + + + + + + + + diff --git a/src/Jenkinsfile.groovy b/src/Jenkinsfile.groovy index 152299d8..8f24e18a 100644 --- a/src/Jenkinsfile.groovy +++ b/src/Jenkinsfile.groovy @@ -44,7 +44,7 @@ class Jenkinsfile { def Map parseScmUrl(String scmUrl) { def matcher = scmUrl =~ /.*(?:\/\/|\@)[^\/:]+[\/:]([^\/]+)\/([^\/.]+)(.git)?/ - def Map results = new HashMap() + def Map results = [:] results.put("organization", matcher[0][1]) results.put("repo", matcher[0][2]) return results diff --git a/src/RegressionStage.groovy b/src/RegressionStage.groovy index 075e1602..f59d16ec 100644 --- a/src/RegressionStage.groovy +++ b/src/RegressionStage.groovy @@ -1,7 +1,7 @@ class RegressionStage implements Stage { public String testCommand - public List automationRepoList = new ArrayList() + public List automationRepoList = [] private String testCommandDirectory private Closure existingDecorations diff --git a/src/TerraformEnvironmentStage.groovy b/src/TerraformEnvironmentStage.groovy index fe83ec42..00c4a4a5 100644 --- a/src/TerraformEnvironmentStage.groovy +++ b/src/TerraformEnvironmentStage.groovy @@ -18,7 +18,7 @@ class TerraformEnvironmentStage implements Stage { TerraformEnvironmentStage(String environment) { this.environment = environment this.jenkinsfile = Jenkinsfile.instance - this.decorations = new HashMap() + this.decorations = [:] } public Stage then(Stage nextStages) { diff --git a/src/TerraformPlugin.groovy b/src/TerraformPlugin.groovy index 009e9b30..ee616f77 100644 --- a/src/TerraformPlugin.groovy +++ b/src/TerraformPlugin.groovy @@ -35,7 +35,7 @@ class TerraformPlugin implements TerraformValidateCommandPlugin, TerraformValida } public TerraformPluginVersion strategyFor(String version) { - if(new SemanticVersion(version).compareTo(new SemanticVersion('0.12.0')) >= 0) { + if(new SemanticVersion(version) >= new SemanticVersion('0.12.0')) { return new TerraformPluginVersion12() } diff --git a/src/TerraformValidateStage.groovy b/src/TerraformValidateStage.groovy index c941436f..552568d0 100644 --- a/src/TerraformValidateStage.groovy +++ b/src/TerraformValidateStage.groovy @@ -9,7 +9,7 @@ class TerraformValidateStage implements Stage { public TerraformValidateStage() { this.jenkinsfile = Jenkinsfile.instance - this.decorations = new HashMap() + this.decorations = [:] } public Stage then(Stage nextStage) { diff --git a/test/BuildStageTest.groovy b/test/BuildStageTest.groovy index 312a0993..5c458bab 100644 --- a/test/BuildStageTest.groovy +++ b/test/BuildStageTest.groovy @@ -15,7 +15,7 @@ class BuildStageTest { void buildsWithoutError() { BuildStage stage = spy(new BuildStage()) - doReturn({ -> }).when(stage).pipelineConfiguration() + doReturn { -> }.when(stage).pipelineConfiguration() stage.build() } diff --git a/test/SemanticVersionTest.groovy b/test/SemanticVersionTest.groovy index e0491c5c..305d7715 100644 --- a/test/SemanticVersionTest.groovy +++ b/test/SemanticVersionTest.groovy @@ -16,9 +16,13 @@ class SemanticVersionTest { @Test void sortsCorrectly() { - List unsorted = SORTED.clone().collect({v -> new SemanticVersion(v)}) - Collections.shuffle(unsorted) - def result = unsorted.sort().collect({sv -> sv.version}) + List versions = SORTED.clone().collect {v -> new SemanticVersion(v)} + // Randomize + Collections.shuffle(versions) + // Then sort + versions.sort() + + def result = versions.collect {sv -> sv.version} assertEquals(SORTED,result) } From 2016867f6a0b9158a951b406038e40a6bccc9aca Mon Sep 17 00:00:00 2001 From: Keith Manning Date: Fri, 24 Jan 2020 17:08:38 -0500 Subject: [PATCH 14/33] Apply basic rules and make corrections --- config/codenarc/codenarc.xml | 4 +++- test/TerraformEnvironmentStageTest.groovy | 10 ++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/config/codenarc/codenarc.xml b/config/codenarc/codenarc.xml index 0557378d..0f98de1d 100644 --- a/config/codenarc/codenarc.xml +++ b/config/codenarc/codenarc.xml @@ -20,7 +20,9 @@ - + + + diff --git a/test/TerraformEnvironmentStageTest.groovy b/test/TerraformEnvironmentStageTest.groovy index 1167c612..839e5dfb 100644 --- a/test/TerraformEnvironmentStageTest.groovy +++ b/test/TerraformEnvironmentStageTest.groovy @@ -60,6 +60,16 @@ class TerraformEnvironmentStageTest { @Test void doesNotAddPluginToOtherInstances() { + def modifiedStage = new TerraformEnvironmentStage('modified') + def unmodifiedStage = new TerraformEnvironmentStage('unmodified') + + def pluginsBefore = unmodifiedStage.getAllPlugins() + + modifiedStage.withEnv('somekey', 'somevalue') + + def pluginsAfter = unmodifiedStage.getAllPlugins() + + assertEquals(pluginsBefore, pluginsAfter) } From 68970874671128c5ab56a58df1353b8359651377 Mon Sep 17 00:00:00 2001 From: Keith Manning Date: Fri, 24 Jan 2020 17:08:56 -0500 Subject: [PATCH 15/33] Apply rules for braces --- config/codenarc/codenarc.xml | 6 ------ 1 file changed, 6 deletions(-) diff --git a/config/codenarc/codenarc.xml b/config/codenarc/codenarc.xml index 0f98de1d..e33c1364 100644 --- a/config/codenarc/codenarc.xml +++ b/config/codenarc/codenarc.xml @@ -23,13 +23,7 @@ - - - - - From f4f81dff74422a84215ff70eb67575dbac8ffaf3 Mon Sep 17 00:00:00 2001 From: Keith Manning Date: Thu, 30 Jan 2020 12:23:54 -0500 Subject: [PATCH 20/33] Revert "Merge pull request #184 from kmanning/issue_87_part_3" This reverts commit ae69fff4d8f648745661c4c346893cf9760c997b, reversing changes made to abdd770f0c6582f59cdf481b957aca0afb1ab6c1. --- build.gradle | 1 - config/codenarc/codenarc.xml | 33 --------------- src/AgentNodePlugin.groovy | 4 +- src/FileParametersPlugin.groovy | 4 +- src/Jenkinsfile.groovy | 3 +- src/RegressionStage.groovy | 2 +- src/S3BackendPlugin.groovy | 7 ++-- src/SemanticVersion.groovy | 17 ++++---- src/TerraformApplyCommand.groovy | 2 +- src/TerraformEnvironmentStage.groovy | 6 +-- src/TerraformInitCommand.groovy | 4 +- src/TerraformPlanCommand.groovy | 2 +- src/TerraformPlugin.groovy | 4 +- src/TerraformValidateCommand.groovy | 5 ++- src/TerraformValidateStage.groovy | 6 +-- src/TfvarsFilesPlugin.groovy | 4 +- test/AnsiColorPluginTest.groovy | 8 ++-- test/AwssumePluginTest.groovy | 16 +++---- test/BuildGraphTest.groovy | 9 ++-- test/BuildStageTest.groovy | 12 +++--- test/ConditionalApplyPluginTest.groovy | 16 ++++--- test/ConfirmApplyPluginTest.groovy | 12 ++---- test/ConsulBackendPluginTest.groovy | 15 +++---- test/CredentialsPluginTest.groovy | 14 ++----- test/CrqPluginTest.groovy | 20 ++++----- test/DefaultEnvironmentPluginTest.groovy | 7 ++-- test/FileParametersPluginTest.groovy | 14 +++---- test/JenkinsfileTest.groovy | 14 ++++--- ...arameterStoreBuildWrapperPluginTest.groovy | 13 +++--- test/ParameterStoreExecPluginTest.groovy | 14 +++---- test/RegressionStageTest.groovy | 17 ++++---- test/S3BackendPluginTest.groovy | 16 +++---- test/SemanticVersionTest.groovy | 42 +++++++++---------- test/TerraformApplyCommandTest.groovy | 20 ++++----- test/TerraformDirectoryPluginTest.groovy | 11 +++-- test/TerraformEnvironmentStageTest.groovy | 27 ++++-------- test/TerraformInitCommandTest.groovy | 15 ++++--- test/TerraformPlanCommandTest.groovy | 17 ++++---- test/TerraformPluginTest.groovy | 18 ++++---- test/TerraformPluginVersion11Test.groovy | 13 ++++-- test/TerraformPluginVersion12Test.groovy | 17 ++++---- test/TerraformValidateCommandTest.groovy | 14 +++---- test/TfvarsFilesPluginTest.groovy | 12 +++--- test/WithAwsPluginTest.groovy | 15 +++---- 44 files changed, 230 insertions(+), 312 deletions(-) delete mode 100644 config/codenarc/codenarc.xml diff --git a/build.gradle b/build.gradle index 9ab48bfe..c00331eb 100644 --- a/build.gradle +++ b/build.gradle @@ -1,6 +1,5 @@ apply plugin: 'groovy' apply plugin: 'jacoco' -apply plugin: 'codenarc' repositories { mavenCentral() diff --git a/config/codenarc/codenarc.xml b/config/codenarc/codenarc.xml deleted file mode 100644 index 6bb07059..00000000 --- a/config/codenarc/codenarc.xml +++ /dev/null @@ -1,33 +0,0 @@ - - - - - - - - - - - - - - - - - diff --git a/src/AgentNodePlugin.groovy b/src/AgentNodePlugin.groovy index 5fb3b1c4..4697bb15 100644 --- a/src/AgentNodePlugin.groovy +++ b/src/AgentNodePlugin.groovy @@ -5,7 +5,7 @@ public class AgentNodePlugin implements TerraformValidateStagePlugin, TerraformE private static String dockerImage private static String dockerOptions - AgentNodePlugin() { } + AgentNodePlugin() {} public static void init() { def plugin = new AgentNodePlugin() @@ -37,7 +37,7 @@ public class AgentNodePlugin implements TerraformValidateStagePlugin, TerraformE public Closure addAgent() { return { closure -> if (dockerImage) { - docker.image(this.dockerImage).inside(this.dockerOptions) { + docker.image(this.dockerImage).inside(this.dockerOptions){ closure() } } else { diff --git a/src/FileParametersPlugin.groovy b/src/FileParametersPlugin.groovy index e14fb316..f9641637 100644 --- a/src/FileParametersPlugin.groovy +++ b/src/FileParametersPlugin.groovy @@ -1,7 +1,7 @@ -import static TerraformEnvironmentStage.ALL - import groovy.text.StreamingTemplateEngine +import static TerraformEnvironmentStage.ALL + class FileParametersPlugin implements TerraformEnvironmentStagePlugin { public static void init() { FileParametersPlugin plugin = new FileParametersPlugin() diff --git a/src/Jenkinsfile.groovy b/src/Jenkinsfile.groovy index 67534773..152299d8 100644 --- a/src/Jenkinsfile.groovy +++ b/src/Jenkinsfile.groovy @@ -44,7 +44,7 @@ class Jenkinsfile { def Map parseScmUrl(String scmUrl) { def matcher = scmUrl =~ /.*(?:\/\/|\@)[^\/:]+[\/:]([^\/]+)\/([^\/.]+)(.git)?/ - def Map results = [:] + def Map results = new HashMap() results.put("organization", matcher[0][1]) results.put("repo", matcher[0][2]) return results @@ -122,6 +122,7 @@ class Jenkinsfile { } throw new RuntimeException("Your pipeline has ${stages.size()} stages - the maximum supported by default is 7. Define a custom pipeline template and assign it to Jenkinsfile.pipelineTemplate to create your pipeline.") + } public getEnv() { diff --git a/src/RegressionStage.groovy b/src/RegressionStage.groovy index f59d16ec..075e1602 100644 --- a/src/RegressionStage.groovy +++ b/src/RegressionStage.groovy @@ -1,7 +1,7 @@ class RegressionStage implements Stage { public String testCommand - public List automationRepoList = [] + public List automationRepoList = new ArrayList() private String testCommandDirectory private Closure existingDecorations diff --git a/src/S3BackendPlugin.groovy b/src/S3BackendPlugin.groovy index f01d9c56..8f99dbca 100644 --- a/src/S3BackendPlugin.groovy +++ b/src/S3BackendPlugin.groovy @@ -20,6 +20,7 @@ class S3BackendPlugin implements TerraformInitCommandPlugin { configs['encrypt'] = getEncrypt(environment) configs['kms_key_id'] = getKmsKeyId(environment) + configs.each { entry -> if (entry.value?.trim()) { command.withBackendConfig("${entry.key}=${entry.value}") @@ -65,12 +66,12 @@ class S3BackendPlugin implements TerraformInitCommandPlugin { String region = env['S3_BACKEND_REGION'] if (region == null) { - println("No S3_BACKEND_REGION found - checking for environment-specific region") - region = env["${environment.toUpperCase()}_S3_BACKEND_REGION"] + println("No S3_BACKEND_REGION found - checking for environment-specific region") + region = env["${environment.toUpperCase()}_S3_BACKEND_REGION"] } if (region == null) { - region = env["${environment}_S3_BACKEND_REGION"] + region = env["${environment}_S3_BACKEND_REGION"] } if (region == null) { diff --git a/src/SemanticVersion.groovy b/src/SemanticVersion.groovy index a45a1e1e..6c2a8ae7 100644 --- a/src/SemanticVersion.groovy +++ b/src/SemanticVersion.groovy @@ -1,3 +1,5 @@ +import java.util.Comparator; + class SemanticVersion implements Comparable { private String versionString @@ -18,25 +20,26 @@ class SemanticVersion implements Comparable { @Override int compareTo(SemanticVersion other) { - for (i in 0.. 1.0.1 : 1.0.1 <=> 1.0.1-rc1 } else if (i == other.components.size()) { return this.components[i].isInteger() ? 1 : -1 //1.0.1 <=> 1.0 : 1.0.1-rc1 <=> 1.0.1 } - if (this.components[i].isInteger() && other.components[i].isInteger()) { //1.0 <=> 1.1 or //1.1 <=> 1.0 + if(this.components[i].isInteger() && other.components[i].isInteger()) { //1.0 <=> 1.1 or //1.1 <=> 1.0 int diff = (this.components[i] as int) <=> (other.components[i] as int) - if (diff != 0) { + if(diff != 0) { return diff } - } else if (this.components[i].isInteger()) { //1.0.3.4 <=> 1.0.3.4b goes to the former. Hmm. + } else if(this.components[i].isInteger()) { //1.0.3.4 <=> 1.0.3.4b goes to the former. Hmm. return 1 - } else if (other.components[i].isInteger()) { //1.0.3.4-rc3 <=> 1.0.3.4 goes to the later. + } else if(other.components[i].isInteger()) { //1.0.3.4-rc3 <=> 1.0.3.4 goes to the later. return -1 } else { int diff = this.components[i] <=> other.components[i] //1.0.3.3 <=> 1.0.3.4 works at least. :-/ - if (diff != 0) { + if(diff != 0) { return diff } } diff --git a/src/TerraformApplyCommand.groovy b/src/TerraformApplyCommand.groovy index 573df6f7..49e4f402 100644 --- a/src/TerraformApplyCommand.groovy +++ b/src/TerraformApplyCommand.groovy @@ -54,7 +54,7 @@ class TerraformApplyCommand { private applyPluginsOnce() { def remainingPlugins = plugins - appliedPlugins - for (TerraformApplyCommandPlugin plugin in remainingPlugins) { + for(TerraformApplyCommandPlugin plugin in remainingPlugins) { plugin.apply(this) appliedPlugins << plugin } diff --git a/src/TerraformEnvironmentStage.groovy b/src/TerraformEnvironmentStage.groovy index 07dd67e4..fe83ec42 100644 --- a/src/TerraformEnvironmentStage.groovy +++ b/src/TerraformEnvironmentStage.groovy @@ -18,7 +18,7 @@ class TerraformEnvironmentStage implements Stage { TerraformEnvironmentStage(String environment) { this.environment = environment this.jenkinsfile = Jenkinsfile.instance - this.decorations = [:] + this.decorations = new HashMap() } public Stage then(Stage nextStages) { @@ -106,7 +106,7 @@ class TerraformEnvironmentStage implements Stage { } } - decorations.put(stageName, newDecoration) + decorations.put(stageName,newDecoration) } private void applyDecorationsAround(String stageName, Closure stageClosure) { @@ -127,7 +127,7 @@ class TerraformEnvironmentStage implements Stage { public void applyPlugins() { // Apply both global and local plugins, in the correct order - for (plugin in getAllPlugins()) { + for(plugin in getAllPlugins()) { plugin.apply(this) } } diff --git a/src/TerraformInitCommand.groovy b/src/TerraformInitCommand.groovy index d7462d2c..15d4586b 100644 --- a/src/TerraformInitCommand.groovy +++ b/src/TerraformInitCommand.groovy @@ -50,7 +50,7 @@ class TerraformInitCommand { if (!input) { pieces << "-input=false" } - if (doBackend) { + if(doBackend) { backendConfigs.each { config -> pieces << "-backend-config=${config}" } @@ -67,7 +67,7 @@ class TerraformInitCommand { private applyPluginsOnce() { def remainingPlugins = globalPlugins - appliedPlugins - for (TerraformInitCommandPlugin plugin in remainingPlugins) { + for(TerraformInitCommandPlugin plugin in remainingPlugins) { plugin.apply(this) appliedPlugins << plugin } diff --git a/src/TerraformPlanCommand.groovy b/src/TerraformPlanCommand.groovy index 1779a62f..6d559c48 100644 --- a/src/TerraformPlanCommand.groovy +++ b/src/TerraformPlanCommand.groovy @@ -55,7 +55,7 @@ class TerraformPlanCommand { private applyPluginsOnce() { def remainingPlugins = plugins - appliedPlugins - for (TerraformPlanCommandPlugin plugin in remainingPlugins) { + for(TerraformPlanCommandPlugin plugin in remainingPlugins) { plugin.apply(this) appliedPlugins << plugin } diff --git a/src/TerraformPlugin.groovy b/src/TerraformPlugin.groovy index 67efd27e..009e9b30 100644 --- a/src/TerraformPlugin.groovy +++ b/src/TerraformPlugin.groovy @@ -23,7 +23,7 @@ class TerraformPlugin implements TerraformValidateCommandPlugin, TerraformValida } public SemanticVersion detectVersion() { - if (version == null) { + if(version == null) { if (fileExists(TERRAFORM_VERSION_FILE)) { version = new SemanticVersion(readFile(TERRAFORM_VERSION_FILE)) } else { @@ -35,7 +35,7 @@ class TerraformPlugin implements TerraformValidateCommandPlugin, TerraformValida } public TerraformPluginVersion strategyFor(String version) { - if (new SemanticVersion(version) >= new SemanticVersion('0.12.0')) { + if(new SemanticVersion(version).compareTo(new SemanticVersion('0.12.0')) >= 0) { return new TerraformPluginVersion12() } diff --git a/src/TerraformValidateCommand.groovy b/src/TerraformValidateCommand.groovy index 46c33d06..3a6b3497 100644 --- a/src/TerraformValidateCommand.groovy +++ b/src/TerraformValidateCommand.groovy @@ -31,7 +31,8 @@ class TerraformValidateCommand { pieces = pieces + prefixes pieces << terraformBinary pieces << command - for (String argument in arguments) { + for(String argument in arguments) + { pieces << argument } if (directory) { @@ -43,7 +44,7 @@ class TerraformValidateCommand { private applyPluginsOnce() { def remainingPlugins = globalPlugins - appliedPlugins - for (TerraformValidateCommandPlugin plugin in remainingPlugins) { + for(TerraformValidateCommandPlugin plugin in remainingPlugins) { plugin.apply(this) appliedPlugins << plugin } diff --git a/src/TerraformValidateStage.groovy b/src/TerraformValidateStage.groovy index c9b07d88..c941436f 100644 --- a/src/TerraformValidateStage.groovy +++ b/src/TerraformValidateStage.groovy @@ -9,7 +9,7 @@ class TerraformValidateStage implements Stage { public TerraformValidateStage() { this.jenkinsfile = Jenkinsfile.instance - this.decorations = [:] + this.decorations = new HashMap() } public Stage then(Stage nextStage) { @@ -60,7 +60,7 @@ class TerraformValidateStage implements Stage { } } - decorations.put(stageName, newDecoration) + decorations.put(stageName,newDecoration) } private void applyDecorationsAround(String stageName, Closure stageClosure) { @@ -76,7 +76,7 @@ class TerraformValidateStage implements Stage { } public void applyPlugins() { - for (plugin in globalPlugins) { + for(plugin in globalPlugins) { plugin.apply(this) } } diff --git a/src/TfvarsFilesPlugin.groovy b/src/TfvarsFilesPlugin.groovy index cddd103a..0146e625 100644 --- a/src/TfvarsFilesPlugin.groovy +++ b/src/TfvarsFilesPlugin.groovy @@ -19,7 +19,7 @@ class TfvarsFilesPlugin implements TerraformPlanCommandPlugin, TerraformApplyCom @Override void apply(TerraformApplyCommand command) { def environmentVarFile = "${directory}/${command.environment}.tfvars" - if (originalContext.fileExists(environmentVarFile)) { + if(originalContext.fileExists(environmentVarFile)) { command.withArgument("-var-file=${environmentVarFile}") } else { originalContext.echo "${environmentVarFile} does not exist." @@ -29,7 +29,7 @@ class TfvarsFilesPlugin implements TerraformPlanCommandPlugin, TerraformApplyCom @Override void apply(TerraformPlanCommand command) { def environmentVarFile = "${directory}/${command.environment}.tfvars" - if (originalContext.fileExists(environmentVarFile)) { + if(originalContext.fileExists(environmentVarFile)) { command.withArgument("-var-file=${environmentVarFile}") } else { originalContext.echo "${environmentVarFile} does not exist." diff --git a/test/AnsiColorPluginTest.groovy b/test/AnsiColorPluginTest.groovy index d7b38484..00004bb3 100644 --- a/test/AnsiColorPluginTest.groovy +++ b/test/AnsiColorPluginTest.groovy @@ -1,11 +1,9 @@ -import static org.hamcrest.Matchers.hasItem -import static org.hamcrest.Matchers.instanceOf -import static org.junit.Assert.assertThat +import static org.junit.Assert.* -import org.junit.Test -import org.junit.After +import org.junit.* import org.junit.runner.RunWith import de.bechte.junit.runners.context.HierarchicalContextRunner +import static org.hamcrest.Matchers.* @RunWith(HierarchicalContextRunner.class) class AnsiColorPluginTest { diff --git a/test/AwssumePluginTest.groovy b/test/AwssumePluginTest.groovy index d1df256f..9728c33d 100644 --- a/test/AwssumePluginTest.groovy +++ b/test/AwssumePluginTest.groovy @@ -1,17 +1,11 @@ -import static org.hamcrest.Matchers.containsString -import static org.hamcrest.Matchers.hasItem -import static org.hamcrest.Matchers.instanceOf -import static org.hamcrest.Matchers.is -import static org.hamcrest.Matchers.not -import static org.junit.Assert.assertThat -import static org.mockito.Mockito.when; -import static org.mockito.Mockito.mock; +import static org.junit.Assert.* -import org.junit.After -import org.junit.Before -import org.junit.Test +import org.junit.* import org.junit.runner.RunWith import de.bechte.junit.runners.context.HierarchicalContextRunner +import static org.mockito.Mockito.when; +import static org.mockito.Mockito.mock; +import static org.hamcrest.Matchers.* @RunWith(HierarchicalContextRunner.class) class AwssumePluginTest { diff --git a/test/BuildGraphTest.groovy b/test/BuildGraphTest.groovy index e63bdeb3..8391145b 100644 --- a/test/BuildGraphTest.groovy +++ b/test/BuildGraphTest.groovy @@ -1,12 +1,11 @@ -import static org.mockito.Mockito.inOrder -import static org.mockito.Mockito.mock -import static org.mockito.Mockito.times -import static org.mockito.Mockito.verify +import static org.junit.Assert.* -import org.junit.Test +import org.junit.* import org.junit.runner.RunWith import de.bechte.junit.runners.context.HierarchicalContextRunner import org.mockito.InOrder +import static org.mockito.Mockito.* +import static org.hamcrest.Matchers.* @RunWith(HierarchicalContextRunner.class) class BuildGraphTest { diff --git a/test/BuildStageTest.groovy b/test/BuildStageTest.groovy index 72e19a17..312a0993 100644 --- a/test/BuildStageTest.groovy +++ b/test/BuildStageTest.groovy @@ -1,10 +1,12 @@ -import static org.mockito.Mockito.spy -import static org.mockito.Mockito.doReturn - -import org.junit.Test +import org.junit.* import org.junit.runner.RunWith import de.bechte.junit.runners.context.HierarchicalContextRunner +import static org.mockito.Mockito.verify +import static org.mockito.Mockito.when +import static org.mockito.Mockito.spy +import static org.mockito.Mockito.doReturn + @RunWith(HierarchicalContextRunner.class) class BuildStageTest { @@ -13,7 +15,7 @@ class BuildStageTest { void buildsWithoutError() { BuildStage stage = spy(new BuildStage()) - doReturn { -> }.when(stage).pipelineConfiguration() + doReturn({ -> }).when(stage).pipelineConfiguration() stage.build() } diff --git a/test/ConditionalApplyPluginTest.groovy b/test/ConditionalApplyPluginTest.groovy index 4348a44b..cc946576 100644 --- a/test/ConditionalApplyPluginTest.groovy +++ b/test/ConditionalApplyPluginTest.groovy @@ -1,16 +1,14 @@ -import static org.hamcrest.Matchers.hasItem -import static org.hamcrest.Matchers.instanceOf -import static org.junit.Assert.assertFalse -import static org.junit.Assert.assertThat -import static org.junit.Assert.assertTrue -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; +import static org.junit.Assert.* -import org.junit.After -import org.junit.Test +import org.junit.* import org.junit.runner.RunWith import de.bechte.junit.runners.context.HierarchicalContextRunner +import static org.hamcrest.Matchers.* +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import static org.mockito.Matchers.* + @RunWith(HierarchicalContextRunner.class) class ConditionalApplyPluginTest { @After diff --git a/test/ConfirmApplyPluginTest.groovy b/test/ConfirmApplyPluginTest.groovy index 18b992d9..2239eec3 100644 --- a/test/ConfirmApplyPluginTest.groovy +++ b/test/ConfirmApplyPluginTest.groovy @@ -1,13 +1,9 @@ -import static org.hamcrest.Matchers.hasItem -import static org.hamcrest.Matchers.instanceOf -import static org.junit.Assert.assertFalse -import static org.junit.Assert.assertThat -import static org.junit.Assert.assertTrue - -import org.junit.After -import org.junit.Test +import static org.junit.Assert.* + +import org.junit.* import org.junit.runner.RunWith import de.bechte.junit.runners.context.HierarchicalContextRunner +import static org.hamcrest.Matchers.* @RunWith(HierarchicalContextRunner.class) class ConfirmApplyPluginTest { diff --git a/test/ConsulBackendPluginTest.groovy b/test/ConsulBackendPluginTest.groovy index 665c77e5..01ece859 100644 --- a/test/ConsulBackendPluginTest.groovy +++ b/test/ConsulBackendPluginTest.groovy @@ -1,16 +1,11 @@ -import static org.hamcrest.Matchers.containsString -import static org.hamcrest.Matchers.hasItem -import static org.hamcrest.Matchers.instanceOf -import static org.hamcrest.Matchers.not -import static org.junit.Assert.assertThat -import static org.mockito.Mockito.when; -import static org.mockito.Mockito.mock; +import static org.junit.Assert.* -import org.junit.After -import org.junit.Before -import org.junit.Test +import org.junit.* import org.junit.runner.RunWith import de.bechte.junit.runners.context.HierarchicalContextRunner +import static org.mockito.Mockito.when; +import static org.mockito.Mockito.mock; +import static org.hamcrest.Matchers.* @RunWith(HierarchicalContextRunner.class) class ConsulBackendPluginTest { diff --git a/test/CredentialsPluginTest.groovy b/test/CredentialsPluginTest.groovy index 70075823..f15faae2 100644 --- a/test/CredentialsPluginTest.groovy +++ b/test/CredentialsPluginTest.groovy @@ -1,15 +1,9 @@ -import static org.hamcrest.Matchers.equalTo -import static org.hamcrest.Matchers.hasItem -import static org.hamcrest.Matchers.hasSize -import static org.hamcrest.Matchers.instanceOf -import static org.hamcrest.Matchers.is -import static org.hamcrest.Matchers.notNullValue -import static org.junit.Assert.assertThat - -import org.junit.After -import org.junit.Test +import static org.junit.Assert.* + +import org.junit.* import org.junit.runner.RunWith import de.bechte.junit.runners.context.HierarchicalContextRunner +import static org.hamcrest.Matchers.* @RunWith(HierarchicalContextRunner.class) class CredentialsPluginTest { diff --git a/test/CrqPluginTest.groovy b/test/CrqPluginTest.groovy index 130ef716..3c16a15c 100644 --- a/test/CrqPluginTest.groovy +++ b/test/CrqPluginTest.groovy @@ -1,16 +1,14 @@ -import static org.hamcrest.Matchers.hasItem -import static org.hamcrest.Matchers.instanceOf -import static org.hamcrest.Matchers.is -import static org.junit.Assert.assertThat -import static org.mockito.Mockito.mock -import static org.mockito.Mockito.spy -import static org.mockito.Mockito.verify -import static org.mockito.Mockito.when - -import org.junit.After -import org.junit.Test +import static org.junit.Assert.* + +import org.junit.* import org.junit.runner.RunWith import de.bechte.junit.runners.context.HierarchicalContextRunner +import static org.mockito.Mockito.when; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.mock; +import static org.mockito.Matchers.* +import static org.hamcrest.Matchers.* +import static org.mockito.Mockito.*; @RunWith(HierarchicalContextRunner.class) class CrqPluginTest { diff --git a/test/DefaultEnvironmentPluginTest.groovy b/test/DefaultEnvironmentPluginTest.groovy index d2390dd1..934963c7 100644 --- a/test/DefaultEnvironmentPluginTest.groovy +++ b/test/DefaultEnvironmentPluginTest.groovy @@ -1,10 +1,9 @@ -import static org.hamcrest.Matchers.hasItem -import static org.hamcrest.Matchers.instanceOf -import static org.junit.Assert.assertThat +import static org.junit.Assert.* -import org.junit.Test +import org.junit.* import org.junit.runner.RunWith import de.bechte.junit.runners.context.HierarchicalContextRunner +import static org.hamcrest.Matchers.* @RunWith(HierarchicalContextRunner.class) class DefaultEnvironmentPluginTest { diff --git a/test/FileParametersPluginTest.groovy b/test/FileParametersPluginTest.groovy index 903cc0f7..634b1a6e 100644 --- a/test/FileParametersPluginTest.groovy +++ b/test/FileParametersPluginTest.groovy @@ -1,14 +1,12 @@ -import static org.hamcrest.Matchers.hasItem -import static org.hamcrest.Matchers.instanceOf -import static org.junit.Assert.assertEquals -import static org.junit.Assert.assertThat -import static org.mockito.Mockito.when; -import static org.mockito.Mockito.spy; +import static org.junit.Assert.* -import org.junit.After -import org.junit.Test +import org.junit.* import org.junit.runner.RunWith import de.bechte.junit.runners.context.HierarchicalContextRunner +import static org.mockito.Mockito.when; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.spy; +import static org.hamcrest.Matchers.* @RunWith(HierarchicalContextRunner.class) class FileParametersPluginTest { diff --git a/test/JenkinsfileTest.groovy b/test/JenkinsfileTest.groovy index 6f1ce005..f519998c 100644 --- a/test/JenkinsfileTest.groovy +++ b/test/JenkinsfileTest.groovy @@ -1,12 +1,14 @@ -import static org.junit.Assert.assertEquals -import static org.mockito.Mockito.mock -import static org.mockito.Mockito.when +import static org.junit.Assert.* -import org.junit.After -import org.junit.Before -import org.junit.Test +import org.junit.* import org.junit.runner.RunWith import de.bechte.junit.runners.context.HierarchicalContextRunner +import Jenkinsfile + +import static org.mockito.Mockito.mock +import static org.mockito.Mockito.when +import static org.hamcrest.Matchers.* + @RunWith(HierarchicalContextRunner.class) class JenkinsfileTest { diff --git a/test/ParameterStoreBuildWrapperPluginTest.groovy b/test/ParameterStoreBuildWrapperPluginTest.groovy index e75db74f..a05aade5 100644 --- a/test/ParameterStoreBuildWrapperPluginTest.groovy +++ b/test/ParameterStoreBuildWrapperPluginTest.groovy @@ -1,14 +1,11 @@ -import static org.hamcrest.Matchers.hasItem -import static org.hamcrest.Matchers.instanceOf -import static org.junit.Assert.assertEquals -import static org.junit.Assert.assertThat -import static org.mockito.Mockito.mock -import static org.mockito.Mockito.when +import static org.junit.Assert.* -import org.junit.After -import org.junit.Test +import org.junit.* import org.junit.runner.RunWith import de.bechte.junit.runners.context.HierarchicalContextRunner +import static org.mockito.Mockito.mock +import static org.mockito.Mockito.when +import static org.hamcrest.Matchers.* @RunWith(HierarchicalContextRunner.class) class ParameterStoreBuildWrapperPluginTest { diff --git a/test/ParameterStoreExecPluginTest.groovy b/test/ParameterStoreExecPluginTest.groovy index e0476dab..50340477 100644 --- a/test/ParameterStoreExecPluginTest.groovy +++ b/test/ParameterStoreExecPluginTest.groovy @@ -1,15 +1,11 @@ -import static org.hamcrest.Matchers.containsString -import static org.hamcrest.Matchers.hasItem -import static org.hamcrest.Matchers.instanceOf -import static org.junit.Assert.assertEquals -import static org.junit.Assert.assertThat -import static org.mockito.Mockito.when; -import static org.mockito.Mockito.mock; +import static org.junit.Assert.* -import org.junit.After -import org.junit.Test +import org.junit.* import org.junit.runner.RunWith import de.bechte.junit.runners.context.HierarchicalContextRunner +import static org.mockito.Mockito.when; +import static org.mockito.Mockito.mock; +import static org.hamcrest.Matchers.* @RunWith(HierarchicalContextRunner.class) class ParameterStoreExecPluginTest { diff --git a/test/RegressionStageTest.groovy b/test/RegressionStageTest.groovy index fde4be77..93144fda 100644 --- a/test/RegressionStageTest.groovy +++ b/test/RegressionStageTest.groovy @@ -1,12 +1,11 @@ +import org.junit.* +import org.junit.runner.RunWith +import de.bechte.junit.runners.context.HierarchicalContextRunner + import static org.mockito.Mockito.mock import static org.mockito.Mockito.verify import static org.mockito.Mockito.when -import org.junit.After -import org.junit.Test -import org.junit.runner.RunWith -import de.bechte.junit.runners.context.HierarchicalContextRunner - @RunWith(HierarchicalContextRunner.class) class RegressionStageTest { @@ -24,7 +23,7 @@ class RegressionStageTest { } @Test - void automationRepoSpecifiedSuccessfullyCallApply() { + void automationRepoSpecifiedSuccessfullyCallApply(){ RegressionStagePlugin fakePlugin = mock(RegressionStagePlugin.class) RegressionStage.addPlugin(fakePlugin) @@ -36,7 +35,7 @@ class RegressionStageTest { } @Test - void automationRepoAndAppRepoSpecifiedSuccessfullyCallApply() { + void automationRepoAndAppRepoSpecifiedSuccessfullyCallApply(){ RegressionStagePlugin fakePlugin = mock(RegressionStagePlugin.class) RegressionStage.addPlugin(fakePlugin) @@ -49,7 +48,7 @@ class RegressionStageTest { } @Test - void automationRepoAndAppRepoWithChangeDirectorySpecifiedSuccessfullyCallApply() { + void automationRepoAndAppRepoWithChangeDirectorySpecifiedSuccessfullyCallApply(){ RegressionStagePlugin fakePlugin = mock(RegressionStagePlugin.class) RegressionStage.addPlugin(fakePlugin) @@ -63,7 +62,7 @@ class RegressionStageTest { } @Test - void noAutomationRepoSpecifiedSuccessfullyCallApply() { + void noAutomationRepoSpecifiedSuccessfullyCallApply(){ RegressionStagePlugin fakePlugin = mock(RegressionStagePlugin.class) RegressionStage.addPlugin(fakePlugin) diff --git a/test/S3BackendPluginTest.groovy b/test/S3BackendPluginTest.groovy index 5467c53d..e32ffcf9 100644 --- a/test/S3BackendPluginTest.groovy +++ b/test/S3BackendPluginTest.groovy @@ -1,16 +1,12 @@ -import static org.hamcrest.Matchers.contains -import static org.hamcrest.Matchers.containsString -import static org.hamcrest.Matchers.instanceOf -import static org.hamcrest.Matchers.is -import static org.hamcrest.Matchers.not -import static org.junit.Assert.assertThat -import static org.mockito.Mockito.when; -import static org.mockito.Mockito.spy; +import static org.junit.Assert.* -import org.junit.After -import org.junit.Test +import org.junit.* import org.junit.runner.RunWith import de.bechte.junit.runners.context.HierarchicalContextRunner +import static org.mockito.Mockito.when; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.spy; +import static org.hamcrest.Matchers.* @RunWith(HierarchicalContextRunner.class) class S3BackendPluginTest { diff --git a/test/SemanticVersionTest.groovy b/test/SemanticVersionTest.groovy index 5701fa7d..e0491c5c 100644 --- a/test/SemanticVersionTest.groovy +++ b/test/SemanticVersionTest.groovy @@ -1,8 +1,8 @@ -import static org.junit.Assert.assertEquals - import org.junit.Test import org.junit.runner.RunWith import org.junit.runners.Parameterized +import static org.junit.Assert.* + @RunWith(Parameterized.class) class SemanticVersionTest { @@ -16,14 +16,10 @@ class SemanticVersionTest { @Test void sortsCorrectly() { - List versions = SORTED.clone().collect { v -> new SemanticVersion(v) } - // Randomize - Collections.shuffle(versions) - // Then sort - versions.sort() - - def result = versions.collect { sv -> sv.version } - assertEquals(SORTED, result) + List unsorted = SORTED.clone().collect({v -> new SemanticVersion(v)}) + Collections.shuffle(unsorted) + def result = unsorted.sort().collect({sv -> sv.version}) + assertEquals(SORTED,result) } static SORTED = [ @@ -34,22 +30,22 @@ class SemanticVersionTest { '0.1.0.2', '0.1.0.10', '0.1.0.23', - '0.2', - '0.2.0.3.1', + '0.2', + '0.2.0.3.1', '0.2.0.4', - '1.0RC1', - '1.0RC2', - '1.0', - '1.0.1.2', - '1.0.2', + '1.0RC1', + '1.0RC2', + '1.0', + '1.0.1.2', + '1.0.2', '1.2.2.3', - '1.2.3', - '1.5.2_04', - '1.5.2_05', - '1.5.2_10', + '1.2.3', + '1.5.2_04', + '1.5.2_05', + '1.5.2_10', '1.6.0_01', - '2.0', - '2.0.0_02', + '2.0', + '2.0.0_02', '3.1' ] } diff --git a/test/TerraformApplyCommandTest.groovy b/test/TerraformApplyCommandTest.groovy index acafaff8..b93bbc61 100644 --- a/test/TerraformApplyCommandTest.groovy +++ b/test/TerraformApplyCommandTest.groovy @@ -1,17 +1,15 @@ -import static org.hamcrest.Matchers.containsString -import static org.hamcrest.Matchers.endsWith -import static org.hamcrest.Matchers.not -import static org.hamcrest.Matchers.startsWith -import static org.junit.Assert.assertThat -import static org.mockito.Mockito.mock -import static org.mockito.Mockito.times -import static org.mockito.Mockito.verify - -import org.junit.After -import org.junit.Test +import static org.junit.Assert.* + +import org.junit.* import org.junit.runner.RunWith import de.bechte.junit.runners.context.HierarchicalContextRunner +import static org.hamcrest.Matchers.* +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.times; + + @RunWith(HierarchicalContextRunner.class) class TerraformApplyCommandTest { public class WithInput { diff --git a/test/TerraformDirectoryPluginTest.groovy b/test/TerraformDirectoryPluginTest.groovy index d154c87e..c858b044 100644 --- a/test/TerraformDirectoryPluginTest.groovy +++ b/test/TerraformDirectoryPluginTest.groovy @@ -1,12 +1,11 @@ -import static org.hamcrest.Matchers.containsString -import static org.hamcrest.Matchers.hasItem -import static org.hamcrest.Matchers.instanceOf -import static org.junit.Assert.assertThat +import static org.junit.Assert.* -import org.junit.After -import org.junit.Test +import org.junit.* import org.junit.runner.RunWith import de.bechte.junit.runners.context.HierarchicalContextRunner +import static org.mockito.Mockito.when; +import static org.mockito.Mockito.mock; +import static org.hamcrest.Matchers.* @RunWith(HierarchicalContextRunner.class) class TerraformDirectoryPluginTest { diff --git a/test/TerraformEnvironmentStageTest.groovy b/test/TerraformEnvironmentStageTest.groovy index 6cf87685..1167c612 100644 --- a/test/TerraformEnvironmentStageTest.groovy +++ b/test/TerraformEnvironmentStageTest.groovy @@ -1,17 +1,13 @@ -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify -import static org.hamcrest.Matchers.hasItem -import static org.hamcrest.Matchers.is -import static org.hamcrest.Matchers.isA -import static org.junit.Assert.assertEquals -import static org.junit.Assert.assertThat -import static org.junit.Assert.assertTrue - -import org.junit.After -import org.junit.Test +import static org.junit.Assert.* + +import org.junit.* import org.junit.runner.RunWith import de.bechte.junit.runners.context.HierarchicalContextRunner +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.hamcrest.Matchers.* + @RunWith(HierarchicalContextRunner.class) class TerraformEnvironmentStageTest { @After @@ -64,16 +60,7 @@ class TerraformEnvironmentStageTest { @Test void doesNotAddPluginToOtherInstances() { - def modifiedStage = new TerraformEnvironmentStage('modified') - def unmodifiedStage = new TerraformEnvironmentStage('unmodified') - - def pluginsBefore = unmodifiedStage.getAllPlugins() - - modifiedStage.withEnv('somekey', 'somevalue') - - def pluginsAfter = unmodifiedStage.getAllPlugins() - assertEquals(pluginsBefore, pluginsAfter) } @Test diff --git a/test/TerraformInitCommandTest.groovy b/test/TerraformInitCommandTest.groovy index 7d482c1c..4a54be3e 100644 --- a/test/TerraformInitCommandTest.groovy +++ b/test/TerraformInitCommandTest.groovy @@ -1,15 +1,14 @@ -import static org.hamcrest.Matchers.containsString -import static org.hamcrest.Matchers.endsWith -import static org.hamcrest.Matchers.not -import static org.junit.Assert.assertThat +import static org.junit.Assert.* + +import org.junit.* +import org.junit.runner.RunWith +import de.bechte.junit.runners.context.HierarchicalContextRunner + +import static org.hamcrest.Matchers.* import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.times; -import org.junit.After -import org.junit.Test -import org.junit.runner.RunWith -import de.bechte.junit.runners.context.HierarchicalContextRunner @RunWith(HierarchicalContextRunner.class) class TerraformInitCommandTest { diff --git a/test/TerraformPlanCommandTest.groovy b/test/TerraformPlanCommandTest.groovy index 33c29892..8531412e 100644 --- a/test/TerraformPlanCommandTest.groovy +++ b/test/TerraformPlanCommandTest.groovy @@ -1,16 +1,14 @@ -import static org.hamcrest.Matchers.containsString -import static org.hamcrest.Matchers.endsWith -import static org.hamcrest.Matchers.not -import static org.hamcrest.Matchers.startsWith -import static org.junit.Assert.assertThat +import static org.junit.Assert.* + +import org.junit.* +import org.junit.runner.RunWith +import de.bechte.junit.runners.context.HierarchicalContextRunner + +import static org.hamcrest.Matchers.* import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.times; -import org.junit.After -import org.junit.Test -import org.junit.runner.RunWith -import de.bechte.junit.runners.context.HierarchicalContextRunner @RunWith(HierarchicalContextRunner.class) class TerraformPlanCommandTest { @@ -88,6 +86,7 @@ class TerraformPlanCommandTest { } } + public class Plugins { @After void resetPlugins() { diff --git a/test/TerraformPluginTest.groovy b/test/TerraformPluginTest.groovy index 30d44c07..a5797ea4 100644 --- a/test/TerraformPluginTest.groovy +++ b/test/TerraformPluginTest.groovy @@ -1,14 +1,16 @@ -import static org.hamcrest.Matchers.instanceOf -import static org.junit.Assert.assertEquals -import static org.junit.Assert.assertThat -import static org.mockito.Mockito.spy -import static org.mockito.Mockito.doReturn; - -import org.junit.After -import org.junit.Test +import org.junit.* import org.junit.runner.RunWith import de.bechte.junit.runners.context.HierarchicalContextRunner +import static org.hamcrest.Matchers.* +import static org.junit.Assert.* +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.spy +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Matchers.* + @RunWith(HierarchicalContextRunner.class) class TerraformPluginTest { diff --git a/test/TerraformPluginVersion11Test.groovy b/test/TerraformPluginVersion11Test.groovy index e87fb367..e63a983d 100644 --- a/test/TerraformPluginVersion11Test.groovy +++ b/test/TerraformPluginVersion11Test.groovy @@ -1,10 +1,15 @@ -import static org.mockito.Mockito.spy -import static org.mockito.Mockito.verify; - -import org.junit.Test +import org.junit.* import org.junit.runner.RunWith import de.bechte.junit.runners.context.HierarchicalContextRunner +import static org.hamcrest.Matchers.* +import static org.junit.Assert.* +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.spy +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Matchers.* + @RunWith(HierarchicalContextRunner.class) class TerraformPluginVersion11Test { diff --git a/test/TerraformPluginVersion12Test.groovy b/test/TerraformPluginVersion12Test.groovy index f01717ec..807171c0 100644 --- a/test/TerraformPluginVersion12Test.groovy +++ b/test/TerraformPluginVersion12Test.groovy @@ -1,14 +1,15 @@ -import static org.hamcrest.Matchers.containsString -import static org.junit.Assert.assertThat -import static org.mockito.Matchers.any -import static org.mockito.Matchers.eq -import static org.mockito.Mockito.spy -import static org.mockito.Mockito.verify; - -import org.junit.Test +import org.junit.* import org.junit.runner.RunWith import de.bechte.junit.runners.context.HierarchicalContextRunner +import static org.hamcrest.Matchers.* +import static org.junit.Assert.* +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.spy +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Matchers.* + @RunWith(HierarchicalContextRunner.class) class TerraformPluginVersion12Test { diff --git a/test/TerraformValidateCommandTest.groovy b/test/TerraformValidateCommandTest.groovy index 89ee0301..c888a8a1 100644 --- a/test/TerraformValidateCommandTest.groovy +++ b/test/TerraformValidateCommandTest.groovy @@ -1,14 +1,14 @@ -import static org.junit.Assert.assertThat -import static org.hamcrest.Matchers.endsWith -import static org.hamcrest.Matchers.startsWith +import static org.junit.Assert.* + +import org.junit.* +import org.junit.runner.RunWith +import de.bechte.junit.runners.context.HierarchicalContextRunner + +import static org.hamcrest.Matchers.* import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.times; -import org.junit.After -import org.junit.Test -import org.junit.runner.RunWith -import de.bechte.junit.runners.context.HierarchicalContextRunner @RunWith(HierarchicalContextRunner.class) class TerraformValidateCommandTest { diff --git a/test/TfvarsFilesPluginTest.groovy b/test/TfvarsFilesPluginTest.groovy index b11fca28..838b7ab0 100644 --- a/test/TfvarsFilesPluginTest.groovy +++ b/test/TfvarsFilesPluginTest.groovy @@ -1,3 +1,9 @@ +import de.bechte.junit.runners.context.HierarchicalContextRunner +import org.junit.After +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith + import static org.hamcrest.Matchers.containsString import static org.hamcrest.Matchers.equalTo import static org.hamcrest.Matchers.hasItem @@ -5,11 +11,6 @@ import static org.hamcrest.Matchers.instanceOf import static org.hamcrest.Matchers.not import static org.junit.Assert.assertThat -import de.bechte.junit.runners.context.HierarchicalContextRunner -import org.junit.After -import org.junit.Test -import org.junit.runner.RunWith - @RunWith(HierarchicalContextRunner.class) class TfvarsFilesPluginTest { @@ -43,6 +44,7 @@ class TfvarsFilesPluginTest { TfvarsFilesPlugin.directory = '.' } + class Init { @After void resetPlugins() { diff --git a/test/WithAwsPluginTest.groovy b/test/WithAwsPluginTest.groovy index cb4b8f72..46edbb44 100644 --- a/test/WithAwsPluginTest.groovy +++ b/test/WithAwsPluginTest.groovy @@ -1,15 +1,11 @@ -import static org.hamcrest.Matchers.hasItem -import static org.hamcrest.Matchers.instanceOf -import static org.hamcrest.Matchers.is -import static org.junit.Assert.assertThat -import static org.junit.Assert.assertTrue -import static org.mockito.Mockito.when; -import static org.mockito.Mockito.mock; +import static org.junit.Assert.* -import org.junit.After -import org.junit.Test +import org.junit.* import org.junit.runner.RunWith import de.bechte.junit.runners.context.HierarchicalContextRunner +import static org.mockito.Mockito.when; +import static org.mockito.Mockito.mock; +import static org.hamcrest.Matchers.* @RunWith(HierarchicalContextRunner.class) class WithAwsPluginTest { @@ -144,3 +140,4 @@ class WithAwsPluginTest { } } + From 7471a591dd4e4030767ee9e274cc62a30d000091 Mon Sep 17 00:00:00 2001 From: Keith Manning Date: Wed, 5 Feb 2020 17:41:40 -0500 Subject: [PATCH 21/33] Restore "Merge pull request #184 from kmanning/issue_87_part_3"" --- build.gradle | 1 + config/codenarc/codenarc.xml | 33 +++++++++++++++ src/AgentNodePlugin.groovy | 4 +- src/FileParametersPlugin.groovy | 4 +- src/Jenkinsfile.groovy | 3 +- src/RegressionStage.groovy | 2 +- src/S3BackendPlugin.groovy | 7 ++-- src/SemanticVersion.groovy | 17 ++++---- src/TerraformApplyCommand.groovy | 2 +- src/TerraformEnvironmentStage.groovy | 6 +-- src/TerraformInitCommand.groovy | 4 +- src/TerraformPlanCommand.groovy | 2 +- src/TerraformPlugin.groovy | 4 +- src/TerraformValidateCommand.groovy | 5 +-- src/TerraformValidateStage.groovy | 6 +-- src/TfvarsFilesPlugin.groovy | 4 +- test/AnsiColorPluginTest.groovy | 8 ++-- test/AwssumePluginTest.groovy | 16 ++++--- test/BuildGraphTest.groovy | 9 ++-- test/BuildStageTest.groovy | 12 +++--- test/ConditionalApplyPluginTest.groovy | 16 +++---- test/ConfirmApplyPluginTest.groovy | 12 ++++-- test/ConsulBackendPluginTest.groovy | 15 ++++--- test/CredentialsPluginTest.groovy | 14 +++++-- test/CrqPluginTest.groovy | 20 +++++---- test/DefaultEnvironmentPluginTest.groovy | 7 ++-- test/FileParametersPluginTest.groovy | 14 ++++--- test/JenkinsfileTest.groovy | 14 +++---- ...arameterStoreBuildWrapperPluginTest.groovy | 13 +++--- test/ParameterStoreExecPluginTest.groovy | 14 ++++--- test/RegressionStageTest.groovy | 17 ++++---- test/S3BackendPluginTest.groovy | 16 ++++--- test/SemanticVersionTest.groovy | 42 ++++++++++--------- test/TerraformApplyCommandTest.groovy | 20 +++++---- test/TerraformDirectoryPluginTest.groovy | 11 ++--- test/TerraformEnvironmentStageTest.groovy | 27 ++++++++---- test/TerraformInitCommandTest.groovy | 15 +++---- test/TerraformPlanCommandTest.groovy | 17 ++++---- test/TerraformPluginTest.groovy | 18 ++++---- test/TerraformPluginVersion11Test.groovy | 13 ++---- test/TerraformPluginVersion12Test.groovy | 17 ++++---- test/TerraformValidateCommandTest.groovy | 14 +++---- test/TfvarsFilesPluginTest.groovy | 12 +++--- test/WithAwsPluginTest.groovy | 15 ++++--- 44 files changed, 312 insertions(+), 230 deletions(-) create mode 100644 config/codenarc/codenarc.xml diff --git a/build.gradle b/build.gradle index c00331eb..9ab48bfe 100644 --- a/build.gradle +++ b/build.gradle @@ -1,5 +1,6 @@ apply plugin: 'groovy' apply plugin: 'jacoco' +apply plugin: 'codenarc' repositories { mavenCentral() diff --git a/config/codenarc/codenarc.xml b/config/codenarc/codenarc.xml new file mode 100644 index 00000000..6bb07059 --- /dev/null +++ b/config/codenarc/codenarc.xml @@ -0,0 +1,33 @@ + + + + + + + + + + + + + + + + + diff --git a/src/AgentNodePlugin.groovy b/src/AgentNodePlugin.groovy index 4697bb15..5fb3b1c4 100644 --- a/src/AgentNodePlugin.groovy +++ b/src/AgentNodePlugin.groovy @@ -5,7 +5,7 @@ public class AgentNodePlugin implements TerraformValidateStagePlugin, TerraformE private static String dockerImage private static String dockerOptions - AgentNodePlugin() {} + AgentNodePlugin() { } public static void init() { def plugin = new AgentNodePlugin() @@ -37,7 +37,7 @@ public class AgentNodePlugin implements TerraformValidateStagePlugin, TerraformE public Closure addAgent() { return { closure -> if (dockerImage) { - docker.image(this.dockerImage).inside(this.dockerOptions){ + docker.image(this.dockerImage).inside(this.dockerOptions) { closure() } } else { diff --git a/src/FileParametersPlugin.groovy b/src/FileParametersPlugin.groovy index f9641637..e14fb316 100644 --- a/src/FileParametersPlugin.groovy +++ b/src/FileParametersPlugin.groovy @@ -1,7 +1,7 @@ -import groovy.text.StreamingTemplateEngine - import static TerraformEnvironmentStage.ALL +import groovy.text.StreamingTemplateEngine + class FileParametersPlugin implements TerraformEnvironmentStagePlugin { public static void init() { FileParametersPlugin plugin = new FileParametersPlugin() diff --git a/src/Jenkinsfile.groovy b/src/Jenkinsfile.groovy index 152299d8..67534773 100644 --- a/src/Jenkinsfile.groovy +++ b/src/Jenkinsfile.groovy @@ -44,7 +44,7 @@ class Jenkinsfile { def Map parseScmUrl(String scmUrl) { def matcher = scmUrl =~ /.*(?:\/\/|\@)[^\/:]+[\/:]([^\/]+)\/([^\/.]+)(.git)?/ - def Map results = new HashMap() + def Map results = [:] results.put("organization", matcher[0][1]) results.put("repo", matcher[0][2]) return results @@ -122,7 +122,6 @@ class Jenkinsfile { } throw new RuntimeException("Your pipeline has ${stages.size()} stages - the maximum supported by default is 7. Define a custom pipeline template and assign it to Jenkinsfile.pipelineTemplate to create your pipeline.") - } public getEnv() { diff --git a/src/RegressionStage.groovy b/src/RegressionStage.groovy index 075e1602..f59d16ec 100644 --- a/src/RegressionStage.groovy +++ b/src/RegressionStage.groovy @@ -1,7 +1,7 @@ class RegressionStage implements Stage { public String testCommand - public List automationRepoList = new ArrayList() + public List automationRepoList = [] private String testCommandDirectory private Closure existingDecorations diff --git a/src/S3BackendPlugin.groovy b/src/S3BackendPlugin.groovy index 8f99dbca..f01d9c56 100644 --- a/src/S3BackendPlugin.groovy +++ b/src/S3BackendPlugin.groovy @@ -20,7 +20,6 @@ class S3BackendPlugin implements TerraformInitCommandPlugin { configs['encrypt'] = getEncrypt(environment) configs['kms_key_id'] = getKmsKeyId(environment) - configs.each { entry -> if (entry.value?.trim()) { command.withBackendConfig("${entry.key}=${entry.value}") @@ -66,12 +65,12 @@ class S3BackendPlugin implements TerraformInitCommandPlugin { String region = env['S3_BACKEND_REGION'] if (region == null) { - println("No S3_BACKEND_REGION found - checking for environment-specific region") - region = env["${environment.toUpperCase()}_S3_BACKEND_REGION"] + println("No S3_BACKEND_REGION found - checking for environment-specific region") + region = env["${environment.toUpperCase()}_S3_BACKEND_REGION"] } if (region == null) { - region = env["${environment}_S3_BACKEND_REGION"] + region = env["${environment}_S3_BACKEND_REGION"] } if (region == null) { diff --git a/src/SemanticVersion.groovy b/src/SemanticVersion.groovy index 6c2a8ae7..a45a1e1e 100644 --- a/src/SemanticVersion.groovy +++ b/src/SemanticVersion.groovy @@ -1,5 +1,3 @@ -import java.util.Comparator; - class SemanticVersion implements Comparable { private String versionString @@ -20,26 +18,25 @@ class SemanticVersion implements Comparable { @Override int compareTo(SemanticVersion other) { - for(i in 0.. 1.0.1 : 1.0.1 <=> 1.0.1-rc1 } else if (i == other.components.size()) { return this.components[i].isInteger() ? 1 : -1 //1.0.1 <=> 1.0 : 1.0.1-rc1 <=> 1.0.1 } - if(this.components[i].isInteger() && other.components[i].isInteger()) { //1.0 <=> 1.1 or //1.1 <=> 1.0 + if (this.components[i].isInteger() && other.components[i].isInteger()) { //1.0 <=> 1.1 or //1.1 <=> 1.0 int diff = (this.components[i] as int) <=> (other.components[i] as int) - if(diff != 0) { + if (diff != 0) { return diff } - } else if(this.components[i].isInteger()) { //1.0.3.4 <=> 1.0.3.4b goes to the former. Hmm. + } else if (this.components[i].isInteger()) { //1.0.3.4 <=> 1.0.3.4b goes to the former. Hmm. return 1 - } else if(other.components[i].isInteger()) { //1.0.3.4-rc3 <=> 1.0.3.4 goes to the later. + } else if (other.components[i].isInteger()) { //1.0.3.4-rc3 <=> 1.0.3.4 goes to the later. return -1 } else { int diff = this.components[i] <=> other.components[i] //1.0.3.3 <=> 1.0.3.4 works at least. :-/ - if(diff != 0) { + if (diff != 0) { return diff } } diff --git a/src/TerraformApplyCommand.groovy b/src/TerraformApplyCommand.groovy index 49e4f402..573df6f7 100644 --- a/src/TerraformApplyCommand.groovy +++ b/src/TerraformApplyCommand.groovy @@ -54,7 +54,7 @@ class TerraformApplyCommand { private applyPluginsOnce() { def remainingPlugins = plugins - appliedPlugins - for(TerraformApplyCommandPlugin plugin in remainingPlugins) { + for (TerraformApplyCommandPlugin plugin in remainingPlugins) { plugin.apply(this) appliedPlugins << plugin } diff --git a/src/TerraformEnvironmentStage.groovy b/src/TerraformEnvironmentStage.groovy index fe83ec42..07dd67e4 100644 --- a/src/TerraformEnvironmentStage.groovy +++ b/src/TerraformEnvironmentStage.groovy @@ -18,7 +18,7 @@ class TerraformEnvironmentStage implements Stage { TerraformEnvironmentStage(String environment) { this.environment = environment this.jenkinsfile = Jenkinsfile.instance - this.decorations = new HashMap() + this.decorations = [:] } public Stage then(Stage nextStages) { @@ -106,7 +106,7 @@ class TerraformEnvironmentStage implements Stage { } } - decorations.put(stageName,newDecoration) + decorations.put(stageName, newDecoration) } private void applyDecorationsAround(String stageName, Closure stageClosure) { @@ -127,7 +127,7 @@ class TerraformEnvironmentStage implements Stage { public void applyPlugins() { // Apply both global and local plugins, in the correct order - for(plugin in getAllPlugins()) { + for (plugin in getAllPlugins()) { plugin.apply(this) } } diff --git a/src/TerraformInitCommand.groovy b/src/TerraformInitCommand.groovy index 15d4586b..d7462d2c 100644 --- a/src/TerraformInitCommand.groovy +++ b/src/TerraformInitCommand.groovy @@ -50,7 +50,7 @@ class TerraformInitCommand { if (!input) { pieces << "-input=false" } - if(doBackend) { + if (doBackend) { backendConfigs.each { config -> pieces << "-backend-config=${config}" } @@ -67,7 +67,7 @@ class TerraformInitCommand { private applyPluginsOnce() { def remainingPlugins = globalPlugins - appliedPlugins - for(TerraformInitCommandPlugin plugin in remainingPlugins) { + for (TerraformInitCommandPlugin plugin in remainingPlugins) { plugin.apply(this) appliedPlugins << plugin } diff --git a/src/TerraformPlanCommand.groovy b/src/TerraformPlanCommand.groovy index 6d559c48..1779a62f 100644 --- a/src/TerraformPlanCommand.groovy +++ b/src/TerraformPlanCommand.groovy @@ -55,7 +55,7 @@ class TerraformPlanCommand { private applyPluginsOnce() { def remainingPlugins = plugins - appliedPlugins - for(TerraformPlanCommandPlugin plugin in remainingPlugins) { + for (TerraformPlanCommandPlugin plugin in remainingPlugins) { plugin.apply(this) appliedPlugins << plugin } diff --git a/src/TerraformPlugin.groovy b/src/TerraformPlugin.groovy index 009e9b30..67efd27e 100644 --- a/src/TerraformPlugin.groovy +++ b/src/TerraformPlugin.groovy @@ -23,7 +23,7 @@ class TerraformPlugin implements TerraformValidateCommandPlugin, TerraformValida } public SemanticVersion detectVersion() { - if(version == null) { + if (version == null) { if (fileExists(TERRAFORM_VERSION_FILE)) { version = new SemanticVersion(readFile(TERRAFORM_VERSION_FILE)) } else { @@ -35,7 +35,7 @@ class TerraformPlugin implements TerraformValidateCommandPlugin, TerraformValida } public TerraformPluginVersion strategyFor(String version) { - if(new SemanticVersion(version).compareTo(new SemanticVersion('0.12.0')) >= 0) { + if (new SemanticVersion(version) >= new SemanticVersion('0.12.0')) { return new TerraformPluginVersion12() } diff --git a/src/TerraformValidateCommand.groovy b/src/TerraformValidateCommand.groovy index 3a6b3497..46c33d06 100644 --- a/src/TerraformValidateCommand.groovy +++ b/src/TerraformValidateCommand.groovy @@ -31,8 +31,7 @@ class TerraformValidateCommand { pieces = pieces + prefixes pieces << terraformBinary pieces << command - for(String argument in arguments) - { + for (String argument in arguments) { pieces << argument } if (directory) { @@ -44,7 +43,7 @@ class TerraformValidateCommand { private applyPluginsOnce() { def remainingPlugins = globalPlugins - appliedPlugins - for(TerraformValidateCommandPlugin plugin in remainingPlugins) { + for (TerraformValidateCommandPlugin plugin in remainingPlugins) { plugin.apply(this) appliedPlugins << plugin } diff --git a/src/TerraformValidateStage.groovy b/src/TerraformValidateStage.groovy index c941436f..c9b07d88 100644 --- a/src/TerraformValidateStage.groovy +++ b/src/TerraformValidateStage.groovy @@ -9,7 +9,7 @@ class TerraformValidateStage implements Stage { public TerraformValidateStage() { this.jenkinsfile = Jenkinsfile.instance - this.decorations = new HashMap() + this.decorations = [:] } public Stage then(Stage nextStage) { @@ -60,7 +60,7 @@ class TerraformValidateStage implements Stage { } } - decorations.put(stageName,newDecoration) + decorations.put(stageName, newDecoration) } private void applyDecorationsAround(String stageName, Closure stageClosure) { @@ -76,7 +76,7 @@ class TerraformValidateStage implements Stage { } public void applyPlugins() { - for(plugin in globalPlugins) { + for (plugin in globalPlugins) { plugin.apply(this) } } diff --git a/src/TfvarsFilesPlugin.groovy b/src/TfvarsFilesPlugin.groovy index 0146e625..cddd103a 100644 --- a/src/TfvarsFilesPlugin.groovy +++ b/src/TfvarsFilesPlugin.groovy @@ -19,7 +19,7 @@ class TfvarsFilesPlugin implements TerraformPlanCommandPlugin, TerraformApplyCom @Override void apply(TerraformApplyCommand command) { def environmentVarFile = "${directory}/${command.environment}.tfvars" - if(originalContext.fileExists(environmentVarFile)) { + if (originalContext.fileExists(environmentVarFile)) { command.withArgument("-var-file=${environmentVarFile}") } else { originalContext.echo "${environmentVarFile} does not exist." @@ -29,7 +29,7 @@ class TfvarsFilesPlugin implements TerraformPlanCommandPlugin, TerraformApplyCom @Override void apply(TerraformPlanCommand command) { def environmentVarFile = "${directory}/${command.environment}.tfvars" - if(originalContext.fileExists(environmentVarFile)) { + if (originalContext.fileExists(environmentVarFile)) { command.withArgument("-var-file=${environmentVarFile}") } else { originalContext.echo "${environmentVarFile} does not exist." diff --git a/test/AnsiColorPluginTest.groovy b/test/AnsiColorPluginTest.groovy index 00004bb3..d7b38484 100644 --- a/test/AnsiColorPluginTest.groovy +++ b/test/AnsiColorPluginTest.groovy @@ -1,9 +1,11 @@ -import static org.junit.Assert.* +import static org.hamcrest.Matchers.hasItem +import static org.hamcrest.Matchers.instanceOf +import static org.junit.Assert.assertThat -import org.junit.* +import org.junit.Test +import org.junit.After import org.junit.runner.RunWith import de.bechte.junit.runners.context.HierarchicalContextRunner -import static org.hamcrest.Matchers.* @RunWith(HierarchicalContextRunner.class) class AnsiColorPluginTest { diff --git a/test/AwssumePluginTest.groovy b/test/AwssumePluginTest.groovy index 9728c33d..d1df256f 100644 --- a/test/AwssumePluginTest.groovy +++ b/test/AwssumePluginTest.groovy @@ -1,11 +1,17 @@ -import static org.junit.Assert.* +import static org.hamcrest.Matchers.containsString +import static org.hamcrest.Matchers.hasItem +import static org.hamcrest.Matchers.instanceOf +import static org.hamcrest.Matchers.is +import static org.hamcrest.Matchers.not +import static org.junit.Assert.assertThat +import static org.mockito.Mockito.when; +import static org.mockito.Mockito.mock; -import org.junit.* +import org.junit.After +import org.junit.Before +import org.junit.Test import org.junit.runner.RunWith import de.bechte.junit.runners.context.HierarchicalContextRunner -import static org.mockito.Mockito.when; -import static org.mockito.Mockito.mock; -import static org.hamcrest.Matchers.* @RunWith(HierarchicalContextRunner.class) class AwssumePluginTest { diff --git a/test/BuildGraphTest.groovy b/test/BuildGraphTest.groovy index 8391145b..e63bdeb3 100644 --- a/test/BuildGraphTest.groovy +++ b/test/BuildGraphTest.groovy @@ -1,11 +1,12 @@ -import static org.junit.Assert.* +import static org.mockito.Mockito.inOrder +import static org.mockito.Mockito.mock +import static org.mockito.Mockito.times +import static org.mockito.Mockito.verify -import org.junit.* +import org.junit.Test import org.junit.runner.RunWith import de.bechte.junit.runners.context.HierarchicalContextRunner import org.mockito.InOrder -import static org.mockito.Mockito.* -import static org.hamcrest.Matchers.* @RunWith(HierarchicalContextRunner.class) class BuildGraphTest { diff --git a/test/BuildStageTest.groovy b/test/BuildStageTest.groovy index 312a0993..72e19a17 100644 --- a/test/BuildStageTest.groovy +++ b/test/BuildStageTest.groovy @@ -1,12 +1,10 @@ -import org.junit.* -import org.junit.runner.RunWith -import de.bechte.junit.runners.context.HierarchicalContextRunner - -import static org.mockito.Mockito.verify -import static org.mockito.Mockito.when import static org.mockito.Mockito.spy import static org.mockito.Mockito.doReturn +import org.junit.Test +import org.junit.runner.RunWith +import de.bechte.junit.runners.context.HierarchicalContextRunner + @RunWith(HierarchicalContextRunner.class) class BuildStageTest { @@ -15,7 +13,7 @@ class BuildStageTest { void buildsWithoutError() { BuildStage stage = spy(new BuildStage()) - doReturn({ -> }).when(stage).pipelineConfiguration() + doReturn { -> }.when(stage).pipelineConfiguration() stage.build() } diff --git a/test/ConditionalApplyPluginTest.groovy b/test/ConditionalApplyPluginTest.groovy index cc946576..4348a44b 100644 --- a/test/ConditionalApplyPluginTest.groovy +++ b/test/ConditionalApplyPluginTest.groovy @@ -1,14 +1,16 @@ -import static org.junit.Assert.* +import static org.hamcrest.Matchers.hasItem +import static org.hamcrest.Matchers.instanceOf +import static org.junit.Assert.assertFalse +import static org.junit.Assert.assertThat +import static org.junit.Assert.assertTrue +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; -import org.junit.* +import org.junit.After +import org.junit.Test import org.junit.runner.RunWith import de.bechte.junit.runners.context.HierarchicalContextRunner -import static org.hamcrest.Matchers.* -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; -import static org.mockito.Matchers.* - @RunWith(HierarchicalContextRunner.class) class ConditionalApplyPluginTest { @After diff --git a/test/ConfirmApplyPluginTest.groovy b/test/ConfirmApplyPluginTest.groovy index 2239eec3..18b992d9 100644 --- a/test/ConfirmApplyPluginTest.groovy +++ b/test/ConfirmApplyPluginTest.groovy @@ -1,9 +1,13 @@ -import static org.junit.Assert.* - -import org.junit.* +import static org.hamcrest.Matchers.hasItem +import static org.hamcrest.Matchers.instanceOf +import static org.junit.Assert.assertFalse +import static org.junit.Assert.assertThat +import static org.junit.Assert.assertTrue + +import org.junit.After +import org.junit.Test import org.junit.runner.RunWith import de.bechte.junit.runners.context.HierarchicalContextRunner -import static org.hamcrest.Matchers.* @RunWith(HierarchicalContextRunner.class) class ConfirmApplyPluginTest { diff --git a/test/ConsulBackendPluginTest.groovy b/test/ConsulBackendPluginTest.groovy index 01ece859..665c77e5 100644 --- a/test/ConsulBackendPluginTest.groovy +++ b/test/ConsulBackendPluginTest.groovy @@ -1,11 +1,16 @@ -import static org.junit.Assert.* +import static org.hamcrest.Matchers.containsString +import static org.hamcrest.Matchers.hasItem +import static org.hamcrest.Matchers.instanceOf +import static org.hamcrest.Matchers.not +import static org.junit.Assert.assertThat +import static org.mockito.Mockito.when; +import static org.mockito.Mockito.mock; -import org.junit.* +import org.junit.After +import org.junit.Before +import org.junit.Test import org.junit.runner.RunWith import de.bechte.junit.runners.context.HierarchicalContextRunner -import static org.mockito.Mockito.when; -import static org.mockito.Mockito.mock; -import static org.hamcrest.Matchers.* @RunWith(HierarchicalContextRunner.class) class ConsulBackendPluginTest { diff --git a/test/CredentialsPluginTest.groovy b/test/CredentialsPluginTest.groovy index f15faae2..70075823 100644 --- a/test/CredentialsPluginTest.groovy +++ b/test/CredentialsPluginTest.groovy @@ -1,9 +1,15 @@ -import static org.junit.Assert.* - -import org.junit.* +import static org.hamcrest.Matchers.equalTo +import static org.hamcrest.Matchers.hasItem +import static org.hamcrest.Matchers.hasSize +import static org.hamcrest.Matchers.instanceOf +import static org.hamcrest.Matchers.is +import static org.hamcrest.Matchers.notNullValue +import static org.junit.Assert.assertThat + +import org.junit.After +import org.junit.Test import org.junit.runner.RunWith import de.bechte.junit.runners.context.HierarchicalContextRunner -import static org.hamcrest.Matchers.* @RunWith(HierarchicalContextRunner.class) class CredentialsPluginTest { diff --git a/test/CrqPluginTest.groovy b/test/CrqPluginTest.groovy index 3c16a15c..130ef716 100644 --- a/test/CrqPluginTest.groovy +++ b/test/CrqPluginTest.groovy @@ -1,14 +1,16 @@ -import static org.junit.Assert.* - -import org.junit.* +import static org.hamcrest.Matchers.hasItem +import static org.hamcrest.Matchers.instanceOf +import static org.hamcrest.Matchers.is +import static org.junit.Assert.assertThat +import static org.mockito.Mockito.mock +import static org.mockito.Mockito.spy +import static org.mockito.Mockito.verify +import static org.mockito.Mockito.when + +import org.junit.After +import org.junit.Test import org.junit.runner.RunWith import de.bechte.junit.runners.context.HierarchicalContextRunner -import static org.mockito.Mockito.when; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.mock; -import static org.mockito.Matchers.* -import static org.hamcrest.Matchers.* -import static org.mockito.Mockito.*; @RunWith(HierarchicalContextRunner.class) class CrqPluginTest { diff --git a/test/DefaultEnvironmentPluginTest.groovy b/test/DefaultEnvironmentPluginTest.groovy index 934963c7..d2390dd1 100644 --- a/test/DefaultEnvironmentPluginTest.groovy +++ b/test/DefaultEnvironmentPluginTest.groovy @@ -1,9 +1,10 @@ -import static org.junit.Assert.* +import static org.hamcrest.Matchers.hasItem +import static org.hamcrest.Matchers.instanceOf +import static org.junit.Assert.assertThat -import org.junit.* +import org.junit.Test import org.junit.runner.RunWith import de.bechte.junit.runners.context.HierarchicalContextRunner -import static org.hamcrest.Matchers.* @RunWith(HierarchicalContextRunner.class) class DefaultEnvironmentPluginTest { diff --git a/test/FileParametersPluginTest.groovy b/test/FileParametersPluginTest.groovy index 634b1a6e..903cc0f7 100644 --- a/test/FileParametersPluginTest.groovy +++ b/test/FileParametersPluginTest.groovy @@ -1,12 +1,14 @@ -import static org.junit.Assert.* +import static org.hamcrest.Matchers.hasItem +import static org.hamcrest.Matchers.instanceOf +import static org.junit.Assert.assertEquals +import static org.junit.Assert.assertThat +import static org.mockito.Mockito.when; +import static org.mockito.Mockito.spy; -import org.junit.* +import org.junit.After +import org.junit.Test import org.junit.runner.RunWith import de.bechte.junit.runners.context.HierarchicalContextRunner -import static org.mockito.Mockito.when; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.spy; -import static org.hamcrest.Matchers.* @RunWith(HierarchicalContextRunner.class) class FileParametersPluginTest { diff --git a/test/JenkinsfileTest.groovy b/test/JenkinsfileTest.groovy index f519998c..6f1ce005 100644 --- a/test/JenkinsfileTest.groovy +++ b/test/JenkinsfileTest.groovy @@ -1,14 +1,12 @@ -import static org.junit.Assert.* - -import org.junit.* -import org.junit.runner.RunWith -import de.bechte.junit.runners.context.HierarchicalContextRunner -import Jenkinsfile - +import static org.junit.Assert.assertEquals import static org.mockito.Mockito.mock import static org.mockito.Mockito.when -import static org.hamcrest.Matchers.* +import org.junit.After +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import de.bechte.junit.runners.context.HierarchicalContextRunner @RunWith(HierarchicalContextRunner.class) class JenkinsfileTest { diff --git a/test/ParameterStoreBuildWrapperPluginTest.groovy b/test/ParameterStoreBuildWrapperPluginTest.groovy index a05aade5..e75db74f 100644 --- a/test/ParameterStoreBuildWrapperPluginTest.groovy +++ b/test/ParameterStoreBuildWrapperPluginTest.groovy @@ -1,11 +1,14 @@ -import static org.junit.Assert.* +import static org.hamcrest.Matchers.hasItem +import static org.hamcrest.Matchers.instanceOf +import static org.junit.Assert.assertEquals +import static org.junit.Assert.assertThat +import static org.mockito.Mockito.mock +import static org.mockito.Mockito.when -import org.junit.* +import org.junit.After +import org.junit.Test import org.junit.runner.RunWith import de.bechte.junit.runners.context.HierarchicalContextRunner -import static org.mockito.Mockito.mock -import static org.mockito.Mockito.when -import static org.hamcrest.Matchers.* @RunWith(HierarchicalContextRunner.class) class ParameterStoreBuildWrapperPluginTest { diff --git a/test/ParameterStoreExecPluginTest.groovy b/test/ParameterStoreExecPluginTest.groovy index 50340477..e0476dab 100644 --- a/test/ParameterStoreExecPluginTest.groovy +++ b/test/ParameterStoreExecPluginTest.groovy @@ -1,11 +1,15 @@ -import static org.junit.Assert.* +import static org.hamcrest.Matchers.containsString +import static org.hamcrest.Matchers.hasItem +import static org.hamcrest.Matchers.instanceOf +import static org.junit.Assert.assertEquals +import static org.junit.Assert.assertThat +import static org.mockito.Mockito.when; +import static org.mockito.Mockito.mock; -import org.junit.* +import org.junit.After +import org.junit.Test import org.junit.runner.RunWith import de.bechte.junit.runners.context.HierarchicalContextRunner -import static org.mockito.Mockito.when; -import static org.mockito.Mockito.mock; -import static org.hamcrest.Matchers.* @RunWith(HierarchicalContextRunner.class) class ParameterStoreExecPluginTest { diff --git a/test/RegressionStageTest.groovy b/test/RegressionStageTest.groovy index 93144fda..fde4be77 100644 --- a/test/RegressionStageTest.groovy +++ b/test/RegressionStageTest.groovy @@ -1,11 +1,12 @@ -import org.junit.* -import org.junit.runner.RunWith -import de.bechte.junit.runners.context.HierarchicalContextRunner - import static org.mockito.Mockito.mock import static org.mockito.Mockito.verify import static org.mockito.Mockito.when +import org.junit.After +import org.junit.Test +import org.junit.runner.RunWith +import de.bechte.junit.runners.context.HierarchicalContextRunner + @RunWith(HierarchicalContextRunner.class) class RegressionStageTest { @@ -23,7 +24,7 @@ class RegressionStageTest { } @Test - void automationRepoSpecifiedSuccessfullyCallApply(){ + void automationRepoSpecifiedSuccessfullyCallApply() { RegressionStagePlugin fakePlugin = mock(RegressionStagePlugin.class) RegressionStage.addPlugin(fakePlugin) @@ -35,7 +36,7 @@ class RegressionStageTest { } @Test - void automationRepoAndAppRepoSpecifiedSuccessfullyCallApply(){ + void automationRepoAndAppRepoSpecifiedSuccessfullyCallApply() { RegressionStagePlugin fakePlugin = mock(RegressionStagePlugin.class) RegressionStage.addPlugin(fakePlugin) @@ -48,7 +49,7 @@ class RegressionStageTest { } @Test - void automationRepoAndAppRepoWithChangeDirectorySpecifiedSuccessfullyCallApply(){ + void automationRepoAndAppRepoWithChangeDirectorySpecifiedSuccessfullyCallApply() { RegressionStagePlugin fakePlugin = mock(RegressionStagePlugin.class) RegressionStage.addPlugin(fakePlugin) @@ -62,7 +63,7 @@ class RegressionStageTest { } @Test - void noAutomationRepoSpecifiedSuccessfullyCallApply(){ + void noAutomationRepoSpecifiedSuccessfullyCallApply() { RegressionStagePlugin fakePlugin = mock(RegressionStagePlugin.class) RegressionStage.addPlugin(fakePlugin) diff --git a/test/S3BackendPluginTest.groovy b/test/S3BackendPluginTest.groovy index e32ffcf9..5467c53d 100644 --- a/test/S3BackendPluginTest.groovy +++ b/test/S3BackendPluginTest.groovy @@ -1,12 +1,16 @@ -import static org.junit.Assert.* +import static org.hamcrest.Matchers.contains +import static org.hamcrest.Matchers.containsString +import static org.hamcrest.Matchers.instanceOf +import static org.hamcrest.Matchers.is +import static org.hamcrest.Matchers.not +import static org.junit.Assert.assertThat +import static org.mockito.Mockito.when; +import static org.mockito.Mockito.spy; -import org.junit.* +import org.junit.After +import org.junit.Test import org.junit.runner.RunWith import de.bechte.junit.runners.context.HierarchicalContextRunner -import static org.mockito.Mockito.when; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.spy; -import static org.hamcrest.Matchers.* @RunWith(HierarchicalContextRunner.class) class S3BackendPluginTest { diff --git a/test/SemanticVersionTest.groovy b/test/SemanticVersionTest.groovy index e0491c5c..5701fa7d 100644 --- a/test/SemanticVersionTest.groovy +++ b/test/SemanticVersionTest.groovy @@ -1,8 +1,8 @@ +import static org.junit.Assert.assertEquals + import org.junit.Test import org.junit.runner.RunWith import org.junit.runners.Parameterized -import static org.junit.Assert.* - @RunWith(Parameterized.class) class SemanticVersionTest { @@ -16,10 +16,14 @@ class SemanticVersionTest { @Test void sortsCorrectly() { - List unsorted = SORTED.clone().collect({v -> new SemanticVersion(v)}) - Collections.shuffle(unsorted) - def result = unsorted.sort().collect({sv -> sv.version}) - assertEquals(SORTED,result) + List versions = SORTED.clone().collect { v -> new SemanticVersion(v) } + // Randomize + Collections.shuffle(versions) + // Then sort + versions.sort() + + def result = versions.collect { sv -> sv.version } + assertEquals(SORTED, result) } static SORTED = [ @@ -30,22 +34,22 @@ class SemanticVersionTest { '0.1.0.2', '0.1.0.10', '0.1.0.23', - '0.2', - '0.2.0.3.1', + '0.2', + '0.2.0.3.1', '0.2.0.4', - '1.0RC1', - '1.0RC2', - '1.0', - '1.0.1.2', - '1.0.2', + '1.0RC1', + '1.0RC2', + '1.0', + '1.0.1.2', + '1.0.2', '1.2.2.3', - '1.2.3', - '1.5.2_04', - '1.5.2_05', - '1.5.2_10', + '1.2.3', + '1.5.2_04', + '1.5.2_05', + '1.5.2_10', '1.6.0_01', - '2.0', - '2.0.0_02', + '2.0', + '2.0.0_02', '3.1' ] } diff --git a/test/TerraformApplyCommandTest.groovy b/test/TerraformApplyCommandTest.groovy index b93bbc61..acafaff8 100644 --- a/test/TerraformApplyCommandTest.groovy +++ b/test/TerraformApplyCommandTest.groovy @@ -1,15 +1,17 @@ -import static org.junit.Assert.* - -import org.junit.* +import static org.hamcrest.Matchers.containsString +import static org.hamcrest.Matchers.endsWith +import static org.hamcrest.Matchers.not +import static org.hamcrest.Matchers.startsWith +import static org.junit.Assert.assertThat +import static org.mockito.Mockito.mock +import static org.mockito.Mockito.times +import static org.mockito.Mockito.verify + +import org.junit.After +import org.junit.Test import org.junit.runner.RunWith import de.bechte.junit.runners.context.HierarchicalContextRunner -import static org.hamcrest.Matchers.* -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.times; - - @RunWith(HierarchicalContextRunner.class) class TerraformApplyCommandTest { public class WithInput { diff --git a/test/TerraformDirectoryPluginTest.groovy b/test/TerraformDirectoryPluginTest.groovy index c858b044..d154c87e 100644 --- a/test/TerraformDirectoryPluginTest.groovy +++ b/test/TerraformDirectoryPluginTest.groovy @@ -1,11 +1,12 @@ -import static org.junit.Assert.* +import static org.hamcrest.Matchers.containsString +import static org.hamcrest.Matchers.hasItem +import static org.hamcrest.Matchers.instanceOf +import static org.junit.Assert.assertThat -import org.junit.* +import org.junit.After +import org.junit.Test import org.junit.runner.RunWith import de.bechte.junit.runners.context.HierarchicalContextRunner -import static org.mockito.Mockito.when; -import static org.mockito.Mockito.mock; -import static org.hamcrest.Matchers.* @RunWith(HierarchicalContextRunner.class) class TerraformDirectoryPluginTest { diff --git a/test/TerraformEnvironmentStageTest.groovy b/test/TerraformEnvironmentStageTest.groovy index 1167c612..6cf87685 100644 --- a/test/TerraformEnvironmentStageTest.groovy +++ b/test/TerraformEnvironmentStageTest.groovy @@ -1,13 +1,17 @@ -import static org.junit.Assert.* - -import org.junit.* +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify +import static org.hamcrest.Matchers.hasItem +import static org.hamcrest.Matchers.is +import static org.hamcrest.Matchers.isA +import static org.junit.Assert.assertEquals +import static org.junit.Assert.assertThat +import static org.junit.Assert.assertTrue + +import org.junit.After +import org.junit.Test import org.junit.runner.RunWith import de.bechte.junit.runners.context.HierarchicalContextRunner -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; -import static org.hamcrest.Matchers.* - @RunWith(HierarchicalContextRunner.class) class TerraformEnvironmentStageTest { @After @@ -60,7 +64,16 @@ class TerraformEnvironmentStageTest { @Test void doesNotAddPluginToOtherInstances() { + def modifiedStage = new TerraformEnvironmentStage('modified') + def unmodifiedStage = new TerraformEnvironmentStage('unmodified') + + def pluginsBefore = unmodifiedStage.getAllPlugins() + + modifiedStage.withEnv('somekey', 'somevalue') + + def pluginsAfter = unmodifiedStage.getAllPlugins() + assertEquals(pluginsBefore, pluginsAfter) } @Test diff --git a/test/TerraformInitCommandTest.groovy b/test/TerraformInitCommandTest.groovy index 4a54be3e..7d482c1c 100644 --- a/test/TerraformInitCommandTest.groovy +++ b/test/TerraformInitCommandTest.groovy @@ -1,14 +1,15 @@ -import static org.junit.Assert.* - -import org.junit.* -import org.junit.runner.RunWith -import de.bechte.junit.runners.context.HierarchicalContextRunner - -import static org.hamcrest.Matchers.* +import static org.hamcrest.Matchers.containsString +import static org.hamcrest.Matchers.endsWith +import static org.hamcrest.Matchers.not +import static org.junit.Assert.assertThat import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.times; +import org.junit.After +import org.junit.Test +import org.junit.runner.RunWith +import de.bechte.junit.runners.context.HierarchicalContextRunner @RunWith(HierarchicalContextRunner.class) class TerraformInitCommandTest { diff --git a/test/TerraformPlanCommandTest.groovy b/test/TerraformPlanCommandTest.groovy index 8531412e..33c29892 100644 --- a/test/TerraformPlanCommandTest.groovy +++ b/test/TerraformPlanCommandTest.groovy @@ -1,14 +1,16 @@ -import static org.junit.Assert.* - -import org.junit.* -import org.junit.runner.RunWith -import de.bechte.junit.runners.context.HierarchicalContextRunner - -import static org.hamcrest.Matchers.* +import static org.hamcrest.Matchers.containsString +import static org.hamcrest.Matchers.endsWith +import static org.hamcrest.Matchers.not +import static org.hamcrest.Matchers.startsWith +import static org.junit.Assert.assertThat import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.times; +import org.junit.After +import org.junit.Test +import org.junit.runner.RunWith +import de.bechte.junit.runners.context.HierarchicalContextRunner @RunWith(HierarchicalContextRunner.class) class TerraformPlanCommandTest { @@ -86,7 +88,6 @@ class TerraformPlanCommandTest { } } - public class Plugins { @After void resetPlugins() { diff --git a/test/TerraformPluginTest.groovy b/test/TerraformPluginTest.groovy index a5797ea4..30d44c07 100644 --- a/test/TerraformPluginTest.groovy +++ b/test/TerraformPluginTest.groovy @@ -1,15 +1,13 @@ -import org.junit.* -import org.junit.runner.RunWith -import de.bechte.junit.runners.context.HierarchicalContextRunner - -import static org.hamcrest.Matchers.* -import static org.junit.Assert.* -import static org.mockito.Mockito.mock; +import static org.hamcrest.Matchers.instanceOf +import static org.junit.Assert.assertEquals +import static org.junit.Assert.assertThat import static org.mockito.Mockito.spy -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; import static org.mockito.Mockito.doReturn; -import static org.mockito.Matchers.* + +import org.junit.After +import org.junit.Test +import org.junit.runner.RunWith +import de.bechte.junit.runners.context.HierarchicalContextRunner @RunWith(HierarchicalContextRunner.class) class TerraformPluginTest { diff --git a/test/TerraformPluginVersion11Test.groovy b/test/TerraformPluginVersion11Test.groovy index e63a983d..e87fb367 100644 --- a/test/TerraformPluginVersion11Test.groovy +++ b/test/TerraformPluginVersion11Test.groovy @@ -1,14 +1,9 @@ -import org.junit.* -import org.junit.runner.RunWith -import de.bechte.junit.runners.context.HierarchicalContextRunner - -import static org.hamcrest.Matchers.* -import static org.junit.Assert.* -import static org.mockito.Mockito.mock; import static org.mockito.Mockito.spy -import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; -import static org.mockito.Matchers.* + +import org.junit.Test +import org.junit.runner.RunWith +import de.bechte.junit.runners.context.HierarchicalContextRunner @RunWith(HierarchicalContextRunner.class) class TerraformPluginVersion11Test { diff --git a/test/TerraformPluginVersion12Test.groovy b/test/TerraformPluginVersion12Test.groovy index 807171c0..f01717ec 100644 --- a/test/TerraformPluginVersion12Test.groovy +++ b/test/TerraformPluginVersion12Test.groovy @@ -1,14 +1,13 @@ -import org.junit.* -import org.junit.runner.RunWith -import de.bechte.junit.runners.context.HierarchicalContextRunner - -import static org.hamcrest.Matchers.* -import static org.junit.Assert.* -import static org.mockito.Mockito.mock; +import static org.hamcrest.Matchers.containsString +import static org.junit.Assert.assertThat +import static org.mockito.Matchers.any +import static org.mockito.Matchers.eq import static org.mockito.Mockito.spy -import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; -import static org.mockito.Matchers.* + +import org.junit.Test +import org.junit.runner.RunWith +import de.bechte.junit.runners.context.HierarchicalContextRunner @RunWith(HierarchicalContextRunner.class) class TerraformPluginVersion12Test { diff --git a/test/TerraformValidateCommandTest.groovy b/test/TerraformValidateCommandTest.groovy index c888a8a1..89ee0301 100644 --- a/test/TerraformValidateCommandTest.groovy +++ b/test/TerraformValidateCommandTest.groovy @@ -1,14 +1,14 @@ -import static org.junit.Assert.* - -import org.junit.* -import org.junit.runner.RunWith -import de.bechte.junit.runners.context.HierarchicalContextRunner - -import static org.hamcrest.Matchers.* +import static org.junit.Assert.assertThat +import static org.hamcrest.Matchers.endsWith +import static org.hamcrest.Matchers.startsWith import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.times; +import org.junit.After +import org.junit.Test +import org.junit.runner.RunWith +import de.bechte.junit.runners.context.HierarchicalContextRunner @RunWith(HierarchicalContextRunner.class) class TerraformValidateCommandTest { diff --git a/test/TfvarsFilesPluginTest.groovy b/test/TfvarsFilesPluginTest.groovy index 838b7ab0..b11fca28 100644 --- a/test/TfvarsFilesPluginTest.groovy +++ b/test/TfvarsFilesPluginTest.groovy @@ -1,9 +1,3 @@ -import de.bechte.junit.runners.context.HierarchicalContextRunner -import org.junit.After -import org.junit.Before -import org.junit.Test -import org.junit.runner.RunWith - import static org.hamcrest.Matchers.containsString import static org.hamcrest.Matchers.equalTo import static org.hamcrest.Matchers.hasItem @@ -11,6 +5,11 @@ import static org.hamcrest.Matchers.instanceOf import static org.hamcrest.Matchers.not import static org.junit.Assert.assertThat +import de.bechte.junit.runners.context.HierarchicalContextRunner +import org.junit.After +import org.junit.Test +import org.junit.runner.RunWith + @RunWith(HierarchicalContextRunner.class) class TfvarsFilesPluginTest { @@ -44,7 +43,6 @@ class TfvarsFilesPluginTest { TfvarsFilesPlugin.directory = '.' } - class Init { @After void resetPlugins() { diff --git a/test/WithAwsPluginTest.groovy b/test/WithAwsPluginTest.groovy index 46edbb44..cb4b8f72 100644 --- a/test/WithAwsPluginTest.groovy +++ b/test/WithAwsPluginTest.groovy @@ -1,11 +1,15 @@ -import static org.junit.Assert.* +import static org.hamcrest.Matchers.hasItem +import static org.hamcrest.Matchers.instanceOf +import static org.hamcrest.Matchers.is +import static org.junit.Assert.assertThat +import static org.junit.Assert.assertTrue +import static org.mockito.Mockito.when; +import static org.mockito.Mockito.mock; -import org.junit.* +import org.junit.After +import org.junit.Test import org.junit.runner.RunWith import de.bechte.junit.runners.context.HierarchicalContextRunner -import static org.mockito.Mockito.when; -import static org.mockito.Mockito.mock; -import static org.hamcrest.Matchers.* @RunWith(HierarchicalContextRunner.class) class WithAwsPluginTest { @@ -140,4 +144,3 @@ class WithAwsPluginTest { } } - From ee269602c58610202f5d34d868588c7dc535ac9c Mon Sep 17 00:00:00 2001 From: Keith Manning Date: Wed, 5 Feb 2020 19:31:54 -0500 Subject: [PATCH 22/33] Fix broken terraform version detection --- src/TerraformPlugin.groovy | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/TerraformPlugin.groovy b/src/TerraformPlugin.groovy index 67efd27e..af2db8a1 100644 --- a/src/TerraformPlugin.groovy +++ b/src/TerraformPlugin.groovy @@ -35,7 +35,10 @@ class TerraformPlugin implements TerraformValidateCommandPlugin, TerraformValida } public TerraformPluginVersion strategyFor(String version) { - if (new SemanticVersion(version) >= new SemanticVersion('0.12.0')) { + // if (new SemanticVersion(version) >= new SemanticVersion('0.12.0')) should be used + // here. Unit tests pass with the above, but running Jenkinsfile in a pipeline context + // does not. Debug statements show that the above will return 0 when it should return 'true'. + if ((new SemanticVersion(version) <=> new SemanticVersion('0.12.0')) >= 0) { return new TerraformPluginVersion12() } From 0d13aa5bce45a1d191b83c36a3209eff19f1dae9 Mon Sep 17 00:00:00 2001 From: Keith Manning Date: Wed, 5 Feb 2020 23:57:16 -0500 Subject: [PATCH 23/33] Simplify terraform version detection --- src/TerraformPlugin.groovy | 16 ++++++++-------- test/TerraformPluginTest.groovy | 6 +++--- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/TerraformPlugin.groovy b/src/TerraformPlugin.groovy index af2db8a1..13661dbe 100644 --- a/src/TerraformPlugin.groovy +++ b/src/TerraformPlugin.groovy @@ -11,7 +11,7 @@ */ class TerraformPlugin implements TerraformValidateCommandPlugin, TerraformValidateStagePlugin { - static SemanticVersion version + static String version static final String DEFAULT_VERSION = '0.11.0' public static TERRAFORM_VERSION_FILE = '.terraform-version' @@ -22,12 +22,12 @@ class TerraformPlugin implements TerraformValidateCommandPlugin, TerraformValida TerraformValidateStage.addPlugin(plugin) } - public SemanticVersion detectVersion() { + public String detectVersion() { if (version == null) { if (fileExists(TERRAFORM_VERSION_FILE)) { - version = new SemanticVersion(readFile(TERRAFORM_VERSION_FILE)) + version = readFile(TERRAFORM_VERSION_FILE) } else { - version = new SemanticVersion(DEFAULT_VERSION) + version = DEFAULT_VERSION } } @@ -45,8 +45,8 @@ class TerraformPlugin implements TerraformValidateCommandPlugin, TerraformValida return new TerraformPluginVersion11() } - static void withVersion(String version) { - this.version = new SemanticVersion(version) + static void withVersion(String userVersion) { + this.version = userVersion } static void resetVersion() { @@ -65,7 +65,7 @@ class TerraformPlugin implements TerraformValidateCommandPlugin, TerraformValida void apply(TerraformValidateCommand command) { def version = detectVersion() - def strategy = strategyFor(version.version) + def strategy = strategyFor(version) strategy.apply(command) } @@ -78,7 +78,7 @@ class TerraformPlugin implements TerraformValidateCommandPlugin, TerraformValida return { closure -> def version = detectVersion() - def strategy = strategyFor(version.version) + def strategy = strategyFor(version) strategy.apply(validateStage) closure() diff --git a/test/TerraformPluginTest.groovy b/test/TerraformPluginTest.groovy index 30d44c07..96943c96 100644 --- a/test/TerraformPluginTest.groovy +++ b/test/TerraformPluginTest.groovy @@ -25,7 +25,7 @@ class TerraformPluginTest { def foundVersion = plugin.detectVersion() - assertEquals(TerraformPlugin.DEFAULT_VERSION, foundVersion.version) + assertEquals(TerraformPlugin.DEFAULT_VERSION, foundVersion) } @Test @@ -37,7 +37,7 @@ class TerraformPluginTest { def foundVersion = plugin.detectVersion() - assertEquals(expectedVersion, foundVersion.version) + assertEquals(expectedVersion, foundVersion) } } @@ -50,7 +50,7 @@ class TerraformPluginTest { @Test void usesVersionEvenIfFileExists() { TerraformPlugin.withVersion('2.0.0') - assertEquals('2.0.0', TerraformPlugin.version.version) + assertEquals('2.0.0', TerraformPlugin.version) } } From e348b2f678f2fb8af09d94be9b314366f410562f Mon Sep 17 00:00:00 2001 From: Keith Manning Date: Thu, 6 Feb 2020 00:09:54 -0500 Subject: [PATCH 24/33] Add convenience method to detect terraform version --- src/Jenkinsfile.groovy | 2 +- src/TerraformPlugin.groovy | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/Jenkinsfile.groovy b/src/Jenkinsfile.groovy index 67534773..95899361 100644 --- a/src/Jenkinsfile.groovy +++ b/src/Jenkinsfile.groovy @@ -88,7 +88,7 @@ class Jenkinsfile { return defaultNodeName ?: instance.getEnv().DEFAULT_NODE_NAME } - public static void build(Closure closure) { + public static build(Closure closure) { closure.delegate = this.instance closure.call() } diff --git a/src/TerraformPlugin.groovy b/src/TerraformPlugin.groovy index 13661dbe..987d9f9f 100644 --- a/src/TerraformPlugin.groovy +++ b/src/TerraformPlugin.groovy @@ -34,6 +34,25 @@ class TerraformPlugin implements TerraformValidateCommandPlugin, TerraformValida return version } + public static String checkVersion() { + return Jenkinsfile.build(pipelineConfiguration()) + } + + private static Closure pipelineConfiguration() { + def closure = { + node { + deleteDir() + checkout(scm) + def plugin = new TerraformPlugin() + + return plugin.detectVersion() + } + } + + closure.resolveStrategy = Closure.DELEGATE_ONLY + return closure + } + public TerraformPluginVersion strategyFor(String version) { // if (new SemanticVersion(version) >= new SemanticVersion('0.12.0')) should be used // here. Unit tests pass with the above, but running Jenkinsfile in a pipeline context From 95ebe11a6eff9f3d7382c5326884b8808d463538 Mon Sep 17 00:00:00 2001 From: Keith Manning Date: Thu, 6 Feb 2020 14:25:44 -0500 Subject: [PATCH 25/33] Update CHANGELOG --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9c0953a7..99879252 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ # Unreleased * [Issue #87](https://github.com/manheim/terraform-pipeline/issues/87) Add travis CI support to the project +* [Issue #186](https://github.com/manheim/terraform-pipeline/issues/186) Add convenience method to detect the version of terraform # v5.3 From 47763332e4abcdc7d125487f6ab6b0e72c40acd0 Mon Sep 17 00:00:00 2001 From: Keith Manning Date: Thu, 6 Feb 2020 16:36:56 -0500 Subject: [PATCH 26/33] Treat codecov as informational --- codecov.yml | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 codecov.yml diff --git a/codecov.yml b/codecov.yml new file mode 100644 index 00000000..482585a9 --- /dev/null +++ b/codecov.yml @@ -0,0 +1,2 @@ +codecov: + require_ci_to_pass: no From dca9b89703fa225f4b7d488ab6ae264422c3d32c Mon Sep 17 00:00:00 2001 From: Keith Manning Date: Tue, 11 Feb 2020 15:11:50 -0500 Subject: [PATCH 27/33] Create a failing test to detect the defect --- test/TerraformPluginTest.groovy | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/test/TerraformPluginTest.groovy b/test/TerraformPluginTest.groovy index 96943c96..34a34ec2 100644 --- a/test/TerraformPluginTest.groovy +++ b/test/TerraformPluginTest.groovy @@ -1,8 +1,8 @@ import static org.hamcrest.Matchers.instanceOf import static org.junit.Assert.assertEquals import static org.junit.Assert.assertThat -import static org.mockito.Mockito.spy import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.spy import org.junit.After import org.junit.Test @@ -88,4 +88,23 @@ class TerraformPluginTest { } } + + class ReadFile { + @Test + void returnsTheContentsOfTheGivenFile() { + def expectedFilename = 'someFilename' + def expectedContent = 'someContent' + def jenkinsOriginal = new Expando() + jenkinsOriginal.readFile = { String filename -> + assertEquals(expectedFilename, filename) + return expectedContent + } + def plugin = spy(new TerraformPlugin()) + doReturn(jenkinsOriginal).when(plugin).getJenkinsOriginal() + + def foundContent = plugin.readFile(expectedFilename) + + assertEquals(expectedContent, foundContent) + } + } } From 43c3521b2e9554f0e2508206bed7fff610596398 Mon Sep 17 00:00:00 2001 From: Keith Manning Date: Tue, 11 Feb 2020 15:12:03 -0500 Subject: [PATCH 28/33] Fix the failing defect --- src/TerraformPlugin.groovy | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/TerraformPlugin.groovy b/src/TerraformPlugin.groovy index 987d9f9f..315572bf 100644 --- a/src/TerraformPlugin.groovy +++ b/src/TerraformPlugin.groovy @@ -77,7 +77,11 @@ class TerraformPlugin implements TerraformValidateCommandPlugin, TerraformValida } public String readFile(String filename) { - return (Jenkinsfile.instance.original.readFile(TERRAFORM_VERSION_FILE) as String).trim() + return (getJenkinsOriginal().readFile(filename) as String).trim() + } + + public getJenkinsOriginal() { + return Jenkinsfile.instance.original } @Override From a7323cf7b3cefcabdc684390300e6e0601a18139 Mon Sep 17 00:00:00 2001 From: Keith Manning Date: Tue, 11 Feb 2020 15:17:30 -0500 Subject: [PATCH 29/33] Exercise trim() behavior --- src/TerraformPlugin.groovy | 3 ++- test/TerraformPluginTest.groovy | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/TerraformPlugin.groovy b/src/TerraformPlugin.groovy index 315572bf..9cc683f8 100644 --- a/src/TerraformPlugin.groovy +++ b/src/TerraformPlugin.groovy @@ -77,7 +77,8 @@ class TerraformPlugin implements TerraformValidateCommandPlugin, TerraformValida } public String readFile(String filename) { - return (getJenkinsOriginal().readFile(filename) as String).trim() + def content = (getJenkinsOriginal().readFile(filename) as String) + return content.trim() } public getJenkinsOriginal() { diff --git a/test/TerraformPluginTest.groovy b/test/TerraformPluginTest.groovy index 34a34ec2..1e512cd2 100644 --- a/test/TerraformPluginTest.groovy +++ b/test/TerraformPluginTest.groovy @@ -106,5 +106,22 @@ class TerraformPluginTest { assertEquals(expectedContent, foundContent) } + + @Test + void trimsWhitespaceFromTheFileContent() { + def expectedFilename = 'someFilename' + def expectedContent = 'someContent' + def jenkinsOriginal = new Expando() + jenkinsOriginal.readFile = { String filename -> + assertEquals(expectedFilename, filename) + return " ${expectedContent} " + } + def plugin = spy(new TerraformPlugin()) + doReturn(jenkinsOriginal).when(plugin).getJenkinsOriginal() + + def foundContent = plugin.readFile(expectedFilename) + + assertEquals(expectedContent, foundContent) + } } } From 4f048503cc8167c7158e5a0cfd98c2536c79b911 Mon Sep 17 00:00:00 2001 From: Keith Manning Date: Tue, 11 Feb 2020 16:18:59 -0500 Subject: [PATCH 30/33] Refactor: use getJenkinsOriginal() in readFile --- src/TerraformPlugin.groovy | 2 +- test/TerraformPluginTest.groovy | 36 +++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/src/TerraformPlugin.groovy b/src/TerraformPlugin.groovy index 9cc683f8..ad9915f5 100644 --- a/src/TerraformPlugin.groovy +++ b/src/TerraformPlugin.groovy @@ -73,7 +73,7 @@ class TerraformPlugin implements TerraformValidateCommandPlugin, TerraformValida } public boolean fileExists(String filename) { - return Jenkinsfile.instance.original.fileExists(filename) + return getJenkinsOriginal().fileExists(filename) } public String readFile(String filename) { diff --git a/test/TerraformPluginTest.groovy b/test/TerraformPluginTest.groovy index 1e512cd2..a7b2ebdc 100644 --- a/test/TerraformPluginTest.groovy +++ b/test/TerraformPluginTest.groovy @@ -1,6 +1,8 @@ import static org.hamcrest.Matchers.instanceOf import static org.junit.Assert.assertEquals +import static org.junit.Assert.assertFalse import static org.junit.Assert.assertThat +import static org.junit.Assert.assertTrue import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.spy @@ -124,4 +126,38 @@ class TerraformPluginTest { assertEquals(expectedContent, foundContent) } } + + class FileExists { + @Test + void returnsTrueIfFileExistsInWorkspace() { + def expectedFilename = 'someFile' + def jenkinsOriginal = new Expando() + jenkinsOriginal.fileExists = { String filename -> + assertEquals(expectedFilename, filename) + return true + } + def plugin = spy(new TerraformPlugin()) + doReturn(jenkinsOriginal).when(plugin).getJenkinsOriginal() + + def isFound = plugin.fileExists(expectedFilename) + + assertTrue(isFound) + } + + @Test + void returnsFalseIfFileDoesNotExistInWorkspace() { + def expectedFilename = 'someFile' + def jenkinsOriginal = new Expando() + jenkinsOriginal.fileExists = { String filename -> + assertEquals(expectedFilename, filename) + return false + } + def plugin = spy(new TerraformPlugin()) + doReturn(jenkinsOriginal).when(plugin).getJenkinsOriginal() + + def isFound = plugin.fileExists(expectedFilename) + + assertFalse(isFound) + } + } } From f755c3eff66e4a9f6be459b874d7cbf25945bab8 Mon Sep 17 00:00:00 2001 From: Keith Manning Date: Tue, 11 Feb 2020 16:27:05 -0500 Subject: [PATCH 31/33] Fix defect in TerraformPlugin.readFile --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 99879252..de46f801 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ * [Issue #87](https://github.com/manheim/terraform-pipeline/issues/87) Add travis CI support to the project * [Issue #186](https://github.com/manheim/terraform-pipeline/issues/186) Add convenience method to detect the version of terraform +* [Issue #185](https://github.com/manheim/terraform-pipeline/issues/185) Fix defect: filename parameter is not used in TerraformPlugin.readFile # v5.3 From 8fb70b5c025b578ae344f51c41c5546784c7948c Mon Sep 17 00:00:00 2001 From: Keith Manning Date: Tue, 11 Feb 2020 17:15:59 -0500 Subject: [PATCH 32/33] Increase code coverage while we're in here --- test/TerraformPluginTest.groovy | 65 +++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/test/TerraformPluginTest.groovy b/test/TerraformPluginTest.groovy index a7b2ebdc..81e25329 100644 --- a/test/TerraformPluginTest.groovy +++ b/test/TerraformPluginTest.groovy @@ -4,7 +4,10 @@ import static org.junit.Assert.assertFalse import static org.junit.Assert.assertThat import static org.junit.Assert.assertTrue import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.mock import static org.mockito.Mockito.spy +import static org.mockito.Mockito.times +import static org.mockito.Mockito.verify import org.junit.After import org.junit.Test @@ -160,4 +163,66 @@ class TerraformPluginTest { assertFalse(isFound) } } + + class ApplyTerraformValidateCommand { + @Test + void shouldApplyTheCorrectStrategyToTerraformValidateCommand() { + def validateCommand = mock(TerraformValidateCommand.class) + def strategy = mock(TerraformPluginVersion.class) + def plugin = spy(new TerraformPlugin()) + doReturn('someVersion').when(plugin).detectVersion() + doReturn(strategy).when(plugin).strategyFor('someVersion') + + plugin.apply(validateCommand) + + verify(strategy, times(1)).apply(validateCommand) + } + } + + class ApplyTerraformValidateStage { + @Test + void shouldDecorateTheGivenStage() { + def validateStage = mock(TerraformValidateStage.class) + def expectedDecoration = mock(Closure.class) + def plugin = spy(new TerraformPlugin()) + doReturn(expectedDecoration).when(plugin).modifyValidateStage(validateStage) + + plugin.apply(validateStage) + + verify(validateStage, times(1)).decorate(TerraformValidateStage.ALL, expectedDecoration) + } + } + + class ModifyTerraformValidateStage { + @Test + void shouldApplyTheCorrectStrategyToTerraformValidateStage() { + def expectedVersion = 'someVersion' + def validateStage = mock(TerraformValidateStage.class) + def strategy = mock(TerraformPluginVersion.class) + def plugin = spy(new TerraformPlugin()) + doReturn(expectedVersion).when(plugin).detectVersion() + doReturn(strategy).when(plugin).strategyFor(expectedVersion) + + def closure = plugin.modifyValidateStage(validateStage) + closure.call { -> } + + verify(strategy, times(1)).apply(validateStage) + } + + @Test + void shouldCallTheSubsequentClosureWhenDone() { + def version = 'someVersion' + def validateStage = mock(TerraformValidateStage.class) + def strategy = mock(TerraformPluginVersion.class) + def plugin = spy(new TerraformPlugin()) + doReturn(version).when(plugin).detectVersion() + doReturn(strategy).when(plugin).strategyFor(version) + def subsequentClosure = mock(Closure.class) + + def closure = plugin.modifyValidateStage(validateStage) + closure.call(subsequentClosure) + + verify(subsequentClosure, times(1)).call() + } + } } From c78eba73658fbc25e54caccee7beed18962cf742 Mon Sep 17 00:00:00 2001 From: kmanning Date: Tue, 11 Feb 2020 19:56:04 -0500 Subject: [PATCH 33/33] Update CHANGELOG to prepare for release --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index de46f801..e75d5515 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,7 @@ # Unreleased +# v5.4 + * [Issue #87](https://github.com/manheim/terraform-pipeline/issues/87) Add travis CI support to the project * [Issue #186](https://github.com/manheim/terraform-pipeline/issues/186) Add convenience method to detect the version of terraform * [Issue #185](https://github.com/manheim/terraform-pipeline/issues/185) Fix defect: filename parameter is not used in TerraformPlugin.readFile