File: enotsup.js

package info (click to toggle)
node-glob 7.1.3-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 936 kB
  • sloc: sh: 89; makefile: 20
file content (65 lines) | stat: -rw-r--r-- 1,689 bytes parent folder | download | duplicates (5)
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
var fs = require('fs')
var readdir = fs.readdir
var readdirSync = fs.readdirSync
var sawAsyncENOTSUP = false
var sawSyncENOTSUP = false

var path = require('path')
var fixtureDir = path.resolve(__dirname, 'fixtures')
var allowedDirs = [
  path.resolve(fixtureDir, 'a'),
  path.resolve(fixtureDir, 'a', 'abcdef'),
  path.resolve(fixtureDir, 'a', 'abcfed')
]

fs.readdirSync = function (p) {
  if (allowedDirs.indexOf(path.resolve(p)) === -1 &&
      !p.match(/[\\\/]node_modules[\\\/]/)) {
    sawSyncENOTSUP = true
    var er = new Error('ENOTSUP: Operation not supported')
    er.path = path
    er.code = 'ENOTSUP'
    throw er
  }
  return readdirSync.call(fs, p)
}

fs.readdir = function (p, cb) {
  if (allowedDirs.indexOf(path.resolve(p)) === -1 &&
      !p.match(/[\\\/]node_modules[\\\/]/)) {
    setTimeout(function () {
      sawAsyncENOTSUP = true
      er = new Error('ENOTSUP: Operation not supported')
      er.path = path
      er.code = 'ENOTSUP'
      return cb(er)
    })
  } else {
    readdir.call(fs, p, cb)
  }
}

var glob = require('../')
var test = require('tap').test
var common = require('../common.js')
process.chdir(__dirname + '/fixtures')

var pattern = 'a/**/h'
var expect = [ 'a/abcdef/g/h', 'a/abcfed/g/h' ]

var options = { strict: true, silent: false }

test(pattern + ' ' + JSON.stringify(options), function (t) {
  var res = glob.sync(pattern, options).sort()
  t.same(res, expect, 'sync results')
  t.ok(sawSyncENOTSUP, 'saw sync ENOTSUP')

  var g = glob(pattern, options, function (er, res) {
    if (er)
      throw er
    t.ok(sawAsyncENOTSUP, 'saw async ENOTSUP')
    res = res.sort()
    t.same(res, expect, 'async results')
    t.end()
  })
})