Skip to content

Commit

Permalink
fix: when file was in a new folder, it created folder
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasbjerre committed Jun 9, 2021
1 parent 1aa3cbc commit 5a96f3b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/main/java/se/bjurr/gitchangelog/api/GitChangelogApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

Expand Down
16 changes: 16 additions & 0 deletions src/test/java/se/bjurr/gitchangelog/api/GitChangelogApiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
}

0 comments on commit 5a96f3b

Please # to comment.