From 837730666853e7f0f453f86724ac49ad5e8e10be Mon Sep 17 00:00:00 2001 From: Scott Frederick Date: Wed, 10 May 2023 13:26:59 -0500 Subject: [PATCH] Improve error when Docker Compose file not found Fixes gh-35383 --- .../compose/core/DockerComposeFile.java | 4 ++-- .../DockerComposeLifecycleManager.java | 8 ++++++- .../DockerComposeLifecycleManagerTests.java | 21 +++++++++++++++++++ 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/spring-boot-project/spring-boot-docker-compose/src/main/java/org/springframework/boot/docker/compose/core/DockerComposeFile.java b/spring-boot-project/spring-boot-docker-compose/src/main/java/org/springframework/boot/docker/compose/core/DockerComposeFile.java index b47d35e86f..8c58e7fef4 100644 --- a/spring-boot-project/spring-boot-docker-compose/src/main/java/org/springframework/boot/docker/compose/core/DockerComposeFile.java +++ b/spring-boot-project/spring-boot-docker-compose/src/main/java/org/springframework/boot/docker/compose/core/DockerComposeFile.java @@ -109,8 +109,8 @@ public final class DockerComposeFile { */ public static DockerComposeFile of(File file) { Assert.notNull(file, "File must not be null"); - Assert.isTrue(file.exists(), () -> "'%s' does not exist".formatted(file)); - Assert.isTrue(file.isFile(), () -> "'%s' is not a file".formatted(file)); + Assert.isTrue(file.exists(), () -> "Docker Compose file '%s' does not exist".formatted(file)); + Assert.isTrue(file.isFile(), () -> "Docker compose file '%s' is not a file".formatted(file)); return new DockerComposeFile(file); } diff --git a/spring-boot-project/spring-boot-docker-compose/src/main/java/org/springframework/boot/docker/compose/lifecycle/DockerComposeLifecycleManager.java b/spring-boot-project/spring-boot-docker-compose/src/main/java/org/springframework/boot/docker/compose/lifecycle/DockerComposeLifecycleManager.java index 55ecf4aaa7..04c7874cd5 100644 --- a/spring-boot-project/spring-boot-docker-compose/src/main/java/org/springframework/boot/docker/compose/lifecycle/DockerComposeLifecycleManager.java +++ b/spring-boot-project/spring-boot-docker-compose/src/main/java/org/springframework/boot/docker/compose/lifecycle/DockerComposeLifecycleManager.java @@ -43,6 +43,7 @@ import org.springframework.core.log.LogMessage; * @author Moritz Halbritter * @author Andy Wilkinson * @author Phillip Webb + * @author Scott Frederick * @see DockerComposeListener */ class DockerComposeLifecycleManager { @@ -124,7 +125,12 @@ class DockerComposeLifecycleManager { protected DockerComposeFile getComposeFile() { DockerComposeFile composeFile = (this.properties.getFile() != null) ? DockerComposeFile.of(this.properties.getFile()) : DockerComposeFile.find(this.workingDirectory); - logger.info(LogMessage.format("Found Docker Compose file '%s'", composeFile)); + if (composeFile == null) { + File dir = (this.workingDirectory != null) ? this.workingDirectory : new File("."); + throw new IllegalStateException("No Docker Compose file found in directory '%s'" + .formatted(dir.toPath().toAbsolutePath().toString())); + } + logger.info(LogMessage.format("Using Docker Compose file '%s'", composeFile)); return composeFile; } diff --git a/spring-boot-project/spring-boot-docker-compose/src/test/java/org/springframework/boot/docker/compose/lifecycle/DockerComposeLifecycleManagerTests.java b/spring-boot-project/spring-boot-docker-compose/src/test/java/org/springframework/boot/docker/compose/lifecycle/DockerComposeLifecycleManagerTests.java index b6a786bb52..81434a0fb6 100644 --- a/spring-boot-project/spring-boot-docker-compose/src/test/java/org/springframework/boot/docker/compose/lifecycle/DockerComposeLifecycleManagerTests.java +++ b/spring-boot-project/spring-boot-docker-compose/src/test/java/org/springframework/boot/docker/compose/lifecycle/DockerComposeLifecycleManagerTests.java @@ -18,6 +18,7 @@ package org.springframework.boot.docker.compose.lifecycle; import java.io.File; import java.io.IOException; +import java.nio.file.Paths; import java.time.Duration; import java.util.ArrayList; import java.util.Collections; @@ -42,6 +43,7 @@ import org.springframework.context.support.GenericApplicationContext; import org.springframework.util.FileCopyUtils; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatIllegalStateException; import static org.mockito.ArgumentMatchers.any; import static org.mockito.BDDMockito.given; import static org.mockito.BDDMockito.then; @@ -54,6 +56,7 @@ import static org.mockito.Mockito.never; * @author Moritz Halbritter * @author Andy Wilkinson * @author Phillip Webb + * @author Scott Frederick */ class DockerComposeLifecycleManagerTests { @@ -113,6 +116,24 @@ class DockerComposeLifecycleManagerTests { then(this.dockerCompose).should(never()).hasDefinedServices(); } + @Test + void startWhenComposeFileNotFoundThrowsException() { + DockerComposeLifecycleManager manager = new DockerComposeLifecycleManager(new File("."), + this.applicationContext, null, this.shutdownHandlers, this.properties, this.eventListeners, + this.skipCheck, this.serviceReadinessChecks); + assertThatIllegalStateException().isThrownBy(manager::start) + .withMessageContaining(Paths.get(".").toAbsolutePath().toString()); + } + + @Test + void startWhenComposeFileNotFoundAndWorkingDirectoryNullThrowsException() { + DockerComposeLifecycleManager manager = new DockerComposeLifecycleManager(null, this.applicationContext, null, + this.shutdownHandlers, this.properties, this.eventListeners, this.skipCheck, + this.serviceReadinessChecks); + assertThatIllegalStateException().isThrownBy(manager::start) + .withMessageContaining(Paths.get(".").toAbsolutePath().toString()); + } + @Test void startWhenInTestDoesNotStart() { given(this.skipCheck.shouldSkip(any(), any())).willReturn(true);