Skip to content

Commit 0d5f6b8

Browse files
committed
Merge branch '3.5.x' into 4.0.x
Closes gh-50089
2 parents a96de88 + 662fef2 commit 0d5f6b8

4 files changed

Lines changed: 37 additions & 20 deletions

File tree

cli/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/init/InitializrService.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.boot.cli.command.init;
1818

19+
import java.io.File;
1920
import java.io.IOException;
2021
import java.net.URI;
2122
import java.nio.charset.Charset;
@@ -243,7 +244,7 @@ private String getContent(HttpEntity entity) throws IOException {
243244
value = value.substring(start + FILENAME_HEADER_PREFIX.length());
244245
int end = value.indexOf('\"');
245246
if (end != -1) {
246-
return value.substring(0, end);
247+
return new File(value.substring(0, end)).getName();
247248
}
248249
}
249250
}

cli/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/init/ProjectGenerator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ private void extractFromStream(ZipInputStream zipStream, boolean overwrite, File
137137
}
138138

139139
private void writeProject(ProjectGenerationResponse entity, String output, boolean overwrite) throws IOException {
140-
File outputFile = new File(output);
140+
File outputFile = new File(System.getProperty("user.dir"), output);
141141
if (outputFile.exists()) {
142142
if (!overwrite) {
143143
throw new ReportableException(

cli/spring-boot-cli/src/test/java/org/springframework/boot/cli/command/init/InitCommandTests.java

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
import joptsimple.OptionSet;
2828
import org.apache.hc.core5.http.HttpHost;
29+
import org.assertj.core.api.SoftAssertionsProvider.ThrowingRunnable;
2930
import org.jspecify.annotations.Nullable;
3031
import org.junit.jupiter.api.Test;
3132
import org.junit.jupiter.api.extension.ExtendWith;
@@ -214,26 +215,30 @@ void generateProjectAndExtractUnknownContentType(@TempDir File tempDir) {
214215

215216
@Test
216217
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+
});
225228
}
226229

227230
@Test
228231
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+
});
237242
}
238243

239244
@Test
@@ -408,6 +413,17 @@ void userAgent() throws Exception {
408413
request.getHeaders("User-Agent")[0].getValue().startsWith("SpringBootCli/"))), isNull());
409414
}
410415

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+
411427
private byte[] createFakeZipArchive(String fileName, String content) throws IOException {
412428
try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
413429
try (ZipOutputStream zos = new ZipOutputStream(bos)) {

cli/spring-boot-cli/src/test/java/org/springframework/boot/cli/command/init/InitializrServiceTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ void loadMetadata() throws Exception {
5050
void generateSimpleProject() throws Exception {
5151
ProjectGenerationRequest request = new ProjectGenerationRequest();
5252
MockHttpProjectGenerationRequest mockHttpRequest = new MockHttpProjectGenerationRequest("application/xml",
53-
"foo.zip");
53+
"nested/path/foo.zip");
5454
ProjectGenerationResponse entity = generateProject(request, mockHttpRequest);
55-
assertProjectEntity(entity, mockHttpRequest.contentType, mockHttpRequest.fileName);
55+
assertProjectEntity(entity, mockHttpRequest.contentType, "foo.zip");
5656
}
5757

5858
@Test

0 commit comments

Comments
 (0)