Skip to content

Commit 34905d6

Browse files
authored
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
1 parent 423d20c commit 34905d6

File tree

3 files changed

+53
-3
lines changed

3 files changed

+53
-3
lines changed

Diff for: pkg/commands/copy_test.go

+40-1
Original file line numberDiff line numberDiff line change
@@ -450,8 +450,9 @@ func Test_CopyEnvAndWildcards(t *testing.T) {
450450

451451
return testDir, filepath.Base(dir)
452452
}
453-
testDir, srcDir := setupDirs(t)
453+
454454
t.Run("copy sources into a dir defined in env variable", func(t *testing.T) {
455+
testDir, srcDir := setupDirs(t)
455456
defer os.RemoveAll(testDir)
456457
expected, err := readDirectory(filepath.Join(testDir, srcDir))
457458
if err != nil {
@@ -487,6 +488,44 @@ func Test_CopyEnvAndWildcards(t *testing.T) {
487488
testutil.CheckDeepEqual(t, expected[i].Name(), f.Name())
488489
testutil.CheckDeepEqual(t, expected[i].Mode(), f.Mode())
489490
}
491+
492+
})
493+
494+
t.Run("copy sources into a dir defined in env variable with no file found", func(t *testing.T) {
495+
testDir, srcDir := setupDirs(t)
496+
defer os.RemoveAll(testDir)
497+
498+
targetPath := filepath.Join(testDir, "target") + "/"
499+
500+
cmd := CopyCommand{
501+
cmd: &instructions.CopyCommand{
502+
//should only dam and bam be copied
503+
SourcesAndDest: instructions.SourcesAndDest{SourcePaths: []string{srcDir + "/tam[s]"}, DestPath: "$TARGET_PATH"},
504+
},
505+
fileContext: util.FileContext{Root: testDir},
506+
}
507+
508+
cfg := &v1.Config{
509+
Cmd: nil,
510+
Env: []string{"TARGET_PATH=" + targetPath},
511+
WorkingDir: testDir,
512+
}
513+
514+
err := cmd.ExecuteCommand(cfg, dockerfile.NewBuildArgs([]string{}))
515+
if err != nil {
516+
t.Fatal(err)
517+
}
518+
testutil.CheckNoError(t, err)
519+
520+
actual, err := readDirectory(targetPath)
521+
522+
//check it should error out since no files are copied and targetPath is not created
523+
if err == nil {
524+
t.Fatal("expected error no dirrectory but got nil")
525+
}
526+
527+
//actual should empty since no files are copied
528+
testutil.CheckDeepEqual(t, 0, len(actual))
490529
})
491530
}
492531

Diff for: pkg/util/command_util.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -286,8 +286,10 @@ func IsSrcsValid(srcsAndDest instructions.SourcesAndDest, resolvedSources []stri
286286
totalFiles++
287287
}
288288
}
289+
// ignore the case where whildcards and there are no files to copy
289290
if totalFiles == 0 {
290-
return errors.New("copy failed: no source files specified")
291+
// using log warning instead of return errors.New("copy failed: no source files specified")
292+
logrus.Warn("No files to copy")
291293
}
292294
// If there are wildcards, and the destination is a file, there must be exactly one file to copy over,
293295
// Otherwise, return an error

Diff for: pkg/util/command_util_test.go

+10-1
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,16 @@ var isSrcValidTests = []struct {
489489
"ignore/baz",
490490
"ignore/bar",
491491
},
492-
shouldErr: true,
492+
shouldErr: false,
493+
},
494+
{
495+
name: "copy two srcs, wildcard and no file match, to file",
496+
srcsAndDest: []string{
497+
"ignore/ba[s]",
498+
"dest",
499+
},
500+
resolvedSources: []string{},
501+
shouldErr: false,
493502
},
494503
}
495504

0 commit comments

Comments
 (0)