Skip to content

Commit

Permalink
fix: warn instead of error when COPY wildcard does not match any files (
Browse files Browse the repository at this point in the history
#3127)

* when using wildcard hitting ResolveEnvAndWildcards ignore error when there is no file match

* adding unit test

* fix unit test wrong check in expected

* fix unit test for make sure there is no file copied

* copy with wildcard warning and success when no match file

* fix Test_IsSrcsValid since its changes the behaviour
  • Loading branch information
prima101112 authored May 14, 2024
1 parent 423d20c commit 34905d6
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 3 deletions.
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 @@ -286,8 +286,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 @@ -489,7 +489,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

0 comments on commit 34905d6

Please sign in to comment.