Skip to content

Commit dce9426

Browse files
authored
Merge pull request #5030 from koba1t/fix/error_when_no_path_match
be error when no path matching
2 parents 0add0f9 + 565cff2 commit dce9426

File tree

5 files changed

+51
-17
lines changed

5 files changed

+51
-17
lines changed

kustomize/commands/create/create_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"reflect"
88
"testing"
99

10+
"github.com/stretchr/testify/assert"
1011
"sigs.k8s.io/kustomize/api/provider"
1112
"sigs.k8s.io/kustomize/api/types"
1213
"sigs.k8s.io/kustomize/kustomize/v5/commands/internal/kustfile"
@@ -53,6 +54,14 @@ func TestCreateWithResources(t *testing.T) {
5354
}
5455
}
5556

57+
func TestCreateWithResourcesWithFileNotFound(t *testing.T) {
58+
fSys := filesys.MakeEmptyDirInMemory()
59+
assert.NoError(t, fSys.WriteFile("foo.yaml", []byte("")))
60+
opts := createFlags{resources: "foo.yaml,bar.yaml"}
61+
err := runCreate(opts, fSys, factory)
62+
assert.EqualError(t, err, "bar.yaml has no match: must build at directory: not a valid directory: 'bar.yaml' doesn't exist")
63+
}
64+
5665
func TestCreateWithNamespace(t *testing.T) {
5766
fSys := filesys.MakeFsInMemory()
5867
want := "foo"

kustomize/commands/edit/add/addcomponent_test.go

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
"github.com/stretchr/testify/assert"
1010
"github.com/stretchr/testify/require"
11+
"sigs.k8s.io/kustomize/api/konfig"
1112
testutils_test "sigs.k8s.io/kustomize/kustomize/v5/commands/internal/testutils"
1213
"sigs.k8s.io/kustomize/kyaml/filesys"
1314
)
@@ -38,7 +39,7 @@ func TestAddComponentHappyPath(t *testing.T) {
3839
}
3940

4041
func TestAddComponentAlreadyThere(t *testing.T) {
41-
fSys := filesys.MakeFsInMemory()
42+
fSys := filesys.MakeEmptyDirInMemory()
4243
err := fSys.WriteFile(componentFileName, []byte(componentFileContent))
4344
require.NoError(t, err)
4445
testutils_test.WriteTestKustomization(fSys)
@@ -51,19 +52,19 @@ func TestAddComponentAlreadyThere(t *testing.T) {
5152
assert.NoError(t, cmd.RunE(cmd, args))
5253
}
5354

55+
// Test for trying to add the kustomization.yaml file itself for resources.
56+
// This adding operation is not allowed.
5457
func TestAddKustomizationFileAsComponent(t *testing.T) {
55-
fSys := filesys.MakeFsInMemory()
56-
err := fSys.WriteFile(componentFileName, []byte(componentFileContent))
57-
require.NoError(t, err)
58+
fSys := filesys.MakeEmptyDirInMemory()
5859
testutils_test.WriteTestKustomization(fSys)
5960

6061
cmd := newCmdAddComponent(fSys)
61-
args := []string{componentFileName}
62+
args := []string{konfig.DefaultKustomizationFileName()}
6263
require.NoError(t, cmd.RunE(cmd, args))
6364

6465
content, err := testutils_test.ReadTestKustomization(fSys)
6566
require.NoError(t, err)
66-
assert.NotContains(t, string(content), componentFileName)
67+
assert.NotContains(t, string(content), konfig.DefaultKustomizationFileName())
6768
}
6869

6970
func TestAddComponentNoArgs(t *testing.T) {
@@ -73,3 +74,13 @@ func TestAddComponentNoArgs(t *testing.T) {
7374
err := cmd.Execute()
7475
assert.EqualError(t, err, "must specify a component file")
7576
}
77+
78+
func TestAddComponentFileNotFound(t *testing.T) {
79+
fSys := filesys.MakeEmptyDirInMemory()
80+
81+
cmd := newCmdAddComponent(fSys)
82+
args := []string{componentFileName}
83+
84+
err := cmd.RunE(cmd, args)
85+
assert.EqualError(t, err, componentFileName+" has no match: must build at directory: not a valid directory: '"+componentFileName+"' doesn't exist")
86+
}

kustomize/commands/edit/add/addresource_test.go

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
"github.com/stretchr/testify/assert"
1010
"github.com/stretchr/testify/require"
11+
"sigs.k8s.io/kustomize/api/konfig"
1112
testutils_test "sigs.k8s.io/kustomize/kustomize/v5/commands/internal/testutils"
1213
"sigs.k8s.io/kustomize/kyaml/filesys"
1314
)
@@ -57,7 +58,7 @@ replacements:
5758
}
5859

