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

253 lines
5.3 KiB
Go
Raw Normal View History

package oscommands
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"
"github.com/jesseduffield/lazygit/pkg/secureexec"
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 secureexec.Command("exit", "1")
2018-08-22 22:31:43 +02:00
},
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 secureexec.Command("echo")
2018-09-01 06:33:01 +02:00
},
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)
return secureexec.Command("echo")
2018-08-22 22:31:43 +02:00
},
func(err error) {
assert.NoError(t, err)
},
},
}
for _, s := range scenarios {
OSCmd := NewDummyOSCommand()
OSCmd.Command = s.command
2020-10-03 06:54:55 +02:00
OSCmd.Config.GetUserConfig().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
// 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
2021-03-01 16:16:48 +02:00
osCommand.Platform.OS = "linux"
2018-08-21 23:17:44 +02:00
actual := osCommand.Quote("hello `test`")
expected := osCommand.Platform.EscapedQuote + "hello \\`test\\`" + osCommand.Platform.EscapedQuote
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-03-01 16:16:48 +02:00
expected := osCommand.Platform.EscapedQuote + "hello 'test'" + osCommand.Platform.EscapedQuote
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"
2018-09-10 06:51:19 +02:00
actual := osCommand.Quote(`hello "test"`)
2021-03-01 16:16:48 +02:00
expected := osCommand.Platform.EscapedQuote + `hello \"test\"` + osCommand.Platform.EscapedQuote
assert.EqualValues(t, expected, actual)
}
// TestOSCommandQuoteWindows tests the quote function for Windows
func TestOSCommandQuoteWindows(t *testing.T) {
osCommand := NewDummyOSCommand()
osCommand.Platform.OS = "windows"
actual := osCommand.Quote(`hello "test"`)
expected := osCommand.Platform.EscapedQuote + `hello "'"'"test"'"'"` + osCommand.Platform.EscapedQuote
2018-09-10 06:51:19 +02:00
assert.EqualValues(t, expected, actual)
}
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
})
}
}