github.com/ovsinc/prototool@v1.3.0/internal/file/file_test.go (about) 1 // Copyright (c) 2018 Uber Technologies, Inc. 2 // 3 // Permission is hereby granted, free of charge, to any person obtaining a copy 4 // of this software and associated documentation files (the "Software"), to deal 5 // in the Software without restriction, including without limitation the rights 6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 // copies of the Software, and to permit persons to whom the Software is 8 // furnished to do so, subject to the following conditions: 9 // 10 // The above copyright notice and this permission notice shall be included in 11 // all copies or substantial portions of the Software. 12 // 13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 // THE SOFTWARE. 20 21 package file 22 23 import ( 24 "os" 25 "path/filepath" 26 "testing" 27 28 "github.com/stretchr/testify/assert" 29 "github.com/stretchr/testify/require" 30 ) 31 32 func TestAbsClean(t *testing.T) { 33 wd, err := os.Getwd() 34 require.NoError(t, err) 35 36 tests := []struct { 37 desc string 38 give string 39 want string 40 }{ 41 { 42 desc: "Empty path", 43 }, 44 { 45 desc: "Root path", 46 give: "/", 47 want: "/", 48 }, 49 { 50 desc: "Clean absolute path", 51 give: "/path/to/foo", 52 want: "/path/to/foo", 53 }, 54 { 55 desc: "Messy absolute path", 56 give: "/path//to///foo", 57 want: "/path/to/foo", 58 }, 59 { 60 desc: "Clean relative path", 61 give: "path/to/foo", 62 want: filepath.Join(wd, "path/to/foo"), 63 }, 64 { 65 desc: "Messy relative path", 66 give: "path//to///foo", 67 want: filepath.Join(wd, "path/to/foo"), 68 }, 69 } 70 for _, tt := range tests { 71 t.Run(tt.desc, func(t *testing.T) { 72 res, err := AbsClean(tt.give) 73 assert.NoError(t, err) 74 assert.Equal(t, tt.want, res) 75 }) 76 } 77 } 78 79 func TestCheckAbs(t *testing.T) { 80 t.Run("Absolute path", func(t *testing.T) { 81 assert.NoError(t, CheckAbs("/path/to/foo")) 82 }) 83 t.Run("Relative path", func(t *testing.T) { 84 assert.EqualError(t, CheckAbs("path/to/foo"), "expected absolute path but was path/to/foo") 85 }) 86 }