2020-09-29 11:10:57 +02:00
|
|
|
package oscommands
|
2018-08-16 05:55:55 +02:00
|
|
|
|
2018-08-21 23:17:44 +02:00
|
|
|
import (
|
2018-08-28 11:12:35 +02:00
|
|
|
"os"
|
2018-08-21 23:17:44 +02:00
|
|
|
"testing"
|
2018-08-16 05:55:55 +02:00
|
|
|
|
2018-08-21 23:17:44 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2021-12-07 12:59:36 +02:00
|
|
|
func TestOSCommandRunWithOutput(t *testing.T) {
|
2018-08-21 23:17:44 +02:00
|
|
|
type scenario struct {
|
|
|
|
command string
|
|
|
|
test func(string, error)
|
2018-08-16 05:55:55 +02:00
|
|
|
}
|
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 {
|
2021-12-07 12:59:36 +02:00
|
|
|
c := NewDummyOSCommand()
|
2021-12-29 05:33:38 +02:00
|
|
|
s.test(c.Cmd.New(s.command).RunWithOutput())
|
2018-08-21 23:17:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-07 12:59:36 +02:00
|
|
|
func TestOSCommandRun(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 {
|
2021-12-07 12:59:36 +02:00
|
|
|
c := NewDummyOSCommand()
|
2021-12-30 04:44:41 +02:00
|
|
|
s.test(c.Cmd.New(s.command).Run())
|
2018-08-16 05:55:55 +02:00
|
|
|
}
|
|
|
|
}
|
2018-08-21 23:17:44 +02:00
|
|
|
|
2018-08-22 22:30:02 +02:00
|
|
|
func TestOSCommandQuote(t *testing.T) {
|
2019-03-02 04:08:09 +02:00
|
|
|
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`")
|
|
|
|
|
2021-10-09 07:53:43 +02: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) {
|
2019-03-02 04:08:09 +02:00
|
|
|
osCommand := NewDummyOSCommand()
|
2018-09-10 06:51:19 +02:00
|
|
|
|
2020-09-29 11:10:57 +02:00
|
|
|
osCommand.Platform.OS = "linux"
|
2018-09-10 06:51:19 +02:00
|
|
|
|
|
|
|
actual := osCommand.Quote("hello 'test'")
|
|
|
|
|
2021-10-09 07:53:43 +02:00
|
|
|
expected := `"hello 'test'"`
|
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) {
|
2019-03-02 04:08:09 +02:00
|
|
|
osCommand := NewDummyOSCommand()
|
2018-09-10 06:51:19 +02:00
|
|
|
|
2020-09-29 11:10:57 +02:00
|
|
|
osCommand.Platform.OS = "linux"
|
2018-09-10 06:51:19 +02:00
|
|
|
|
|
|
|
actual := osCommand.Quote(`hello "test"`)
|
|
|
|
|
2021-10-09 07:53:43 +02:00
|
|
|
expected := `"hello \"test\""`
|
2021-03-01 16:16:48 +02: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 07:53:43 +02:00
|
|
|
actual := osCommand.Quote(`hello "test" 'test2'`)
|
2021-03-01 16:16:48 +02:00
|
|
|
|
2021-10-09 07:53:43 +02:00
|
|
|
expected := `\"hello "'"'"test"'"'" 'test2'\"`
|
2018-09-10 06:51:19 +02:00
|
|
|
|
|
|
|
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() {
|
2022-03-19 00:38:49 +02:00
|
|
|
if err := os.Mkdir("testDirectory", 0o644); err != nil {
|
2018-08-28 11:12:35 +02: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 08:19:01 +02:00
|
|
|
s.test(FileType(s.path))
|
2018-08-28 11:12:35 +02:00
|
|
|
_ = os.RemoveAll(s.path)
|
|
|
|
}
|
|
|
|
}
|