Skip to content

Commit

Permalink
Merge pull request #188 from manheim/v5.4-dev-fix
Browse files Browse the repository at this point in the history
Issue 87 Part 3: Add codenarc and fix terraform-version comparison
  • Loading branch information
kmanning committed Feb 6, 2020
2 parents f4f81df + ee26960 commit 00d7cef
Show file tree
Hide file tree
Showing 44 changed files with 315 additions and 230 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
apply plugin: 'groovy'
apply plugin: 'jacoco'
apply plugin: 'codenarc'

repositories {
mavenCentral()
Expand Down
33 changes: 33 additions & 0 deletions config/codenarc/codenarc.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<!--
~ Copyright 2010 the original author or authors.
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<ruleset xmlns="http://codenarc.org/ruleset/1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://codenarc.org/ruleset/1.0 http://codenarc.org/ruleset-schema.xsd"
xsi:noNamespaceSchemaLocation="http://codenarc.org/ruleset-schema.xsd">
<ruleset-ref path='rulesets/groovyism.xml'>
<exclude name='GStringExpressionWithinString'/>
<exclude name='GetterMethodCouldBeProperty'/>
</ruleset-ref>
<ruleset-ref path='rulesets/basic.xml'>
<exclude name='EmptyClass'/>
</ruleset-ref>
<ruleset-ref path='rulesets/braces.xml'/>
<ruleset-ref path='rulesets/imports.xml'/>
<ruleset-ref path='rulesets/formatting.xml'>
<exclude name='ClassJavadoc'/>
<exclude name='LineLength'/>
<exclude name='SpaceAroundMapEntryColon'/>
</ruleset-ref>
</ruleset>
4 changes: 2 additions & 2 deletions src/AgentNodePlugin.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions src/FileParametersPlugin.groovy
Original file line number Diff line number Diff line change
@@ -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()
Expand Down
3 changes: 1 addition & 2 deletions src/Jenkinsfile.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class Jenkinsfile {

def Map parseScmUrl(String scmUrl) {
def matcher = scmUrl =~ /.*(?:\/\/|\@)[^\/:]+[\/:]([^\/]+)\/([^\/.]+)(.git)?/
def Map results = new HashMap<String,String>()
def Map results = [:]
results.put("organization", matcher[0][1])
results.put("repo", matcher[0][2])
return results
Expand Down Expand Up @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion src/RegressionStage.groovy
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class RegressionStage implements Stage {

public String testCommand
public List automationRepoList = new ArrayList<String>()
public List automationRepoList = []
private String testCommandDirectory

private Closure existingDecorations
Expand Down
7 changes: 3 additions & 4 deletions src/S3BackendPlugin.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Expand Down Expand Up @@ -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) {
Expand Down
17 changes: 7 additions & 10 deletions src/SemanticVersion.groovy
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import java.util.Comparator;

class SemanticVersion implements Comparable<SemanticVersion> {

private String versionString
Expand All @@ -20,26 +18,25 @@ class SemanticVersion implements Comparable<SemanticVersion> {

@Override
int compareTo(SemanticVersion other) {
for(i in 0..<Math.max(this.components.size(), other.components.size())) {

if(i == this.components.size()) {
for (i in 0..<Math.max(this.components.size(), other.components.size())) {
if (i == this.components.size()) {
return other.components[i].isInteger() ? -1 : 1 //1.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
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/TerraformApplyCommand.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
6 changes: 3 additions & 3 deletions src/TerraformEnvironmentStage.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class TerraformEnvironmentStage implements Stage {
TerraformEnvironmentStage(String environment) {
this.environment = environment
this.jenkinsfile = Jenkinsfile.instance
this.decorations = new HashMap<String,Closure>()
this.decorations = [:]
}

public Stage then(Stage nextStages) {
Expand Down Expand Up @@ -106,7 +106,7 @@ class TerraformEnvironmentStage implements Stage {
}
}

decorations.put(stageName,newDecoration)
decorations.put(stageName, newDecoration)
}

private void applyDecorationsAround(String stageName, Closure stageClosure) {
Expand All @@ -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)
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/TerraformInitCommand.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class TerraformInitCommand {
if (!input) {
pieces << "-input=false"
}
if(doBackend) {
if (doBackend) {
backendConfigs.each { config ->
pieces << "-backend-config=${config}"
}
Expand All @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion src/TerraformPlanCommand.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
7 changes: 5 additions & 2 deletions src/TerraformPlugin.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -35,7 +35,10 @@ 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')) 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()
}

Expand Down
5 changes: 2 additions & 3 deletions src/TerraformValidateCommand.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
}
Expand Down
6 changes: 3 additions & 3 deletions src/TerraformValidateStage.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class TerraformValidateStage implements Stage {

public TerraformValidateStage() {
this.jenkinsfile = Jenkinsfile.instance
this.decorations = new HashMap<String,Closure>()
this.decorations = [:]
}

public Stage then(Stage nextStage) {
Expand Down Expand Up @@ -60,7 +60,7 @@ class TerraformValidateStage implements Stage {
}
}

decorations.put(stageName,newDecoration)
decorations.put(stageName, newDecoration)
}

private void applyDecorationsAround(String stageName, Closure stageClosure) {
Expand All @@ -76,7 +76,7 @@ class TerraformValidateStage implements Stage {
}

public void applyPlugins() {
for(plugin in globalPlugins) {
for (plugin in globalPlugins) {
plugin.apply(this)
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/TfvarsFilesPlugin.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand All @@ -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."
Expand Down
8 changes: 5 additions & 3 deletions test/AnsiColorPluginTest.groovy
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
16 changes: 11 additions & 5 deletions test/AwssumePluginTest.groovy
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
9 changes: 5 additions & 4 deletions test/BuildGraphTest.groovy
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
12 changes: 5 additions & 7 deletions test/BuildStageTest.groovy
Original file line number Diff line number Diff line change
@@ -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 {

Expand All @@ -15,7 +13,7 @@ class BuildStageTest {
void buildsWithoutError() {
BuildStage stage = spy(new BuildStage())

doReturn({ -> }).when(stage).pipelineConfiguration()
doReturn { -> }.when(stage).pipelineConfiguration()

stage.build()
}
Expand Down
Loading

0 comments on commit 00d7cef

Please sign in to comment.