|
26 | 26 |
|
27 | 27 | import joptsimple.OptionSet; |
28 | 28 | import org.apache.hc.core5.http.HttpHost; |
| 29 | +import org.assertj.core.api.SoftAssertionsProvider.ThrowingRunnable; |
29 | 30 | import org.jspecify.annotations.Nullable; |
30 | 31 | import org.junit.jupiter.api.Test; |
31 | 32 | import org.junit.jupiter.api.extension.ExtendWith; |
@@ -214,26 +215,30 @@ void generateProjectAndExtractUnknownContentType(@TempDir File tempDir) { |
214 | 215 |
|
215 | 216 | @Test |
216 | 217 | void fileNotOverwrittenByDefault(@TempDir File tempDir) throws Exception { |
217 | | - File file = new File(tempDir, "test.file"); |
218 | | - file.createNewFile(); |
219 | | - long fileLength = file.length(); |
220 | | - MockHttpProjectGenerationRequest request = new MockHttpProjectGenerationRequest("application/zip", |
221 | | - file.getAbsolutePath()); |
222 | | - mockSuccessfulProjectGeneration(request); |
223 | | - assertThat(this.command.run()).as("Should have failed").isEqualTo(ExitStatus.ERROR); |
224 | | - assertThat(file.length()).as("File should not have changed").isEqualTo(fileLength); |
| 218 | + withUserDir(tempDir, () -> { |
| 219 | + File file = new File(tempDir, "test.file"); |
| 220 | + file.createNewFile(); |
| 221 | + long fileLength = file.length(); |
| 222 | + MockHttpProjectGenerationRequest request = new MockHttpProjectGenerationRequest("application/zip", |
| 223 | + file.getAbsolutePath()); |
| 224 | + mockSuccessfulProjectGeneration(request); |
| 225 | + assertThat(this.command.run()).as("Should have failed").isEqualTo(ExitStatus.ERROR); |
| 226 | + assertThat(file.length()).as("File should not have changed").isEqualTo(fileLength); |
| 227 | + }); |
225 | 228 | } |
226 | 229 |
|
227 | 230 | @Test |
228 | 231 | void overwriteFile(@TempDir File tempDir) throws Exception { |
229 | | - File file = new File(tempDir, "test.file"); |
230 | | - file.createNewFile(); |
231 | | - long fileLength = file.length(); |
232 | | - MockHttpProjectGenerationRequest request = new MockHttpProjectGenerationRequest("application/zip", |
233 | | - file.getAbsolutePath()); |
234 | | - mockSuccessfulProjectGeneration(request); |
235 | | - assertThat(this.command.run("--force")).isEqualTo(ExitStatus.OK); |
236 | | - assertThat(fileLength).as("File should have changed").isNotEqualTo(file.length()); |
| 232 | + withUserDir(tempDir, () -> { |
| 233 | + File file = new File(tempDir, "test.file"); |
| 234 | + file.createNewFile(); |
| 235 | + long fileLength = file.length(); |
| 236 | + MockHttpProjectGenerationRequest request = new MockHttpProjectGenerationRequest("application/zip", |
| 237 | + file.getAbsolutePath()); |
| 238 | + mockSuccessfulProjectGeneration(request); |
| 239 | + assertThat(this.command.run("--force")).isEqualTo(ExitStatus.OK); |
| 240 | + assertThat(fileLength).as("File should have changed").isNotEqualTo(file.length()); |
| 241 | + }); |
237 | 242 | } |
238 | 243 |
|
239 | 244 | @Test |
@@ -408,6 +413,17 @@ void userAgent() throws Exception { |
408 | 413 | request.getHeaders("User-Agent")[0].getValue().startsWith("SpringBootCli/"))), isNull()); |
409 | 414 | } |
410 | 415 |
|
| 416 | + private void withUserDir(File userDir, ThrowingRunnable action) throws Exception { |
| 417 | + String previous = System.getProperty("user.dir"); |
| 418 | + System.setProperty("user.dir", userDir.getAbsolutePath()); |
| 419 | + try { |
| 420 | + action.run(); |
| 421 | + } |
| 422 | + finally { |
| 423 | + System.setProperty("user.dir", previous); |
| 424 | + } |
| 425 | + } |
| 426 | + |
411 | 427 | private byte[] createFakeZipArchive(String fileName, String content) throws IOException { |
412 | 428 | try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) { |
413 | 429 | try (ZipOutputStream zos = new ZipOutputStream(bos)) { |
|
0 commit comments