Skip to content

Commit 44308fe

Browse files
manuelFcushon
authored andcommitted
Keep going if a file cannot be read
and report a non-zero exit status, similar to handling of syntax errors. MOE_MIGRATED_REVID=222038590
1 parent b92129d commit 44308fe

File tree

3 files changed

+41
-6
lines changed

3 files changed

+41
-6
lines changed

core/src/main/java/com/google/googlejavaformat/java/Main.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ private int formatFiles(CommandLineOptions parameters, JavaFormatterOptions opti
111111

112112
Map<Path, String> inputs = new LinkedHashMap<>();
113113
Map<Path, Future<String>> results = new LinkedHashMap<>();
114+
boolean allOk = true;
115+
114116
for (String fileName : parameters.files()) {
115117
if (!fileName.endsWith(".java")) {
116118
errWriter.println("Skipping non-Java file: " + fileName);
@@ -120,15 +122,15 @@ private int formatFiles(CommandLineOptions parameters, JavaFormatterOptions opti
120122
String input;
121123
try {
122124
input = new String(Files.readAllBytes(path), UTF_8);
125+
inputs.put(path, input);
126+
results.put(
127+
path, executorService.submit(new FormatFileCallable(parameters, input, options)));
123128
} catch (IOException e) {
124129
errWriter.println(fileName + ": could not read file: " + e.getMessage());
125-
return 1;
130+
allOk = false;
126131
}
127-
inputs.put(path, input);
128-
results.put(path, executorService.submit(new FormatFileCallable(parameters, input, options)));
129132
}
130133

131-
boolean allOk = true;
132134
for (Map.Entry<Path, Future<String>> result : results.entrySet()) {
133135
Path path = result.getKey();
134136
String formatted;

core/src/test/java/com/google/googlejavaformat/java/FormatterTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ public void testFormatNonJavaFiles() throws Exception {
8383
assertThat(main.format("foo.go")).isEqualTo(0);
8484
assertThat(err.toString()).contains("Skipping non-Java file: " + "foo.go");
8585

86-
// should fail because the file does not exist
87-
assertThat(main.format("Foo.java")).isNotEqualTo(0);
86+
// format still fails on missing files
87+
assertThat(main.format("Foo.java")).isEqualTo(1);
8888
assertThat(err.toString()).contains("Foo.java: could not read file: ");
8989
}
9090

core/src/test/java/com/google/googlejavaformat/java/MainTest.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import com.google.common.io.ByteStreams;
2323
import java.io.BufferedWriter;
2424
import java.io.ByteArrayInputStream;
25+
import java.io.File;
2526
import java.io.InputStream;
2627
import java.io.OutputStreamWriter;
2728
import java.io.PrintWriter;
@@ -393,6 +394,38 @@ public void dryRunFiles() throws Exception {
393394
assertThat(err.toString()).isEmpty();
394395
}
395396

397+
@Test
398+
public void keepGoingWhenFilesDontExist() throws Exception {
399+
Path a = testFolder.newFile("A.java").toPath();
400+
Path b = testFolder.newFile("B.java").toPath();
401+
File cFile = testFolder.newFile("C.java");
402+
Path c = cFile.toPath();
403+
cFile.delete();
404+
405+
Files.write(a, "class A{}\n".getBytes(UTF_8));
406+
Files.write(b, "class B{}\n".getBytes(UTF_8));
407+
408+
StringWriter out = new StringWriter();
409+
StringWriter err = new StringWriter();
410+
Main main = new Main(new PrintWriter(out, true), new PrintWriter(err, true), System.in);
411+
412+
int exitCode =
413+
main.format(
414+
"",
415+
a.toAbsolutePath().toString(),
416+
c.toAbsolutePath().toString(),
417+
b.toAbsolutePath().toString());
418+
419+
// Formatter returns failure if a file was not present.
420+
assertThat(exitCode).isEqualTo(1);
421+
422+
// Present files were correctly formatted.
423+
assertThat(out.toString()).isEqualTo("class A {}\nclass B {}\n");
424+
425+
// File not found still showed error.
426+
assertThat(err.toString()).isNotEmpty();
427+
}
428+
396429
@Test
397430
public void exitIfChangedStdin() throws Exception {
398431
Path path = testFolder.newFile("Test.java").toPath();

0 commit comments

Comments
 (0)