test-find-accessible-sync.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. 'use strict'
  2. var test = require('tape')
  3. var path = require('path')
  4. var requireInject = require('require-inject')
  5. var configure = requireInject('../lib/configure', {
  6. 'graceful-fs': {
  7. 'closeSync': function (fd) { 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. var dir = path.sep + 'testdir'
  19. var readableFile = 'readable_file'
  20. var anotherReadableFile = 'another_readable_file'
  21. var readableFileInDir = 'somedir' + path.sep + readableFile
  22. var 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. })