Improve error when Docker Compose file not found
Fixes gh-35383
This commit is contained in:
parent
f911b85f64
commit
8377306668
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Reference in New Issue