Skip to content

Commit

Permalink
Merge pull request #905 from poy/fixes/904
Browse files Browse the repository at this point in the history
when copying, skip files with the same name
  • Loading branch information
tejal29 authored Dec 9, 2019
2 parents 56f92e7 + 0a2f295 commit 03b1b1c
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
6 changes: 6 additions & 0 deletions pkg/util/fs_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,12 @@ func CopyFile(src, dest, buildcontext string) (bool, error) {
logrus.Debugf("%s found in .dockerignore, ignoring", src)
return true, nil
}
if src == dest {
// This is a no-op. Move on, but don't list it as ignored.
// We have to make sure we do this so we don't overwrite our own file.
// See iusse #904 for an example.
return false, nil
}
fi, err := os.Stat(src)
if err != nil {
return false, err
Expand Down
38 changes: 38 additions & 0 deletions pkg/util/fs_util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -825,3 +825,41 @@ func Test_correctDockerignoreFileIsUsed(t *testing.T) {
}
}
}

func Test_CopyFile_skips_self(t *testing.T) {
t.Parallel()
tempDir, err := ioutil.TempDir("", "kaniko_test")
if err != nil {
t.Fatal(err)
}

tempFile := filepath.Join(tempDir, "foo")
expected := "bar"

if err := ioutil.WriteFile(
tempFile,
[]byte(expected),
0755,
); err != nil {
t.Fatal(err)
}

ignored, err := CopyFile(tempFile, tempFile, "")
if err != nil {
t.Fatal(err)
}

if ignored {
t.Fatal("expected file to NOT be ignored")
}

// Ensure file has expected contents
actualData, err := ioutil.ReadFile(tempFile)
if err != nil {
t.Fatal(err)
}

if actual := string(actualData); actual != expected {
t.Fatalf("expected file contents to be %q, but got %q", expected, actual)
}
}

0 comments on commit 03b1b1c

Please sign in to comment.