Improve error when Docker Compose file not found

Fixes gh-35383
pull/35417/head
Scott Frederick 2 years ago
parent f911b85f64
commit 8377306668

@ -109,8 +109,8 @@ public final class DockerComposeFile {
*/ */
public static DockerComposeFile of(File file) { public static DockerComposeFile of(File file) {
Assert.notNull(file, "File must not be null"); Assert.notNull(file, "File must not be null");
Assert.isTrue(file.exists(), () -> "'%s' does not exist".formatted(file)); Assert.isTrue(file.exists(), () -> "Docker Compose file '%s' does not exist".formatted(file));
Assert.isTrue(file.isFile(), () -> "'%s' is not a file".formatted(file)); Assert.isTrue(file.isFile(), () -> "Docker compose file '%s' is not a file".formatted(file));
return new DockerComposeFile(file); return new DockerComposeFile(file);
} }

@ -43,6 +43,7 @@ import org.springframework.core.log.LogMessage;
* @author Moritz Halbritter * @author Moritz Halbritter
* @author Andy Wilkinson * @author Andy Wilkinson
* @author Phillip Webb * @author Phillip Webb
* @author Scott Frederick
* @see DockerComposeListener * @see DockerComposeListener
*/ */
class DockerComposeLifecycleManager { class DockerComposeLifecycleManager {
@ -124,7 +125,12 @@ class DockerComposeLifecycleManager {
protected DockerComposeFile getComposeFile() { protected DockerComposeFile getComposeFile() {
DockerComposeFile composeFile = (this.properties.getFile() != null) DockerComposeFile composeFile = (this.properties.getFile() != null)
? DockerComposeFile.of(this.properties.getFile()) : DockerComposeFile.find(this.workingDirectory); ? 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; return composeFile;
} }

@ -18,6 +18,7 @@ package org.springframework.boot.docker.compose.lifecycle;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Paths;
import java.time.Duration; import java.time.Duration;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
@ -42,6 +43,7 @@ import org.springframework.context.support.GenericApplicationContext;
import org.springframework.util.FileCopyUtils; import org.springframework.util.FileCopyUtils;
import static org.assertj.core.api.Assertions.assertThat; 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.ArgumentMatchers.any;
import static org.mockito.BDDMockito.given; import static org.mockito.BDDMockito.given;
import static org.mockito.BDDMockito.then; import static org.mockito.BDDMockito.then;
@ -54,6 +56,7 @@ import static org.mockito.Mockito.never;
* @author Moritz Halbritter * @author Moritz Halbritter
* @author Andy Wilkinson * @author Andy Wilkinson
* @author Phillip Webb * @author Phillip Webb
* @author Scott Frederick
*/ */
class DockerComposeLifecycleManagerTests { class DockerComposeLifecycleManagerTests {
@ -113,6 +116,24 @@ class DockerComposeLifecycleManagerTests {
then(this.dockerCompose).should(never()).hasDefinedServices(); 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 @Test
void startWhenInTestDoesNotStart() { void startWhenInTestDoesNotStart() {
given(this.skipCheck.shouldSkip(any(), any())).willReturn(true); given(this.skipCheck.shouldSkip(any(), any())).willReturn(true);

Loading…
Cancel
Save