diff --git a/cli/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/init/InitializrService.java b/cli/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/init/InitializrService.java index 26f89c90159..ee7b21891ed 100644 --- a/cli/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/init/InitializrService.java +++ b/cli/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/init/InitializrService.java @@ -16,6 +16,7 @@ package org.springframework.boot.cli.command.init; +import java.io.File; import java.io.IOException; import java.net.URI; import java.nio.charset.Charset; @@ -243,7 +244,7 @@ class InitializrService { value = value.substring(start + FILENAME_HEADER_PREFIX.length()); int end = value.indexOf('\"'); if (end != -1) { - return value.substring(0, end); + return new File(value.substring(0, end)).getName(); } } } diff --git a/cli/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/init/ProjectGenerator.java b/cli/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/init/ProjectGenerator.java index f23a2fdceab..559caebb431 100644 --- a/cli/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/init/ProjectGenerator.java +++ b/cli/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/init/ProjectGenerator.java @@ -137,7 +137,7 @@ class ProjectGenerator { } private void writeProject(ProjectGenerationResponse entity, String output, boolean overwrite) throws IOException { - File outputFile = new File(output); + File outputFile = new File(System.getProperty("user.dir"), output); if (outputFile.exists()) { if (!overwrite) { throw new ReportableException( diff --git a/cli/spring-boot-cli/src/test/java/org/springframework/boot/cli/command/init/InitCommandTests.java b/cli/spring-boot-cli/src/test/java/org/springframework/boot/cli/command/init/InitCommandTests.java index 9399ded93ec..53ad18de35f 100644 --- a/cli/spring-boot-cli/src/test/java/org/springframework/boot/cli/command/init/InitCommandTests.java +++ b/cli/spring-boot-cli/src/test/java/org/springframework/boot/cli/command/init/InitCommandTests.java @@ -26,6 +26,7 @@ import java.util.zip.ZipOutputStream; import joptsimple.OptionSet; import org.apache.hc.core5.http.HttpHost; +import org.assertj.core.api.SoftAssertionsProvider.ThrowingRunnable; import org.jspecify.annotations.Nullable; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -214,26 +215,30 @@ class InitCommandTests extends AbstractHttpClientMockTests { @Test void fileNotOverwrittenByDefault(@TempDir File tempDir) throws Exception { - File file = new File(tempDir, "test.file"); - file.createNewFile(); - long fileLength = file.length(); - MockHttpProjectGenerationRequest request = new MockHttpProjectGenerationRequest("application/zip", - file.getAbsolutePath()); - mockSuccessfulProjectGeneration(request); - assertThat(this.command.run()).as("Should have failed").isEqualTo(ExitStatus.ERROR); - assertThat(file.length()).as("File should not have changed").isEqualTo(fileLength); + withUserDir(tempDir, () -> { + File file = new File(tempDir, "test.file"); + file.createNewFile(); + long fileLength = file.length(); + MockHttpProjectGenerationRequest request = new MockHttpProjectGenerationRequest("application/zip", + file.getAbsolutePath()); + mockSuccessfulProjectGeneration(request); + assertThat(this.command.run()).as("Should have failed").isEqualTo(ExitStatus.ERROR); + assertThat(file.length()).as("File should not have changed").isEqualTo(fileLength); + }); } @Test void overwriteFile(@TempDir File tempDir) throws Exception { - File file = new File(tempDir, "test.file"); - file.createNewFile(); - long fileLength = file.length(); - MockHttpProjectGenerationRequest request = new MockHttpProjectGenerationRequest("application/zip", - file.getAbsolutePath()); - mockSuccessfulProjectGeneration(request); - assertThat(this.command.run("--force")).isEqualTo(ExitStatus.OK); - assertThat(fileLength).as("File should have changed").isNotEqualTo(file.length()); + withUserDir(tempDir, () -> { + File file = new File(tempDir, "test.file"); + file.createNewFile(); + long fileLength = file.length(); + MockHttpProjectGenerationRequest request = new MockHttpProjectGenerationRequest("application/zip", + file.getAbsolutePath()); + mockSuccessfulProjectGeneration(request); + assertThat(this.command.run("--force")).isEqualTo(ExitStatus.OK); + assertThat(fileLength).as("File should have changed").isNotEqualTo(file.length()); + }); } @Test @@ -408,6 +413,17 @@ class InitCommandTests extends AbstractHttpClientMockTests { request.getHeaders("User-Agent")[0].getValue().startsWith("SpringBootCli/"))), isNull()); } + private void withUserDir(File userDir, ThrowingRunnable action) throws Exception { + String previous = System.getProperty("user.dir"); + System.setProperty("user.dir", userDir.getAbsolutePath()); + try { + action.run(); + } + finally { + System.setProperty("user.dir", previous); + } + } + private byte[] createFakeZipArchive(String fileName, String content) throws IOException { try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) { try (ZipOutputStream zos = new ZipOutputStream(bos)) { diff --git a/cli/spring-boot-cli/src/test/java/org/springframework/boot/cli/command/init/InitializrServiceTests.java b/cli/spring-boot-cli/src/test/java/org/springframework/boot/cli/command/init/InitializrServiceTests.java index e5b4ea7e889..21673fbf923 100644 --- a/cli/spring-boot-cli/src/test/java/org/springframework/boot/cli/command/init/InitializrServiceTests.java +++ b/cli/spring-boot-cli/src/test/java/org/springframework/boot/cli/command/init/InitializrServiceTests.java @@ -50,9 +50,9 @@ class InitializrServiceTests extends AbstractHttpClientMockTests { void generateSimpleProject() throws Exception { ProjectGenerationRequest request = new ProjectGenerationRequest(); MockHttpProjectGenerationRequest mockHttpRequest = new MockHttpProjectGenerationRequest("application/xml", - "foo.zip"); + "nested/path/foo.zip"); ProjectGenerationResponse entity = generateProject(request, mockHttpRequest); - assertProjectEntity(entity, mockHttpRequest.contentType, mockHttpRequest.fileName); + assertProjectEntity(entity, mockHttpRequest.contentType, "foo.zip"); } @Test