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

360 lines
7.2 KiB
Go
Raw Normal View History

package commands
2018-08-21 23:17:44 +02:00
import (
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-09-01 04:13:41 +02:00
"github.com/jesseduffield/lazygit/pkg/config"
"github.com/spf13/viper"
2018-08-21 23:17:44 +02:00
"github.com/stretchr/testify/assert"
2018-09-01 04:13:41 +02:00
yaml "gopkg.in/yaml.v2"
2018-08-21 23:17:44 +02:00
)
2018-08-22 22:30:02 +02:00
func newDummyOSCommand() *OSCommand {
2018-09-01 04:13:41 +02:00
return NewOSCommand(newDummyLog(), newDummyAppConfig())
}
func newDummyAppConfig() *config.AppConfig {
appConfig := &config.AppConfig{
Name: "lazygit",
Version: "unversioned",
Commit: "",
BuildDate: "",
Debug: false,
BuildSource: "",
UserConfig: viper.New(),
}
_ = yaml.Unmarshal([]byte{}, appConfig.AppState)
return appConfig
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 {
2018-08-22 22:30:02 +02:00
s.test(newDummyOSCommand().RunCommandWithOutput(s.command))
2018-08-21 23:17:44 +02:00
}
}
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 {
2018-08-22 22:30:02 +02:00
s.test(newDummyOSCommand().RunCommand(s.command))
}
}
2018-08-21 23:17:44 +02:00
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()
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))
}
}
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()
OSCmd.command = s.command
OSCmd.getGlobalGitConfig = s.getGlobalGitConfig
OSCmd.getenv = s.getenv
s.test(OSCmd.EditFile(s.filename))
}
}
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()
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-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 TestOSCommandQuoteDoubleQuote(t *testing.T) {
osCommand := newDummyOSCommand()
osCommand.Platform.os = "linux"
actual := osCommand.Quote(`hello "test"`)
expected := osCommand.Platform.escapedQuote + "hello \"test\"" + osCommand.Platform.escapedQuote
assert.EqualValues(t, expected, actual)
}
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
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))
_ = os.RemoveAll(s.path)
}
}