Add ShellEscapeIncludingSpaces(string) am: ddb5ed7e1f am: 41ea2da6af

Original change: https://android-review.googlesource.com/c/platform/build/blueprint/+/1620991

MUST ONLY BE SUBMITTED BY AUTOMERGER

Change-Id: Ic7fd9d76711bb1cf77e7da3cd57bb0a9727f8a19
This commit is contained in:
Jooyung Han 2021-03-12 09:59:58 +00:00 committed by Automerger Merge Worker
commit 7f2f6ea504
2 changed files with 75 additions and 18 deletions

View file

@ -56,12 +56,7 @@ func ShellEscapeList(slice []string) []string {
}
// ShellEscapeList takes string that may contain characters that are meaningful to bash and
// escapes it if necessary by wrapping it in single quotes, and replacing internal single quotes with
// '\'' (one single quote to end the quoting, a shell-escaped single quote to insert a real single
// quote, and then a single quote to restarting quoting.
func ShellEscape(s string) string {
shellUnsafeChar := func(r rune) bool {
func shellUnsafeChar(r rune) bool {
switch {
case 'A' <= r && r <= 'Z',
'a' <= r && r <= 'z',
@ -72,14 +67,33 @@ func ShellEscape(s string) string {
r == '=',
r == '.',
r == ',',
r == '/',
r == ' ':
r == '/':
return false
default:
return true
}
}
// ShellEscape takes string that may contain characters that are meaningful to bash and
// escapes it if necessary by wrapping it in single quotes, and replacing internal single quotes with
// '\'' (one single quote to end the quoting, a shell-escaped single quote to insert a real single
// quote, and then a single quote to restarting quoting.
func ShellEscape(s string) string {
shellUnsafeCharNotSpace := func(r rune) bool {
return r != ' ' && shellUnsafeChar(r)
}
if strings.IndexFunc(s, shellUnsafeCharNotSpace) == -1 {
// No escaping necessary
return s
}
return `'` + singleQuoteReplacer.Replace(s) + `'`
}
// ShellEscapeIncludingSpaces escapes the input `s` in a similar way to ShellEscape except that
// this treats spaces as meaningful characters.
func ShellEscapeIncludingSpaces(s string) string {
if strings.IndexFunc(s, shellUnsafeChar) == -1 {
// No escaping necessary
return s

View file

@ -91,6 +91,24 @@ var shellEscapeTestCase = []escapeTestCase{
},
}
var shellEscapeIncludingSpacesTestCase = []escapeTestCase{
{
name: "no escaping",
in: `test`,
out: `test`,
},
{
name: "spacing",
in: `arg1 arg2`,
out: `'arg1 arg2'`,
},
{
name: "single quote",
in: `'arg'`,
out: `''\''arg'\'''`,
},
}
func TestNinjaEscaping(t *testing.T) {
for _, testCase := range ninjaEscapeTestCase {
got := NinjaEscape(testCase.in)
@ -109,6 +127,15 @@ func TestShellEscaping(t *testing.T) {
}
}
func TestShellEscapeIncludingSpaces(t *testing.T) {
for _, testCase := range shellEscapeIncludingSpacesTestCase {
got := ShellEscapeIncludingSpaces(testCase.in)
if got != testCase.out {
t.Errorf("%s: expected `%s` got `%s`", testCase.name, testCase.out, got)
}
}
}
func TestExternalShellEscaping(t *testing.T) {
if testing.Short() {
return
@ -124,3 +151,19 @@ func TestExternalShellEscaping(t *testing.T) {
}
}
}
func TestExternalShellEscapeIncludingSpaces(t *testing.T) {
if testing.Short() {
return
}
for _, testCase := range shellEscapeIncludingSpacesTestCase {
cmd := "echo -n " + ShellEscapeIncludingSpaces(testCase.in)
got, err := exec.Command("/bin/sh", "-c", cmd).Output()
if err != nil {
t.Error(err)
}
if string(got) != testCase.in {
t.Errorf("%s: expected `%s` got `%s`", testCase.name, testCase.in, got)
}
}
}