From d238d8952ba5e64d15f4cfbb4295aa76da38e835 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luka=20Marku=C5=A1i=C4=87?= Date: Tue, 2 Aug 2022 23:46:02 +0200 Subject: [PATCH] Add AppendLineToFile tests --- pkg/commands/oscommands/os_test.go | 41 ++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/pkg/commands/oscommands/os_test.go b/pkg/commands/oscommands/os_test.go index 0152fec58..389fdfc8e 100644 --- a/pkg/commands/oscommands/os_test.go +++ b/pkg/commands/oscommands/os_test.go @@ -1,6 +1,7 @@ package oscommands import ( + "io/ioutil" "os" "testing" @@ -135,3 +136,43 @@ func TestOSCommandFileType(t *testing.T) { _ = os.RemoveAll(s.path) } } + +func TestOSCommandAppendLineToFile(t *testing.T) { + type scenario struct { + path string + setup func(string) + } + + scenarios := []scenario{ + { + "testFile", + func(path string) { + if err := ioutil.WriteFile(path, []byte("hello"), 0o600); err != nil { + panic(err) + } + }, + }, + { + "testFileWithNewline", + func(path string) { + if err := ioutil.WriteFile(path, []byte("hello\n"), 0o600); err != nil { + panic(err) + } + }, + }, + } + + for _, s := range scenarios { + s.setup(s.path) + osCommand := NewDummyOSCommand() + if err := osCommand.AppendLineToFile(s.path, "world"); err != nil { + panic(err) + } + f, err := ioutil.ReadFile(s.path) + if err != nil { + panic(err) + } + assert.EqualValues(t, "hello\nworld\n", string(f)) + _ = os.RemoveAll(s.path) + } +}