1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-01-24 05:36:19 +02:00

197 lines
3.8 KiB
Go
Raw Normal View History

package oscommands
2018-08-21 23:17:44 +02:00
import (
2018-08-28 19:12:35 +10:00
"os"
2022-08-03 14:06:12 +02:00
"path/filepath"
2018-08-21 23:17:44 +02:00
"testing"
2018-08-21 23:17:44 +02:00
"github.com/stretchr/testify/assert"
)
2021-12-07 21:59:36 +11:00
func TestOSCommandRun(t *testing.T) {
2018-08-21 23:17:44 +02:00
type scenario struct {
args []string
test func(error)
2018-08-21 23:17:44 +02:00
}
scenarios := []scenario{
{
[]string{"rmdir", "unexisting-folder"},
2018-08-21 23:17:44 +02:00
func(err error) {
2018-09-04 06:29:48 +02:00
assert.Regexp(t, "rmdir.*unexisting-folder.*", err.Error())
2018-08-21 23:17:44 +02:00
},
},
}
for _, s := range scenarios {
2021-12-07 21:59:36 +11:00
c := NewDummyOSCommand()
s.test(c.Cmd.New(s.args).Run())
}
}
2018-08-21 23:17:44 +02:00
2018-08-22 22:30:02 +02:00
func TestOSCommandQuote(t *testing.T) {
osCommand := NewDummyOSCommand()
2018-08-21 23:17:44 +02:00
2021-03-01 23:16:48 +09:00
osCommand.Platform.OS = "linux"
2018-08-21 23:17:44 +02:00
actual := osCommand.Quote("hello `test`")
2021-10-09 14:53:43 +09:00
expected := "\"hello \\`test\\`\""
2018-08-21 23:17:44 +02:00
assert.EqualValues(t, expected, actual)
}
2018-09-10 17:36:59 +02:00
// TestOSCommandQuoteSingleQuote tests the quote function with ' quotes explicitly for Linux
2018-09-10 06:51:19 +02:00
func TestOSCommandQuoteSingleQuote(t *testing.T) {
osCommand := NewDummyOSCommand()
2018-09-10 06:51:19 +02:00
osCommand.Platform.OS = "linux"
2018-09-10 06:51:19 +02:00
actual := osCommand.Quote("hello 'test'")
2021-10-09 14:53:43 +09:00
expected := `"hello 'test'"`
2018-09-10 06:51:19 +02:00
assert.EqualValues(t, expected, actual)
}
2018-11-28 12:33:52 +13:00
// TestOSCommandQuoteDoubleQuote tests the quote function with " quotes explicitly for Linux
2018-09-10 06:51:19 +02:00
func TestOSCommandQuoteDoubleQuote(t *testing.T) {
osCommand := NewDummyOSCommand()
2018-09-10 06:51:19 +02:00
osCommand.Platform.OS = "linux"
2018-09-10 06:51:19 +02:00
actual := osCommand.Quote(`hello "test"`)
2021-10-09 14:53:43 +09:00
expected := `"hello \"test\""`
2021-03-01 23:16:48 +09:00
assert.EqualValues(t, expected, actual)
}
// TestOSCommandQuoteWindows tests the quote function for Windows
func TestOSCommandQuoteWindows(t *testing.T) {
osCommand := NewDummyOSCommand()
osCommand.Platform.OS = "windows"
2021-10-09 14:53:43 +09:00
actual := osCommand.Quote(`hello "test" 'test2'`)
2021-03-01 23:16:48 +09:00
2021-10-09 14:53:43 +09:00
expected := `\"hello "'"'"test"'"'" 'test2'\"`
2018-09-10 06:51:19 +02:00
assert.EqualValues(t, expected, actual)
}
2018-08-28 19:12:35 +10:00
func TestOSCommandFileType(t *testing.T) {
type scenario struct {
path string
setup func()
test func(string)
}
scenarios := []scenario{
{
"testFile",
func() {
if _, err := os.Create("testFile"); err != nil {
panic(err)
}
},
func(output string) {
assert.EqualValues(t, "file", output)
},
},
{
"file with spaces",
func() {
if _, err := os.Create("file with spaces"); err != nil {
panic(err)
}
},
func(output string) {
assert.EqualValues(t, "file", output)
},
},
{
"testDirectory",
func() {
2022-03-19 09:38:49 +11:00
if err := os.Mkdir("testDirectory", 0o644); err != nil {
2018-08-28 19:12:35 +10:00
panic(err)
}
},
func(output string) {
assert.EqualValues(t, "directory", output)
},
},
{
"nonExistant",
func() {},
func(output string) {
assert.EqualValues(t, "other", output)
},
},
}
for _, s := range scenarios {
s.setup()
2021-12-30 17:19:01 +11:00
s.test(FileType(s.path))
2018-08-28 19:12:35 +10:00
_ = os.RemoveAll(s.path)
}
}
2022-08-02 23:46:02 +02:00
func TestOSCommandAppendLineToFile(t *testing.T) {
type scenario struct {
path string
setup func(string)
2022-08-04 13:52:04 +02:00
test func(string)
2022-08-02 23:46:02 +02:00
}
scenarios := []scenario{
{
2022-08-03 14:06:12 +02:00
filepath.Join(os.TempDir(), "testFile"),
2022-08-02 23:46:02 +02:00
func(path string) {
2022-09-13 18:11:03 +08:00
if err := os.WriteFile(path, []byte("hello"), 0o600); err != nil {
2022-08-02 23:46:02 +02:00
panic(err)
}
},
2022-08-04 13:52:04 +02:00
func(output string) {
assert.EqualValues(t, "hello\nworld\n", output)
},
},
{
filepath.Join(os.TempDir(), "emptyTestFile"),
func(path string) {
2022-09-13 18:11:03 +08:00
if err := os.WriteFile(path, []byte(""), 0o600); err != nil {
2022-08-04 13:52:04 +02:00
panic(err)
}
},
func(output string) {
assert.EqualValues(t, "world\n", output)
},
2022-08-02 23:46:02 +02:00
},
{
2022-08-03 14:06:12 +02:00
filepath.Join(os.TempDir(), "testFileWithNewline"),
2022-08-02 23:46:02 +02:00
func(path string) {
2022-09-13 18:11:03 +08:00
if err := os.WriteFile(path, []byte("hello\n"), 0o600); err != nil {
2022-08-02 23:46:02 +02:00
panic(err)
}
},
2022-08-04 13:52:04 +02:00
func(output string) {
assert.EqualValues(t, "hello\nworld\n", output)
},
2022-08-02 23:46:02 +02:00
},
}
for _, s := range scenarios {
s.setup(s.path)
osCommand := NewDummyOSCommand()
if err := osCommand.AppendLineToFile(s.path, "world"); err != nil {
panic(err)
}
2022-09-13 18:11:03 +08:00
f, err := os.ReadFile(s.path)
2022-08-02 23:46:02 +02:00
if err != nil {
panic(err)
}
2022-08-04 13:52:04 +02:00
s.test(string(f))
2022-08-02 23:46:02 +02:00
_ = os.RemoveAll(s.path)
}
}