You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

test-find-accessible-sync.js 2.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. 'use strict'
  2. const { describe, it } = require('mocha')
  3. const assert = require('assert')
  4. const path = require('path')
  5. const requireInject = require('require-inject')
  6. const 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. const dir = path.sep + 'testdir'
  20. const readableFile = 'readable_file'
  21. const anotherReadableFile = 'another_readable_file'
  22. const readableFileInDir = 'somedir' + path.sep + readableFile
  23. const readableFiles = [
  24. path.resolve(dir, readableFile),
  25. path.resolve(dir, anotherReadableFile),
  26. path.resolve(dir, readableFileInDir)
  27. ]
  28. describe('find-accessible-sync', function () {
  29. it('find accessible - empty array', function () {
  30. var candidates = []
  31. var found = configure.test.findAccessibleSync('test', dir, candidates)
  32. assert.strictEqual(found, undefined)
  33. })
  34. it('find accessible - single item array, readable', function () {
  35. var candidates = [readableFile]
  36. var found = configure.test.findAccessibleSync('test', dir, candidates)
  37. assert.strictEqual(found, path.resolve(dir, readableFile))
  38. })
  39. it('find accessible - single item array, readable in subdir', function () {
  40. var candidates = [readableFileInDir]
  41. var found = configure.test.findAccessibleSync('test', dir, candidates)
  42. assert.strictEqual(found, path.resolve(dir, readableFileInDir))
  43. })
  44. it('find accessible - single item array, unreadable', function () {
  45. var candidates = ['unreadable_file']
  46. var found = configure.test.findAccessibleSync('test', dir, candidates)
  47. assert.strictEqual(found, undefined)
  48. })
  49. it('find accessible - multi item array, no matches', function () {
  50. var candidates = ['non_existent_file', 'unreadable_file']
  51. var found = configure.test.findAccessibleSync('test', dir, candidates)
  52. assert.strictEqual(found, undefined)
  53. })
  54. it('find accessible - multi item array, single match', function () {
  55. var candidates = ['non_existent_file', readableFile]
  56. var found = configure.test.findAccessibleSync('test', dir, candidates)
  57. assert.strictEqual(found, path.resolve(dir, readableFile))
  58. })
  59. it('find accessible - multi item array, return first match', function () {
  60. var candidates = ['non_existent_file', anotherReadableFile, readableFile]
  61. var found = configure.test.findAccessibleSync('test', dir, candidates)
  62. assert.strictEqual(found, path.resolve(dir, anotherReadableFile))
  63. })
  64. })