Skip to content

Commit

Permalink
Add tests for ValidateStage
Browse files Browse the repository at this point in the history
  • Loading branch information
kmanning committed Sep 13, 2020
1 parent 0415a7e commit fd67c20
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/TerraformValidateStage.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class TerraformValidateStage implements Stage, DecoratableStage {
Jenkinsfile.build(pipelineConfiguration())
}

private Closure pipelineConfiguration() {
public Closure pipelineConfiguration() {
applyPlugins()

def validateCommand = TerraformValidateCommand.instance()
Expand Down
2 changes: 1 addition & 1 deletion test/DummyJenkinsfile.groovy
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class DummyJenkinsfile {
public docker
public scm
public env
public env = [:]

public DummyJenkinsfile() {
docker = this
Expand Down
16 changes: 16 additions & 0 deletions test/TerraformPluginVersion12Test.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ 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.assertTrue
import static org.mockito.Matchers.any
import static org.mockito.Matchers.eq
import static org.mockito.Mockito.spy
Expand All @@ -21,6 +22,21 @@ class TerraformPluginVersion12Test {
TerraformFormatCommand.reset()
}

class AddInitBefore {
@Test
void runsTheInnerClosure() {
def plugin = new TerraformPluginVersion12()
def wasRun = false
def innerClosure = { wasRun = true }

def beforeClosure = plugin.addInitBefore()
beforeClosure.delegate = new DummyJenkinsfile()
beforeClosure(innerClosure)

assertTrue(wasRun)
}
}

class InitCommandForValidate {
@Test
void createsCommandWithNoBackend() {
Expand Down
39 changes: 39 additions & 0 deletions test/TerraformValidateStageTest.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import static org.mockito.Mockito.spy
import static org.hamcrest.Matchers.isA
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

@RunWith(HierarchicalContextRunner.class)
class TerraformValidateStageTest {
@After
void resetPlugins() {
TerraformValidateStage.resetPlugins()
}

public class PipelineConfiguration {
@Test
void returnsAJobDslClosure() {
def validateStage = new TerraformValidateStage()

def result = validateStage.pipelineConfiguration()

assertThat(result, isA(Closure.class))
}

// This should be split into separate tests, and assert behavior
@Test
void justExerciseClosureNoAssertions() {
Jenkinsfile.instance = spy(new Jenkinsfile())
Jenkinsfile.original = new DummyJenkinsfile()
def validateStage = new TerraformValidateStage()

def closure = validateStage.pipelineConfiguration()
closure.delegate = Jenkinsfile.original
closure()
}
}
}

0 comments on commit fd67c20

Please sign in to comment.