command_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package cmd
  2. import (
  3. "runtime"
  4. "strings"
  5. "testing"
  6. "time"
  7. "github.com/stretchr/testify/assert"
  8. )
  9. func TestRunCommand(t *testing.T) {
  10. // TODO Windows: Port this test
  11. if runtime.GOOS == "windows" {
  12. t.Skip("Needs porting to Windows")
  13. }
  14. var cmd string
  15. if runtime.GOOS == "solaris" {
  16. cmd = "gls"
  17. } else {
  18. cmd = "ls"
  19. }
  20. result := RunCommand(cmd)
  21. result.Assert(t, Expected{})
  22. result = RunCommand("doesnotexists")
  23. expectedError := `exec: "doesnotexists": executable file not found`
  24. result.Assert(t, Expected{ExitCode: 127, Error: expectedError})
  25. result = RunCommand(cmd, "-z")
  26. result.Assert(t, Expected{
  27. ExitCode: 2,
  28. Error: "exit status 2",
  29. Err: "invalid option",
  30. })
  31. assert.Contains(t, result.Combined(), "invalid option")
  32. }
  33. func TestRunCommandWithCombined(t *testing.T) {
  34. // TODO Windows: Port this test
  35. if runtime.GOOS == "windows" {
  36. t.Skip("Needs porting to Windows")
  37. }
  38. result := RunCommand("ls", "-a")
  39. result.Assert(t, Expected{})
  40. assert.Contains(t, result.Combined(), "..")
  41. assert.Contains(t, result.Stdout(), "..")
  42. }
  43. func TestRunCommandWithTimeoutFinished(t *testing.T) {
  44. // TODO Windows: Port this test
  45. if runtime.GOOS == "windows" {
  46. t.Skip("Needs porting to Windows")
  47. }
  48. result := RunCmd(Cmd{
  49. Command: []string{"ls", "-a"},
  50. Timeout: 50 * time.Millisecond,
  51. })
  52. result.Assert(t, Expected{Out: ".."})
  53. }
  54. func TestRunCommandWithTimeoutKilled(t *testing.T) {
  55. // TODO Windows: Port this test
  56. if runtime.GOOS == "windows" {
  57. t.Skip("Needs porting to Windows")
  58. }
  59. command := []string{"sh", "-c", "while true ; do echo 1 ; sleep .5 ; done"}
  60. result := RunCmd(Cmd{Command: command, Timeout: 1250 * time.Millisecond})
  61. result.Assert(t, Expected{Timeout: true})
  62. ones := strings.Split(result.Stdout(), "\n")
  63. assert.Len(t, ones, 4)
  64. }
  65. func TestRunCommandWithErrors(t *testing.T) {
  66. result := RunCommand("/foobar")
  67. result.Assert(t, Expected{Error: "foobar", ExitCode: 127})
  68. }
  69. func TestRunCommandWithStdoutStderr(t *testing.T) {
  70. result := RunCommand("echo", "hello", "world")
  71. result.Assert(t, Expected{Out: "hello world\n", Err: None})
  72. }
  73. func TestRunCommandWithStdoutStderrError(t *testing.T) {
  74. result := RunCommand("doesnotexists")
  75. expected := `exec: "doesnotexists": executable file not found`
  76. result.Assert(t, Expected{Out: None, Err: None, ExitCode: 127, Error: expected})
  77. switch runtime.GOOS {
  78. case "windows":
  79. expected = "ls: unknown option"
  80. case "solaris":
  81. expected = "gls: invalid option"
  82. default:
  83. expected = "ls: invalid option"
  84. }
  85. var cmd string
  86. if runtime.GOOS == "solaris" {
  87. cmd = "gls"
  88. } else {
  89. cmd = "ls"
  90. }
  91. result = RunCommand(cmd, "-z")
  92. result.Assert(t, Expected{
  93. Out: None,
  94. Err: expected,
  95. ExitCode: 2,
  96. Error: "exit status 2",
  97. })
  98. }