@Test
public void testIgnoresTestSrcdirWhenJavaRunfilesIsUndefinedAndJustFails() throws Exception {

  Path dir =
      Files.createTempDirectory(
          FileSystems.getDefault().getPath(System.getenv("TEST_TMPDIR")), null);
  Runfiles.create(
      ImmutableMap.of(
          "RUNFILES_DIR", dir.toString(),
          "RUNFILES_MANIFEST_FILE", "ignored when RUNFILES_MANIFEST_ONLY is not set to 1",
          "TEST_SRCDIR", "should always be ignored"));
  Runfiles.create(
      ImmutableMap.of(
          "JAVA_RUNFILES", dir.toString(),
          "RUNFILES_MANIFEST_FILE", "ignored when RUNFILES_MANIFEST_ONLY is not set to 1",
          "TEST_SRCDIR", "should always be ignored"));
  try {
    // The method must ignore TEST_SRCDIR, for the scenario when Bazel runs a test which itself
    // runs Bazel to build and run java_binary. The java_binary should not pick up the test's
    // TEST_SRCDIR.
    Runfiles.create(
        ImmutableMap.of(
            "RUNFILES_DIR", "",
            "JAVA_RUNFILES", "",
            "RUNFILES_MANIFEST_FILE", "ignored when RUNFILES_MANIFEST_ONLY is not set to 1",
            "TEST_SRCDIR", "should always be ignored"));
    fail();
  } catch (IOException e) {
    assertThat(e).hasMessageThat().contains("$RUNFILES_DIR and $JAVA_RUNFILES");
  }
}