Skip to content

Commit 5286a5d

Browse files
authored
Fix FileManifest::writeUsing (#2602)
#2599 introduced FileManifest::writeUsing, which does not return the resolved Path being written to, so its usage in FileManifest::writeJson resulted in the relative Path being returned from that method, instead of the resolved Path like before. I updated writeUsing to return the resolved Path, and updated some tests.
1 parent ff09e3d commit 5286a5d

File tree

3 files changed

+30
-12
lines changed

3 files changed

+30
-12
lines changed

smithy-build/src/main/java/software/amazon/smithy/build/FileManifest.java

+8-6
Original file line numberDiff line numberDiff line change
@@ -215,8 +215,7 @@ default Path writeFile(String path, InputStream fileContentsInputStream) {
215215
*/
216216
@SuppressWarnings("unused")
217217
default Path writeJson(Path path, Node node) {
218-
writeUsing(path, (writer) -> Node.prettyPrintJsonToWriter(node, writer));
219-
return path;
218+
return writeUsing(path, (writer) -> Node.prettyPrintJsonToWriter(node, writer));
220219
}
221220

222221
/**
@@ -236,16 +235,19 @@ default Path writeJson(String path, Node node) {
236235
*
237236
* @param path Relative path to write to.
238237
* @param consumer Node data to write to JSON.
238+
* @return Returns the resolved path.
239239
*/
240-
default void writeUsing(Path path, Consumer<Writer> consumer) {
241-
path = addFile(path);
240+
default Path writeUsing(Path path, Consumer<Writer> consumer) {
241+
Path resolved = addFile(path);
242242

243-
try (Writer writer = Files.newBufferedWriter(path)) {
243+
try (Writer writer = Files.newBufferedWriter(resolved)) {
244244
consumer.accept(writer);
245245
writer.write('\n');
246246
} catch (IOException e) {
247-
throw new SmithyBuildException("Unable to create a write to file `" + path + "`: " + e.getMessage(), e);
247+
throw new SmithyBuildException("Unable to create a write to file `" + resolved + "`: " + e.getMessage(), e);
248248
}
249+
250+
return resolved;
249251
}
250252

251253
/**

smithy-build/src/main/java/software/amazon/smithy/build/MockManifest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,10 @@ public Path writeFile(Path path, InputStream fileContentsInputStream) {
132132
}
133133

134134
@Override
135-
public void writeUsing(Path path, Consumer<Writer> consumer) {
135+
public Path writeUsing(Path path, Consumer<Writer> consumer) {
136136
Writer writer = new StringWriter();
137137
consumer.accept(writer);
138-
writeFile(path, writer.toString());
138+
return writeFile(path, writer.toString());
139139
}
140140

141141
/**

smithy-build/src/test/java/software/amazon/smithy/build/FileManifestTest.java

+20-4
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,11 @@ public void mergesRelativeWithBasePath() throws IOException {
7373
@Test
7474
public void writesJsonFiles() throws IOException {
7575
FileManifest a = FileManifest.create(outputDirectory);
76-
a.writeJson("foo/file.json", Node.objectNode());
76+
Path resolved = a.writeJson("foo/file.json", Node.objectNode());
7777

78-
assertThat(Files.isDirectory(outputDirectory.resolve("foo")), is(true));
79-
assertThat(Files.isRegularFile(outputDirectory.resolve("foo/file.json")), is(true));
80-
assertThat(new String(Files.readAllBytes(outputDirectory.resolve("foo/file.json"))), equalTo("{}\n"));
78+
assertThat(resolved, equalTo(outputDirectory.resolve("foo/file.json")));
79+
assertThat(Files.isRegularFile(resolved), is(true));
80+
assertThat(new String(Files.readAllBytes(resolved)), equalTo("{}\n"));
8181
}
8282

8383
@Test
@@ -107,4 +107,20 @@ public void writesClassResources() {
107107

108108
assertThat(Files.isRegularFile(outputDirectory.resolve("test.txt")), is(true));
109109
}
110+
111+
@Test
112+
public void writesWithWriter() throws IOException {
113+
FileManifest a = FileManifest.create(outputDirectory);
114+
Path resolved = a.writeUsing(Paths.get("foo/bar.txt"), (w) -> {
115+
try {
116+
w.write("foo");
117+
} catch (IOException e) {
118+
throw new RuntimeException(e);
119+
}
120+
});
121+
122+
assertThat(resolved, equalTo(outputDirectory.resolve("foo/bar.txt")));
123+
assertThat(Files.isRegularFile(resolved), is(true));
124+
assertThat(new String(Files.readAllBytes(resolved)).trim(), equalTo("foo"));
125+
}
110126
}

0 commit comments

Comments
 (0)