Skip to content

Commit

Permalink
Improve error when Docker Compose file not found
Browse files Browse the repository at this point in the history
Fixes gh-35383
  • Loading branch information
scottfrederick committed May 10, 2023
1 parent f911b85 commit 8377306
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ public static DockerComposeFile find(File workingDirectory) {
*/
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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
* @author Moritz Halbritter
* @author Andy Wilkinson
* @author Phillip Webb
* @author Scott Frederick
* @see DockerComposeListener
*/
class DockerComposeLifecycleManager {
Expand Down Expand Up @@ -124,7 +125,12 @@ void start() {
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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

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;
Expand All @@ -42,6 +43,7 @@
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;
Expand All @@ -54,6 +56,7 @@
* @author Moritz Halbritter
* @author Andy Wilkinson
* @author Phillip Webb
* @author Scott Frederick
*/
class DockerComposeLifecycleManagerTests {

Expand Down Expand Up @@ -113,6 +116,24 @@ void startWhenEnabledFalseDoesNotStart() {
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);
Expand Down

0 comments on commit 8377306

Please sign in to comment.