diff --git a/src/main/java/se/bjurr/gitchangelog/api/GitChangelogApi.java b/src/main/java/se/bjurr/gitchangelog/api/GitChangelogApi.java index 81852830..197dfd58 100644 --- a/src/main/java/se/bjurr/gitchangelog/api/GitChangelogApi.java +++ b/src/main/java/se/bjurr/gitchangelog/api/GitChangelogApi.java @@ -172,7 +172,11 @@ public String render() throws GitChangelogRepositoryException { * @throws IOException When file cannot be written. */ public void toFile(final File file) throws GitChangelogRepositoryException, IOException { - file.mkdirs(); + final File parentFile = file.getParentFile(); + final boolean folderExists = parentFile.exists() || parentFile.mkdirs(); + if (!folderExists) { + throw new RuntimeException("Folder " + parentFile.getAbsolutePath() + " cannot be created"); + } Files.write(file.toPath(), this.render().getBytes(UTF_8)); } diff --git a/src/test/java/se/bjurr/gitchangelog/api/GitChangelogApiTest.java b/src/test/java/se/bjurr/gitchangelog/api/GitChangelogApiTest.java index eb30159b..0fa6c74a 100644 --- a/src/test/java/se/bjurr/gitchangelog/api/GitChangelogApiTest.java +++ b/src/test/java/se/bjurr/gitchangelog/api/GitChangelogApiTest.java @@ -8,6 +8,7 @@ import java.net.URL; import java.nio.file.Files; +import java.nio.file.Path; import java.nio.file.Paths; import java.util.HashMap; import java.util.Map; @@ -273,4 +274,19 @@ public void testThatRevertedCommitsAreRemoved() throws Exception { ApprovalsWrapper.verify(given); } + + @Test + public void testThatFileCanBeSupplied() throws Exception { + final String templatePath = "templatetest/testThatRevertedCommitsAreRemoved.mustache"; + + final Path path = Paths.get("build", "testdirtocreate", "testThatFileCanBeSupplied.md"); + gitChangelogApiBuilder() // + .withFromCommit("aa1fd33") // + .withToCommit("4c6e078") // + .withTemplatePath(templatePath) // + .toFile(path.toFile()); + + System.out.println(path.toFile().getAbsolutePath()); + assertThat(path.toFile()).exists().isFile(); + } }