test-find-accessible-sync.js 2.5 KB

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