diff options
author | 2017-07-28 10:22:57 +0200 | |
---|---|---|
committer | 2017-07-31 16:05:56 +0200 | |
commit | c2c938ae2e75b5b881f06b18cce86dc87bae6fe6 (patch) | |
tree | 391b596c7ef66eeac9a0ef864f112bc6eb0f09c3 /src/test/py | |
parent | 639bba946ea01f5cf0a2cdb51c46012a602814a1 (diff) |
Apply native binary launcher to sh_binary
This change:
1. Added launcher to @bazel_tools
If the host platform is Windows, we use a prebuilt launcher.exe
, otherwise the launcher needs to be built with MSVC first.
2. Launching sh_binary using native launcher.
Change-Id: I5a63135455057fbfe04ff0cce7ec7994ef0c347a
PiperOrigin-RevId: 163442540
Diffstat (limited to 'src/test/py')
-rw-r--r-- | src/test/py/bazel/BUILD | 4 | ||||
-rw-r--r-- | src/test/py/bazel/launcher_test.py (renamed from src/test/py/bazel/launcher_script_test.py) | 80 |
2 files changed, 59 insertions, 25 deletions
diff --git a/src/test/py/bazel/BUILD b/src/test/py/bazel/BUILD index f744c21e1f..73bd4a383a 100644 --- a/src/test/py/bazel/BUILD +++ b/src/test/py/bazel/BUILD @@ -65,8 +65,8 @@ py_test( ) py_test( - name = "launcher_script_test", + name = "launcher_test", size = "medium", - srcs = ["launcher_script_test.py"], + srcs = ["launcher_test.py"], deps = [":test_base"], ) diff --git a/src/test/py/bazel/launcher_script_test.py b/src/test/py/bazel/launcher_test.py index 690c7fb6d5..6c92dd7c0c 100644 --- a/src/test/py/bazel/launcher_script_test.py +++ b/src/test/py/bazel/launcher_test.py @@ -19,7 +19,7 @@ import unittest from src.test.py.bazel import test_base -class LauncherScriptTest(test_base.TestBase): +class LauncherTest(test_base.TestBase): def testJavaBinaryLauncher(self): self.ScratchFile('WORKSPACE') @@ -75,13 +75,9 @@ class LauncherScriptTest(test_base.TestBase): # On Linux/MacOS, all sh_binary rules generate an output file with # the same name as the rule, and this is a symlink to the file in # `srcs`. (Bazel allows only one file in `sh_binary.srcs`.) - # On Windows, if the rule's name and the srcs's name end with the - # same extension, and this extension is one of ".exe", ".cmd", or - # ".bat", then sh_binary makes a copy of the output file, with the - # same name as the rule. Otherwise (if the rule's name doesn't end - # with such an extension, or the extension of it doesn't match the - # main file's) then Bazel creates a %{rulename}.cmd output which is - # a similar launcher script to that generated by java_binary rules. + # On Windows, if the srcs's extension is one of ".exe", ".cmd", or + # ".bat", then Bazel requires the rule's name has the same + # extension, and the output file will be a copy of the source file. 'sh_binary(', ' name = "bin1.sh",', ' srcs = ["foo.sh"],', @@ -112,30 +108,37 @@ class LauncherScriptTest(test_base.TestBase): self.AssertExitCode(exit_code, 0, stderr) bazel_bin = stdout[0] - exit_code, _, stderr = self.RunBazel(['build', '//foo:all']) + exit_code, _, stderr = self.RunBazel(['build', '//foo:bin1.sh']) self.AssertExitCode(exit_code, 0, stderr) - bin1 = os.path.join(bazel_bin, 'foo', 'bin1.sh.cmd' + bin1 = os.path.join(bazel_bin, 'foo', 'bin1.sh.exe' if self.IsWindows() else 'bin1.sh') self.assertTrue(os.path.exists(bin1)) self.assertTrue( os.path.isdir(os.path.join(bazel_bin, 'foo/bin1.sh.runfiles'))) + exit_code, _, stderr = self.RunBazel(['build', '//foo:bin2.cmd']) + self.AssertExitCode(exit_code, 0, stderr) + bin2 = os.path.join(bazel_bin, 'foo/bin2.cmd') self.assertTrue(os.path.exists(bin2)) self.assertTrue( os.path.isdir(os.path.join(bazel_bin, 'foo/bin2.cmd.runfiles'))) - bin3 = os.path.join(bazel_bin, 'foo', 'bin3.bat.cmd' - if self.IsWindows() else 'bin3.bat') - self.assertTrue(os.path.exists(bin3)) - self.assertTrue( - os.path.isdir(os.path.join(bazel_bin, 'foo/bin3.bat.runfiles'))) + exit_code, _, stderr = self.RunBazel(['build', '//foo:bin3.bat']) + if self.IsWindows(): + self.AssertExitCode(exit_code, 1, stderr) + self.assertIn('target name extension should match source file extension.', + os.linesep.join(stderr)) + else: + bin3 = os.path.join(bazel_bin, 'foo', 'bin3.bat') + self.assertTrue(os.path.exists(bin3)) + self.assertTrue( + os.path.isdir(os.path.join(bazel_bin, 'foo/bin3.bat.runfiles'))) if self.IsWindows(): self.assertTrue(os.path.isfile(bin1)) self.assertTrue(os.path.isfile(bin2)) - self.assertTrue(os.path.isfile(bin3)) else: self.assertTrue(os.path.islink(bin1)) self.assertTrue(os.path.islink(bin2)) @@ -148,9 +151,6 @@ class LauncherScriptTest(test_base.TestBase): self.AssertRunfilesManifestContains( os.path.join(bazel_bin, 'foo/bin2.cmd.runfiles/MANIFEST'), '__main__/bar/bar.txt') - self.AssertRunfilesManifestContains( - os.path.join(bazel_bin, 'foo/bin3.bat.runfiles/MANIFEST'), - '__main__/bar/bar.txt') else: self.assertTrue( os.path.islink( @@ -174,9 +174,43 @@ class LauncherScriptTest(test_base.TestBase): self.AssertExitCode(exit_code, 0, stderr) self.assertEqual(stdout[0], 'hello batch') - exit_code, stdout, stderr = self.RunProgram([bin3]) - self.AssertExitCode(exit_code, 0, stderr) - self.assertEqual(stdout[0], 'hello batch') + def testShBinaryArgumentPassing(self): + self.ScratchFile('WORKSPACE') + self.ScratchFile('foo/BUILD', [ + 'sh_binary(', + ' name = "bin",', + ' srcs = ["bin.sh"],', + ')', + ]) + foo_sh = self.ScratchFile('foo/bin.sh', [ + '#!/bin/bash', + '# Store arguments in a array', + 'args=("$@")', + '# Get the number of arguments', + 'N=${#args[@]}', + '# Echo each argument', + 'for (( i=0;i<$N;i++)); do', + ' echo ${args[${i}]}', + 'done', + ]) + os.chmod(foo_sh, stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR) + + exit_code, stdout, stderr = self.RunBazel(['info', 'bazel-bin']) + self.AssertExitCode(exit_code, 0, stderr) + bazel_bin = stdout[0] + + exit_code, _, stderr = self.RunBazel(['build', '//foo:bin']) + self.AssertExitCode(exit_code, 0, stderr) + + bin1 = os.path.join(bazel_bin, 'foo', 'bin.exe' + if self.IsWindows() else 'bin') + self.assertTrue(os.path.exists(bin1)) + self.assertTrue(os.path.isdir(os.path.join(bazel_bin, 'foo/bin.runfiles'))) + + arguments = ['a', 'a b', '"b"', 'C:\\a\\b\\', '"C:\\a b\\c\\"'] + exit_code, stdout, stderr = self.RunProgram([bin1] + arguments) + self.AssertExitCode(exit_code, 0, stderr) + self.assertEqual(stdout, arguments) def testPyBinaryLauncher(self): self.ScratchFile('WORKSPACE') @@ -220,7 +254,7 @@ class LauncherScriptTest(test_base.TestBase): ' with open(sys.argv[1], "w") as f:', ' f.write("Hello World!")', 'else:', - ' print "Hello World!"', + ' print("Hello World!")', ]) self.ScratchFile('bar/BUILD', ['exports_files(["bar.txt"])']) self.ScratchFile('bar/bar.txt', ['hello']) |