java,runfiles: fix tests with TEST_SRCDIR

Update //src/test/py/bazel:runfiles_test to test
that the Java (and Python) runfiles libraries do
NOT pick up TEST_SRCDIR from their environment.

See https://bazel-review.googlesource.com/c/bazel/+/37190
See https://github.com/bazelbuild/bazel/issues/4598

Change-Id: I06eb50c8cb4c93a331e51cd38ebdd7c1bcf38bba
PiperOrigin-RevId: 184994372
diff --git a/src/test/py/bazel/runfiles_test.py b/src/test/py/bazel/runfiles_test.py
index ca3cc0b..abe23d0 100644
--- a/src/test/py/bazel/runfiles_test.py
+++ b/src/test/py/bazel/runfiles_test.py
@@ -56,12 +56,14 @@
 
     self.assertTrue(os.path.exists(bin_path))
 
-    exit_code, stdout, stderr = self.RunProgram([bin_path])
+    exit_code, stdout, stderr = self.RunProgram(
+        [bin_path], env_add={"TEST_SRCDIR": "__ignore_me__"})
     self.AssertExitCode(exit_code, 0, stderr)
     if len(stdout) != 2:
       self.fail("stdout: %s" % stdout)
     self.assertEqual(stdout[0], "Hello Java Foo!")
     six.assertRegex(self, stdout[1], "^rloc=.*/foo/datadep/hello.txt")
+    self.assertNotIn("__ignore_me__", stdout[1])
     with open(stdout[1].split("=", 1)[1], "r") as f:
       lines = [l.strip() for l in f.readlines()]
     if len(lines) != 1:
@@ -96,14 +98,17 @@
 
     self.assertTrue(os.path.exists(bin_path))
 
-    exit_code, stdout, stderr = self.RunProgram([bin_path])
+    exit_code, stdout, stderr = self.RunProgram(
+        [bin_path], env_add={"TEST_SRCDIR": "__ignore_me__"})
     self.AssertExitCode(exit_code, 0, stderr)
     if len(stdout) < 4:
       self.fail("stdout: %s" % stdout)
     self.assertEqual(stdout[0], "Hello Python Foo!")
     six.assertRegex(self, stdout[1], "^rloc=.*/foo/datadep/hello.txt")
+    self.assertNotIn("__ignore_me__", stdout[1])
     self.assertEqual(stdout[2], "Hello Python Bar!")
     six.assertRegex(self, stdout[3], "^rloc=.*/bar/bar-py-data.txt")
+    self.assertNotIn("__ignore_me__", stdout[3])
 
     with open(stdout[1].split("=", 1)[1], "r") as f:
       lines = [l.strip() for l in f.readlines()]
diff --git a/src/test/py/bazel/testdata/runfiles_test/foo/runfiles.py b/src/test/py/bazel/testdata/runfiles_test/foo/runfiles.py
index 6b2ba94..6d5493f 100644
--- a/src/test/py/bazel/testdata/runfiles_test/foo/runfiles.py
+++ b/src/test/py/bazel/testdata/runfiles_test/foo/runfiles.py
@@ -32,6 +32,15 @@
     return "foo_ws/bar/bar-%s" % lang
 
 
+def SplitToLines(stdouterr):
+  if isinstance(stdouterr, bytes):
+    # Python3's communicate() returns bytes.
+    return [l.strip() for l in stdouterr.decode().split("\n")]
+  else:
+    # Python2's communicate() returns str.
+    return [l.strip() for l in stdouterr.split("\n")]
+
+
 def main():
   print("Hello Python Foo!")
   r = runfiles.Create()
@@ -49,8 +58,13 @@
       env=env,
       stdout=subprocess.PIPE,
       stderr=subprocess.PIPE)
-  for e in p.communicate():
-    print(e)
+  out, err = p.communicate()
+  out = SplitToLines(out)
+  if len(out) >= 2:
+    print(out[0])  # e.g. "Hello Python Bar!"
+    print(out[1])  # e.g. "rloc=/tmp/foo_ws/bar/bar-py-data.txt"
+  else:
+    raise Exception("ERROR: error running bar-py: %s" % SplitToLines(err))
 
 
 if __name__ == "__main__":