Initial commit
[yaffs-website] / node_modules / globule / node_modules / glob / test / nocase-nomagic.js
1 var fs = require('graceful-fs');
2 var test = require('tap').test;
3 var glob = require('../');
4
5 test('mock fs', function(t) {
6   var stat = fs.stat
7   var statSync = fs.statSync
8   var readdir = fs.readdir
9   var readdirSync = fs.readdirSync
10
11   function fakeStat(path) {
12     var ret
13     switch (path.toLowerCase()) {
14       case '/tmp': case '/tmp/':
15         ret = { isDirectory: function() { return true } }
16         break
17       case '/tmp/a':
18         ret = { isDirectory: function() { return false } }
19         break
20     }
21     return ret
22   }
23
24   fs.stat = function(path, cb) {
25     var f = fakeStat(path);
26     if (f) {
27       process.nextTick(function() {
28         cb(null, f)
29       })
30     } else {
31       stat.call(fs, path, cb)
32     }
33   }
34
35   fs.statSync = function(path) {
36     return fakeStat(path) || statSync.call(fs, path)
37   }
38
39   function fakeReaddir(path) {
40     var ret
41     switch (path.toLowerCase()) {
42       case '/tmp': case '/tmp/':
43         ret = [ 'a', 'A' ]
44         break
45       case '/':
46         ret = ['tmp', 'tMp', 'tMP', 'TMP']
47     }
48     return ret
49   }
50
51   fs.readdir = function(path, cb) {
52     var f = fakeReaddir(path)
53     if (f)
54       process.nextTick(function() {
55         cb(null, f)
56       })
57     else
58       readdir.call(fs, path, cb)
59   }
60
61   fs.readdirSync = function(path) {
62     return fakeReaddir(path) || readdirSync.call(fs, path)
63   }
64
65   t.pass('mocked')
66   t.end()
67 })
68
69 test('nocase, nomagic', function(t) {
70   var n = 2
71   var want = [ '/TMP/A',
72                '/TMP/a',
73                '/tMP/A',
74                '/tMP/a',
75                '/tMp/A',
76                '/tMp/a',
77                '/tmp/A',
78                '/tmp/a' ]
79   glob('/tmp/a', { nocase: true }, function(er, res) {
80     if (er)
81       throw er
82     t.same(res.sort(), want)
83     if (--n === 0) t.end()
84   })
85   glob('/tmp/A', { nocase: true }, function(er, res) {
86     if (er)
87       throw er
88     t.same(res.sort(), want)
89     if (--n === 0) t.end()
90   })
91 })
92
93 test('nocase, with some magic', function(t) {
94   t.plan(2)
95   var want = [ '/TMP/A',
96                '/TMP/a',
97                '/tMP/A',
98                '/tMP/a',
99                '/tMp/A',
100                '/tMp/a',
101                '/tmp/A',
102                '/tmp/a' ]
103   glob('/tmp/*', { nocase: true }, function(er, res) {
104     if (er)
105       throw er
106     t.same(res.sort(), want)
107   })
108   glob('/tmp/*', { nocase: true }, function(er, res) {
109     if (er)
110       throw er
111     t.same(res.sort(), want)
112   })
113 })