1414# limitations under the License.
1515
1616import os
17+ import shutil
18+ import stat
1719import unittest
20+
1821import six
1922from src .test .py .bazel import test_base
2023
@@ -228,7 +231,7 @@ def testRunfilesLibrariesFindRunfilesWithRunfilesManifestEnvvar(self):
228231 # runfiles tree, Bazel actually creates empty __init__.py files (again
229232 # on every platform). However to keep these manifest entries correct,
230233 # they need to have a space character.
231- # We could probably strip thses lines completely, but this test doesn't
234+ # We could probably strip these lines completely, but this test doesn't
232235 # aim to exercise what would happen in that case.
233236 mock_manifest_data = [
234237 mock_manifest_line
@@ -239,6 +242,18 @@ def testRunfilesLibrariesFindRunfilesWithRunfilesManifestEnvvar(self):
239242 substitute_manifest = self .ScratchFile (
240243 "mock-%s.runfiles/MANIFEST" % lang [0 ], mock_manifest_data )
241244
245+ # remove the original manifest file and directory so the launcher picks
246+ # the one in the environment variable RUNFILES_MANIFEST_FILE
247+ manifest_dir = bin_path + ".runfiles"
248+ manifest_dir_file = os .path .join (manifest_dir , "MANIFEST" )
249+ os .chmod (manifest_dir_file , stat .S_IRWXU )
250+ shutil .rmtree (manifest_dir )
251+ self .assertFalse (os .path .exists (manifest_dir ))
252+
253+ os .chmod (manifest_path , stat .S_IRWXU )
254+ os .remove (manifest_path )
255+ self .assertFalse (os .path .exists (manifest_path ))
256+
242257 exit_code , stdout , stderr = self .RunProgram (
243258 [bin_path ],
244259 env_remove = set (["RUNFILES_DIR" ]),
@@ -313,6 +328,68 @@ def testLegacyExternalRunfilesOption(self):
313328 "host/bin/bin.runfiles_manifest" )
314329 self .AssertFileContentNotContains (manifest_path , "__main__/external/A" )
315330
331+ def testRunUnderWithRunfiles (self ):
332+ for s , t , exe in [
333+ ("WORKSPACE.mock" , "WORKSPACE" , False ),
334+ ("bar/BUILD.mock" , "bar/BUILD" , False ),
335+ ("bar/bar.py" , "bar/bar.py" , True ),
336+ ("bar/bar-py-data.txt" , "bar/bar-py-data.txt" , False ),
337+ ("bar/Bar.java" , "bar/Bar.java" , False ),
338+ ("bar/bar-java-data.txt" , "bar/bar-java-data.txt" , False ),
339+ ("bar/bar.sh" , "bar/bar.sh" , True ),
340+ ("bar/bar-sh-data.txt" , "bar/bar-sh-data.txt" , False ),
341+ ("bar/bar-run-under.sh" , "bar/bar-run-under.sh" , True ),
342+ ("bar/bar.cc" , "bar/bar.cc" , False ),
343+ ("bar/bar-cc-data.txt" , "bar/bar-cc-data.txt" , False ),
344+ ]:
345+ self .CopyFile (
346+ self .Rlocation ("io_bazel/src/test/py/bazel/testdata/runfiles_test/" +
347+ s ), t , exe )
348+
349+ exit_code , stdout , stderr = self .RunBazel (["info" , "bazel-bin" ])
350+ self .AssertExitCode (exit_code , 0 , stderr )
351+ bazel_bin = stdout [0 ]
352+
353+ # build bar-sh-run-under target to run targets under it
354+ exit_code , _ , stderr = self .RunBazel (
355+ ["build" , "--verbose_failures" , "//bar:bar-sh-run-under" ])
356+ self .AssertExitCode (exit_code , 0 , stderr )
357+
358+ if test_base .TestBase .IsWindows ():
359+ run_under_bin_path = os .path .join (bazel_bin , "bar/bar-sh-run-under.exe" )
360+ else :
361+ run_under_bin_path = os .path .join (bazel_bin , "bar/bar-sh-run-under" )
362+
363+ self .assertTrue (os .path .exists (run_under_bin_path ))
364+
365+ for lang in [("py" , "Python" , "bar.py" ), ("java" , "Java" , "Bar.java" ),
366+ ("sh" , "Bash" , "bar.sh" ), ("cc" , "C++" , "bar.cc" )]:
367+ exit_code , stdout , stderr = self .RunBazel ([
368+ "run" , "--verbose_failures" , "--run_under=//bar:bar-sh-run-under" ,
369+ "//bar:bar-" + lang [0 ]
370+ ])
371+ self .AssertExitCode (exit_code , 0 , stderr )
372+
373+ if test_base .TestBase .IsWindows ():
374+ bin_path = os .path .join (bazel_bin , "bar/bar-%s.exe" % lang [0 ])
375+ else :
376+ bin_path = os .path .join (bazel_bin , "bar/bar-" + lang [0 ])
377+
378+ self .assertTrue (os .path .exists (bin_path ))
379+
380+ if len (stdout ) < 3 :
381+ self .fail ("stdout(%s): %s" % (lang [0 ], stdout ))
382+
383+ self .assertEqual (stdout [0 ], "Hello Bash Bar Run under!" )
384+ self .assertEqual (stdout [1 ], "Hello %s Bar!" % lang [1 ])
385+ six .assertRegex (self , stdout [2 ], "^rloc=.*/bar/bar-%s-data.txt" % lang [0 ])
386+
387+ with open (stdout [2 ].split ("=" , 1 )[1 ], "r" ) as f :
388+ lines = [l .strip () for l in f .readlines ()]
389+ if len (lines ) != 1 :
390+ self .fail ("lines(%s): %s" % (lang [0 ], lines ))
391+ self .assertEqual (lines [0 ], "data for " + lang [2 ])
392+
316393
317394if __name__ == "__main__" :
318395 unittest .main ()
0 commit comments