staticmd_test.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. }
  72. func TestIsMarkdown(t *testing.T) {
  73. t.Parallel()
  74. // test matching types
  75. for i := range extensions {
  76. if !isMarkdown("file." + extensions[i]) {
  77. t.FailNow()
  78. }
  79. }
  80. // test non matching type
  81. if isMarkdown("nope") {
  82. t.FailNow()
  83. }
  84. }