From 15390073f288b869f4c3f6485a7ec457b2145ee7 Mon Sep 17 00:00:00 2001 From: Thiago Nache Carvalho Date: Wed, 28 Jun 2023 06:53:20 -0300 Subject: [PATCH] Create truncates the file if it already exist. We should use OpenFile with append mode. Also fixes the test to stop if it cannot write data to the database --- gobdb.go | 2 +- gobdb_test.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/gobdb.go b/gobdb.go index 2840129..a734f51 100644 --- a/gobdb.go +++ b/gobdb.go @@ -108,7 +108,7 @@ func (db Gobdb[T]) List() []T { // during the process, the method returns the error with details about what // went wrong. func (db *Gobdb[T]) Add(d ...T) error { - file, err := os.Create(db.path) + file, err := os.OpenFile(db.path, os.O_APPEND|os.O_WRONLY, os.ModeAppend) if err != nil { return fmt.Errorf("unable to open file: %w", err) } diff --git a/gobdb_test.go b/gobdb_test.go index e9f2520..ff67720 100644 --- a/gobdb_test.go +++ b/gobdb_test.go @@ -30,7 +30,7 @@ func TestAdd(t *testing.T) { want := []string{"barbara", "victor", "walter"} err = db.Add(want...) if err != nil { - t.Errorf("unable to add data %v: %s", want, err) + t.Fatalf("unable to add data %v: %s", want, err) } got := db.List() if !cmp.Equal(want, got) {