watch_pattern_test.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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, "/path/{dir1,dir2}/**/*.php", "/path")
  30. hasDir(t, ".", relativeDir(t, ""))
  31. hasDir(t, "./", relativeDir(t, ""))
  32. hasDir(t, "./**", relativeDir(t, ""))
  33. hasDir(t, "..", relativeDir(t, "/.."))
  34. }
  35. func TestValidRecursiveDirectories(t *testing.T) {
  36. shouldMatch(t, "/path", "/path/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, "/path/**/", "/path/subpath/file.php")
  42. shouldMatch(t, ".", relativeDir(t, "file.php"))
  43. shouldMatch(t, ".", relativeDir(t, "subpath/file.php"))
  44. shouldMatch(t, "./**", relativeDir(t, "subpath/file.php"))
  45. shouldMatch(t, "..", relativeDir(t, "subpath/file.php"))
  46. }
  47. func TestInvalidRecursiveDirectories(t *testing.T) {
  48. shouldNotMatch(t, "/path", "/other/file.php")
  49. shouldNotMatch(t, "/path/**", "/other/file.php")
  50. shouldNotMatch(t, ".", "/other/file.php")
  51. }
  52. func TestValidNonRecursiveFilePatterns(t *testing.T) {
  53. shouldMatch(t, "/*.php", "/file.php")
  54. shouldMatch(t, "/path/*.php", "/path/file.php")
  55. shouldMatch(t, "/path/?ile.php", "/path/file.php")
  56. shouldMatch(t, "/path/file.php", "/path/file.php")
  57. shouldMatch(t, "*.php", relativeDir(t, "file.php"))
  58. shouldMatch(t, "./*.php", relativeDir(t, "file.php"))
  59. }
  60. func TestInValidNonRecursiveFilePatterns(t *testing.T) {
  61. shouldNotMatch(t, "/path/*.txt", "/path/file.php")
  62. shouldNotMatch(t, "/path/*.php", "/path/subpath/file.php")
  63. shouldNotMatch(t, "/*.php", "/path/file.php")
  64. shouldNotMatch(t, "*.txt", relativeDir(t, "file.php"))
  65. shouldNotMatch(t, "*.php", relativeDir(t, "subpath/file.php"))
  66. }
  67. func TestValidRecursiveFilePatterns(t *testing.T) {
  68. shouldMatch(t, "/path/**/*.php", "/path/file.php")
  69. shouldMatch(t, "/path/**/*.php", "/path/subpath/file.php")
  70. shouldMatch(t, "/path/**/?ile.php", "/path/subpath/file.php")
  71. shouldMatch(t, "/path/**/file.php", "/path/subpath/file.php")
  72. shouldMatch(t, "**/*.php", relativeDir(t, "file.php"))
  73. shouldMatch(t, "**/*.php", relativeDir(t, "subpath/file.php"))
  74. shouldMatch(t, "./**/*.php", relativeDir(t, "subpath/file.php"))
  75. }
  76. func TestInvalidRecursiveFilePatterns(t *testing.T) {
  77. shouldNotMatch(t, "/path/**/*.txt", "/path/file.php")
  78. shouldNotMatch(t, "/path/**/*.txt", "/other/file.php")
  79. shouldNotMatch(t, "/path/**/*.txt", "/path/subpath/file.php")
  80. shouldNotMatch(t, "/path/**/?ilm.php", "/path/subpath/file.php")
  81. shouldNotMatch(t, "**/*.php", "/other/file.php")
  82. shouldNotMatch(t, ".**/*.php", "/other/file.php")
  83. shouldNotMatch(t, "./**/*.php", "/other/file.php")
  84. }
  85. func TestValidDirectoryPatterns(t *testing.T) {
  86. shouldMatch(t, "/path/*/*.php", "/path/subpath/file.php")
  87. shouldMatch(t, "/path/*/*/*.php", "/path/subpath/subpath/file.php")
  88. shouldMatch(t, "/path/?/*.php", "/path/1/file.php")
  89. shouldMatch(t, "/path/**/vendor/*.php", "/path/vendor/file.php")
  90. shouldMatch(t, "/path/**/vendor/*.php", "/path/subpath/vendor/file.php")
  91. shouldMatch(t, "/path/**/vendor/**/*.php", "/path/vendor/file.php")
  92. shouldMatch(t, "/path/**/vendor/**/*.php", "/path/subpath/subpath/vendor/subpath/subpath/file.php")
  93. shouldMatch(t, "/path/**/vendor/*/*.php", "/path/subpath/subpath/vendor/subpath/file.php")
  94. shouldMatch(t, "/path*/path*/*", "/path1/path2/file.php")
  95. }
  96. func TestInvalidDirectoryPatterns(t *testing.T) {
  97. shouldNotMatch(t, "/path/subpath/*.php", "/path/other/file.php")
  98. shouldNotMatch(t, "/path/*/*.php", "/path/subpath/subpath/file.php")
  99. shouldNotMatch(t, "/path/?/*.php", "/path/subpath/file.php")
  100. shouldNotMatch(t, "/path/*/*/*.php", "/path/subpath/file.php")
  101. shouldNotMatch(t, "/path/*/*/*.php", "/path/subpath/subpath/subpath/file.php")
  102. shouldNotMatch(t, "/path/**/vendor/*.php", "/path/subpath/vendor/subpath/file.php")
  103. shouldNotMatch(t, "/path/**/vendor/*.php", "/path/subpath/file.php")
  104. shouldNotMatch(t, "/path/**/vendor/**/*.php", "/path/subpath/file.php")
  105. shouldNotMatch(t, "/path/**/vendor/**/*.txt", "/path/subpath/vendor/subpath/file.php")
  106. shouldNotMatch(t, "/path/**/vendor/**/*.php", "/path/subpath/subpath/subpath/file.php")
  107. shouldNotMatch(t, "/path/**/vendor/*/*.php", "/path/subpath/vendor/subpath/subpath/file.php")
  108. shouldNotMatch(t, "/path*/path*", "/path1/path1/file.php")
  109. }
  110. func TestValidExtendedPatterns(t *testing.T) {
  111. shouldMatch(t, "/path/*.{php}", "/path/file.php")
  112. shouldMatch(t, "/path/*.{php,twig}", "/path/file.php")
  113. shouldMatch(t, "/path/*.{php,twig}", "/path/file.twig")
  114. shouldMatch(t, "/path/**/{file.php,file.twig}", "/path/subpath/file.twig")
  115. shouldMatch(t, "/path/{dir1,dir2}/file.php", "/path/dir1/file.php")
  116. shouldMatch(t, "/path/{dir1,dir2}/file.php", "/path/dir2/file.php")
  117. shouldMatch(t, "/app/{app,config,resources}/**/*.php", "/app/app/subpath/file.php")
  118. shouldMatch(t, "/app/{app,config,resources}/**/*.php", "/app/config/subpath/file.php")
  119. }
  120. func TestInValidExtendedPatterns(t *testing.T) {
  121. shouldNotMatch(t, "/path/*.{php}", "/path/file.txt")
  122. shouldNotMatch(t, "/path/*.{php,twig}", "/path/file.txt")
  123. shouldNotMatch(t, "/path/{file.php,file.twig}", "/path/file.txt")
  124. shouldNotMatch(t, "/path/{dir1,dir2}/file.php", "/path/dir3/file.php")
  125. shouldNotMatch(t, "/path/{dir1,dir2}/**/*.php", "/path/dir1/subpath/file.txt")
  126. }
  127. func relativeDir(t *testing.T, relativePath string) string {
  128. dir, err := filepath.Abs("./" + relativePath)
  129. assert.NoError(t, err)
  130. return dir
  131. }
  132. func hasDir(t *testing.T, pattern string, dir string) {
  133. watchPattern, err := parseFilePattern(pattern)
  134. assert.NoError(t, err)
  135. assert.Equal(t, dir, watchPattern.dir)
  136. }
  137. func shouldMatch(t *testing.T, pattern string, fileName string) {
  138. watchPattern, err := parseFilePattern(pattern)
  139. assert.NoError(t, err)
  140. assert.True(t, watchPattern.allowReload(fileName, 0, 0))
  141. }
  142. func shouldNotMatch(t *testing.T, pattern string, fileName string) {
  143. watchPattern, err := parseFilePattern(pattern)
  144. assert.NoError(t, err)
  145. assert.False(t, watchPattern.allowReload(fileName, 0, 0))
  146. }