mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-05-11 04:15:48 +02:00
Merge pull request #2004 from mark2185/fix-add-to-gitignore-newline
This commit is contained in:
commit
b77f3160f1
10 changed files with 87 additions and 10 deletions
|
@ -109,13 +109,34 @@ 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)
|
||||
}
|
||||
|
||||
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 err == nil {
|
||||
_, err = f.WriteString(line + "\n")
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return utils.WrapError(err)
|
||||
}
|
||||
|
|
|
@ -1,7 +1,9 @@
|
|||
package oscommands
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
@ -135,3 +137,61 @@ func TestOSCommandFileType(t *testing.T) {
|
|||
_ = os.RemoveAll(s.path)
|
||||
}
|
||||
}
|
||||
|
||||
func TestOSCommandAppendLineToFile(t *testing.T) {
|
||||
type scenario struct {
|
||||
path string
|
||||
setup func(string)
|
||||
test func(string)
|
||||
}
|
||||
|
||||
scenarios := []scenario{
|
||||
{
|
||||
filepath.Join(os.TempDir(), "testFile"),
|
||||
func(path string) {
|
||||
if err := ioutil.WriteFile(path, []byte("hello"), 0o600); err != nil {
|
||||
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"),
|
||||
func(path string) {
|
||||
if err := ioutil.WriteFile(path, []byte("hello\n"), 0o600); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
},
|
||||
func(output string) {
|
||||
assert.EqualValues(t, "hello\nworld\n", output)
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
s.test(string(f))
|
||||
_ = os.RemoveAll(s.path)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,8 +3,6 @@
|
|||
filemode = true
|
||||
bare = false
|
||||
logallrefupdates = true
|
||||
ignorecase = true
|
||||
precomposeunicode = true
|
||||
[user]
|
||||
email = CI@example.com
|
||||
name = CI
|
||||
|
|
|
@ -4,4 +4,3 @@
|
|||
# exclude patterns (uncomment them if you want to use them):
|
||||
# *.[oa]
|
||||
# *~
|
||||
.DS_Store
|
||||
|
|
|
@ -1 +1 @@
|
|||
0000000000000000000000000000000000000000 9dd04ee245b7d6f1f80aa2b428111cbac4a4e37d CI <CI@example.com> 1657012500 +1000 commit (initial): Initial commit
|
||||
0000000000000000000000000000000000000000 04535177acab8a81c84b0b1b44ee3aea76b0e36e CI <CI@example.com> 1659528492 +0200 commit (initial): Initial commit
|
||||
|
|
|
@ -1 +1 @@
|
|||
0000000000000000000000000000000000000000 9dd04ee245b7d6f1f80aa2b428111cbac4a4e37d CI <CI@example.com> 1657012500 +1000 commit (initial): Initial commit
|
||||
0000000000000000000000000000000000000000 04535177acab8a81c84b0b1b44ee3aea76b0e36e CI <CI@example.com> 1659528492 +0200 commit (initial): Initial commit
|
||||
|
|
Binary file not shown.
Binary file not shown.
|
@ -1 +1 @@
|
|||
9dd04ee245b7d6f1f80aa2b428111cbac4a4e37d
|
||||
04535177acab8a81c84b0b1b44ee3aea76b0e36e
|
||||
|
|
|
@ -1,2 +1 @@
|
|||
|
||||
myfile1
|
||||
myfile1
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue