-
-
Notifications
You must be signed in to change notification settings - Fork 468
/
nocase-nomagic.js
143 lines (131 loc) · 3.41 KB
/
nocase-nomagic.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
require("./global-leakage.js")
var fs = require('fs')
var test = require('tap').test;
var glob = require('../');
var cwd = process.cwd()
var drive = 'c'
if (/^[a-zA-Z]:[\\\/]/.test(cwd)) {
drive = cwd.charAt(0).toLowerCase()
}
test('mock fs', function(t) {
var stat = fs.stat
var statSync = fs.statSync
var readdir = fs.readdir
var readdirSync = fs.readdirSync
function fakeStat(path) {
var ret
switch (path.toLowerCase().replace(/\\/g, '/')) {
case '/tmp': case '/tmp/': case drive+':\\tmp': case drive+':\\tmp\\':
ret = { isDirectory: function() { return true } }
break
case '/tmp/a': case drive+':/tmp/a':
ret = { isDirectory: function() { return false } }
break
}
return ret
}
fs.stat = function(path, cb) {
var f = fakeStat(path);
if (f) {
process.nextTick(function() {
cb(null, f)
})
} else {
stat.call(fs, path, cb)
}
}
fs.statSync = function(path) {
return fakeStat(path) || statSync.call(fs, path)
}
function fakeReaddir(path) {
var ret
switch (path.toLowerCase().replace(/\\/g, '/')) {
case '/tmp': case '/tmp/': case drive+':/tmp': case drive+':/tmp/':
ret = [ 'a', 'A' ]
break
case '/': case drive+':/':
ret = ['tmp', 'tMp', 'tMP', 'TMP']
}
return ret
}
fs.readdir = function(path, cb) {
var f = fakeReaddir(path)
if (f)
process.nextTick(function() {
cb(null, f)
})
else
readdir.call(fs, path, cb)
}
fs.readdirSync = function(path) {
return fakeReaddir(path) || readdirSync.call(fs, path)
}
t.pass('mocked')
t.end()
})
test('nocase, nomagic', function(t) {
var n = 2
var want = [ '/TMP/A',
'/TMP/a',
'/tMP/A',
'/tMP/a',
'/tMp/A',
'/tMp/a',
'/tmp/A',
'/tmp/a' ]
if(process.platform.match(/^win/)) {
want = want.map(function(p) {
return drive+':' + p
})
}
glob('/tmp/a', { nocase: true }, function(er, res) {
if (er)
throw er
if (process.platform.match(/^win/))
res = res.map(function (r) {
return r.replace(/\\/g, '/').replace(new RegExp('^' + drive + ':'), drive+':')
})
t.same(res.sort(), want)
if (--n === 0) t.end()
})
glob('/tmp/A', { nocase: true }, function(er, res) {
if (er)
throw er
if (process.platform.match(/^win/))
res = res.map(function (r) {
return r.replace(/\\/g, '/').replace(new RegExp('^' + drive + ':'), drive+':')
})
t.same(res.sort(), want)
if (--n === 0) t.end()
})
})
test('nocase, with some magic', function(t) {
t.plan(2)
var want = [ '/TMP/A',
'/TMP/a',
'/tMP/A',
'/tMP/a',
'/tMp/A',
'/tMp/a',
'/tmp/A',
'/tmp/a' ]
if(process.platform.match(/^win/)) {
want = want.map(function(p) {
return drive + ':' + p
})
}
glob('/tmp/*', { nocase: true }, function(er, res) {
if (er)
throw er
if (process.platform.match(/^win/))
res = res.map(function (r) { return r.replace(/\\/g, '/') })
t.same(res.sort(), want)
})
glob('/tmp/*', { nocase: true }, function(er, res) {
if (er)
throw er
if (process.platform.match(/^win/))
res = res.map(function (r) { return r.replace(/\\/g, '/') })
t.same(res.sort(), want)
})
})