defaults.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. // http://www.bashcookbook.com/bashinfo/source/bash-1.14.7/tests/glob-test
  2. //
  3. // TODO: Some of these tests do very bad things with backslashes, and will
  4. // most likely fail badly on windows. They should probably be skipped.
  5. var tap = require("tap")
  6. , globalBefore = Object.keys(global)
  7. , mm = require("../")
  8. , files = [ "a", "b", "c", "d", "abc"
  9. , "abd", "abe", "bb", "bcd"
  10. , "ca", "cb", "dd", "de"
  11. , "bdir/", "bdir/cfile"]
  12. , next = files.concat([ "a-b", "aXb"
  13. , ".x", ".y" ])
  14. tap.test("basic tests", function (t) {
  15. var start = Date.now()
  16. // [ pattern, [matches], MM opts, files, TAP opts]
  17. ; [ "http://www.bashcookbook.com/bashinfo" +
  18. "/source/bash-1.14.7/tests/glob-test"
  19. , ["a*", ["a", "abc", "abd", "abe"]]
  20. , ["X*", ["X*"], {nonull: true}]
  21. // allow null glob expansion
  22. , ["X*", []]
  23. // isaacs: Slightly different than bash/sh/ksh
  24. // \\* is not un-escaped to literal "*" in a failed match,
  25. // but it does make it get treated as a literal star
  26. , ["\\*", ["\\*"], {nonull: true}]
  27. , ["\\**", ["\\**"], {nonull: true}]
  28. , ["\\*\\*", ["\\*\\*"], {nonull: true}]
  29. , ["b*/", ["bdir/"]]
  30. , ["c*", ["c", "ca", "cb"]]
  31. , ["**", files]
  32. , ["\\.\\./*/", ["\\.\\./*/"], {nonull: true}]
  33. , ["s/\\..*//", ["s/\\..*//"], {nonull: true}]
  34. , "legendary larry crashes bashes"
  35. , ["/^root:/{s/^[^:]*:[^:]*:\([^:]*\).*$/\\1/"
  36. , ["/^root:/{s/^[^:]*:[^:]*:\([^:]*\).*$/\\1/"], {nonull: true}]
  37. , ["/^root:/{s/^[^:]*:[^:]*:\([^:]*\).*$/\1/"
  38. , ["/^root:/{s/^[^:]*:[^:]*:\([^:]*\).*$/\1/"], {nonull: true}]
  39. , "character classes"
  40. , ["[a-c]b*", ["abc", "abd", "abe", "bb", "cb"]]
  41. , ["[a-y]*[^c]", ["abd", "abe", "bb", "bcd",
  42. "bdir/", "ca", "cb", "dd", "de"]]
  43. , ["a*[^c]", ["abd", "abe"]]
  44. , function () { files.push("a-b", "aXb") }
  45. , ["a[X-]b", ["a-b", "aXb"]]
  46. , function () { files.push(".x", ".y") }
  47. , ["[^a-c]*", ["d", "dd", "de"]]
  48. , function () { files.push("a*b/", "a*b/ooo") }
  49. , ["a\\*b/*", ["a*b/ooo"]]
  50. , ["a\\*?/*", ["a*b/ooo"]]
  51. , ["*\\\\!*", [], {null: true}, ["echo !7"]]
  52. , ["*\\!*", ["echo !7"], null, ["echo !7"]]
  53. , ["*.\\*", ["r.*"], null, ["r.*"]]
  54. , ["a[b]c", ["abc"]]
  55. , ["a[\\b]c", ["abc"]]
  56. , ["a?c", ["abc"]]
  57. , ["a\\*c", [], {null: true}, ["abc"]]
  58. , ["", [""], { null: true }, [""]]
  59. , "http://www.opensource.apple.com/source/bash/bash-23/" +
  60. "bash/tests/glob-test"
  61. , function () { files.push("man/", "man/man1/", "man/man1/bash.1") }
  62. , ["*/man*/bash.*", ["man/man1/bash.1"]]
  63. , ["man/man1/bash.1", ["man/man1/bash.1"]]
  64. , ["a***c", ["abc"], null, ["abc"]]
  65. , ["a*****?c", ["abc"], null, ["abc"]]
  66. , ["?*****??", ["abc"], null, ["abc"]]
  67. , ["*****??", ["abc"], null, ["abc"]]
  68. , ["?*****?c", ["abc"], null, ["abc"]]
  69. , ["?***?****c", ["abc"], null, ["abc"]]
  70. , ["?***?****?", ["abc"], null, ["abc"]]
  71. , ["?***?****", ["abc"], null, ["abc"]]
  72. , ["*******c", ["abc"], null, ["abc"]]
  73. , ["*******?", ["abc"], null, ["abc"]]
  74. , ["a*cd**?**??k", ["abcdecdhjk"], null, ["abcdecdhjk"]]
  75. , ["a**?**cd**?**??k", ["abcdecdhjk"], null, ["abcdecdhjk"]]
  76. , ["a**?**cd**?**??k***", ["abcdecdhjk"], null, ["abcdecdhjk"]]
  77. , ["a**?**cd**?**??***k", ["abcdecdhjk"], null, ["abcdecdhjk"]]
  78. , ["a**?**cd**?**??***k**", ["abcdecdhjk"], null, ["abcdecdhjk"]]
  79. , ["a****c**?**??*****", ["abcdecdhjk"], null, ["abcdecdhjk"]]
  80. , ["[-abc]", ["-"], null, ["-"]]
  81. , ["[abc-]", ["-"], null, ["-"]]
  82. , ["\\", ["\\"], null, ["\\"]]
  83. , ["[\\\\]", ["\\"], null, ["\\"]]
  84. , ["[[]", ["["], null, ["["]]
  85. , ["[", ["["], null, ["["]]
  86. , ["[*", ["[abc"], null, ["[abc"]]
  87. , "a right bracket shall lose its special meaning and\n" +
  88. "represent itself in a bracket expression if it occurs\n" +
  89. "first in the list. -- POSIX.2 2.8.3.2"
  90. , ["[]]", ["]"], null, ["]"]]
  91. , ["[]-]", ["]"], null, ["]"]]
  92. , ["[a-\z]", ["p"], null, ["p"]]
  93. , ["??**********?****?", [], { null: true }, ["abc"]]
  94. , ["??**********?****c", [], { null: true }, ["abc"]]
  95. , ["?************c****?****", [], { null: true }, ["abc"]]
  96. , ["*c*?**", [], { null: true }, ["abc"]]
  97. , ["a*****c*?**", [], { null: true }, ["abc"]]
  98. , ["a********???*******", [], { null: true }, ["abc"]]
  99. , ["[]", [], { null: true }, ["a"]]
  100. , ["[abc", [], { null: true }, ["["]]
  101. , "nocase tests"
  102. , ["XYZ", ["xYz"], { nocase: true, null: true }
  103. , ["xYz", "ABC", "IjK"]]
  104. , ["ab*", ["ABC"], { nocase: true, null: true }
  105. , ["xYz", "ABC", "IjK"]]
  106. , ["[ia]?[ck]", ["ABC", "IjK"], { nocase: true, null: true }
  107. , ["xYz", "ABC", "IjK"]]
  108. // [ pattern, [matches], MM opts, files, TAP opts]
  109. , "onestar/twostar"
  110. , ["{/*,*}", [], {null: true}, ["/asdf/asdf/asdf"]]
  111. , ["{/?,*}", ["/a", "bb"], {null: true}
  112. , ["/a", "/b/b", "/a/b/c", "bb"]]
  113. , "dots should not match unless requested"
  114. , ["**", ["a/b"], {}, ["a/b", "a/.d", ".a/.d"]]
  115. // .. and . can only match patterns starting with .,
  116. // even when options.dot is set.
  117. , function () {
  118. files = ["a/./b", "a/../b", "a/c/b", "a/.d/b"]
  119. }
  120. , ["a/*/b", ["a/c/b", "a/.d/b"], {dot: true}]
  121. , ["a/.*/b", ["a/./b", "a/../b", "a/.d/b"], {dot: true}]
  122. , ["a/*/b", ["a/c/b"], {dot:false}]
  123. , ["a/.*/b", ["a/./b", "a/../b", "a/.d/b"], {dot: false}]
  124. // this also tests that changing the options needs
  125. // to change the cache key, even if the pattern is
  126. // the same!
  127. , ["**", ["a/b","a/.d",".a/.d"], { dot: true }
  128. , [ ".a/.d", "a/.d", "a/b"]]
  129. , "paren sets cannot contain slashes"
  130. , ["*(a/b)", ["*(a/b)"], {nonull: true}, ["a/b"]]
  131. // brace sets trump all else.
  132. //
  133. // invalid glob pattern. fails on bash4 and bsdglob.
  134. // however, in this implementation, it's easier just
  135. // to do the intuitive thing, and let brace-expansion
  136. // actually come before parsing any extglob patterns,
  137. // like the documentation seems to say.
  138. //
  139. // XXX: if anyone complains about this, either fix it
  140. // or tell them to grow up and stop complaining.
  141. //
  142. // bash/bsdglob says this:
  143. // , ["*(a|{b),c)}", ["*(a|{b),c)}"], {}, ["a", "ab", "ac", "ad"]]
  144. // but we do this instead:
  145. , ["*(a|{b),c)}", ["a", "ab", "ac"], {}, ["a", "ab", "ac", "ad"]]
  146. // test partial parsing in the presence of comment/negation chars
  147. , ["[!a*", ["[!ab"], {}, ["[!ab", "[ab"]]
  148. , ["[#a*", ["[#ab"], {}, ["[#ab", "[ab"]]
  149. // like: {a,b|c\\,d\\\|e} except it's unclosed, so it has to be escaped.
  150. , ["+(a|*\\|c\\\\|d\\\\\\|e\\\\\\\\|f\\\\\\\\\\|g"
  151. , ["+(a|b\\|c\\\\|d\\\\|e\\\\\\\\|f\\\\\\\\|g"]
  152. , {}
  153. , ["+(a|b\\|c\\\\|d\\\\|e\\\\\\\\|f\\\\\\\\|g", "a", "b\\c"]]
  154. // crazy nested {,,} and *(||) tests.
  155. , function () {
  156. files = [ "a", "b", "c", "d"
  157. , "ab", "ac", "ad"
  158. , "bc", "cb"
  159. , "bc,d", "c,db", "c,d"
  160. , "d)", "(b|c", "*(b|c"
  161. , "b|c", "b|cc", "cb|c"
  162. , "x(a|b|c)", "x(a|c)"
  163. , "(a|b|c)", "(a|c)"]
  164. }
  165. , ["*(a|{b,c})", ["a", "b", "c", "ab", "ac"]]
  166. , ["{a,*(b|c,d)}", ["a","(b|c", "*(b|c", "d)"]]
  167. // a
  168. // *(b|c)
  169. // *(b|d)
  170. , ["{a,*(b|{c,d})}", ["a","b", "bc", "cb", "c", "d"]]
  171. , ["*(a|{b|c,c})", ["a", "b", "c", "ab", "ac", "bc", "cb"]]
  172. // test various flag settings.
  173. , [ "*(a|{b|c,c})", ["x(a|b|c)", "x(a|c)", "(a|b|c)", "(a|c)"]
  174. , { noext: true } ]
  175. , ["a?b", ["x/y/acb", "acb/"], {matchBase: true}
  176. , ["x/y/acb", "acb/", "acb/d/e", "x/y/acb/d"] ]
  177. , ["#*", ["#a", "#b"], {nocomment: true}, ["#a", "#b", "c#d"]]
  178. // begin channelling Boole and deMorgan...
  179. , "negation tests"
  180. , function () {
  181. files = ["d", "e", "!ab", "!abc", "a!b", "\\!a"]
  182. }
  183. // anything that is NOT a* matches.
  184. , ["!a*", ["\\!a", "d", "e", "!ab", "!abc"]]
  185. // anything that IS !a* matches.
  186. , ["!a*", ["!ab", "!abc"], {nonegate: true}]
  187. // anything that IS a* matches
  188. , ["!!a*", ["a!b"]]
  189. // anything that is NOT !a* matches
  190. , ["!\\!a*", ["a!b", "d", "e", "\\!a"]]
  191. // negation nestled within a pattern
  192. , function () {
  193. files = [ "foo.js"
  194. , "foo.bar"
  195. // can't match this one without negative lookbehind.
  196. , "foo.js.js"
  197. , "blar.js"
  198. , "foo."
  199. , "boo.js.boo" ]
  200. }
  201. , ["*.!(js)", ["foo.bar", "foo.", "boo.js.boo"] ]
  202. ].forEach(function (c) {
  203. if (typeof c === "function") return c()
  204. if (typeof c === "string") return t.comment(c)
  205. var pattern = c[0]
  206. , expect = c[1].sort(alpha)
  207. , options = c[2]
  208. , f = c[3] || files
  209. , tapOpts = c[4] || {}
  210. // options.debug = true
  211. var Class = mm.defaults(options).Minimatch
  212. var m = new Class(pattern, {})
  213. var r = m.makeRe()
  214. tapOpts.re = String(r) || JSON.stringify(r)
  215. tapOpts.files = JSON.stringify(f)
  216. tapOpts.pattern = pattern
  217. tapOpts.set = m.set
  218. tapOpts.negated = m.negate
  219. var actual = mm.match(f, pattern, options)
  220. actual.sort(alpha)
  221. t.equivalent( actual, expect
  222. , JSON.stringify(pattern) + " " + JSON.stringify(expect)
  223. , tapOpts )
  224. })
  225. t.comment("time=" + (Date.now() - start) + "ms")
  226. t.end()
  227. })
  228. tap.test("global leak test", function (t) {
  229. var globalAfter = Object.keys(global)
  230. t.equivalent(globalAfter, globalBefore, "no new globals, please")
  231. t.end()
  232. })
  233. function alpha (a, b) {
  234. return a > b ? 1 : -1
  235. }