findup-test.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. var assert = require('chai').assert,
  2. Path = require('path'),
  3. fs = require('fs'),
  4. findup = require('..');
  5. describe('find-up', function(){
  6. var fixtureDir = Path.join(__dirname, 'fixture', 'f', 'e', 'd', 'c', 'b', 'a'),
  7. fsExists = fs.exists ? fs.exists : Path.exists;
  8. it('accept a function', function(done){
  9. findup(fixtureDir, function(dir, cb){
  10. return fsExists(Path.join(dir, 'config.json'), cb);
  11. }, function(err, file){
  12. assert.ifError(err);
  13. assert.equal(file, Path.join(__dirname, 'fixture', 'f', 'e', 'd', 'c'));
  14. done();
  15. });
  16. });
  17. it('accept a string', function(done){
  18. findup(fixtureDir, 'config.json', function(err, file){
  19. assert.ifError(err);
  20. assert.equal(file, Path.join(__dirname, 'fixture', 'f', 'e', 'd', 'c'));
  21. done();
  22. });
  23. });
  24. it('is usable with the Object syntax', function(done) {
  25. new findup.FindUp(fixtureDir, 'config.json', function(err, file){
  26. assert.ifError(err);
  27. assert.equal(file, Path.join(__dirname, 'fixture', 'f', 'e', 'd', 'c'));
  28. done();
  29. });
  30. });
  31. it('find several files when using with the EventEmitter syntax', function(done){
  32. var ee = new findup.FindUp(fixtureDir, 'config.json');
  33. ee.once('found', function(file){
  34. assert.equal(file, Path.join(__dirname, 'fixture', 'f', 'e', 'd', 'c'));
  35. ee.once('found', function(file){
  36. assert.equal(file, Path.join(__dirname, 'fixture'));
  37. ee.once('end', function(){
  38. done();
  39. });
  40. });
  41. });
  42. });
  43. it('return files in top dir', function(done){
  44. findup(fixtureDir, 'top.json', function(err, file){
  45. assert.ifError(err);
  46. assert.equal(file, Path.join(__dirname, 'fixture', 'f', 'e', 'd', 'c', 'b', 'a'));
  47. done();
  48. });
  49. });
  50. it('return files in root dir', function(done){
  51. findup(fixtureDir, 'dev', function(err, file){
  52. assert.ifError(err);
  53. assert.equal(file, '/');
  54. done();
  55. });
  56. });
  57. it('return an error when looking for non existiong files', function(done){
  58. findup(fixtureDir, 'toto.json', function(err, file){
  59. assert.isNotNull(err);
  60. done();
  61. });
  62. });
  63. it('return an error when looking in a non existing directory', function(done){
  64. findup('dsqkjfnqsdkjghq', 'toto.json', function(err, file){
  65. assert.isNotNull(err);
  66. done();
  67. });
  68. });
  69. it('trigger an error event when looking in a non existing directory', function(done){
  70. findup('dsqkjfnqsdkjghq', 'toto.json').on('error', function(err, files){
  71. assert.isNotNull(err);
  72. done();
  73. });
  74. });
  75. describe('Sync API', function(){
  76. it('accept a string', function(){
  77. var file = findup.sync(fixtureDir, 'config.json');
  78. assert.equal(file, Path.join(__dirname, 'fixture', 'f', 'e', 'd', 'c'));
  79. });
  80. it('return a file in top dir', function(){
  81. var file = findup.sync(fixtureDir, 'top.json');
  82. assert.equal(file, Path.join(__dirname, 'fixture', 'f', 'e', 'd', 'c', 'b', 'a'));
  83. });
  84. it('throw error when looking for a non existing file', function(){
  85. assert.throw(function(){
  86. findup.sync(fixtureDir, 'toto.json');
  87. });
  88. });
  89. it('throw error when looking for in a non existing directory', function(){
  90. assert.throw(function(){
  91. findup.sync('uhjhbjkg,nfg', 'toto.json');
  92. });
  93. });
  94. });
  95. });