github.com/wmuizelaar/kpt@v0.0.0-20221018115725-bd564717b2ed/internal/util/argutil/argutil_test.go (about)

     1  // Copyright 2019 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  //      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 argutil_test
    16  
    17  import (
    18  	"os"
    19  	"path/filepath"
    20  	"testing"
    21  
    22  	"github.com/GoogleContainerTools/kpt/internal/printer/fake"
    23  	"github.com/GoogleContainerTools/kpt/internal/testutil"
    24  	. "github.com/GoogleContainerTools/kpt/internal/util/argutil"
    25  	"github.com/stretchr/testify/assert"
    26  )
    27  
    28  func TestParseDirVersion(t *testing.T) {
    29  	tests := []struct {
    30  		in     string
    31  		expDir string
    32  		expVer string
    33  		expErr error
    34  	}{
    35  		{ // dir without version
    36  			in:     "dir1",
    37  			expDir: "dir1",
    38  			expVer: "",
    39  			expErr: nil,
    40  		},
    41  		{ // version without dir
    42  			in:     "@ver1",
    43  			expDir: "",
    44  			expVer: "ver1",
    45  			expErr: nil,
    46  		},
    47  		{ // dir with ver
    48  			in:     "/some/dir1@ver1",
    49  			expDir: "/some/dir1",
    50  			expVer: "ver1",
    51  			expErr: nil,
    52  		},
    53  		{ // multiple version
    54  			in:     "/some@dir2@ver1",
    55  			expDir: "",
    56  			expVer: "",
    57  			expErr: ErrMultiVersion,
    58  		},
    59  		{ // empty
    60  			in:     "",
    61  			expDir: "",
    62  			expVer: "",
    63  			expErr: nil,
    64  		},
    65  	}
    66  
    67  	for _, test := range tests {
    68  		gotDir, gotVer, gotErr := ParseDirVersion(test.in)
    69  
    70  		assert.Equal(t, gotErr, test.expErr)
    71  		assert.Equal(t, gotDir, test.expDir)
    72  		assert.Equal(t, gotVer, test.expVer)
    73  	}
    74  }
    75  
    76  func TestParseDirVersionWithDefaults(t *testing.T) {
    77  	tests := []struct {
    78  		in     string
    79  		expDir string
    80  		expVer string
    81  		expErr error
    82  	}{
    83  		{ // dir without version
    84  			in:     "dir1",
    85  			expDir: "dir1",
    86  			expVer: "master",
    87  			expErr: nil,
    88  		},
    89  		{ // version without dir
    90  			in:     "@ver1",
    91  			expDir: "./",
    92  			expVer: "ver1",
    93  			expErr: nil,
    94  		},
    95  		{ // dir with ver
    96  			in:     "/some/dir1@ver1",
    97  			expDir: "/some/dir1",
    98  			expVer: "ver1",
    99  			expErr: nil,
   100  		},
   101  		{ // multiple version
   102  			in:     "/some@dir2@ver1",
   103  			expDir: "",
   104  			expVer: "",
   105  			expErr: ErrMultiVersion,
   106  		},
   107  		{ // empty
   108  			in:     "",
   109  			expDir: "./",
   110  			expVer: "master",
   111  			expErr: nil,
   112  		},
   113  	}
   114  
   115  	for _, test := range tests {
   116  		gotDir, gotVer, gotErr := ParseDirVersionWithDefaults(test.in)
   117  
   118  		assert.Equal(t, gotErr, test.expErr)
   119  		assert.Equal(t, gotDir, test.expDir)
   120  		assert.Equal(t, gotVer, test.expVer)
   121  	}
   122  }
   123  
   124  func TestResolveSymlink(t *testing.T) {
   125  	dir := t.TempDir()
   126  	defer testutil.Chdir(t, dir)()
   127  	err := os.MkdirAll(filepath.Join(dir, "foo"), 0700)
   128  	assert.NoError(t, err)
   129  	err = os.Symlink("foo", "foo-link")
   130  	assert.NoError(t, err)
   131  	err = os.Symlink("foo-link", "link-to-foo-link")
   132  	assert.NoError(t, err)
   133  
   134  	actual1, err := ResolveSymlink(fake.CtxWithDefaultPrinter(), "foo-link")
   135  	assert.NoError(t, err)
   136  	assert.Equal(t, "foo", actual1)
   137  
   138  	actual2, err := ResolveSymlink(fake.CtxWithDefaultPrinter(), "link-to-foo-link")
   139  	assert.NoError(t, err)
   140  	assert.Equal(t, "foo", actual2)
   141  
   142  	actual3, err := ResolveSymlink(fake.CtxWithDefaultPrinter(), ".")
   143  	assert.NoError(t, err)
   144  	assert.Equal(t, ".", actual3)
   145  
   146  	_, err = ResolveSymlink(fake.CtxWithDefaultPrinter(), "baz")
   147  	assert.Error(t, err)
   148  	assert.Equal(t, "lstat baz: no such file or directory", err.Error())
   149  }