bash-comparison.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // basic test
  2. // show that it does the same thing by default as the shell.
  3. var tap = require("tap")
  4. , child_process = require("child_process")
  5. , bashResults = require("./bash-results.json")
  6. , globs = Object.keys(bashResults)
  7. , glob = require("../")
  8. , path = require("path")
  9. // run from the root of the project
  10. // this is usually where you're at anyway, but be sure.
  11. process.chdir(path.resolve(__dirname, ".."))
  12. function alphasort (a, b) {
  13. a = a.toLowerCase()
  14. b = b.toLowerCase()
  15. return a > b ? 1 : a < b ? -1 : 0
  16. }
  17. globs.forEach(function (pattern) {
  18. var expect = bashResults[pattern]
  19. // anything regarding the symlink thing will fail on windows, so just skip it
  20. if (process.platform === "win32" &&
  21. expect.some(function (m) {
  22. return /\/symlink\//.test(m)
  23. }))
  24. return
  25. tap.test(pattern, function (t) {
  26. glob(pattern, function (er, matches) {
  27. if (er)
  28. throw er
  29. // sort and unmark, just to match the shell results
  30. matches = cleanResults(matches)
  31. t.deepEqual(matches, expect, pattern)
  32. t.end()
  33. })
  34. })
  35. tap.test(pattern + " sync", function (t) {
  36. var matches = cleanResults(glob.sync(pattern))
  37. t.deepEqual(matches, expect, "should match shell")
  38. t.end()
  39. })
  40. })
  41. function cleanResults (m) {
  42. // normalize discrepancies in ordering, duplication,
  43. // and ending slashes.
  44. return m.map(function (m) {
  45. return m.replace(/\/+/g, "/").replace(/\/$/, "")
  46. }).sort(alphasort).reduce(function (set, f) {
  47. if (f !== set[set.length - 1]) set.push(f)
  48. return set
  49. }, []).sort(alphasort).map(function (f) {
  50. // de-windows
  51. return (process.platform !== 'win32') ? f
  52. : f.replace(/^[a-zA-Z]:[\/\\]+/, '/').replace(/[\\\/]+/g, '/')
  53. })
  54. }