From 5dc19d9563adb3c10a2ad1f30e8f404d5ca29ab9 Mon Sep 17 00:00:00 2001 From: Rohan Kumar Date: Fri, 17 Nov 2023 14:07:50 +0530 Subject: [PATCH] fix (jkube-kit-spring-boot) : Update create dir check in SpringBootDevtoolsUtils mkdirs() returns false when directory is already present, only throw exception when directory doesn't exist and mkdir returned false. Signed-off-by: Rohan Kumar --- .../springboot/SpringBootDevtoolsUtils.java | 10 ++++- .../SpringBootDevtoolsUtilsTest.java | 42 +++++++++++++------ 2 files changed, 38 insertions(+), 14 deletions(-) diff --git a/jkube-kit/jkube-kit-spring-boot/src/main/java/org/eclipse/jkube/springboot/SpringBootDevtoolsUtils.java b/jkube-kit/jkube-kit-spring-boot/src/main/java/org/eclipse/jkube/springboot/SpringBootDevtoolsUtils.java index 5a4e1b084c..ba46608fba 100644 --- a/jkube-kit/jkube-kit-spring-boot/src/main/java/org/eclipse/jkube/springboot/SpringBootDevtoolsUtils.java +++ b/jkube-kit/jkube-kit-spring-boot/src/main/java/org/eclipse/jkube/springboot/SpringBootDevtoolsUtils.java @@ -17,6 +17,7 @@ import org.apache.commons.io.FileUtils; import org.eclipse.jkube.generator.javaexec.FatJarDetector; import org.eclipse.jkube.kit.common.JavaProject; +import org.eclipse.jkube.kit.common.util.FileUtil; import org.eclipse.jkube.kit.common.util.JKubeProjectUtil; import org.eclipse.jkube.kit.common.util.SpringBootUtil; @@ -66,10 +67,15 @@ private static void addSecretTokenToApplicationProperties(JavaProject project) { private static void appendSecretTokenToFile(JavaProject project, String path, String token) { File file = new File(project.getBaseDirectory(), path); - boolean dirCreated = file.getParentFile().mkdirs(); - if (!dirCreated) { + try { + FileUtil.createDirectory(file.getParentFile()); + } catch (IOException ioException) { throw new IllegalStateException("Failure in creating directory " + file.getParentFile().getAbsolutePath()); } + writeRemoteSecretToFile(file, token); + } + + private static void writeRemoteSecretToFile(File file, String token) { String text = String.format("%s" + "# Remote secret added by jkube-kit-plugin\n" + "%s=%s\n", diff --git a/jkube-kit/jkube-kit-spring-boot/src/test/java/org/eclipse/jkube/springboot/SpringBootDevtoolsUtilsTest.java b/jkube-kit/jkube-kit-spring-boot/src/test/java/org/eclipse/jkube/springboot/SpringBootDevtoolsUtilsTest.java index 53fbb1eea4..2d526c4232 100644 --- a/jkube-kit/jkube-kit-spring-boot/src/test/java/org/eclipse/jkube/springboot/SpringBootDevtoolsUtilsTest.java +++ b/jkube-kit/jkube-kit-spring-boot/src/test/java/org/eclipse/jkube/springboot/SpringBootDevtoolsUtilsTest.java @@ -53,7 +53,7 @@ void setup() { } @Test - void ensureSpringDevToolSecretToken_whenNoTokenFound_thenAppendTokenAndThrowException() throws IOException { + void ensureSpringDevToolSecretToken_whenNoTokenFound_thenAppendToken() throws IOException { // Given JavaProject javaProject = createSpringBootJavaProject(); @@ -61,17 +61,21 @@ void ensureSpringDevToolSecretToken_whenNoTokenFound_thenAppendTokenAndThrowExce boolean result = SpringBootDevtoolsUtils.ensureSpringDevToolSecretToken(javaProject); // Then - assertThat(result).isFalse(); - Path srcApplicationProperties = temporaryFolder.toPath() - .resolve("src").resolve("main").resolve("resources").resolve("application.properties"); - Path targetApplicationProperties = temporaryFolder.toPath() - .resolve("target").resolve("classes").resolve("application.properties"); - assertThat(new String(Files.readAllBytes(srcApplicationProperties))) - .contains("# Remote secret added by jkube-kit-plugin") - .contains("spring.devtools.remote.secret="); - assertThat(new String(Files.readAllBytes(targetApplicationProperties))) - .contains("# Remote secret added by jkube-kit-plugin") - .contains("spring.devtools.remote.secret="); + thenSpringBootDevtoolsTokenAddedToProject(result); + } + + @Test + void ensureSpringDevToolSecretToken_whenNoTokenFoundButTargetDirAlreadyExists_thenAppendToken() throws IOException { + // Given + JavaProject javaProject = createSpringBootJavaProject(); + Files.createDirectory(temporaryFolder.toPath().resolve("target")); + Files.createDirectory(temporaryFolder.toPath().resolve("target").resolve("classes")); + + // When + boolean result = SpringBootDevtoolsUtils.ensureSpringDevToolSecretToken(javaProject); + + // Then + thenSpringBootDevtoolsTokenAddedToProject(result); } @Test @@ -199,4 +203,18 @@ private void createDummyJar(File jarFile) throws IOException { JarOutputStream jarOutputStream = new JarOutputStream(Files.newOutputStream(jarFile.toPath()), manifest); jarOutputStream.closeEntry(); } + + private void thenSpringBootDevtoolsTokenAddedToProject(boolean result) throws IOException { + assertThat(result).isFalse(); + Path srcApplicationProperties = temporaryFolder.toPath() + .resolve("src").resolve("main").resolve("resources").resolve("application.properties"); + Path targetApplicationProperties = temporaryFolder.toPath() + .resolve("target").resolve("classes").resolve("application.properties"); + assertThat(new String(Files.readAllBytes(srcApplicationProperties))) + .contains("# Remote secret added by jkube-kit-plugin") + .contains("spring.devtools.remote.secret="); + assertThat(new String(Files.readAllBytes(targetApplicationProperties))) + .contains("# Remote secret added by jkube-kit-plugin") + .contains("spring.devtools.remote.secret="); + } }