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

Method TestRacyGit

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

Source from the content-addressed store, hash-verified

253}
254
255func (s *NoderSuite) TestRacyGit(c *C) {
256 td, err := os.MkdirTemp(c.MkDir(), "racy-git")
257 c.Assert(err, IsNil)
258 fs := osfs.New(td)
259
260 origContent := []byte("foo")
261 err = WriteFile(fs, "racyfile", origContent, 0o644)
262 c.Assert(err, IsNil)
263
264 fi, err := fs.Stat("racyfile")
265 c.Assert(err, IsNil)
266 modTime := fi.ModTime()
267
268 fooHasher := plumbing.NewHasher(plumbing.BlobObject, int64(len(origContent)))
269 _, err = fooHasher.Write(origContent)
270 c.Assert(err, IsNil)
271 fooHash := fooHasher.Sum()
272
273 idx := &index.Index{
274 Version: 2,
275 Entries: []*index.Entry{
276 {
277 Name: "racyfile",
278 Hash: fooHash,
279 Size: uint32(len(origContent)),
280 ModifiedAt: modTime,
281 Mode: filemode.Regular,
282 },
283 },
284 ModTime: modTime,
285 }
286
287 newContent := []byte("bar")
288 c.Assert(len(origContent), Equals, len(newContent), Commentf("test setup requires same size"))
289
290 err = WriteFile(fs, "racyfile", newContent, 0o644)
291 c.Assert(err, IsNil)
292
293 err = os.Chtimes(filepath.Join(td, "racyfile"), modTime, modTime)
294 c.Assert(err, IsNil)
295
296 fi, err = fs.Stat("racyfile")
297 c.Assert(err, IsNil)
298 c.Assert(uint32(len(origContent)), Equals, uint32(fi.Size()), Commentf("size should match"))
299 c.Assert(modTime, Equals, fi.ModTime(), Commentf("mtime should match"))
300
301 actualContent, err := util.ReadFile(fs, "racyfile")
302 c.Assert(err, IsNil)
303 c.Assert(actualContent, DeepEquals, newContent, Commentf("content should have changed"))
304 c.Assert(origContent, Not(DeepEquals), actualContent, Commentf("content should be different"))
305
306 barHasher := plumbing.NewHasher(plumbing.BlobObject, int64(len(newContent)))
307 _, err = barHasher.Write(newContent)
308 c.Assert(err, IsNil)
309 barHash := barHasher.Sum()
310 c.Assert(fooHash, Not(DeepEquals), barHash, Commentf("hashes should be different"))
311
312 fsNode := NewRootNodeWithOptions(fs, nil, Options{Index: idx})

Callers

nothing calls this directly

Calls 13

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

Tested by

no test coverage detected