go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/common/system/shell/shell_test.go (about) 1 // Copyright 2022 The LUCI Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package shell 16 17 import ( 18 "strings" 19 "testing" 20 "testing/quick" 21 "unicode/utf8" 22 23 "github.com/google/go-cmp/cmp" 24 ) 25 26 // TestQuoteUnix tests the unix quoter. 27 func TestQuoteUnix(t *testing.T) { 28 t.Parallel() 29 cases := []struct { 30 in []string 31 out string 32 }{ 33 { 34 in: []string{``}, 35 out: `""`, 36 }, 37 { 38 in: []string{`$`}, 39 out: `"\$"`, 40 }, 41 { 42 in: []string{`a`}, 43 out: `"a"`, 44 }, 45 { 46 in: []string{`"`}, 47 out: `"\""`, 48 }, 49 { 50 in: []string{`\`}, 51 out: `"\\"`, 52 }, 53 { 54 in: []string{"`"}, 55 out: "\"\\`\"", 56 }, 57 { 58 in: []string{"a", "b"}, 59 out: `"a" "b"`, 60 }, 61 } 62 63 for _, tt := range cases { 64 tt := tt 65 t.Run(strings.Join(tt.in, " "), func(t *testing.T) { 66 t.Parallel() 67 expected := tt.out 68 actual := QuoteUnix(tt.in...) 69 if diff := cmp.Diff(expected, actual); diff != "" { 70 t.Errorf("unexpected diff (-want +got): %s", diff) 71 } 72 }) 73 } 74 } 75 76 // TestQuoteUnixUnicode tests the property that the input is valid UTF8 if and only if the output is UTF8 when the input is a single string. 77 func TestQuoteUnixUnicode(t *testing.T) { 78 t.Parallel() 79 f := func(in []byte) bool { 80 inputIsUTF8 := utf8.Valid(in) 81 out := []byte(QuoteUnix(string(in))) 82 outputIsUTF8 := utf8.Valid(out) 83 return inputIsUTF8 == outputIsUTF8 84 } 85 if err := quick.Check(f, &quick.Config{ 86 MaxCount: 10000, 87 }); err != nil { 88 t.Error(err) 89 } 90 }