-
Notifications
You must be signed in to change notification settings - Fork 261
Expand file tree
/
Copy pathcmddiff_test.go
More file actions
98 lines (83 loc) · 3.01 KB
/
Copy pathcmddiff_test.go
File metadata and controls
98 lines (83 loc) · 3.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Copyright 2019 The kpt Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package diff_test
import (
"os"
"path/filepath"
"strings"
"testing"
"github.com/kptdev/kpt/commands/pkg/diff"
"github.com/kptdev/kpt/commands/pkg/get"
"github.com/kptdev/kpt/internal/testutil"
"github.com/kptdev/kpt/pkg/printer/fake"
"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
)
func TestMain(m *testing.M) {
os.Exit(testutil.ConfigureTestKptCache(m))
}
func TestCmdInvalidDiffType(t *testing.T) {
runner := diff.NewRunner(fake.CtxWithDefaultPrinter(), "")
runner.C.SetArgs([]string{"--diff-type", "invalid"})
err := runner.C.Execute()
assert.EqualError(t,
err,
"invalid diff-type 'invalid': supported diff-types are: local, remote, combined, 3way")
}
func TestCmdInvalidDiffTool(t *testing.T) {
runner := diff.NewRunner(fake.CtxWithDefaultPrinter(), "")
runner.C.SetArgs([]string{"--diff-tool", "nodiff"})
err := runner.C.Execute()
assert.EqualError(t,
err,
"diff-tool 'nodiff' not found in the PATH")
}
func TestCmdExecute(t *testing.T) {
g, w, clean := testutil.SetupRepoAndWorkspace(t, testutil.Content{
Data: testutil.Dataset1,
Branch: "master",
})
defer clean()
defer testutil.Chdir(t, w.WorkspaceDirectory)()
dest := filepath.Join(w.WorkspaceDirectory, g.RepoName)
getRunner := get.NewRunner(fake.CtxWithDefaultPrinter(), "")
getRunner.Command.SetArgs([]string{"file://" + g.RepoDirectory + ".git/"})
err := getRunner.Command.Execute()
assert.NoError(t, err)
runner := diff.NewRunner(fake.CtxWithDefaultPrinter(), "")
runner.C.SetArgs([]string{dest, "--diff-type", "local"})
err = runner.C.Execute()
assert.NoError(t, err)
}
func TestCmd_flagAndArgParsing_Symlink(t *testing.T) {
dir := t.TempDir()
defer testutil.Chdir(t, dir)()
err := os.MkdirAll(filepath.Join(dir, "path", "to", "pkg", "dir"), 0700)
assert.NoError(t, err)
err = os.Symlink(filepath.Join("path", "to", "pkg", "dir"), "foo")
if err != nil {
t.Skipf("skipping test due to symlink creation failure (requires admin/developer mode on Windows): %v", err)
}
// verify the branch ref is set to the correct value
r := diff.NewRunner(fake.CtxWithDefaultPrinter(), "kpt")
r.C.RunE = NoOpRunE
r.C.SetArgs([]string{"foo" + "@refs/heads/foo"})
err = r.C.Execute()
assert.NoError(t, err)
cwd, err := os.Getwd()
assert.NoError(t, err)
expected := filepath.Join(cwd, "path", "to", "pkg", "dir")
assert.Equal(t, strings.ToLower(expected), strings.ToLower(r.Path))
}
var NoOpRunE = func(_ *cobra.Command, _ []string) error { return nil }