go.mway.dev/x@v0.0.0-20240520034138-950aede9a3fb/env/path_test.go (about) 1 // Copyright (c) 2023 Matt Way 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 5 // deal in the Software without restriction, including without limitation the 6 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 7 // sell 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 18 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 19 // IN THE THE SOFTWARE. 20 21 package env 22 23 import ( 24 "errors" 25 "testing" 26 27 "github.com/stretchr/testify/require" 28 ) 29 30 func TestPath(t *testing.T) { 31 withDummyPath(t.Name(), func() { 32 path := NewPath() 33 require.Equal(t, t.Name(), path.Value()) 34 35 path, err := path.Prepend("a", "b", "c") 36 require.NoError(t, err) 37 require.Equal(t, "a:b:c:"+t.Name(), path.String()) 38 39 path, err = path.Append("d", "e", "f") 40 require.NoError(t, err) 41 require.Equal(t, "a:b:c:"+t.Name()+":d:e:f", path.String()) 42 }) 43 } 44 45 func TestPath_Append_Empty(t *testing.T) { 46 withDummyPath(t.Name(), func() { 47 path := NewPath().MustAppend().MustAppend("", "foo") 48 require.Equal(t, t.Name()+":foo", path.String()) 49 }) 50 } 51 52 func TestPath_Prepend_Error(t *testing.T) { 53 withDummyPath(t.Name(), func() { 54 wantErr := errors.New("setenv error") 55 withStub(&_osSetenv, osSetenvReturning(wantErr), func() { 56 path := NewPath() 57 _, err := path.Prepend("foo") 58 require.ErrorIs(t, err, wantErr) 59 require.Panics(t, func() { 60 path.MustPrepend("foo") 61 }) 62 }) 63 }) 64 } 65 66 func TestPath_Append_Error(t *testing.T) { 67 withDummyPath(t.Name(), func() { 68 wantErr := errors.New("setenv error") 69 withStub(&_osSetenv, osSetenvReturning(wantErr), func() { 70 path := NewPath() 71 _, err := path.Append("foo") 72 require.ErrorIs(t, err, wantErr) 73 require.Panics(t, func() { 74 path.MustAppend("foo") 75 }) 76 }) 77 }) 78 } 79 80 func TestPath_Prepend_Empty(t *testing.T) { 81 withDummyPath(t.Name(), func() { 82 path := NewPath().MustPrepend().MustPrepend("", "foo") 83 require.Equal(t, "foo:"+t.Name(), path.String()) 84 }) 85 } 86 87 func TestPath_Restore_Error(t *testing.T) { 88 withDummyPath(t.Name(), func() { 89 wantErr := errors.New("setenv error") 90 withStub(&_osSetenv, osSetenvReturning(wantErr), func() { 91 path := NewPath() 92 require.ErrorIs(t, path.Restore(), wantErr) 93 require.Panics(t, path.MustRestore) 94 }) 95 }) 96 }