From 86d5654d20f8671a9bf4687469d53c6aca8f274a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luka=20Marku=C5=A1i=C4=87?= Date: Fri, 17 Jun 2022 14:45:37 +0200 Subject: [PATCH 1/7] Preserve trailing newline setting when adding to gitignore --- pkg/commands/oscommands/os.go | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/pkg/commands/oscommands/os.go b/pkg/commands/oscommands/os.go index b9ea928be..3a42d09db 100644 --- a/pkg/commands/oscommands/os.go +++ b/pkg/commands/oscommands/os.go @@ -109,13 +109,32 @@ func (c *OSCommand) Quote(message string) string { // AppendLineToFile adds a new line in file func (c *OSCommand) AppendLineToFile(filename, line string) error { c.LogCommand(fmt.Sprintf("Appending '%s' to file '%s'", line, filename), false) - f, err := os.OpenFile(filename, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0o600) + f, err := os.OpenFile(filename, os.O_APPEND|os.O_RDWR|os.O_CREATE, 0o600) if err != nil { return utils.WrapError(err) } defer f.Close() - _, err = f.WriteString("\n" + line) + info, err := os.Stat(filename) + if err != nil { + return utils.WrapError(err) + } + + // read last char + buf := make([]byte, 1) + if _, err := f.ReadAt(buf, info.Size()-1); err != nil { + return utils.WrapError(err) + } + + // if the last byte of the file is not a newline, add it + if []byte("\n")[0] != buf[0] { + _, err = f.WriteString("\n") + } + + if err == nil { + _, err = f.WriteString(line + "\n") + } + if err != nil { return utils.WrapError(err) } 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 2/7] 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) + } +} From d56bb0b8ef0d9f3289e6e47288d79ed4d84088b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luka=20Marku=C5=A1i=C4=87?= Date: Wed, 3 Aug 2022 07:51:13 +0200 Subject: [PATCH 3/7] Fix the integration test --- pkg/commands/oscommands/os.go | 8 +++++--- .../gitignoreMenu/expected/repo/.git_keep/config | 2 -- .../expected/repo/.git_keep/info/exclude | 1 - .../expected/repo/.git_keep/logs/HEAD | 2 +- .../repo/.git_keep/logs/refs/heads/master | 2 +- .../02/2c2391c4f9e4a963e1c35a087f08772a4ea0f0 | Bin 0 -> 123 bytes .../9d/d04ee245b7d6f1f80aa2b428111cbac4a4e37d | Bin 122 -> 0 bytes .../expected/repo/.git_keep/refs/heads/master | 2 +- .../gitignoreMenu/expected/repo/lg_ignore_file | 2 +- 9 files changed, 9 insertions(+), 10 deletions(-) create mode 100644 test/integration/gitignoreMenu/expected/repo/.git_keep/objects/02/2c2391c4f9e4a963e1c35a087f08772a4ea0f0 delete mode 100644 test/integration/gitignoreMenu/expected/repo/.git_keep/objects/9d/d04ee245b7d6f1f80aa2b428111cbac4a4e37d diff --git a/pkg/commands/oscommands/os.go b/pkg/commands/oscommands/os.go index 3a42d09db..5f89f976f 100644 --- a/pkg/commands/oscommands/os.go +++ b/pkg/commands/oscommands/os.go @@ -120,10 +120,12 @@ func (c *OSCommand) AppendLineToFile(filename, line string) error { return utils.WrapError(err) } - // read last char buf := make([]byte, 1) - if _, err := f.ReadAt(buf, info.Size()-1); err != nil { - return utils.WrapError(err) + if info.Size() > 0 { + // read last char + if _, err := f.ReadAt(buf, info.Size()-1); err != nil { + return utils.WrapError(err) + } } // if the last byte of the file is not a newline, add it diff --git a/test/integration/gitignoreMenu/expected/repo/.git_keep/config b/test/integration/gitignoreMenu/expected/repo/.git_keep/config index 8ae104545..596ebaeb3 100644 --- a/test/integration/gitignoreMenu/expected/repo/.git_keep/config +++ b/test/integration/gitignoreMenu/expected/repo/.git_keep/config @@ -3,8 +3,6 @@ filemode = true bare = false logallrefupdates = true - ignorecase = true - precomposeunicode = true [user] email = CI@example.com name = CI diff --git a/test/integration/gitignoreMenu/expected/repo/.git_keep/info/exclude b/test/integration/gitignoreMenu/expected/repo/.git_keep/info/exclude index 8e9f2071f..a5196d1be 100644 --- a/test/integration/gitignoreMenu/expected/repo/.git_keep/info/exclude +++ b/test/integration/gitignoreMenu/expected/repo/.git_keep/info/exclude @@ -4,4 +4,3 @@ # exclude patterns (uncomment them if you want to use them): # *.[oa] # *~ -.DS_Store diff --git a/test/integration/gitignoreMenu/expected/repo/.git_keep/logs/HEAD b/test/integration/gitignoreMenu/expected/repo/.git_keep/logs/HEAD index bef74759d..698da4260 100644 --- a/test/integration/gitignoreMenu/expected/repo/.git_keep/logs/HEAD +++ b/test/integration/gitignoreMenu/expected/repo/.git_keep/logs/HEAD @@ -1 +1 @@ -0000000000000000000000000000000000000000 9dd04ee245b7d6f1f80aa2b428111cbac4a4e37d CI 1657012500 +1000 commit (initial): Initial commit +0000000000000000000000000000000000000000 022c2391c4f9e4a963e1c35a087f08772a4ea0f0 CI 1659505392 +0200 commit (initial): Initial commit diff --git a/test/integration/gitignoreMenu/expected/repo/.git_keep/logs/refs/heads/master b/test/integration/gitignoreMenu/expected/repo/.git_keep/logs/refs/heads/master index bef74759d..698da4260 100644 --- a/test/integration/gitignoreMenu/expected/repo/.git_keep/logs/refs/heads/master +++ b/test/integration/gitignoreMenu/expected/repo/.git_keep/logs/refs/heads/master @@ -1 +1 @@ -0000000000000000000000000000000000000000 9dd04ee245b7d6f1f80aa2b428111cbac4a4e37d CI 1657012500 +1000 commit (initial): Initial commit +0000000000000000000000000000000000000000 022c2391c4f9e4a963e1c35a087f08772a4ea0f0 CI 1659505392 +0200 commit (initial): Initial commit diff --git a/test/integration/gitignoreMenu/expected/repo/.git_keep/objects/02/2c2391c4f9e4a963e1c35a087f08772a4ea0f0 b/test/integration/gitignoreMenu/expected/repo/.git_keep/objects/02/2c2391c4f9e4a963e1c35a087f08772a4ea0f0 new file mode 100644 index 0000000000000000000000000000000000000000..9655076174cc25095c060cc8a925fedb1d274c7a GIT binary patch literal 123 zcmV->0EGW|0ga783d0}}0DJZodk>V&#ubH93OV@<*KDW|YY7(m{08!YUWXYbw^~bw zk*7=FRl$lUUb3^4#i@jZ4wbE}esO`kUq#ghv?hJ}v+XcPxXtmQ-&Eh5UURE=7##;j do&v#uglGoCLA(R)*{ z{(R6)1c;;aa}DGw2jNsOpm^dX&LO!N7Z#y}WXf9~V}mKfb;@`7l>S=fR7byo4gP}G cc| Date: Wed, 3 Aug 2022 13:57:26 +0200 Subject: [PATCH 4/7] Update pkg/commands/oscommands/os.go Co-authored-by: Ryoga --- pkg/commands/oscommands/os.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/pkg/commands/oscommands/os.go b/pkg/commands/oscommands/os.go index 5f89f976f..2a7cc1328 100644 --- a/pkg/commands/oscommands/os.go +++ b/pkg/commands/oscommands/os.go @@ -120,17 +120,17 @@ func (c *OSCommand) AppendLineToFile(filename, line string) error { return utils.WrapError(err) } - buf := make([]byte, 1) if info.Size() > 0 { // read last char + buf := make([]byte, 1) if _, err := f.ReadAt(buf, info.Size()-1); err != nil { return utils.WrapError(err) } - } - // if the last byte of the file is not a newline, add it - if []byte("\n")[0] != buf[0] { - _, err = f.WriteString("\n") + // if the last byte of the file is not a newline, add it + if []byte("\n")[0] != buf[0] { + _, err = f.WriteString("\n") + } } if err == nil { From 6160d85d4f883f606a680f6bd03c63200c6d5bdb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luka=20Marku=C5=A1i=C4=87?= Date: Wed, 3 Aug 2022 14:06:12 +0200 Subject: [PATCH 5/7] Use tmpdir for tests --- pkg/commands/oscommands/os_test.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/pkg/commands/oscommands/os_test.go b/pkg/commands/oscommands/os_test.go index 389fdfc8e..abbdc9068 100644 --- a/pkg/commands/oscommands/os_test.go +++ b/pkg/commands/oscommands/os_test.go @@ -3,6 +3,7 @@ package oscommands import ( "io/ioutil" "os" + "path/filepath" "testing" "github.com/stretchr/testify/assert" @@ -145,7 +146,7 @@ func TestOSCommandAppendLineToFile(t *testing.T) { scenarios := []scenario{ { - "testFile", + filepath.Join(os.TempDir(), "testFile"), func(path string) { if err := ioutil.WriteFile(path, []byte("hello"), 0o600); err != nil { panic(err) @@ -153,7 +154,7 @@ func TestOSCommandAppendLineToFile(t *testing.T) { }, }, { - "testFileWithNewline", + filepath.Join(os.TempDir(), "testFileWithNewline"), func(path string) { if err := ioutil.WriteFile(path, []byte("hello\n"), 0o600); err != nil { panic(err) From 57f86b8f907e73a9c314536443d9d0e76461db90 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luka=20Marku=C5=A1i=C4=87?= Date: Wed, 3 Aug 2022 14:08:52 +0200 Subject: [PATCH 6/7] Rerun integration test --- .../expected/repo/.git_keep/logs/HEAD | 2 +- .../repo/.git_keep/logs/refs/heads/master | 2 +- .../02/2c2391c4f9e4a963e1c35a087f08772a4ea0f0 | Bin 123 -> 0 bytes .../04/535177acab8a81c84b0b1b44ee3aea76b0e36e | Bin 0 -> 122 bytes .../expected/repo/.git_keep/refs/heads/master | 2 +- .../gitignoreMenu/expected/repo/lg_ignore_file | 1 - 6 files changed, 3 insertions(+), 4 deletions(-) delete mode 100644 test/integration/gitignoreMenu/expected/repo/.git_keep/objects/02/2c2391c4f9e4a963e1c35a087f08772a4ea0f0 create mode 100644 test/integration/gitignoreMenu/expected/repo/.git_keep/objects/04/535177acab8a81c84b0b1b44ee3aea76b0e36e diff --git a/test/integration/gitignoreMenu/expected/repo/.git_keep/logs/HEAD b/test/integration/gitignoreMenu/expected/repo/.git_keep/logs/HEAD index 698da4260..329f08e98 100644 --- a/test/integration/gitignoreMenu/expected/repo/.git_keep/logs/HEAD +++ b/test/integration/gitignoreMenu/expected/repo/.git_keep/logs/HEAD @@ -1 +1 @@ -0000000000000000000000000000000000000000 022c2391c4f9e4a963e1c35a087f08772a4ea0f0 CI 1659505392 +0200 commit (initial): Initial commit +0000000000000000000000000000000000000000 04535177acab8a81c84b0b1b44ee3aea76b0e36e CI 1659528492 +0200 commit (initial): Initial commit diff --git a/test/integration/gitignoreMenu/expected/repo/.git_keep/logs/refs/heads/master b/test/integration/gitignoreMenu/expected/repo/.git_keep/logs/refs/heads/master index 698da4260..329f08e98 100644 --- a/test/integration/gitignoreMenu/expected/repo/.git_keep/logs/refs/heads/master +++ b/test/integration/gitignoreMenu/expected/repo/.git_keep/logs/refs/heads/master @@ -1 +1 @@ -0000000000000000000000000000000000000000 022c2391c4f9e4a963e1c35a087f08772a4ea0f0 CI 1659505392 +0200 commit (initial): Initial commit +0000000000000000000000000000000000000000 04535177acab8a81c84b0b1b44ee3aea76b0e36e CI 1659528492 +0200 commit (initial): Initial commit diff --git a/test/integration/gitignoreMenu/expected/repo/.git_keep/objects/02/2c2391c4f9e4a963e1c35a087f08772a4ea0f0 b/test/integration/gitignoreMenu/expected/repo/.git_keep/objects/02/2c2391c4f9e4a963e1c35a087f08772a4ea0f0 deleted file mode 100644 index 9655076174cc25095c060cc8a925fedb1d274c7a..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 123 zcmV->0EGW|0ga783d0}}0DJZodk>V&#ubH93OV@<*KDW|YY7(m{08!YUWXYbw^~bw zk*7=FRl$lUUb3^4#i@jZ4wbE}esO`kUq#ghv?hJ}v+XcPxXtmQ-&Eh5UURE=7##;j do&v#uglG Date: Thu, 4 Aug 2022 13:52:04 +0200 Subject: [PATCH 7/7] Test appending to empty file --- pkg/commands/oscommands/os_test.go | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/pkg/commands/oscommands/os_test.go b/pkg/commands/oscommands/os_test.go index abbdc9068..969224405 100644 --- a/pkg/commands/oscommands/os_test.go +++ b/pkg/commands/oscommands/os_test.go @@ -142,6 +142,7 @@ func TestOSCommandAppendLineToFile(t *testing.T) { type scenario struct { path string setup func(string) + test func(string) } scenarios := []scenario{ @@ -152,6 +153,20 @@ func TestOSCommandAppendLineToFile(t *testing.T) { panic(err) } }, + func(output string) { + assert.EqualValues(t, "hello\nworld\n", output) + }, + }, + { + filepath.Join(os.TempDir(), "emptyTestFile"), + func(path string) { + if err := ioutil.WriteFile(path, []byte(""), 0o600); err != nil { + panic(err) + } + }, + func(output string) { + assert.EqualValues(t, "world\n", output) + }, }, { filepath.Join(os.TempDir(), "testFileWithNewline"), @@ -160,6 +175,9 @@ func TestOSCommandAppendLineToFile(t *testing.T) { panic(err) } }, + func(output string) { + assert.EqualValues(t, "hello\nworld\n", output) + }, }, } @@ -173,7 +191,7 @@ func TestOSCommandAppendLineToFile(t *testing.T) { if err != nil { panic(err) } - assert.EqualValues(t, "hello\nworld\n", string(f)) + s.test(string(f)) _ = os.RemoveAll(s.path) } }