1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2024-12-12 11:15:00 +02:00
lazygit/pkg/commands/os_test.go

378 lines
7.5 KiB
Go
Raw Normal View History

package commands
2018-08-21 23:17:44 +02:00
import (
2018-12-05 10:33:46 +02:00
"io/ioutil"
2018-08-28 11:12:35 +02:00
"os"
2018-08-22 22:31:43 +02:00
"os/exec"
2018-08-21 23:17:44 +02:00
"testing"
2018-08-21 23:17:44 +02:00
"github.com/stretchr/testify/assert"
)
2018-11-30 02:47:14 +02:00
// TestOSCommandRunCommandWithOutput is a function.
2018-08-22 22:30:02 +02:00
func TestOSCommandRunCommandWithOutput(t *testing.T) {
2018-08-21 23:17:44 +02:00
type scenario struct {
command string
test func(string, error)
}
2018-08-21 23:17:44 +02:00
scenarios := []scenario{
{
"echo -n '123'",
func(output string, err error) {
assert.NoError(t, err)
assert.EqualValues(t, "123", output)
},
},
{
"rmdir unexisting-folder",
func(output string, 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 {
s.test(NewDummyOSCommand().RunCommandWithOutput(s.command))
2018-08-21 23:17:44 +02:00
}
}
2018-11-30 02:47:14 +02:00
// TestOSCommandRunCommand is a function.
2018-08-22 22:30:02 +02:00
func TestOSCommandRunCommand(t *testing.T) {
2018-08-21 23:17:44 +02:00
type scenario struct {
command string
test func(error)
}
scenarios := []scenario{
{
"rmdir unexisting-folder",
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 {
s.test(NewDummyOSCommand().RunCommand(s.command))
}
}
2018-08-21 23:17:44 +02:00
2018-11-30 02:47:14 +02:00
// TestOSCommandOpenFile is a function.
2018-08-22 22:31:43 +02:00
func TestOSCommandOpenFile(t *testing.T) {
type scenario struct {
filename string
command func(string, ...string) *exec.Cmd
test func(error)
}
scenarios := []scenario{
{
"test",
func(name string, arg ...string) *exec.Cmd {
return exec.Command("exit", "1")
},
func(err error) {
2018-09-01 04:13:41 +02:00
assert.Error(t, err)
2018-08-22 22:31:43 +02:00
},
},
{
"test",
func(name string, arg ...string) *exec.Cmd {
2018-09-01 06:33:01 +02:00
assert.Equal(t, "open", name)
assert.Equal(t, []string{"test"}, arg)
return exec.Command("echo")
},
func(err error) {
assert.NoError(t, err)
},
},
{
"filename with spaces",
func(name string, arg ...string) *exec.Cmd {
assert.Equal(t, "open", name)
assert.Equal(t, []string{"filename with spaces"}, arg)
2018-08-22 22:31:43 +02:00
return exec.Command("echo")
},
func(err error) {
assert.NoError(t, err)
},
},
}
for _, s := range scenarios {
OSCmd := NewDummyOSCommand()
2018-08-22 22:31:43 +02:00
OSCmd.command = s.command
2018-09-01 06:33:01 +02:00
OSCmd.Config.GetUserConfig().Set("os.openCommand", "open {{filename}}")
2018-08-22 22:31:43 +02:00
s.test(OSCmd.OpenFile(s.filename))
}
}
2018-11-30 02:47:14 +02:00
// TestOSCommandEditFile is a function.
2018-08-22 22:31:43 +02:00
func TestOSCommandEditFile(t *testing.T) {
type scenario struct {
filename string
command func(string, ...string) *exec.Cmd
getenv func(string) string
getGlobalGitConfig func(string) (string, error)
test func(*exec.Cmd, error)
}
scenarios := []scenario{
{
"test",
func(name string, arg ...string) *exec.Cmd {
return exec.Command("exit", "1")
},
func(env string) string {
return ""
},
func(cf string) (string, error) {
return "", nil
},
func(cmd *exec.Cmd, err error) {
assert.EqualError(t, err, "No editor defined in $VISUAL, $EDITOR, or git config")
},
},
{
"test",
func(name string, arg ...string) *exec.Cmd {
if name == "which" {
return exec.Command("exit", "1")
}
assert.EqualValues(t, "nano", name)
return nil
},
func(env string) string {
return ""
},
func(cf string) (string, error) {
return "nano", nil
},
func(cmd *exec.Cmd, err error) {
assert.NoError(t, err)
},
},
{
"test",
func(name string, arg ...string) *exec.Cmd {
if name == "which" {
return exec.Command("exit", "1")
}
assert.EqualValues(t, "nano", name)
return nil
},
func(env string) string {
if env == "VISUAL" {
return "nano"
}
return ""
},
func(cf string) (string, error) {
return "", nil
},
func(cmd *exec.Cmd, err error) {
assert.NoError(t, err)
},
},
{
"test",
func(name string, arg ...string) *exec.Cmd {
if name == "which" {
return exec.Command("exit", "1")
}
assert.EqualValues(t, "emacs", name)
return nil
},
func(env string) string {
if env == "EDITOR" {
return "emacs"
}
return ""
},
func(cf string) (string, error) {
return "", nil
},
func(cmd *exec.Cmd, err error) {
assert.NoError(t, err)
},
},
{
"test",
func(name string, arg ...string) *exec.Cmd {
if name == "which" {
return exec.Command("echo")
}
assert.EqualValues(t, "vi", name)
return nil
},
func(env string) string {
return ""
},
func(cf string) (string, error) {
return "", nil
},
func(cmd *exec.Cmd, err error) {
assert.NoError(t, err)
},
},
}
for _, s := range scenarios {
OSCmd := NewDummyOSCommand()
2018-08-22 22:31:43 +02:00
OSCmd.command = s.command
OSCmd.getGlobalGitConfig = s.getGlobalGitConfig
OSCmd.getenv = s.getenv
s.test(OSCmd.EditFile(s.filename))
}
}
2018-11-30 02:47:14 +02:00
// TestOSCommandQuote is a function.
2018-08-22 22:30:02 +02:00
func TestOSCommandQuote(t *testing.T) {
osCommand := NewDummyOSCommand()
2018-08-21 23:17:44 +02:00
actual := osCommand.Quote("hello `test`")
expected := osCommand.Platform.escapedQuote + "hello \\`test\\`" + osCommand.Platform.escapedQuote
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"
actual := osCommand.Quote("hello 'test'")
expected := osCommand.Platform.fallbackEscapedQuote + "hello 'test'" + osCommand.Platform.fallbackEscapedQuote
2018-09-10 06:51:19 +02:00
assert.EqualValues(t, expected, actual)
}
2018-11-28 01:33:52 +02: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"
actual := osCommand.Quote(`hello "test"`)
expected := osCommand.Platform.escapedQuote + "hello \"test\"" + osCommand.Platform.escapedQuote
assert.EqualValues(t, expected, actual)
}
2018-11-30 02:47:14 +02:00
// TestOSCommandUnquote is a function.
2018-08-22 22:30:02 +02:00
func TestOSCommandUnquote(t *testing.T) {
osCommand := NewDummyOSCommand()
2018-08-21 23:17:44 +02:00
actual := osCommand.Unquote(`hello "test"`)
expected := "hello test"
assert.EqualValues(t, expected, actual)
}
2018-08-28 11:12:35 +02:00
2018-11-30 02:47:14 +02:00
// TestOSCommandFileType is a function.
2018-08-28 11:12:35 +02: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() {
if err := os.Mkdir("testDirectory", 0644); err != nil {
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()
s.test(NewDummyOSCommand().FileType(s.path))
2018-08-28 11:12:35 +02:00
_ = os.RemoveAll(s.path)
}
}
2018-12-05 10:33:46 +02:00
func TestOSCommandCreateTempFile(t *testing.T) {
type scenario struct {
testName string
filename string
content string
test func(string, error)
}
scenarios := []scenario{
{
"valid case",
"filename",
"content",
func(path string, err error) {
assert.NoError(t, err)
content, err := ioutil.ReadFile(path)
assert.NoError(t, err)
assert.Equal(t, "content", string(content))
},
},
}
for _, s := range scenarios {
t.Run(s.testName, func(t *testing.T) {
s.test(NewDummyOSCommand().CreateTempFile(s.filename, s.content))
2018-12-05 10:33:46 +02:00
})
}
}