MCPcopy
hub / github.com/go-git/go-git / TestZeroIndexModTime

Method TestZeroIndexModTime

utils/merkletrie/filesystem/node_test.go:332–388  ·  view source on GitHub ↗
(c *C)

Source from the content-addressed store, hash-verified

330}
331
332func (s *NoderSuite) TestZeroIndexModTime(c *C) {
333 fs := memfs.New()
334
335 // Write a file with known content
336 content := []byte("foo")
337 err := WriteFile(fs, "testfile", content, 0o644)
338 c.Assert(err, IsNil)
339
340 // Get file info
341 fi, err := fs.Stat("testfile")
342 c.Assert(err, IsNil)
343 modTime := fi.ModTime()
344
345 // Calculate the actual hash for the file content
346 actualHasher := plumbing.NewHasher(plumbing.BlobObject, int64(len(content)))
347 _, err = actualHasher.Write(content)
348 c.Assert(err, IsNil)
349 actualHash := actualHasher.Sum()
350
351 // Create a fake hash for the index entry (different from actual)
352 fakeContent := []byte("bar")
353 fakeHasher := plumbing.NewHasher(plumbing.BlobObject, int64(len(fakeContent)))
354 _, err = fakeHasher.Write(fakeContent)
355 c.Assert(err, IsNil)
356 fakeHash := fakeHasher.Sum()
357 c.Assert(actualHash, Not(DeepEquals), fakeHash, Commentf("test setup: hashes should be different"))
358
359 // Create an index with matching metadata but zero ModTime and wrong hash
360 idx := &index.Index{
361 Version: 2,
362 Entries: []*index.Entry{
363 {
364 Name: "testfile",
365 Hash: fakeHash, // Wrong hash to prove it gets re-hashed
366 Size: uint32(len(content)),
367 ModifiedAt: modTime,
368 Mode: filemode.Regular,
369 },
370 },
371 ModTime: time.Time{}, // Zero time - simulates in-memory storage
372 }
373
374 // Create node with this index
375 fsNode := NewRootNodeWithOptions(fs, nil, Options{Index: idx})
376
377 children, err := fsNode.Children()
378 c.Assert(err, IsNil)
379 c.Assert(children, HasLen, 1, Commentf("should have one file"))
380
381 fileNode := children[0]
382 fileHash := fileNode.Hash()
383
384 // The expected hash should be the actual file content, not the fake hash from index
385 expectedHash := append(actualHash[:], filemode.Regular.Bytes()...)
386
387 c.Assert(expectedHash, DeepEquals, fileHash, Commentf("should hash actual file content when idx.ModTime is zero, not use stale index hash"))
388}

Callers

nothing calls this directly

Calls 10

SumMethod · 0.95
NewHasherFunction · 0.92
WriteFileFunction · 0.85
NewRootNodeWithOptionsFunction · 0.85
ModTimeMethod · 0.80
StatMethod · 0.65
ChildrenMethod · 0.65
HashMethod · 0.65
WriteMethod · 0.45
BytesMethod · 0.45

Tested by

no test coverage detected