fix: warn instead of error when COPY wildcard does not match any files (#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
This commit is contained in:
parent
423d20cee2
commit
34905d62e2
|
|
@ -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 {
|
||||
|
|
@ -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))
|
||||
})
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
},
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue