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") +}