From 568b9c3f4748c2c62ce5e8f0296f25d2ffae62fa Mon Sep 17 00:00:00 2001 From: nikandfor Date: Sun, 8 Jan 2023 16:42:58 +0400 Subject: [PATCH] memfs: fix trunc-append case --- memfs/memory.go | 8 ++++---- memfs/memory_test.go | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/memfs/memory.go b/memfs/memory.go index f217693..dab7396 100644 --- a/memfs/memory.go +++ b/memfs/memory.go @@ -310,14 +310,14 @@ func (f *file) Duplicate(filename string, mode os.FileMode, flag int) billy.File flag: flag, } - if isAppend(flag) { - new.position = int64(new.content.Len()) - } - if isTruncate(flag) { new.content.Truncate() } + if isAppend(flag) { + new.position = int64(new.content.Len()) + } + return new } diff --git a/memfs/memory_test.go b/memfs/memory_test.go index 8bab98c..b23f7ae 100644 --- a/memfs/memory_test.go +++ b/memfs/memory_test.go @@ -8,6 +8,7 @@ import ( "github.com/go-git/go-billy/v5" "github.com/go-git/go-billy/v5/test" + "github.com/go-git/go-billy/v5/util" . "gopkg.in/check.v1" ) @@ -83,3 +84,22 @@ func (s *MemorySuite) TestOrder(c *C) { } } } + +func (s *MemorySuite) TestTruncateAppend(c *C) { + err := util.WriteFile(s.FS, "truncate_append", []byte("file-content"), 0666) + c.Assert(err, IsNil) + + f, err := s.FS.OpenFile("truncate_append", os.O_WRONLY|os.O_TRUNC|os.O_APPEND, 0666) + c.Assert(err, IsNil) + + n, err := f.Write([]byte("replace")) + c.Assert(err, IsNil) + c.Assert(n, Equals, len("replace")) + + err = f.Close() + c.Assert(err, IsNil) + + data, err := util.ReadFile(s.FS, "truncate_append") + c.Assert(err, IsNil) + c.Assert(string(data), Equals, "replace") +}