00-setup.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. // just a little pre-run script to set up the fixtures.
  2. // zz-finish cleans it up
  3. var mkdirp = require("mkdirp")
  4. var path = require("path")
  5. var i = 0
  6. var tap = require("tap")
  7. var fs = require("fs")
  8. var rimraf = require("rimraf")
  9. var files =
  10. [ "a/.abcdef/x/y/z/a"
  11. , "a/abcdef/g/h"
  12. , "a/abcfed/g/h"
  13. , "a/b/c/d"
  14. , "a/bc/e/f"
  15. , "a/c/d/c/b"
  16. , "a/cb/e/f"
  17. ]
  18. var symlinkTo = path.resolve(__dirname, "a/symlink/a/b/c")
  19. var symlinkFrom = "../.."
  20. files = files.map(function (f) {
  21. return path.resolve(__dirname, f)
  22. })
  23. tap.test("remove fixtures", function (t) {
  24. rimraf(path.resolve(__dirname, "a"), function (er) {
  25. t.ifError(er, "remove fixtures")
  26. t.end()
  27. })
  28. })
  29. files.forEach(function (f) {
  30. tap.test(f, function (t) {
  31. var d = path.dirname(f)
  32. mkdirp(d, 0755, function (er) {
  33. if (er) {
  34. t.fail(er)
  35. return t.bailout()
  36. }
  37. fs.writeFile(f, "i like tests", function (er) {
  38. t.ifError(er, "make file")
  39. t.end()
  40. })
  41. })
  42. })
  43. })
  44. if (process.platform !== "win32") {
  45. tap.test("symlinky", function (t) {
  46. var d = path.dirname(symlinkTo)
  47. console.error("mkdirp", d)
  48. mkdirp(d, 0755, function (er) {
  49. t.ifError(er)
  50. fs.symlink(symlinkFrom, symlinkTo, "dir", function (er) {
  51. t.ifError(er, "make symlink")
  52. t.end()
  53. })
  54. })
  55. })
  56. }
  57. ;["foo","bar","baz","asdf","quux","qwer","rewq"].forEach(function (w) {
  58. w = "/tmp/glob-test/" + w
  59. tap.test("create " + w, function (t) {
  60. mkdirp(w, function (er) {
  61. if (er)
  62. throw er
  63. t.pass(w)
  64. t.end()
  65. })
  66. })
  67. })
  68. // generate the bash pattern test-fixtures if possible
  69. if (process.platform === "win32" || !process.env.TEST_REGEN) {
  70. console.error("Windows, or TEST_REGEN unset. Using cached fixtures.")
  71. return
  72. }
  73. var spawn = require("child_process").spawn;
  74. var globs =
  75. // put more patterns here.
  76. // anything that would be directly in / should be in /tmp/glob-test
  77. ["test/a/*/+(c|g)/./d"
  78. ,"test/a/**/[cg]/../[cg]"
  79. ,"test/a/{b,c,d,e,f}/**/g"
  80. ,"test/a/b/**"
  81. ,"test/**/g"
  82. ,"test/a/abc{fed,def}/g/h"
  83. ,"test/a/abc{fed/g,def}/**/"
  84. ,"test/a/abc{fed/g,def}/**///**/"
  85. ,"test/**/a/**/"
  86. ,"test/+(a|b|c)/a{/,bc*}/**"
  87. ,"test/*/*/*/f"
  88. ,"test/**/f"
  89. ,"test/a/symlink/a/b/c/a/b/c/a/b/c//a/b/c////a/b/c/**/b/c/**"
  90. ,"{./*/*,/tmp/glob-test/*}"
  91. ,"{/tmp/glob-test/*,*}" // evil owl face! how you taunt me!
  92. ,"test/a/!(symlink)/**"
  93. ]
  94. var bashOutput = {}
  95. var fs = require("fs")
  96. globs.forEach(function (pattern) {
  97. tap.test("generate fixture " + pattern, function (t) {
  98. var cmd = "shopt -s globstar && " +
  99. "shopt -s extglob && " +
  100. "shopt -s nullglob && " +
  101. // "shopt >&2; " +
  102. "eval \'for i in " + pattern + "; do echo $i; done\'"
  103. var cp = spawn("bash", ["-c", cmd], { cwd: path.dirname(__dirname) })
  104. var out = []
  105. cp.stdout.on("data", function (c) {
  106. out.push(c)
  107. })
  108. cp.stderr.pipe(process.stderr)
  109. cp.on("close", function (code) {
  110. out = flatten(out)
  111. if (!out)
  112. out = []
  113. else
  114. out = cleanResults(out.split(/\r*\n/))
  115. bashOutput[pattern] = out
  116. t.notOk(code, "bash test should finish nicely")
  117. t.end()
  118. })
  119. })
  120. })
  121. tap.test("save fixtures", function (t) {
  122. var fname = path.resolve(__dirname, "bash-results.json")
  123. var data = JSON.stringify(bashOutput, null, 2) + "\n"
  124. fs.writeFile(fname, data, function (er) {
  125. t.ifError(er)
  126. t.end()
  127. })
  128. })
  129. function cleanResults (m) {
  130. // normalize discrepancies in ordering, duplication,
  131. // and ending slashes.
  132. return m.map(function (m) {
  133. return m.replace(/\/+/g, "/").replace(/\/$/, "")
  134. }).sort(alphasort).reduce(function (set, f) {
  135. if (f !== set[set.length - 1]) set.push(f)
  136. return set
  137. }, []).sort(alphasort).map(function (f) {
  138. // de-windows
  139. return (process.platform !== 'win32') ? f
  140. : f.replace(/^[a-zA-Z]:\\\\/, '/').replace(/\\/g, '/')
  141. })
  142. }
  143. function flatten (chunks) {
  144. var s = 0
  145. chunks.forEach(function (c) { s += c.length })
  146. var out = new Buffer(s)
  147. s = 0
  148. chunks.forEach(function (c) {
  149. c.copy(out, s)
  150. s += c.length
  151. })
  152. return out.toString().trim()
  153. }
  154. function alphasort (a, b) {
  155. a = a.toLowerCase()
  156. b = b.toLowerCase()
  157. return a > b ? 1 : a < b ? -1 : 0
  158. }