Skip to content

Commit 2151da0

Browse files
committed
Ensure packObjects test helper produces deterministic hashes by iterating in sorted order
1 parent 2010338 commit 2151da0

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

internal/files/writer.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ func writeObject(rootDir string, cacheObjectsDir string, reader *db.TarReader, h
9090
return err
9191
}
9292
hashHex := hex.EncodeToString(content)
93-
return hardlinkDir(filepath.Join(cacheObjectsDir, hashHex, header.Name), path)
93+
return HardlinkDir(filepath.Join(cacheObjectsDir, hashHex, header.Name), path)
9494

9595
case tar.TypeReg:
9696
dir := filepath.Dir(path)
@@ -193,7 +193,7 @@ func makeSymlink(oldname, newname string) error {
193193
return nil
194194
}
195195

196-
func hardlinkDir(olddir, newdir string) error {
196+
func HardlinkDir(olddir, newdir string) error {
197197
if fileExists(newdir) {
198198
err := os.RemoveAll(newdir)
199199
if err != nil {
@@ -203,7 +203,7 @@ func hardlinkDir(olddir, newdir string) error {
203203

204204
return filepath.Walk(olddir, func(oldpath string, info fs.FileInfo, err error) error {
205205
if err != nil {
206-
return err
206+
return fmt.Errorf("failed to walk dir: %v, %w", info, err)
207207
}
208208

209209
newpath := filepath.Join(newdir, strings.TrimPrefix(oldpath, olddir))

test/shared_test.go

+12-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"net"
1111
"os"
1212
"path/filepath"
13+
"sort"
1314
"strings"
1415
"testing"
1516

@@ -223,13 +224,22 @@ func packObjects(tc util.TestCtx, objects map[string]expectedObject) []byte {
223224
contentWriter := db.NewTarWriter()
224225
defer contentWriter.Close()
225226

226-
for path, info := range objects {
227+
var keys []string
228+
for key := range objects {
229+
keys = append(keys, key)
230+
}
231+
232+
sort.Strings(keys)
233+
234+
// iterate the objects for packing in a deterministic order
235+
for _, key := range keys {
236+
info := objects[key]
227237
mode := info.mode
228238
if mode == 0 {
229239
mode = 0755
230240
}
231241

232-
object := db.NewUncachedTarObject(path, mode, int64(len(info.content)), info.deleted, []byte(info.content))
242+
object := db.NewUncachedTarObject(key, mode, int64(len(info.content)), info.deleted, []byte(info.content))
233243

234244
err := contentWriter.WriteObject(&object)
235245
require.NoError(tc.T(), err, "write content to TAR")

0 commit comments

Comments
 (0)