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-12-05 10:33:46 +02:00
|
|
|
"io/ioutil"
|
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
|
|
|
// TestOSCommandRunWithOutput is a function.
|
|
|
|
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
|
|
|
// TestOSCommandRun is a function.
|
|
|
|
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-29 05:33:38 +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-11-30 02:47:14 +02:00
|
|
|
// TestOSCommandQuote is a function.
|
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-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()
|
2019-03-02 04:08:09 +02:00
|
|
|
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) {
|
2019-03-02 04:08:09 +02:00
|
|
|
s.test(NewDummyOSCommand().CreateTempFile(s.filename, s.content))
|
2018-12-05 10:33:46 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|