watch_pattern_test.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. //go:build !nowatcher
  2. package watcher
  3. import (
  4. "path/filepath"
  5. "testing"
  6. "github.com/stretchr/testify/assert"
  7. )
  8. func TestDisallowOnEventTypeBiggerThan3(t *testing.T) {
  9. const fileName = "/some/path/watch-me.php"
  10. const eventType = 4
  11. watchPattern, err := parseFilePattern("/some/path")
  12. assert.NoError(t, err)
  13. assert.False(t, watchPattern.allowReload(fileName, eventType, 0))
  14. }
  15. func TestDisallowOnPathTypeBiggerThan2(t *testing.T) {
  16. const fileName = "/some/path/watch-me.php"
  17. const pathType = 3
  18. watchPattern, err := parseFilePattern("/some/path")
  19. assert.NoError(t, err)
  20. assert.False(t, watchPattern.allowReload(fileName, 0, pathType))
  21. }
  22. func TestWatchesCorrectDir(t *testing.T) {
  23. hasDir(t, "/path", "/path")
  24. hasDir(t, "/path/", "/path")
  25. hasDir(t, "/path/**/*.php", "/path")
  26. hasDir(t, "/path/*.php", "/path")
  27. hasDir(t, "/path/*/*.php", "/path")
  28. hasDir(t, "/path/?dir/*.php", "/path")
  29. hasDir(t, ".", relativeDir(t, ""))
  30. hasDir(t, "./", relativeDir(t, ""))
  31. hasDir(t, "./**", relativeDir(t, ""))
  32. hasDir(t, "..", relativeDir(t, "/.."))
  33. }
  34. func TestValidRecursiveDirectories(t *testing.T) {
  35. shouldMatch(t, "/path", "/path/file.php")
  36. shouldMatch(t, "/path", "/path/subpath/file.php")
  37. shouldMatch(t, "/path/", "/path/subpath/file.php")
  38. shouldMatch(t, "/path**", "/path/subpath/file.php")
  39. shouldMatch(t, "/path/**", "/path/subpath/file.php")
  40. shouldMatch(t, "/path/**/", "/path/subpath/file.php")
  41. shouldMatch(t, ".", relativeDir(t, "file.php"))
  42. shouldMatch(t, ".", relativeDir(t, "subpath/file.php"))
  43. shouldMatch(t, "./**", relativeDir(t, "subpath/file.php"))
  44. shouldMatch(t, "..", relativeDir(t, "subpath/file.php"))
  45. }
  46. func TestInvalidRecursiveDirectories(t *testing.T) {
  47. shouldNotMatch(t, "/path", "/other/file.php")
  48. shouldNotMatch(t, "/path/**", "/other/file.php")
  49. shouldNotMatch(t, ".", "/other/file.php")
  50. }
  51. func TestValidNonRecursiveFilePatterns(t *testing.T) {
  52. shouldMatch(t, "/*.php", "/file.php")
  53. shouldMatch(t, "/path/*.php", "/path/file.php")
  54. shouldMatch(t, "/path/?ile.php", "/path/file.php")
  55. shouldMatch(t, "/path/file.php", "/path/file.php")
  56. shouldMatch(t, "*.php", relativeDir(t, "file.php"))
  57. shouldMatch(t, "./*.php", relativeDir(t, "file.php"))
  58. }
  59. func TestInValidNonRecursiveFilePatterns(t *testing.T) {
  60. shouldNotMatch(t, "/path/*.txt", "/path/file.php")
  61. shouldNotMatch(t, "/path/*.php", "/path/subpath/file.php")
  62. shouldNotMatch(t, "/*.php", "/path/file.php")
  63. shouldNotMatch(t, "*.txt", relativeDir(t, "file.php"))
  64. shouldNotMatch(t, "*.php", relativeDir(t, "subpath/file.php"))
  65. }
  66. func TestValidRecursiveFilePatterns(t *testing.T) {
  67. shouldMatch(t, "/path/**/*.php", "/path/file.php")
  68. shouldMatch(t, "/path/**/*.php", "/path/subpath/file.php")
  69. shouldMatch(t, "/path/**/?ile.php", "/path/subpath/file.php")
  70. shouldMatch(t, "/path/**/file.php", "/path/subpath/file.php")
  71. shouldMatch(t, "**/*.php", relativeDir(t, "file.php"))
  72. shouldMatch(t, "**/*.php", relativeDir(t, "subpath/file.php"))
  73. shouldMatch(t, "./**/*.php", relativeDir(t, "subpath/file.php"))
  74. }
  75. func TestInvalidRecursiveFilePatterns(t *testing.T) {
  76. shouldNotMatch(t, "/path/**/*.txt", "/path/file.php")
  77. shouldNotMatch(t, "/path/**/*.txt", "/other/file.php")
  78. shouldNotMatch(t, "/path/**/*.txt", "/path/subpath/file.php")
  79. shouldNotMatch(t, "/path/**/?ilm.php", "/path/subpath/file.php")
  80. shouldNotMatch(t, "**/*.php", "/other/file.php")
  81. shouldNotMatch(t, ".**/*.php", "/other/file.php")
  82. shouldNotMatch(t, "./**/*.php", "/other/file.php")
  83. }
  84. func TestValidDirectoryPatterns(t *testing.T) {
  85. shouldMatch(t, "/path/*/*.php", "/path/subpath/file.php")
  86. shouldMatch(t, "/path/*/*/*.php", "/path/subpath/subpath/file.php")
  87. shouldMatch(t, "/path/?/*.php", "/path/1/file.php")
  88. shouldMatch(t, "/path/**/vendor/*.php", "/path/vendor/file.php")
  89. shouldMatch(t, "/path/**/vendor/*.php", "/path/subpath/vendor/file.php")
  90. shouldMatch(t, "/path/**/vendor/**/*.php", "/path/vendor/file.php")
  91. shouldMatch(t, "/path/**/vendor/**/*.php", "/path/subpath/subpath/vendor/subpath/subpath/file.php")
  92. shouldMatch(t, "/path/**/vendor/*/*.php", "/path/subpath/subpath/vendor/subpath/file.php")
  93. shouldMatch(t, "/path*/path*/*", "/path1/path2/file.php")
  94. }
  95. func TestInvalidDirectoryPatterns(t *testing.T) {
  96. shouldNotMatch(t, "/path/subpath/*.php", "/path/other/file.php")
  97. shouldNotMatch(t, "/path/*/*.php", "/path/subpath/subpath/file.php")
  98. shouldNotMatch(t, "/path/?/*.php", "/path/subpath/file.php")
  99. shouldNotMatch(t, "/path/*/*/*.php", "/path/subpath/file.php")
  100. shouldNotMatch(t, "/path/*/*/*.php", "/path/subpath/subpath/subpath/file.php")
  101. shouldNotMatch(t, "/path/**/vendor/*.php", "/path/subpath/vendor/subpath/file.php")
  102. shouldNotMatch(t, "/path/**/vendor/*.php", "/path/subpath/file.php")
  103. shouldNotMatch(t, "/path/**/vendor/**/*.php", "/path/subpath/file.php")
  104. shouldNotMatch(t, "/path/**/vendor/**/*.txt", "/path/subpath/vendor/subpath/file.php")
  105. shouldNotMatch(t, "/path/**/vendor/**/*.php", "/path/subpath/subpath/subpath/file.php")
  106. shouldNotMatch(t, "/path/**/vendor/*/*.php", "/path/subpath/vendor/subpath/subpath/file.php")
  107. shouldNotMatch(t, "/path*/path*", "/path1/path1/file.php")
  108. }
  109. func TestValidExtendedPatterns(t *testing.T) {
  110. shouldMatch(t, "/path/*.{php}", "/path/file.php")
  111. shouldMatch(t, "/path/*.{php,twig}", "/path/file.php")
  112. shouldMatch(t, "/path/*.{php,twig}", "/path/file.twig")
  113. shouldMatch(t, "/path/**/{file.php,file.twig}", "/path/subpath/file.twig")
  114. shouldMatch(t, "/path/{folder1,folder2}/file.php", "/path/folder1/file.php")
  115. }
  116. func TestInValidExtendedPatterns(t *testing.T) {
  117. shouldNotMatch(t, "/path/*.{php}", "/path/file.txt")
  118. shouldNotMatch(t, "/path/*.{php,twig}", "/path/file.txt")
  119. shouldNotMatch(t, "/path/{file.php,file.twig}", "/path/file.txt")
  120. shouldNotMatch(t, "/path/{folder1,folder2}/file.php", "/path/folder3/file.php")
  121. }
  122. func relativeDir(t *testing.T, relativePath string) string {
  123. dir, err := filepath.Abs("./" + relativePath)
  124. assert.NoError(t, err)
  125. return dir
  126. }
  127. func hasDir(t *testing.T, pattern string, dir string) {
  128. watchPattern, err := parseFilePattern(pattern)
  129. assert.NoError(t, err)
  130. assert.Equal(t, dir, watchPattern.dir)
  131. }
  132. func shouldMatch(t *testing.T, pattern string, fileName string) {
  133. watchPattern, err := parseFilePattern(pattern)
  134. assert.NoError(t, err)
  135. assert.True(t, watchPattern.allowReload(fileName, 0, 0))
  136. }
  137. func shouldNotMatch(t *testing.T, pattern string, fileName string) {
  138. watchPattern, err := parseFilePattern(pattern)
  139. assert.NoError(t, err)
  140. assert.False(t, watchPattern.allowReload(fileName, 0, 0))
  141. }