mirror of
https://github.com/GoogleContainerTools/kaniko
synced 2026-10-01 01:43:46 +02:00
when copying, skip files with the same name
When using the COPY command, if the source and destination have the same the file should be skipped rather than copied. This is to prevent the file from being overwritten and therefore producing an empty file. fixes #904
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user