diff --git a/pkg/commands/copy_test.go b/pkg/commands/copy_test.go index 5f967622b7..87be63f86a 100755 --- a/pkg/commands/copy_test.go +++ b/pkg/commands/copy_test.go @@ -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)) }) } diff --git a/pkg/util/command_util.go b/pkg/util/command_util.go index f2866770b0..d20ed52075 100644 --- a/pkg/util/command_util.go +++ b/pkg/util/command_util.go @@ -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 diff --git a/pkg/util/command_util_test.go b/pkg/util/command_util_test.go index 963427820a..81bbbbc461 100644 --- a/pkg/util/command_util_test.go +++ b/pkg/util/command_util_test.go @@ -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, }, }