Skip to content

Commit

Permalink
fix symlink error (#425)
Browse files Browse the repository at this point in the history
  • Loading branch information
airycanon authored Apr 10, 2023
1 parent 169f6ef commit b1a6ad7
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 7 deletions.
16 changes: 14 additions & 2 deletions hash/hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (
"hash"
"hash/fnv"
"io/fs"
"io/ioutil"
"os"
"path/filepath"

"github.com/davecgh/go-spew/spew"
Expand Down Expand Up @@ -110,7 +110,19 @@ func HashFolder(ctx context.Context, folder string, filters ...HashFolderFilter)
}
}
if !d.IsDir() {
if data, readErr := ioutil.ReadFile(path); readErr == nil {
var (
link string
data []byte
readErr error
)
if d.Type() == os.ModeSymlink {
link, readErr = os.Readlink(path)
data = []byte(link)
} else {
data, readErr = os.ReadFile(path)
}

if readErr == nil {
digestHash := digest.FromBytes(data)
log.Debugf("hash for path: %s hash: %q", path, digestHash)
digests = append(digests, digestHash)
Expand Down
29 changes: 24 additions & 5 deletions hash/hash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,15 @@ import (
"fmt"
"hash/adler32"
"io/fs"
"k8s.io/apimachinery/pkg/util/rand"
"os"
"os/exec"
"strings"
"testing"

"github.com/davecgh/go-spew/spew"
"github.com/katanomi/pkg/command/io"
"github.com/onsi/gomega"

"github.com/davecgh/go-spew/spew"
"k8s.io/apimachinery/pkg/util/rand"
)

type A struct {
Expand Down Expand Up @@ -346,6 +345,26 @@ func TestHashFolder(t *testing.T) {
return !strings.HasPrefix(path, "testdata/copy/rand")
},
},
"with symlink": {
Folder: "testdata/copy",
Action: func(folder string, t *testing.T) error {
os.RemoveAll("testdata/copy")
if err := io.Copy("testdata/chart", "testdata/copy"); err != nil {
return err
}
if err := os.Symlink("../chart", "testdata/copy/symlink"); err != nil {
return err
}

return nil
},
// new hash
Expected: "sha256:81cbbdb10d09c20294eea4347b594d6c13c8c9d8ea1e4a3e0cb28e2052482c97",
Error: nil,
AfterAction: func() {
os.RemoveAll("testdata/copy")
},
},
}

for k, test := range table {
Expand All @@ -361,13 +380,13 @@ func TestHashFolder(t *testing.T) {
if test.AfterAction != nil {
test.AfterAction()
}
g.Expect(hashResult).To(gomega.ContainSubstring(test.Expected))
t.Logf("expected: %q == %q", hashResult, test.Expected)
if test.Error == nil {
g.Expect(err).To(gomega.BeNil())
} else {
g.Expect(err).ToNot(gomega.BeNil())
}
g.Expect(hashResult).To(gomega.ContainSubstring(test.Expected))
t.Logf("expected: %q == %q", hashResult, test.Expected)
})
}
}
Expand Down

0 comments on commit b1a6ad7

Please sign in to comment.