5960
func TestAddResourceAlreadyThere(t *testing.T) {
60-
fSys := filesys.MakeFsInMemory()
61+
fSys := filesys.MakeEmptyDirInMemory()
6162
err := fSys.WriteFile(resourceFileName, []byte(resourceFileContent))
6263
require.NoError(t, err)
6364
testutils_test.WriteTestKustomization(fSys)
@@ -70,20 +71,20 @@ func TestAddResourceAlreadyThere(t *testing.T) {
7071
assert.NoError(t, cmd.RunE(cmd, args))
7172
}
7273

74+
// Test for trying to add the kustomization.yaml file itself for resources.
75+
// This adding operation is not allowed.
7376
func TestAddKustomizationFileAsResource(t *testing.T) {
74-
fSys := filesys.MakeFsInMemory()
75-
err := fSys.WriteFile(resourceFileName, []byte(resourceFileContent))
76-
require.NoError(t, err)
77+
fSys := filesys.MakeEmptyDirInMemory()
7778
testutils_test.WriteTestKustomization(fSys)
7879

7980
cmd := newCmdAddResource(fSys)
80-
args := []string{resourceFileName}
81+
args := []string{konfig.DefaultKustomizationFileName()}
8182
assert.NoError(t, cmd.RunE(cmd, args))
8283

8384
content, err := testutils_test.ReadTestKustomization(fSys)
8485
assert.NoError(t, err)
8586

86-
assert.NotContains(t, string(content), resourceFileName)
87+
assert.NotContains(t, string(content), konfig.DefaultKustomizationFileName())
8788
}
8889

8990
func TestAddResourceNoArgs(t *testing.T) {
@@ -94,3 +95,13 @@ func TestAddResourceNoArgs(t *testing.T) {
9495
assert.Error(t, err)
9596
assert.Equal(t, "must specify a resource file", err.Error())
9697
}
98+
99+
func TestAddResourceFileNotFound(t *testing.T) {
100+
fSys := filesys.MakeEmptyDirInMemory()
101+
102+
cmd := newCmdAddResource(fSys)
103+
args := []string{resourceFileName}
104+
105+
err := cmd.RunE(cmd, args)
106+
assert.EqualError(t, err, resourceFileName+" has no match: must build at directory: not a valid directory: '"+resourceFileName+"' doesn't exist")
107+
}

kustomize/commands/internal/util/util.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ func GlobPatterns(fSys filesys.FileSystem, patterns []string) ([]string, error)
3030
return result, nil
3131
}
3232

33-
// GlobPatterns accepts a slice of glob strings and returns the set of
34-
// matching file paths. If files are not found, will try load from remote.
33+
// GlobPatterns accepts a slice of glob strings and returns the set of matching file paths. If files are not found, will try load from remote.
34+
// It returns an error if there are no matching files or it can't load from remote.
3535
func GlobPatternsWithLoader(fSys filesys.FileSystem, ldr ifc.Loader, patterns []string) ([]string, error) {
3636
var result []string
3737
for _, pattern := range patterns {
@@ -42,7 +42,7 @@ func GlobPatternsWithLoader(fSys filesys.FileSystem, ldr ifc.Loader, patterns []
4242
if len(files) == 0 {
4343
loader, err := ldr.New(pattern)
4444
if err != nil {
45-
log.Printf("%s has no match", pattern)
45+
return nil, fmt.Errorf("%s has no match: %w", pattern, err)
4646
} else {
4747
result = append(result, pattern)
4848
if loader != nil {

kustomize/commands/internal/util/util_test.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,11 @@ func TestGlobPatternsWithLoaderRemoteFile(t *testing.T) {
8989
}
9090

9191
// test load invalid file
92-
resources, err = GlobPatternsWithLoader(fSys, ldr, []string{"http://invalid"})
93-
if err != nil {
92+
invalidURL := "http://invalid"
93+
resources, err = GlobPatternsWithLoader(fSys, ldr, []string{invalidURL})
94+
if err == nil {
95+
t.Fatalf("expected error but did not receive one")
96+
} else if err.Error() != invalidURL+" has no match: "+invalidURL+" not exist" {
9497
t.Fatalf("unexpected load error: %v", err)
9598
}
9699
if len(resources) > 0 {

0 commit comments

Comments
 (0)