Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

give warn instead of error when wildcard not match any files #3127

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 40 additions & 1 deletion pkg/commands/copy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -450,8 +450,9 @@ func Test_CopyEnvAndWildcards(t *testing.T) {

return testDir, filepath.Base(dir)
}
testDir, srcDir := setupDirs(t)

t.Run("copy sources into a dir defined in env variable", func(t *testing.T) {
testDir, srcDir := setupDirs(t)
defer os.RemoveAll(testDir)
expected, err := readDirectory(filepath.Join(testDir, srcDir))
if err != nil {
Expand Down Expand Up @@ -487,6 +488,44 @@ func Test_CopyEnvAndWildcards(t *testing.T) {
testutil.CheckDeepEqual(t, expected[i].Name(), f.Name())
testutil.CheckDeepEqual(t, expected[i].Mode(), f.Mode())
}

})

t.Run("copy sources into a dir defined in env variable with no file found", func(t *testing.T) {
testDir, srcDir := setupDirs(t)
defer os.RemoveAll(testDir)

targetPath := filepath.Join(testDir, "target") + "/"

cmd := CopyCommand{
cmd: &instructions.CopyCommand{
//should only dam and bam be copied
SourcesAndDest: instructions.SourcesAndDest{SourcePaths: []string{srcDir + "/tam[s]"}, DestPath: "$TARGET_PATH"},
},
fileContext: util.FileContext{Root: testDir},
}

cfg := &v1.Config{
Cmd: nil,
Env: []string{"TARGET_PATH=" + targetPath},
WorkingDir: testDir,
}

err := cmd.ExecuteCommand(cfg, dockerfile.NewBuildArgs([]string{}))
if err != nil {
t.Fatal(err)
}
testutil.CheckNoError(t, err)

actual, err := readDirectory(targetPath)

//check it should error out since no files are copied and targetPath is not created
if err == nil {
t.Fatal("expected error no dirrectory but got nil")
}

//actual should empty since no files are copied
testutil.CheckDeepEqual(t, 0, len(actual))
})
}

Expand Down
4 changes: 3 additions & 1 deletion pkg/util/command_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,8 +285,10 @@ func IsSrcsValid(srcsAndDest instructions.SourcesAndDest, resolvedSources []stri
totalFiles++
}
}
// ignore the case where whildcards and there are no files to copy
if totalFiles == 0 {
return errors.New("copy failed: no source files specified")
// using log warning instead of return errors.New("copy failed: no source files specified")
logrus.Warn("No files to copy")
}
// If there are wildcards, and the destination is a file, there must be exactly one file to copy over,
// Otherwise, return an error
Expand Down
11 changes: 10 additions & 1 deletion pkg/util/command_util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,16 @@ var isSrcValidTests = []struct {
"ignore/baz",
"ignore/bar",
},
shouldErr: true,
shouldErr: false,
},
{
name: "copy two srcs, wildcard and no file match, to file",
srcsAndDest: []string{
"ignore/ba[s]",
"dest",
},
resolvedSources: []string{},
shouldErr: false,
},
}

Expand Down
Loading