Make sure R8 fails at non-existing configuration files.

Bug: 130898520
Change-Id: Ieb0cb4762f1ab87340be6e4a17cbdea0b1543956
diff --git a/src/test/java/com/android/tools/r8/TestBase.java b/src/test/java/com/android/tools/r8/TestBase.java
index 87643f0..16ebddf 100644
--- a/src/test/java/com/android/tools/r8/TestBase.java
+++ b/src/test/java/com/android/tools/r8/TestBase.java
@@ -253,6 +253,10 @@
     return writeTextToTempFile(System.lineSeparator(), Arrays.asList(lines));
   }
 
+  protected void writeTextToTempFile(Path file, String... lines) throws IOException {
+    writeTextToTempFile(file, System.lineSeparator(), Arrays.asList(lines));
+  }
+
   /**
    * Write lines of text to a temporary file, along with the specified line separator.
    *
@@ -263,6 +267,11 @@
     return writeTextToTempFile(lineSeparator, lines, true);
   }
 
+  protected void writeTextToTempFile(Path file, String lineSeparator, List<String> lines)
+      throws IOException {
+    writeTextToTempFile(file, lineSeparator, lines, true);
+  }
+
   /**
    * Write lines of text to a temporary file, along with the specified line separator.
    *
@@ -273,12 +282,21 @@
       String lineSeparator, List<String> lines, boolean includeTerminatingLineSeparator)
       throws IOException {
     Path file = temp.newFile().toPath();
+    writeTextToTempFile(file, lineSeparator, lines, includeTerminatingLineSeparator);
+    return file;
+  }
+
+  protected void writeTextToTempFile(
+      Path file,
+      String lineSeparator,
+      List<String> lines,
+      boolean includeTerminatingLineSeparator)
+      throws IOException {
     String contents = String.join(lineSeparator, lines);
     if (includeTerminatingLineSeparator) {
       contents += lineSeparator;
     }
     Files.write(file, contents.getBytes(StandardCharsets.UTF_8));
-    return file;
   }
 
   /** Build an AndroidApp with the specified test classes as byte array. */
diff --git a/src/test/java/com/android/tools/r8/proguard/NonExistingConfigFileTest.java b/src/test/java/com/android/tools/r8/proguard/NonExistingConfigFileTest.java
new file mode 100644
index 0000000..7d0dc70
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/proguard/NonExistingConfigFileTest.java
@@ -0,0 +1,53 @@
+// Copyright (c) 2019, the R8 project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+package com.android.tools.r8.proguard;
+
+import static com.android.tools.r8.utils.codeinspector.Matchers.isPresent;
+import static com.android.tools.r8.utils.codeinspector.Matchers.isRenamed;
+import static org.hamcrest.CoreMatchers.containsString;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.Assert.fail;
+
+import com.android.tools.r8.CompilationFailedException;
+import com.android.tools.r8.TestBase;
+import com.android.tools.r8.utils.FileUtils;
+import com.android.tools.r8.utils.codeinspector.ClassSubject;
+import java.nio.file.Path;
+import org.junit.Test;
+
+public class NonExistingConfigFileTest extends TestBase {
+
+  @Test
+  public void testWithExistingFile() throws Exception {
+    Path pg = temp.getRoot().toPath().resolve("existing/directory/pg.txt");
+    pg.getParent().toFile().mkdirs();
+    FileUtils.writeTextFile(pg, "-keep,allowobfuscation class " + TestClass.class.getName());
+    testForR8(Backend.DEX)
+        .addInnerClasses(NonExistingConfigFileTest.class)
+        .addKeepRuleFiles(pg)
+        .compile()
+        .assertNoMessages()
+        .inspect(inspector -> {
+          ClassSubject clazz = inspector.clazz(TestClass.class);
+          assertThat(clazz, isPresent());
+          assertThat(clazz, isRenamed());
+        });
+  }
+
+  @Test
+  public void testWithNonExistingFile() throws Exception {
+    Path pg = temp.getRoot().toPath().resolve("not/a/directory/pg.txt");
+    try {
+      testForR8(Backend.DEX)
+          .addInnerClasses(NonExistingConfigFileTest.class)
+          .addKeepRuleFiles(pg)
+          .compile();
+      fail("Expect to fail");
+    } catch (CompilationFailedException e) {
+      assertThat(e.getCause().getMessage(), containsString("Failed to read file"));
+    }
+  }
+
+  static class TestClass {}
+}
\ No newline at end of file