|
| 1 | +import static org.hamcrest.Matchers.containsString |
| 2 | +import static org.hamcrest.Matchers.not |
| 3 | +import static org.hamcrest.MatcherAssert.assertThat |
| 4 | +import static org.mockito.Mockito.mock |
| 5 | +import static org.mockito.Mockito.times |
| 6 | +import static org.mockito.Mockito.verify |
| 7 | + |
| 8 | +import org.junit.jupiter.api.Nested |
| 9 | +import org.junit.jupiter.api.Test |
| 10 | +import org.junit.jupiter.api.extension.ExtendWith |
| 11 | + |
| 12 | +@ExtendWith(ResetStaticStateExtension.class) |
| 13 | +class TerraformOutputCommandTest { |
| 14 | + @Nested |
| 15 | + public class WithJson { |
| 16 | + @Test |
| 17 | + void defaultsToFalse() { |
| 18 | + def command = new TerraformOutputCommand() |
| 19 | + |
| 20 | + def actualCommand = command.toString() |
| 21 | + assertThat(actualCommand, not(containsString("-json"))) |
| 22 | + } |
| 23 | + |
| 24 | + @Test |
| 25 | + void skipsJsonFlagWhenFalse() { |
| 26 | + def command = new TerraformOutputCommand().withJson(false) |
| 27 | + |
| 28 | + def actualCommand = command.toString() |
| 29 | + assertThat(actualCommand, not(containsString(" -json"))) |
| 30 | + } |
| 31 | + |
| 32 | + @Test |
| 33 | + void addsJsonFlagWhenTrue() { |
| 34 | + def command = new TerraformOutputCommand().withJson(true) |
| 35 | + |
| 36 | + def actualCommand = command.toString() |
| 37 | + assertThat(actualCommand, containsString(" -json")) |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + @Nested |
| 42 | + public class WithRedirectFile { |
| 43 | + @Test |
| 44 | + void defaultsToEmpty() { |
| 45 | + def command = new TerraformOutputCommand() |
| 46 | + |
| 47 | + def actualCommand = command.toString() |
| 48 | + assertThat(actualCommand, not(containsString(">"))) |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + void addsRedirectWhenSet() { |
| 53 | + def command = new TerraformOutputCommand().withRedirectFile("foo") |
| 54 | + |
| 55 | + def actualCommand = command.toString() |
| 56 | + assertThat(actualCommand, containsString(">foo")) |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + @Nested |
| 61 | + public class Plugins { |
| 62 | + @Test |
| 63 | + void areAppliedToTheCommand() { |
| 64 | + TerraformOutputCommandPlugin plugin = mock(TerraformOutputCommandPlugin.class) |
| 65 | + TerraformOutputCommand.addPlugin(plugin) |
| 66 | + |
| 67 | + TerraformOutputCommand command = TerraformOutputCommand.instanceFor("env") |
| 68 | + command.toString() |
| 69 | + |
| 70 | + verify(plugin).apply(command) |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + void areAppliedExactlyOnce() { |
| 75 | + TerraformOutputCommandPlugin plugin = mock(TerraformOutputCommandPlugin.class) |
| 76 | + TerraformOutputCommand.addPlugin(plugin) |
| 77 | + |
| 78 | + TerraformOutputCommand command = TerraformOutputCommand.instanceFor("env") |
| 79 | + |
| 80 | + String firstCommand = command.toString() |
| 81 | + String secondCommand = command.toString() |
| 82 | + |
| 83 | + verify(plugin, times(1)).apply(command) |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + void areAppliedEvenAfterCommandAlreadyInstantiated() { |
| 88 | + TerraformOutputCommandPlugin firstPlugin = mock(TerraformOutputCommandPlugin.class) |
| 89 | + TerraformOutputCommandPlugin secondPlugin = mock(TerraformOutputCommandPlugin.class) |
| 90 | + |
| 91 | + TerraformOutputCommand.addPlugin(firstPlugin) |
| 92 | + TerraformOutputCommand command = TerraformOutputCommand.instanceFor("env") |
| 93 | + |
| 94 | + TerraformOutputCommand.addPlugin(secondPlugin) |
| 95 | + |
| 96 | + command.toString() |
| 97 | + |
| 98 | + verify(secondPlugin, times(1)).apply(command) |
| 99 | + } |
| 100 | + } |
| 101 | +} |
0 commit comments