|
| 1 | +package io.cucumber.messages.cli; |
| 2 | + |
| 3 | +import org.junit.jupiter.api.AfterEach; |
| 4 | +import org.junit.jupiter.api.BeforeEach; |
| 5 | +import org.junit.jupiter.api.Test; |
| 6 | +import org.junit.jupiter.api.io.TempDir; |
| 7 | +import picocli.CommandLine; |
| 8 | + |
| 9 | +import java.io.ByteArrayOutputStream; |
| 10 | +import java.io.IOException; |
| 11 | +import java.io.InputStream; |
| 12 | +import java.io.PrintStream; |
| 13 | +import java.io.PrintWriter; |
| 14 | +import java.io.StringWriter; |
| 15 | +import java.nio.file.Files; |
| 16 | +import java.nio.file.Path; |
| 17 | +import java.nio.file.Paths; |
| 18 | + |
| 19 | +import static java.nio.charset.StandardCharsets.UTF_8; |
| 20 | +import static java.nio.file.Files.newInputStream; |
| 21 | +import static java.nio.file.Files.readString; |
| 22 | +import static org.assertj.core.api.Assertions.assertThat; |
| 23 | +import static org.junit.jupiter.api.Assertions.assertAll; |
| 24 | + |
| 25 | +class TestngXmlCommandTest { |
| 26 | + |
| 27 | + static final Path minimalFeatureNdjson = Paths.get("../testdata/minimal.feature.ndjson"); |
| 28 | + static final Path minimalFeatureXml = Paths.get("../testdata/testng/minimal.feature.xml"); |
| 29 | + |
| 30 | + final ByteArrayOutputStream stdOut = new ByteArrayOutputStream(); |
| 31 | + final StringWriter stdErr = new StringWriter(); |
| 32 | + CommandLine cmd; |
| 33 | + InputStream originalSystemIn; |
| 34 | + PrintStream originalSystemOut; |
| 35 | + |
| 36 | + @TempDir |
| 37 | + Path tmp; |
| 38 | + |
| 39 | + @BeforeEach |
| 40 | + void setup() { |
| 41 | + cmd = MessagesCli.createCommandLine(); |
| 42 | + // TODO: Use mocking, but has wrong type. Ask pico CLI for mock with PrintStream. |
| 43 | + originalSystemIn = System.in; |
| 44 | + originalSystemOut = System.out; |
| 45 | + System.setOut(new PrintStream(stdOut)); |
| 46 | + cmd.setErr(new PrintWriter(stdErr)); |
| 47 | + } |
| 48 | + |
| 49 | + @AfterEach |
| 50 | + void cleanup() { |
| 51 | + System.setIn(originalSystemIn); |
| 52 | + System.setOut(originalSystemOut); |
| 53 | + // Helps with debugging |
| 54 | + System.out.println(stdOut.toString(UTF_8)); |
| 55 | + System.out.println(stdErr); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + void help() { |
| 60 | + int exitCode = cmd.execute("testng-xml", "--help"); |
| 61 | + assertThat(exitCode).isZero(); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + void writeToSystemOut() { |
| 66 | + var exitCode = cmd.execute("testng-xml", "../testdata/minimal.feature.ndjson"); |
| 67 | + assertAll( |
| 68 | + () -> assertThat(exitCode).isZero(), |
| 69 | + () -> assertThat(stdOut.toString()) |
| 70 | + .isEqualTo(readString(minimalFeatureXml)) |
| 71 | + ); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + void failsToReadNonExistingFile() { |
| 76 | + var exitCode = cmd.execute("testng-xml", "../testdata/no-such.feature.ndjson"); |
| 77 | + assertAll( |
| 78 | + () -> assertThat(exitCode).isEqualTo(2), |
| 79 | + () -> assertThat(stdErr.toString()) |
| 80 | + .contains("Invalid argument, could not read '../testdata/no-such.feature.ndjson'") |
| 81 | + ); |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + void readsFromSystemIn() throws IOException { |
| 86 | + System.setIn(newInputStream(minimalFeatureNdjson)); |
| 87 | + var exitCode = cmd.execute("testng-xml", "-"); |
| 88 | + assertAll( |
| 89 | + () -> assertThat(exitCode).isZero(), |
| 90 | + () -> assertThat(stdOut.toString()) |
| 91 | + .isEqualTo(readString(minimalFeatureXml)) |
| 92 | + ); |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + void writesToOutputFile() { |
| 97 | + var destination = tmp.resolve("minimal.feature.xml"); |
| 98 | + var exitCode = cmd.execute("testng-xml", "../testdata/minimal.feature.ndjson", "--output", destination.toString()); |
| 99 | + assertAll( |
| 100 | + () -> assertThat(exitCode).isZero(), |
| 101 | + () -> assertThat(readString(destination)) |
| 102 | + .isEqualTo(readString(minimalFeatureXml)) |
| 103 | + ); |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + void doesNotOverwriteWhenWritingToDirectory() { |
| 108 | + var exitCode1 = cmd.execute("testng-xml", "../testdata/minimal.feature.ndjson", "--output", tmp.toString()); |
| 109 | + var exitCode2 = cmd.execute("testng-xml", "../testdata/minimal.feature.ndjson", "--output", tmp.toString()); |
| 110 | + assertAll( |
| 111 | + () -> assertThat(exitCode1).isZero(), |
| 112 | + () -> assertThat(tmp.resolve("minimal.feature.xml")).exists(), |
| 113 | + () -> assertThat(exitCode2).isZero(), |
| 114 | + () -> assertThat(tmp.resolve("minimal.feature.1.xml")).exists() |
| 115 | + ); |
| 116 | + } |
| 117 | + |
| 118 | + @Test |
| 119 | + void failsToWriteToReadOnlyOutputFile() throws IOException { |
| 120 | + var destination = Files.createFile(tmp.resolve("minimal.feature.xml")); |
| 121 | + var isReadOnly = destination.toFile().setReadOnly(); |
| 122 | + assertThat(isReadOnly).isTrue(); |
| 123 | + |
| 124 | + var exitCode = cmd.execute("testng-xml", "../testdata/minimal.feature.ndjson", "--output", destination.toString()); |
| 125 | + assertAll( |
| 126 | + () -> assertThat(exitCode).isEqualTo(2), |
| 127 | + () -> assertThat(stdErr.toString()) |
| 128 | + .contains("Invalid value '%s' for option '--output': Could not write to '%s'" |
| 129 | + .formatted(destination, destination)) |
| 130 | + ); |
| 131 | + } |
| 132 | + |
| 133 | + @Test |
| 134 | + void writesFileToCurrentWorkingDirectory() throws IOException { |
| 135 | + var destination = Paths.get("minimal.feature.xml"); |
| 136 | + Files.deleteIfExists(destination); |
| 137 | + |
| 138 | + var exitCode = cmd.execute("testng-xml", "../testdata/minimal.feature.ndjson", "--output"); |
| 139 | + assertAll( |
| 140 | + () -> assertThat(exitCode).isZero(), |
| 141 | + () -> assertThat(readString(destination)) |
| 142 | + .isEqualTo(readString(minimalFeatureXml)) |
| 143 | + ); |
| 144 | + } |
| 145 | + |
| 146 | + @Test |
| 147 | + void canNotGuessFileNameWhenReadingFromSystemIn() throws IOException { |
| 148 | + System.setIn(newInputStream(minimalFeatureNdjson)); |
| 149 | + var exitCode = cmd.execute("testng-xml", "-", "--output"); |
| 150 | + assertAll( |
| 151 | + () -> assertThat(exitCode).isEqualTo(2), |
| 152 | + () -> assertThat(stdErr.toString()) |
| 153 | + .contains("Invalid value '' for option '--output': When reading from standard input, output can not be a directory") |
| 154 | + ); |
| 155 | + } |
| 156 | + |
| 157 | + @Test |
| 158 | + void useExampleNamingStrategy() { |
| 159 | + var exitCode = cmd.execute("testng-xml", "../testdata/examples-tables.feature.ndjson", "--example-naming-strategy", "NUMBER"); |
| 160 | + assertAll( |
| 161 | + () -> assertThat(exitCode).isZero(), |
| 162 | + () -> assertThat(stdOut.toString()) |
| 163 | + .contains("name=\"Eating cucumbers with <friends> friends - #1.1\"") |
| 164 | + ); |
| 165 | + } |
| 166 | + |
| 167 | +} |
0 commit comments