diff options
author | Anthony HAMON <anthony.hamon@iadvize.com> | 2018-08-21 23:17:44 +0200 |
---|---|---|
committer | Anthony HAMON <anthony.hamon@iadvize.com> | 2018-08-26 01:58:19 +0200 |
commit | f91e2b12dbc02e3dc05f5ae746d16a702ba57c27 (patch) | |
tree | ae45bc3349af4311263b3ea0c0dd7cadc706bb0a /pkg | |
parent | 364c1ac5e75867bd0f016c01ebc8e52bcbf2ce63 (diff) |
add tests to pkg/commands
Diffstat (limited to 'pkg')
-rw-r--r-- | pkg/commands/os_test.go | 80 |
1 files changed, 70 insertions, 10 deletions
diff --git a/pkg/commands/os_test.go b/pkg/commands/os_test.go index 29540aff6..128caa1b2 100644 --- a/pkg/commands/os_test.go +++ b/pkg/commands/os_test.go @@ -1,16 +1,76 @@ package commands -import "testing" +import ( + "testing" -func TestQuote(t *testing.T) { - osCommand := &OSCommand{ - Log: nil, - Platform: getPlatform(), + "github.com/Sirupsen/logrus" + + "github.com/stretchr/testify/assert" +) + +func TestRunCommandWithOutput(t *testing.T) { + type scenario struct { + command string + test func(string, error) } - test := "hello `test`" - expected := osCommand.Platform.escapedQuote + "hello \\`test\\`" + osCommand.Platform.escapedQuote - test = osCommand.Quote(test) - if test != expected { - t.Error("Expected " + expected + ", got " + test) + + 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) { + assert.Regexp(t, ".*No such file or directory.*", err.Error()) + }, + }, + } + + for _, s := range scenarios { + s.test(NewOSCommand(logrus.New()).RunCommandWithOutput(s.command)) + } +} + +func TestRunCommand(t *testing.T) { + type scenario struct { + command string + test func(error) + } + + scenarios := []scenario{ + { + "rmdir unexisting-folder", + func(err error) { + assert.Regexp(t, ".*No such file or directory.*", err.Error()) + }, + }, + } + + for _, s := range scenarios { + s.test(NewOSCommand(logrus.New()).RunCommand(s.command)) } } + +func TestQuote(t *testing.T) { + osCommand := NewOSCommand(logrus.New()) + + actual := osCommand.Quote("hello `test`") + + expected := osCommand.Platform.escapedQuote + "hello \\`test\\`" + osCommand.Platform.escapedQuote + + assert.EqualValues(t, expected, actual) +} + +func TestUnquote(t *testing.T) { + osCommand := NewOSCommand(logrus.New()) + + actual := osCommand.Unquote(`hello "test"`) + + expected := "hello test" + + assert.EqualValues(t, expected, actual) +} |