github.com/apptainer/singularity@v3.1.1+incompatible/internal/pkg/util/shell/escape_test.go (about) 1 // Copyright (c) 2018, Sylabs, Inc. All rights reserved. 2 // This software is licensed under a 3-clause BSD license. Please 3 // consult LICENSE file distributed with the sources of this project regarding 4 // your rights to use or distribute this software. 5 6 package shell 7 8 import "testing" 9 10 func TestArgsQuoted(t *testing.T) { 11 var quoteTests = []struct { 12 name string 13 input []string 14 expected string 15 }{ 16 {"Single arg", []string{`Hello`}, `"Hello"`}, 17 {"Two args", []string{`Hello`, `me`}, `"Hello" "me"`}, 18 {"Three args", []string{`Hello`, `there`, `me`}, `"Hello" "there" "me"`}, 19 {`Args with escaping`, []string{`Hello`, `\n me`}, `"Hello" "\\n me"`}, 20 } 21 22 for _, test := range quoteTests { 23 t.Run(test.name, func(t *testing.T) { 24 quoted := ArgsQuoted(test.input) 25 if quoted != test.expected { 26 t.Errorf("got %s, expected %s", quoted, test.expected) 27 } 28 }) 29 } 30 31 } 32 33 func TestEscape(t *testing.T) { 34 var escapeTests = []struct { 35 input string 36 expected string 37 }{ 38 {`Hello \n me`, `Hello \\n me`}, 39 {`"Hello"`, `\"Hello\"`}, 40 {"`ls`", "\\`ls\\`"}, 41 {`$PATH`, `\$PATH`}, 42 } 43 44 for _, test := range escapeTests { 45 t.Run(test.input, func(t *testing.T) { 46 escaped := Escape(test.input) 47 if escaped != test.expected { 48 t.Errorf("got %s, expected %s", escaped, test.expected) 49 } 50 }) 51 } 52 53 }