1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
|
// Copyright (c) 2018-2021 Sylabs, Inc. All rights reserved.
// This software is licensed under a 3-clause BSD license. Please
// consult LICENSE file distributed with the sources of this project regarding
// your rights to use or distribute this software.
package shell
import "testing"
func TestArgsQuoted(t *testing.T) {
quoteTests := []struct {
name string
input []string
expected string
}{
{"Single arg", []string{`Hello`}, `"Hello"`},
{"Two args", []string{`Hello`, `me`}, `"Hello" "me"`},
{"Three args", []string{`Hello`, `there`, `me`}, `"Hello" "there" "me"`},
{`Args with escaping`, []string{`Hello`, `\n me`}, `"Hello" "\\n me"`},
}
for _, test := range quoteTests {
t.Run(test.name, func(t *testing.T) {
quoted := ArgsQuoted(test.input)
if quoted != test.expected {
t.Errorf("got %s, expected %s", quoted, test.expected)
}
})
}
}
func TestEscape(t *testing.T) {
escapeTests := []struct {
input string
expected string
}{
{`Hello \n me`, `Hello \\n me`},
{`"Hello"`, `\"Hello\"`},
{"`ls`", "\\`ls\\`"},
{`$PATH`, `\$PATH`},
}
for _, test := range escapeTests {
t.Run(test.input, func(t *testing.T) {
escaped := Escape(test.input)
if escaped != test.expected {
t.Errorf("got %s, expected %s", escaped, test.expected)
}
})
}
}
func TestEscapeDoubleQuotes(t *testing.T) {
escapeQuotesTests := []struct {
input string
expected string
}{
{`Hello`, `Hello`},
{`"Hello"`, `\"Hello\"`},
{`Hell"o`, `Hell\"o`},
}
for _, test := range escapeQuotesTests {
t.Run(test.input, func(t *testing.T) {
escaped := EscapeDoubleQuotes(test.input)
if escaped != test.expected {
t.Errorf("got %s, expected %s", escaped, test.expected)
}
})
}
}
func TestEscapeSingleQuotes(t *testing.T) {
escapeQuotesTests := []struct {
input string
expected string
}{
{`Hello`, `Hello`},
{`'Hello'`, `'"'"'Hello'"'"'`},
{`Hell'o`, `Hell'"'"'o`},
}
for _, test := range escapeQuotesTests {
t.Run(test.input, func(t *testing.T) {
escaped := EscapeSingleQuotes(test.input)
if escaped != test.expected {
t.Errorf("got %s, expected %s", escaped, test.expected)
}
})
}
}
|