Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Test genrule escaping with Windows #17484

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 39 additions & 1 deletion src/test/py/bazel/launcher_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ def _buildAndCheckArgumentPassing(self, package, target_name):
os.path.join(bazel_bin, '%s/%s%s.runfiles' % (package, target_name,
bin_suffix))))

arguments = ['a', 'a b', '"b"', 'C:\\a\\b\\', '"C:\\a b\\c\\"']
arguments = ['a', 'a b', '"b"', 'C:\\a\\b\\', '"C:\\a b\\c\\"', 'f(x).y']
exit_code, stdout, stderr = self.RunProgram([bin1] + arguments)
self.AssertExitCode(exit_code, 0, stderr)
self.assertEqual(stdout, arguments)
Expand Down Expand Up @@ -327,6 +327,44 @@ def testShBinaryArgumentPassing(self):

self._buildAndCheckArgumentPassing('foo', 'bin')

def testShBinaryGenrule(self):
self.CreateWorkspaceWithDefaultRepos('WORKSPACE')
self.ScratchFile('foo/BUILD', [
'sh_binary(',
' name = "bin",',
' srcs = ["bin.sh"],',
')',
'genrule(',
' name = "gen",',
' outs = ["out.txt"],',
' cmd = "$(location :bin) a \'b c\' \'f(x).y\' > $@",',
' tools = [":bin"],',
')',
])
foo_sh = self.ScratchFile('foo/bin.sh', [
'#!/usr/bin/env 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', '-s', '//foo:gen'])
self.AssertExitCode(exit_code, 0, stderr)

with open(os.path.join(bazel_bin, 'foo/out.txt')) as f:
lines = [line.strip() for line in f.readlines()]
self.assertEqual(lines, ['a', 'b c', 'f(x).y'])

def testPyBinaryLauncher(self):
self.CreateWorkspaceWithDefaultRepos('WORKSPACE')
self.ScratchFile(
Expand Down
7 changes: 7 additions & 0 deletions src/tools/launcher/bash_launcher.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include <stdio.h>
#include <sstream>
#include <string>
#include <vector>
Expand Down Expand Up @@ -51,6 +52,9 @@ ExitCode BashBinaryLauncher::Launch() {
}

vector<wstring> origin_args = this->GetCommandlineArguments();
for (int i = 0; i < origin_args.size(); i++) {
fwprintf(stderr, L"origin_arg[%d]: %ls\n", i, origin_args[i].c_str());
}
wostringstream bash_command;
wstring bash_main_file = GetBinaryPathWithoutExtension(GetLauncherPath());
bash_command << BashEscapeArg(bash_main_file);
Expand All @@ -62,6 +66,9 @@ ExitCode BashBinaryLauncher::Launch() {
vector<wstring> args;
args.push_back(L"-c");
args.push_back(BashEscapeArg(bash_command.str()));
for (int i = 0; i < args.size(); i++) {
fwprintf(stderr, L"arg[%d]: %ls\n", i, args[i].c_str());
}
return this->LaunchProcess(bash_binary, args);
}

Expand Down
12 changes: 12 additions & 0 deletions src/tools/launcher/util/launcher_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,18 @@ std::wstring BashEscapeArg(const std::wstring& argument) {
escaped_arg += L"\\\\";
break;

case L'&':
case L'|':
case L'(':
case L')':
case L'<':
case L'>':
case L'^':
// Escape special characters.
escaped_arg += L'^';
escaped_arg += ch;
break;

default:
escaped_arg += ch;
}
Expand Down