|
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.junit.jupiter.api.Test; |
30 | 31 | import org.junit.jupiter.api.extension.ExtendWith; |
31 | 32 | import org.junit.jupiter.api.io.TempDir; |
@@ -213,26 +214,30 @@ void generateProjectAndExtractUnknownContentType(@TempDir File tempDir) { |
213 | 214 |
|
214 | 215 | @Test |
215 | 216 | void fileNotOverwrittenByDefault(@TempDir File tempDir) throws Exception { |
216 | | - File file = new File(tempDir, "test.file"); |
217 | | - file.createNewFile(); |
218 | | - long fileLength = file.length(); |
219 | | - MockHttpProjectGenerationRequest request = new MockHttpProjectGenerationRequest("application/zip", |
220 | | - file.getAbsolutePath()); |
221 | | - mockSuccessfulProjectGeneration(request); |
222 | | - assertThat(this.command.run()).as("Should have failed").isEqualTo(ExitStatus.ERROR); |
223 | | - assertThat(file.length()).as("File should not have changed").isEqualTo(fileLength); |
| 217 | + withUserDir(tempDir, () -> { |
| 218 | + File file = new File(tempDir, "test.file"); |
| 219 | + file.createNewFile(); |
| 220 | + long fileLength = file.length(); |
| 221 | + MockHttpProjectGenerationRequest request = new MockHttpProjectGenerationRequest("application/zip", |
| 222 | + file.getAbsolutePath()); |
| 223 | + mockSuccessfulProjectGeneration(request); |
| 224 | + assertThat(this.command.run()).as("Should have failed").isEqualTo(ExitStatus.ERROR); |
| 225 | + assertThat(file.length()).as("File should not have changed").isEqualTo(fileLength); |
| 226 | + }); |
224 | 227 | } |
225 | 228 |
|
226 | 229 | @Test |
227 | 230 | void overwriteFile(@TempDir File tempDir) throws Exception { |
228 | | - File file = new File(tempDir, "test.file"); |
229 | | - file.createNewFile(); |
230 | | - long fileLength = file.length(); |
231 | | - MockHttpProjectGenerationRequest request = new MockHttpProjectGenerationRequest("application/zip", |
232 | | - file.getAbsolutePath()); |
233 | | - mockSuccessfulProjectGeneration(request); |
234 | | - assertThat(this.command.run("--force")).isEqualTo(ExitStatus.OK); |
235 | | - assertThat(fileLength).as("File should have changed").isNotEqualTo(file.length()); |
| 231 | + withUserDir(tempDir, () -> { |
| 232 | + File file = new File(tempDir, "test.file"); |
| 233 | + file.createNewFile(); |
| 234 | + long fileLength = file.length(); |
| 235 | + MockHttpProjectGenerationRequest request = new MockHttpProjectGenerationRequest("application/zip", |
| 236 | + file.getAbsolutePath()); |
| 237 | + mockSuccessfulProjectGeneration(request); |
| 238 | + assertThat(this.command.run("--force")).isEqualTo(ExitStatus.OK); |
| 239 | + assertThat(fileLength).as("File should have changed").isNotEqualTo(file.length()); |
| 240 | + }); |
236 | 241 | } |
237 | 242 |
|
238 | 243 | @Test |
@@ -399,6 +404,17 @@ void userAgent() throws Exception { |
399 | 404 | request.getHeaders("User-Agent")[0].getValue().startsWith("SpringBootCli/"))), isNull()); |
400 | 405 | } |
401 | 406 |
|
| 407 | + private void withUserDir(File userDir, ThrowingRunnable action) throws Exception { |
| 408 | + String previous = System.getProperty("user.dir"); |
| 409 | + System.setProperty("user.dir", userDir.getAbsolutePath()); |
| 410 | + try { |
| 411 | + action.run(); |
| 412 | + } |
| 413 | + finally { |
| 414 | + System.setProperty("user.dir", previous); |
| 415 | + } |
| 416 | + } |
| 417 | + |
402 | 418 | private byte[] createFakeZipArchive(String fileName, String content) throws IOException { |
403 | 419 | try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) { |
404 | 420 | try (ZipOutputStream zos = new ZipOutputStream(bos)) { |
|
0 commit comments