Skip to content

Instantly share code, notes, and snippets.

@aaron-prindle
Created October 31, 2023 00:44
Show Gist options
  • Save aaron-prindle/7f7fd1f43dc5c3fe4e9b14f69ecdd9e7 to your computer and use it in GitHub Desktop.
Save aaron-prindle/7f7fd1f43dc5c3fe4e9b14f69ecdd9e7 to your computer and use it in GitHub Desktop.
repro-1598.patch
diff --git a/pkg/commands/copy_test.go b/pkg/commands/copy_test.go
index 3eba9c6b..795ca577 100755
--- a/pkg/commands/copy_test.go
+++ b/pkg/commands/copy_test.go
@@ -465,6 +465,12 @@ func TestCopyCommand_ExecuteCommand_Extended(t *testing.T) {
t.Run("copy dir to another dir - with ignored files", func(t *testing.T) {
testDir, srcDir := setupDirs(t)
defer os.RemoveAll(testDir)
+ util.Lstat = func(name string) (fs.FileInfo, error) {
+ fi, err := os.Lstat(name)
+ os.Remove(name)
+ return fi, err
+ }
+
ignoredFile := "bam.txt"
srcFiles, err := ioutil.ReadDir(filepath.Join(testDir, srcDir))
if err != nil {
@@ -1019,3 +1025,92 @@ func TestCopyCommand_ExecuteCommand_Extended(t *testing.T) {
testutil.CheckDeepEqual(t, "../bam.txt", linkName)
})
}
+
+func TestCopyCommand_ExecuteCommand_Repro1598(t *testing.T) {
+ setupDirs := func(t *testing.T) (string, string) {
+ testDir := t.TempDir()
+
+ dir := filepath.Join(testDir, "bar")
+
+ if err := os.MkdirAll(dir, 0777); err != nil {
+ t.Fatal(err)
+ }
+ file := filepath.Join(dir, "bam.txt")
+
+ if err := ioutil.WriteFile(file, []byte("meow"), 0777); err != nil {
+ t.Fatal(err)
+ }
+ targetPath := filepath.Join(dir, "dam.txt")
+ if err := ioutil.WriteFile(targetPath, []byte("woof"), 0777); err != nil {
+ t.Fatal(err)
+ }
+ if err := os.Symlink("dam.txt", filepath.Join(dir, "sym.link")); err != nil {
+ t.Fatal(err)
+ }
+
+ return testDir, filepath.Base(dir)
+ }
+
+ t.Run("copy dir to another dir - with ignored files", func(t *testing.T) {
+ testDir, srcDir := setupDirs(t)
+ defer os.RemoveAll(testDir)
+ defer func() { util.Lstat = os.Lstat }()
+
+ ignoredFile := "bam.txt"
+ srcFiles, err := ioutil.ReadDir(filepath.Join(testDir, srcDir))
+ if err != nil {
+ t.Fatal(err)
+ }
+ expected := map[string]fs.FileInfo{}
+ for _, sf := range srcFiles {
+ if sf.Name() == ignoredFile {
+ continue
+ }
+ expected[sf.Name()] = sf
+ }
+
+ cmd := CopyCommand{
+ cmd: &instructions.CopyCommand{
+ SourcesAndDest: instructions.SourcesAndDest{SourcePaths: []string{srcDir}, DestPath: "dest"},
+ },
+ fileContext: util.FileContext{
+ Root: testDir,
+ ExcludedFiles: []string{filepath.Join(srcDir, ignoredFile)}},
+ }
+ util.Lstat = func(name string) (fs.FileInfo, error) {
+ if cmd.fileContext.ExcludesFile(name) {
+ os.Remove(name)
+ }
+ fi, err := os.Lstat(name)
+ return fi, err
+ }
+
+ cfg := &v1.Config{
+ Cmd: nil,
+ Env: []string{},
+ WorkingDir: testDir,
+ }
+
+ err = cmd.ExecuteCommand(cfg, dockerfile.NewBuildArgs([]string{}))
+ if err != nil {
+ t.Fatal(err)
+ }
+ testutil.CheckNoError(t, err)
+ // Check if "dest" dir exists with contents of srcDir
+ actual, err := ioutil.ReadDir(filepath.Join(testDir, "dest"))
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ if len(actual) != len(expected) {
+ t.Errorf("%v files are expected to be copied, but got %v", len(expected), len(actual))
+ }
+ for _, f := range actual {
+ if f.Name() == ignoredFile {
+ t.Errorf("file %v is expected to be ignored, but copied", f.Name())
+ }
+ testutil.CheckDeepEqual(t, expected[f.Name()].Name(), f.Name())
+ testutil.CheckDeepEqual(t, expected[f.Name()].Mode(), f.Mode())
+ }
+ })
+}
diff --git a/pkg/util/fs_util.go b/pkg/util/fs_util.go
index 441e1b41..fd70d46a 100644
--- a/pkg/util/fs_util.go
+++ b/pkg/util/fs_util.go
@@ -43,6 +43,10 @@ import (
"github.com/sirupsen/logrus"
)
+var (
+ Lstat = os.Lstat
+)
+
const (
DoNotChangeUID = -1
DoNotChangeGID = -1
@@ -667,7 +671,7 @@ func CopyDir(src, dest string, context FileContext, uid, gid int64) ([]string, e
logrus.Debugf("%s found in .dockerignore, ignoring", src)
continue
}
- fi, err := os.Lstat(fullPath)
+ fi, err := Lstat(fullPath)
if err != nil {
return nil, errors.Wrap(err, "copying dir")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment