Skip to content

Commit 47e27a7

Browse files
committed
[Core] Replace parentFile.makeDirs with Files.createDirectories(parentFile)
While `makeDirs` makes an attempt at creating the directories `createDirectories` will throw an exception when the directories could not be created or if the file exists but was not a directory. See: #2108
1 parent 703b6aa commit 47e27a7

File tree

4 files changed

+39
-15
lines changed

4 files changed

+39
-15
lines changed

compatibility/src/test/java/io/cucumber/compatibility/CompatibilityTest.java

+10-11
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,19 @@
1313
import org.junit.jupiter.params.ParameterizedTest;
1414
import org.junit.jupiter.params.provider.MethodSource;
1515

16-
import java.io.File;
17-
import java.io.FileOutputStream;
1816
import java.io.IOException;
1917
import java.io.InputStream;
2018
import java.nio.file.Files;
2119
import java.nio.file.Path;
20+
import java.nio.file.Paths;
2221
import java.util.ArrayList;
2322
import java.util.Comparator;
2423
import java.util.LinkedHashMap;
2524
import java.util.List;
2625
import java.util.Map;
2726
import java.util.stream.Collectors;
2827

28+
import static java.nio.file.Files.newOutputStream;
2929
import static org.hamcrest.CoreMatchers.is;
3030
import static org.hamcrest.MatcherAssert.assertThat;
3131
import static org.hamcrest.collection.IsIterableContainingInRelativeOrder.containsInRelativeOrder;
@@ -36,11 +36,10 @@ public class CompatibilityTest {
3636
@ParameterizedTest
3737
@MethodSource("io.cucumber.compatibility.TestCase#testCases")
3838
void produces_expected_output_for(TestCase testCase) throws IOException {
39-
File parentDir = new File("target/messages/" + testCase.getId());
40-
parentDir.mkdirs();
41-
File outputNdjson = new File(parentDir, "out.ndjson");
42-
File outputHtml = new File(parentDir, "out.html");
43-
File outputJson = new File(parentDir, "out.json");
39+
Path parentDir = Files.createDirectories(Paths.get("target", "messages", testCase.getId()));
40+
Path outputNdjson = parentDir.resolve("out.ndjson");
41+
Path outputHtml = parentDir.resolve("out.html");
42+
Path outputJson = parentDir.resolve("out.json");
4443

4544
try {
4645
Runtime.builder()
@@ -49,17 +48,17 @@ void produces_expected_output_for(TestCase testCase) throws IOException {
4948
.addFeature(testCase.getFeature())
5049
.build())
5150
.withAdditionalPlugins(
52-
new MessageFormatter(new FileOutputStream(outputNdjson)),
53-
new HtmlFormatter(new FileOutputStream(outputHtml)),
54-
new JsonFormatter(new FileOutputStream(outputJson)))
51+
new MessageFormatter(newOutputStream(outputNdjson)),
52+
new HtmlFormatter(newOutputStream(outputHtml)),
53+
new JsonFormatter(newOutputStream(outputJson)))
5554
.build()
5655
.run();
5756
} catch (Exception ignored) {
5857

5958
}
6059

6160
List<Messages.Envelope> expected = readAllMessages(testCase.getExpectedFile());
62-
List<Messages.Envelope> actual = readAllMessages(outputNdjson.toPath());
61+
List<Messages.Envelope> actual = readAllMessages(outputNdjson);
6362

6463
Map<String, List<GeneratedMessageV3>> expectedEnvelopes = openEnvelopes(expected);
6564
Map<String, List<GeneratedMessageV3>> actualEnvelopes = openEnvelopes(actual);

core/src/main/java/io/cucumber/core/plugin/PluginFactory.java

+24-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.net.URI;
1919
import java.net.URISyntaxException;
2020
import java.net.URL;
21+
import java.nio.file.Files;
2122
import java.util.Arrays;
2223
import java.util.HashMap;
2324
import java.util.Map;
@@ -199,11 +200,32 @@ private static OutputStream openStream(String arg) throws IOException {
199200
}
200201

201202
private static FileOutputStream createFileOutputStream(File file) {
203+
File canonicalFile;
202204
try {
203-
File parentFile = file.getParentFile();
205+
canonicalFile = file.getCanonicalFile();
206+
} catch (IOException e) {
207+
throw new IllegalArgumentException(String.format("" +
208+
"Couldn't get the canonical file of %s.\n" +
209+
"The details are in the stack trace below:",
210+
file),
211+
e);
212+
}
213+
214+
try {
215+
File parentFile = canonicalFile.getParentFile();
204216
if (parentFile != null) {
205-
parentFile.mkdirs();
217+
Files.createDirectories(parentFile.toPath());
206218
}
219+
} catch (IOException e) {
220+
throw new IllegalArgumentException(String.format("" +
221+
"Couldn't create parent directories of %s.\n" +
222+
"Make sure the the directory isn't a file.\n" +
223+
"The details are in the stack trace below:",
224+
file),
225+
e);
226+
}
227+
228+
try {
207229
return new FileOutputStream(file);
208230
} catch (FileNotFoundException e) {
209231
throw new IllegalArgumentException(String.format("" +

java/src/main/groovy/generate-annotations.groovy

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import groovy.text.SimpleTemplateEngine
22
import io.cucumber.gherkin.GherkinDialectProvider
33

4+
import java.nio.file.Files
45
import java.text.Normalizer
56

67
SimpleTemplateEngine engine = new SimpleTemplateEngine()
@@ -26,7 +27,7 @@ dialectProvider.getLanguages().each { language ->
2627
if (!file.exists()) {
2728
// Haitian has two translations that only differ by case - Sipozeke and SipozeKe
2829
// Some file systems are unable to distiguish between them and overwrite the other one :-(
29-
file.parentFile.mkdirs()
30+
Files.createDirectories(file.parentFile.toPath())
3031
file.write(template.toString(), "UTF-8")
3132
}
3233
}

java8/src/main/groovy/generate-interfaces.groovy

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import groovy.text.SimpleTemplateEngine
22
import io.cucumber.gherkin.GherkinDialectProvider
33

4+
import java.nio.file.Files
5+
46
SimpleTemplateEngine engine = new SimpleTemplateEngine()
57

68
def unsupported = ["em"] // The generated files for Emoij do not compile.
@@ -16,7 +18,7 @@ dialectProvider.getLanguages().each { language ->
1618
def binding = ["i18n": dialect, "className": className, "language_name": name]
1719
def template = engine.createTemplate(templateSource).make(binding)
1820
def file = new File(project.baseDir, "target/generated-sources/i18n/java/io/cucumber/java8/${className}.java")
19-
file.parentFile.mkdirs()
21+
Files.createDirectories(file.parentFile.toPath())
2022
file.write(template.toString(), "UTF-8")
2123
}
2224
}

0 commit comments

Comments
 (0)