|
6 | 6 | import java.io.IOException; |
7 | 7 | import java.nio.file.Files; |
8 | 8 | import java.nio.file.Path; |
| 9 | +import java.util.ArrayList; |
| 10 | +import java.util.List; |
9 | 11 | import java.util.concurrent.TimeUnit; |
| 12 | +import java.util.stream.Stream; |
10 | 13 |
|
11 | 14 | import static org.junit.jupiter.api.Assertions.assertEquals; |
12 | 15 | import static org.junit.jupiter.api.Assertions.assertFalse; |
13 | 16 | import static org.junit.jupiter.api.Assertions.assertTrue; |
14 | 17 |
|
15 | 18 | /** |
16 | | - * Exercises the {@code generate} command from its fixed {@code sqlcj.yaml} |
17 | | - * entry point in an isolated working directory. |
| 19 | + * Exercises the {@code generate} command from its default {@code sqlcj.yaml} |
| 20 | + * entry point and from an explicit {@code --config} path, in an isolated |
| 21 | + * working directory. |
18 | 22 | */ |
19 | 23 | class GenerateCommandTest { |
20 | 24 |
|
@@ -69,6 +73,75 @@ void shouldGenerateFromConfigurationInWorkingDirectory() throws Exception { |
69 | 73 | assertTrue(source.contains("public GetUserResult getUser(Long id)")); |
70 | 74 | } |
71 | 75 |
|
| 76 | + @Test |
| 77 | + void shouldGenerateFromExplicitConfigurationPathInAnotherDirectory() throws Exception { |
| 78 | + Path project = Files.createDirectory(workingDirectory.resolve("project")); |
| 79 | + Path elsewhere = Files.createDirectory(workingDirectory.resolve("elsewhere")); |
| 80 | + |
| 81 | + Files.writeString(project.resolve("schema.sql"), SCHEMA); |
| 82 | + Files.writeString(project.resolve("queries.sql"), QUERIES); |
| 83 | + Files.writeString( |
| 84 | + project.resolve("sqlcj-users.yaml"), |
| 85 | + """ |
| 86 | + version: "1" |
| 87 | + sql: |
| 88 | + - name: Users |
| 89 | + schema: schema.sql |
| 90 | + queries: queries.sql |
| 91 | + java: |
| 92 | + package: dev.example.generated |
| 93 | + out: generated |
| 94 | + """ |
| 95 | + ); |
| 96 | + |
| 97 | + Result result = runGenerateIn(elsewhere, "--config", "../project/sqlcj-users.yaml"); |
| 98 | + |
| 99 | + assertEquals(0, result.exitCode(), result.error()); |
| 100 | + |
| 101 | + Path generated = project.resolve("generated/dev/example/generated/UsersRepository.java"); |
| 102 | + |
| 103 | + assertTrue(Files.exists(generated), result.error()); |
| 104 | + assertTrue( |
| 105 | + Files.readString(generated).contains("public GetUserResult getUser(Long id)") |
| 106 | + ); |
| 107 | + |
| 108 | + try (Stream<Path> entries = Files.list(elsewhere)) { |
| 109 | + assertEquals(List.of(), entries.toList()); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + @Test |
| 114 | + void shouldFailWithoutFallbackWhenExplicitConfigurationIsMissing() throws Exception { |
| 115 | + Files.writeString(workingDirectory.resolve("schema.sql"), SCHEMA); |
| 116 | + Files.writeString(workingDirectory.resolve("queries.sql"), QUERIES); |
| 117 | + Files.writeString( |
| 118 | + workingDirectory.resolve("sqlcj.yaml"), |
| 119 | + """ |
| 120 | + version: "1" |
| 121 | + sql: |
| 122 | + - name: Users |
| 123 | + schema: schema.sql |
| 124 | + queries: queries.sql |
| 125 | + java: |
| 126 | + package: dev.example.generated |
| 127 | + out: generated |
| 128 | + """ |
| 129 | + ); |
| 130 | + |
| 131 | + Result result = runGenerate("--config", "missing.yaml"); |
| 132 | + |
| 133 | + assertEquals(1, result.exitCode(), result.error()); |
| 134 | + assertTrue( |
| 135 | + result.error().contains( |
| 136 | + "sqlcj: Cannot read configuration file: " |
| 137 | + + workingDirectory.resolve("missing.yaml") |
| 138 | + ), |
| 139 | + result.error() |
| 140 | + ); |
| 141 | + assertFalse(result.error().contains("\tat ")); |
| 142 | + assertFalse(Files.exists(workingDirectory.resolve("generated"))); |
| 143 | + } |
| 144 | + |
72 | 145 | @Test |
73 | 146 | void shouldFailWithConciseDiagnosticForMalformedConfiguration() throws Exception { |
74 | 147 | Files.writeString( |
@@ -171,17 +244,28 @@ void shouldFailWithConciseDiagnosticForInvalidQuery() throws Exception { |
171 | 244 | assertFalse(Files.exists(workingDirectory.resolve("generated"))); |
172 | 245 | } |
173 | 246 |
|
174 | | - private Result runGenerate() throws IOException, InterruptedException { |
| 247 | + private Result runGenerate(String... arguments) throws IOException, InterruptedException { |
| 248 | + return runGenerateIn(workingDirectory, arguments); |
| 249 | + } |
| 250 | + |
| 251 | + private Result runGenerateIn(Path directory, String... arguments) |
| 252 | + throws IOException, InterruptedException { |
175 | 253 | Path java = Path.of(System.getProperty("java.home"), "bin", "java"); |
176 | 254 |
|
177 | | - Process process = new ProcessBuilder( |
178 | | - java.toString(), |
179 | | - "-classpath", |
180 | | - System.getProperty("java.class.path"), |
181 | | - "dev.sqlcj.Main", |
182 | | - "generate" |
183 | | - ) |
184 | | - .directory(workingDirectory.toFile()) |
| 255 | + List<String> command = new ArrayList<>( |
| 256 | + List.of( |
| 257 | + java.toString(), |
| 258 | + "-classpath", |
| 259 | + System.getProperty("java.class.path"), |
| 260 | + "dev.sqlcj.Main", |
| 261 | + "generate" |
| 262 | + ) |
| 263 | + ); |
| 264 | + |
| 265 | + command.addAll(List.of(arguments)); |
| 266 | + |
| 267 | + Process process = new ProcessBuilder(command) |
| 268 | + .directory(directory.toFile()) |
185 | 269 | .start(); |
186 | 270 |
|
187 | 271 | String output = new String(process.getInputStream().readAllBytes()); |
|
0 commit comments