14
14
# limitations under the License.
15
15
16
16
import os
17
+ import shutil
18
+ import stat
17
19
import unittest
20
+
18
21
import six
19
22
from src .test .py .bazel import test_base
20
23
@@ -228,7 +231,7 @@ def testRunfilesLibrariesFindRunfilesWithRunfilesManifestEnvvar(self):
228
231
# runfiles tree, Bazel actually creates empty __init__.py files (again
229
232
# on every platform). However to keep these manifest entries correct,
230
233
# 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
232
235
# aim to exercise what would happen in that case.
233
236
mock_manifest_data = [
234
237
mock_manifest_line
@@ -239,6 +242,18 @@ def testRunfilesLibrariesFindRunfilesWithRunfilesManifestEnvvar(self):
239
242
substitute_manifest = self .ScratchFile (
240
243
"mock-%s.runfiles/MANIFEST" % lang [0 ], mock_manifest_data )
241
244
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
+
242
257
exit_code , stdout , stderr = self .RunProgram (
243
258
[bin_path ],
244
259
env_remove = set (["RUNFILES_DIR" ]),
@@ -313,6 +328,68 @@ def testLegacyExternalRunfilesOption(self):
313
328
"host/bin/bin.runfiles_manifest" )
314
329
self .AssertFileContentNotContains (manifest_path , "__main__/external/A" )
315
330
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
+
316
393
317
394
if __name__ == "__main__" :
318
395
unittest .main ()
0 commit comments