nocase-nomagic.js 2.3 KB

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