From f91e2b12dbc02e3dc05f5ae746d16a702ba57c27 Mon Sep 17 00:00:00 2001 From: Anthony HAMON Date: Tue, 21 Aug 2018 23:17:44 +0200 Subject: [PATCH] add tests to pkg/commands --- pkg/commands/os_test.go | 80 +++++++++++++++++++++++++++++++++++------ 1 file 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) +}