github.com/SamarSidharth/kpt@v0.0.0-20231122062228-c7d747ae3ace/commands/pkg/diff/cmddiff_test.go (about) 1 // Copyright 2019 The kpt Authors 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 diff_test 16 17 import ( 18 "os" 19 "path/filepath" 20 "testing" 21 22 "github.com/GoogleContainerTools/kpt/commands/pkg/diff" 23 "github.com/GoogleContainerTools/kpt/commands/pkg/get" 24 "github.com/GoogleContainerTools/kpt/internal/testutil" 25 "github.com/GoogleContainerTools/kpt/pkg/printer/fake" 26 "github.com/spf13/cobra" 27 "github.com/stretchr/testify/assert" 28 ) 29 30 func TestMain(m *testing.M) { 31 os.Exit(testutil.ConfigureTestKptCache(m)) 32 } 33 34 func TestCmdInvalidDiffType(t *testing.T) { 35 runner := diff.NewRunner(fake.CtxWithDefaultPrinter(), "") 36 runner.C.SetArgs([]string{"--diff-type", "invalid"}) 37 err := runner.C.Execute() 38 assert.EqualError(t, 39 err, 40 "invalid diff-type 'invalid': supported diff-types are: local, remote, combined, 3way") 41 } 42 43 func TestCmdInvalidDiffTool(t *testing.T) { 44 runner := diff.NewRunner(fake.CtxWithDefaultPrinter(), "") 45 runner.C.SetArgs([]string{"--diff-tool", "nodiff"}) 46 err := runner.C.Execute() 47 assert.EqualError(t, 48 err, 49 "diff-tool 'nodiff' not found in the PATH") 50 } 51 52 func TestCmdExecute(t *testing.T) { 53 g, w, clean := testutil.SetupRepoAndWorkspace(t, testutil.Content{ 54 Data: testutil.Dataset1, 55 Branch: "master", 56 }) 57 defer clean() 58 59 defer testutil.Chdir(t, w.WorkspaceDirectory)() 60 61 dest := filepath.Join(w.WorkspaceDirectory, g.RepoName) 62 63 getRunner := get.NewRunner(fake.CtxWithDefaultPrinter(), "") 64 getRunner.Command.SetArgs([]string{"file://" + g.RepoDirectory + ".git/", "./"}) 65 err := getRunner.Command.Execute() 66 assert.NoError(t, err) 67 68 runner := diff.NewRunner(fake.CtxWithDefaultPrinter(), "") 69 runner.C.SetArgs([]string{dest, "--diff-type", "local"}) 70 err = runner.C.Execute() 71 assert.NoError(t, err) 72 } 73 74 func TestCmd_flagAndArgParsing_Symlink(t *testing.T) { 75 dir := t.TempDir() 76 defer testutil.Chdir(t, dir)() 77 78 err := os.MkdirAll(filepath.Join(dir, "path", "to", "pkg", "dir"), 0700) 79 assert.NoError(t, err) 80 err = os.Symlink(filepath.Join("path", "to", "pkg", "dir"), "foo") 81 assert.NoError(t, err) 82 83 // verify the branch ref is set to the correct value 84 r := diff.NewRunner(fake.CtxWithDefaultPrinter(), "kpt") 85 r.C.RunE = NoOpRunE 86 r.C.SetArgs([]string{"foo" + "@refs/heads/foo"}) 87 err = r.C.Execute() 88 assert.NoError(t, err) 89 cwd, err := os.Getwd() 90 assert.NoError(t, err) 91 assert.Equal(t, filepath.Join(cwd, "path", "to", "pkg", "dir"), r.Path) 92 } 93 94 var NoOpRunE = func(cmd *cobra.Command, args []string) error { return nil }