github.com/actions-on-google/gactions@v3.2.0+incompatible/cmd/gactions/cli/decrypt/decrypt_test.go (about) 1 // Copyright 2020 Google LLC 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 // https://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 decrypt 16 17 import ( 18 "path/filepath" 19 "runtime" 20 "strings" 21 "testing" 22 ) 23 24 func osAbs(p string) string { 25 if runtime.GOOS == "windows" && strings.HasPrefix(p, "/") { 26 return filepath.Join("c:", filepath.FromSlash(p)) 27 } 28 return filepath.FromSlash(p) 29 } 30 31 func TestNormPath(t *testing.T) { 32 tests := []struct { 33 p string 34 root string 35 want string 36 }{ 37 { 38 p: "foo.txt", 39 root: osAbs("/home/user/myproject/"), 40 want: filepath.Join(osAbs("/home/user/myproject/"), "foo.txt"), 41 }, 42 { 43 p: osAbs("/home/foo.txt"), 44 root: osAbs("/home/user/myproject/"), 45 want: osAbs("/home/foo.txt"), 46 }, 47 { 48 p: osAbs("./bar/foo.txt"), 49 root: osAbs("/home/user/myproject/"), 50 want: osAbs("/home/user/myproject/bar/foo.txt"), 51 }, 52 { 53 p: osAbs("../bar/foo.txt"), 54 root: osAbs("/home/user/myproject/"), 55 want: osAbs("/home/user/bar/foo.txt"), 56 }, 57 } 58 for _, tc := range tests { 59 if got := normPath(tc.p, tc.root); got != tc.want { 60 t.Errorf("normPath(%v, %v) returned %v, but want %v", tc.p, tc.root, got, tc.want) 61 } 62 } 63 }