Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix symlink error #425

Merged
merged 1 commit into from
Apr 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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