|
| 1 | +'use strict' |
| 2 | + |
| 3 | +var test = require('tape') |
| 4 | +var path = require('path') |
| 5 | +var requireInject = require('require-inject') |
| 6 | +var configure = requireInject('../lib/configure', { |
| 7 | + 'graceful-fs': { |
| 8 | + 'closeSync': function (fd) { 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 | + |
| 20 | +var dir = path.sep + 'testdir' |
| 21 | +var readableFile = 'readable_file' |
| 22 | +var anotherReadableFile = 'another_readable_file' |
| 23 | +var readableFileInDir = 'somedir' + path.sep + readableFile |
| 24 | +var readableFiles = [ |
| 25 | + path.resolve(dir, readableFile), |
| 26 | + path.resolve(dir, anotherReadableFile), |
| 27 | + path.resolve(dir, readableFileInDir) |
| 28 | +] |
| 29 | + |
| 30 | +test('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 | + |
| 38 | +test('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 | + |
| 46 | +test('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 | + |
| 54 | +test('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 | + |
| 62 | + |
| 63 | +test('find accessible - multi item array, no matches', function (t) { |
| 64 | + t.plan(1) |
| 65 | + |
| 66 | + var candidates = [ 'non_existent_file', 'unreadable_file' ] |
| 67 | + var found = configure.test.findAccessibleSync('test', dir, candidates) |
| 68 | + t.strictEqual(found, undefined) |
| 69 | +}) |
| 70 | + |
| 71 | + |
| 72 | +test('find accessible - multi item array, single match', function (t) { |
| 73 | + t.plan(1) |
| 74 | + |
| 75 | + var candidates = [ 'non_existent_file', readableFile ] |
| 76 | + var found = configure.test.findAccessibleSync('test', dir, candidates) |
| 77 | + t.strictEqual(found, path.resolve(dir, readableFile)) |
| 78 | +}) |
| 79 | + |
| 80 | +test('find accessible - multi item array, return first match', function (t) { |
| 81 | + t.plan(1) |
| 82 | + |
| 83 | + var candidates = [ 'non_existent_file', anotherReadableFile, readableFile ] |
| 84 | + var found = configure.test.findAccessibleSync('test', dir, candidates) |
| 85 | + t.strictEqual(found, path.resolve(dir, anotherReadableFile)) |
| 86 | +}) |
0 commit comments