1 // just a little pre-run script to set up the fixtures.
2 // zz-finish cleans it up
4 var mkdirp = require("mkdirp")
5 var path = require("path")
7 var tap = require("tap")
9 var rimraf = require("rimraf")
21 var symlinkTo = path.resolve(__dirname, "a/symlink/a/b/c")
22 var symlinkFrom = "../.."
24 files = files.map(function (f) {
25 return path.resolve(__dirname, f)
28 tap.test("remove fixtures", function (t) {
29 rimraf(path.resolve(__dirname, "a"), function (er) {
30 t.ifError(er, "remove fixtures")
35 files.forEach(function (f) {
36 tap.test(f, function (t) {
37 var d = path.dirname(f)
38 mkdirp(d, 0755, function (er) {
43 fs.writeFile(f, "i like tests", function (er) {
44 t.ifError(er, "make file")
51 if (process.platform !== "win32") {
52 tap.test("symlinky", function (t) {
53 var d = path.dirname(symlinkTo)
54 console.error("mkdirp", d)
55 mkdirp(d, 0755, function (er) {
57 fs.symlink(symlinkFrom, symlinkTo, "dir", function (er) {
58 t.ifError(er, "make symlink")
65 ;["foo","bar","baz","asdf","quux","qwer","rewq"].forEach(function (w) {
66 w = "/tmp/glob-test/" + w
67 tap.test("create " + w, function (t) {
68 mkdirp(w, function (er) {
78 // generate the bash pattern test-fixtures if possible
79 if (process.platform === "win32" || !process.env.TEST_REGEN) {
80 console.error("Windows, or TEST_REGEN unset. Using cached fixtures.")
84 var spawn = require("child_process").spawn;
86 // put more patterns here.
87 // anything that would be directly in / should be in /tmp/glob-test
88 ["test/a/*/+(c|g)/./d"
89 ,"test/a/**/[cg]/../[cg]"
90 ,"test/a/{b,c,d,e,f}/**/g"
93 ,"test/a/abc{fed,def}/g/h"
94 ,"test/a/abc{fed/g,def}/**/"
95 ,"test/a/abc{fed/g,def}/**///**/"
97 ,"test/+(a|b|c)/a{/,bc*}/**"
100 ,"test/a/symlink/a/b/c/a/b/c/a/b/c//a/b/c////a/b/c/**/b/c/**"
101 ,"{./*/*,/tmp/glob-test/*}"
102 ,"{/tmp/glob-test/*,*}" // evil owl face! how you taunt me!
103 ,"test/a/!(symlink)/**"
106 var fs = require("fs")
108 globs.forEach(function (pattern) {
109 tap.test("generate fixture " + pattern, function (t) {
110 var cmd = "shopt -s globstar && " +
111 "shopt -s extglob && " +
112 "shopt -s nullglob && " +
114 "eval \'for i in " + pattern + "; do echo $i; done\'"
115 var cp = spawn("bash", ["-c", cmd], { cwd: path.dirname(__dirname) })
117 cp.stdout.on("data", function (c) {
120 cp.stderr.pipe(process.stderr)
121 cp.on("close", function (code) {
126 out = cleanResults(out.split(/\r*\n/))
128 bashOutput[pattern] = out
129 t.notOk(code, "bash test should finish nicely")
135 tap.test("save fixtures", function (t) {
136 var fname = path.resolve(__dirname, "bash-results.json")
137 var data = JSON.stringify(bashOutput, null, 2) + "\n"
138 fs.writeFile(fname, data, function (er) {
144 function cleanResults (m) {
145 // normalize discrepancies in ordering, duplication,
146 // and ending slashes.
147 return m.map(function (m) {
148 return m.replace(/\/+/g, "/").replace(/\/$/, "")
149 }).sort(alphasort).reduce(function (set, f) {
150 if (f !== set[set.length - 1]) set.push(f)
152 }, []).sort(alphasort).map(function (f) {
154 return (process.platform !== 'win32') ? f
155 : f.replace(/^[a-zA-Z]:\\\\/, '/').replace(/\\/g, '/')
159 function flatten (chunks) {
161 chunks.forEach(function (c) { s += c.length })
162 var out = new Buffer(s)
164 chunks.forEach(function (c) {
169 return out.toString().trim()
172 function alphasort (a, b) {
175 return a > b ? 1 : a < b ? -1 : 0