static_test.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package static
  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. // set expected defaults
  31. notExist = false
  32. statError = nil
  33. // test stat success exists fail
  34. if _, e := exists(""); e != nil {
  35. t.FailNow()
  36. }
  37. // test stat success exists success
  38. notExist = true
  39. if _, e := exists(""); e != nil {
  40. t.FailNow()
  41. }
  42. // test stat fail
  43. statError = mockError
  44. if _, e := exists(""); e != nil {
  45. t.FailNow()
  46. }
  47. // test stat fail exists success
  48. notExist = false
  49. if _, e := exists(""); e == nil {
  50. t.FailNow()
  51. }
  52. }
  53. func TestVersion(t *testing.T) {
  54. t.Parallel()
  55. // test with byte array
  56. compare := "newp"
  57. mockCmdByteArray = []byte(compare)
  58. if v := version(""); v != compare {
  59. t.FailNow()
  60. }
  61. // test with error
  62. mockCmdError = mockError
  63. if v := version(""); v == compare || len(v) == 0 {
  64. t.FailNow()
  65. }
  66. }
  67. func TestBasename(t *testing.T) {
  68. t.Parallel()
  69. f := "/some/long/path"
  70. if o := basename(f); len(o) == 0 {
  71. t.FailNow()
  72. }
  73. }
  74. func TestIsMarkdown(t *testing.T) {
  75. t.Parallel()
  76. // test matching types
  77. for i := range extensions {
  78. if !isMarkdown("file." + extensions[i]) {
  79. t.FailNow()
  80. }
  81. }
  82. // test non matching type
  83. if isMarkdown("nope") {
  84. t.FailNow()
  85. }
  86. }