staticmd_test.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package staticmd
  2. import (
  3. "errors"
  4. "os"
  5. "testing"
  6. )
  7. func init() {
  8. runnable = mockCmd{}
  9. stat = func(_ string) (os.FileInfo, error) { return nil, statError }
  10. isNotExist = func(_ error) bool { return notExist }
  11. }
  12. var mockError = errors.New("mock error")
  13. var mockCmdByteArray []byte
  14. var mockCmdError error
  15. var statError error
  16. var notExist bool
  17. type mockCmd struct{}
  18. func (self mockCmd) Run(command string, args ...string) ([]byte, error) {
  19. return mockCmdByteArray, mockCmdError
  20. }
  21. func TestPlacebo(_ *testing.T) {}
  22. func TestCmd(t *testing.T) {
  23. t.Parallel()
  24. c := cmd{}
  25. if _, e := c.Run(""); e == nil {
  26. t.FailNow()
  27. }
  28. }
  29. func TestExists(t *testing.T) {
  30. t.Parallel()
  31. // test stat success exists fail
  32. if _, e := exists(""); e != nil {
  33. t.FailNow()
  34. }
  35. // test stat success exists success
  36. notExist = true
  37. if _, e := exists(""); e != nil {
  38. t.FailNow()
  39. }
  40. // test stat fail
  41. statError = mockError
  42. if _, e := exists(""); e != nil {
  43. t.FailNow()
  44. }
  45. // test stat fail exists success
  46. notExist = false
  47. if _, e := exists(""); e == nil {
  48. t.FailNow()
  49. }
  50. }
  51. func TestVersion(t *testing.T) {
  52. t.Parallel()
  53. // test with byte array
  54. compare := "newp"
  55. mockCmdByteArray = []byte(compare)
  56. if v := version(""); v != compare {
  57. t.FailNow()
  58. }
  59. // test with error
  60. mockCmdError = mockError
  61. if v := version(""); v == compare || len(v) == 0 {
  62. t.FailNow()
  63. }
  64. }
  65. func TestBasename(t *testing.T) {
  66. t.Parallel()
  67. f := "/some/long/path"
  68. if o := basename(f); len(o) == 0 {
  69. t.FailNow()
  70. }
  71. }