UNPKG

2.58 kBJavaScriptView Raw
1'use strict'
2
3const test = require('tap').test
4const path = require('path')
5const requireInject = require('require-inject')
6const configure = requireInject('../lib/configure', {
7 'graceful-fs': {
8 closeSync: function () { return undefined },
9 openSync: function (path) {
10 if (readableFiles.some(function (f) { return f === path })) {
11 return 0
12 } else {
13 var error = new Error('ENOENT - not found')
14 throw error
15 }
16 }
17 }
18})
19
20const dir = path.sep + 'testdir'
21const readableFile = 'readable_file'
22const anotherReadableFile = 'another_readable_file'
23const readableFileInDir = 'somedir' + path.sep + readableFile
24const readableFiles = [
25 path.resolve(dir, readableFile),
26 path.resolve(dir, anotherReadableFile),
27 path.resolve(dir, readableFileInDir)
28]
29
30test('find accessible - empty array', function (t) {
31 t.plan(1)
32
33 var candidates = []
34 var found = configure.test.findAccessibleSync('test', dir, candidates)
35 t.strictEqual(found, undefined)
36})
37
38test('find accessible - single item array, readable', function (t) {
39 t.plan(1)
40
41 var candidates = [readableFile]
42 var found = configure.test.findAccessibleSync('test', dir, candidates)
43 t.strictEqual(found, path.resolve(dir, readableFile))
44})
45
46test('find accessible - single item array, readable in subdir', function (t) {
47 t.plan(1)
48
49 var candidates = [readableFileInDir]
50 var found = configure.test.findAccessibleSync('test', dir, candidates)
51 t.strictEqual(found, path.resolve(dir, readableFileInDir))
52})
53
54test('find accessible - single item array, unreadable', function (t) {
55 t.plan(1)
56
57 var candidates = ['unreadable_file']
58 var found = configure.test.findAccessibleSync('test', dir, candidates)
59 t.strictEqual(found, undefined)
60})
61
62test('find accessible - multi item array, no matches', function (t) {
63 t.plan(1)
64
65 var candidates = ['non_existent_file', 'unreadable_file']
66 var found = configure.test.findAccessibleSync('test', dir, candidates)
67 t.strictEqual(found, undefined)
68})
69
70test('find accessible - multi item array, single match', function (t) {
71 t.plan(1)
72
73 var candidates = ['non_existent_file', readableFile]
74 var found = configure.test.findAccessibleSync('test', dir, candidates)
75 t.strictEqual(found, path.resolve(dir, readableFile))
76})
77
78test('find accessible - multi item array, return first match', function (t) {
79 t.plan(1)
80
81 var candidates = ['non_existent_file', anotherReadableFile, readableFile]
82 var found = configure.test.findAccessibleSync('test', dir, candidates)
83 t.strictEqual(found, path.resolve(dir, anotherReadableFile))
84})