Skip to content

Commit

Permalink
Update should accept empty path with version
Browse files Browse the repository at this point in the history
  • Loading branch information
phanimarupaka committed Nov 4, 2021
1 parent bc04a55 commit 3efabc4
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 5 deletions.
49 changes: 49 additions & 0 deletions internal/cmdupdate/cmdupdate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,55 @@ func TestCmd_successNoGit(t *testing.T) {
}
}

func TestCmd_onlyVersionAsInput(t *testing.T) {
g, w, clean := testutil.SetupRepoAndWorkspace(t, testutil.Content{
Data: testutil.Dataset1,
Branch: "master",
})
defer clean()

err := os.RemoveAll(".git")
if !assert.NoError(t, err) {
t.FailNow()
}
dest := filepath.Join(w.WorkspaceDirectory, g.RepoName)

// clone the repo
getCmd := cmdget.NewRunner(fake.CtxWithDefaultPrinter(), "kpt")
getCmd.Command.SetArgs([]string{"file://" + g.RepoDirectory + ".git", w.WorkspaceDirectory})
err = getCmd.Command.Execute()
if !assert.NoError(t, err) {
return
}
if !g.AssertEqual(t, filepath.Join(g.DatasetDirectory, testutil.Dataset1), dest, true) {
return
}

// update the master branch
if !assert.NoError(t, g.ReplaceData(testutil.Dataset2)) {
return
}

// commit the upstream but not the local
_, err = g.Commit("new dataset")
if !assert.NoError(t, err) {
return
}

// update the cloned package
updateCmd := cmdupdate.NewRunner(fake.CtxWithDefaultPrinter(), "kpt")
defer testutil.Chdir(t, dest)()
updateCmd.Command.SetArgs([]string{"@master"})
err = updateCmd.Command.Execute()
if !assert.NoError(t, err) {
t.FailNow()
}

if !g.AssertEqual(t, filepath.Join(g.DatasetDirectory, testutil.Dataset2), dest, true) {
return
}
}

// NoOpRunE is a noop function to replace the run function of a command. Useful for testing argument parsing.
var NoOpRunE = func(cmd *cobra.Command, args []string) error { return nil }

Expand Down
12 changes: 7 additions & 5 deletions internal/util/argutil/argutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,13 @@ func ParseFieldPath(path string) ([]string, error) {
func ResolveSymlink(ctx context.Context, path string) (string, error) {
isSymlink := false
f, err := os.Lstat(path)
if err != nil {
return "", err
}
if f.Mode().Type() == os.ModeSymlink {
isSymlink = true
if err == nil {
// this step only helps with printing WARN message by checking if the input
// path has symlink, so do not error out at this phase and let
// filepath.EvalSymlinks(path) handle the cases
if f.Mode().Type() == os.ModeSymlink {
isSymlink = true
}
}
rp, err := filepath.EvalSymlinks(path)
if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions internal/util/argutil/argutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,10 @@ func TestResolveSymlink(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, "foo", actual2)

actual3, err := ResolveSymlink(fake.CtxWithDefaultPrinter(), ".")
assert.NoError(t, err)
assert.Equal(t, "", actual3)

_, err = ResolveSymlink(fake.CtxWithDefaultPrinter(), "baz")
assert.Error(t, err)
assert.Equal(t, "lstat baz: no such file or directory", err.Error())
Expand Down

0 comments on commit 3efabc4

Please sign in to comment.