findup-sync_test.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. 'use strict';
  2. // Nodejs lib.
  3. var path = require('path');
  4. var findup = require('../lib/findup-sync.js');
  5. // Get a relative path.
  6. var rel = function(abspath) {
  7. return typeof abspath === 'string' ? path.relative('.', abspath) : abspath;
  8. };
  9. exports['findup'] = {
  10. setUp: function(done) {
  11. this.cwd = process.cwd();
  12. done();
  13. },
  14. tearDown: function(done) {
  15. process.chdir(this.cwd);
  16. done();
  17. },
  18. 'simple': function(test) {
  19. test.expect(8);
  20. var opts = {cwd: 'test/fixtures/a/b'};
  21. test.equal(rel(findup('foo.txt', opts)), path.normalize('test/fixtures/a/foo.txt'), 'should find files');
  22. test.equal(rel(findup('bar.txt', opts)), path.normalize('test/fixtures/a/b/bar.txt'), 'should find files');
  23. test.equal(rel(findup('a.txt', opts)), path.normalize('test/fixtures/a.txt'), 'should find files');
  24. test.equal(rel(findup('?.txt', opts)), path.normalize('test/fixtures/a.txt'), 'should support glob patterns');
  25. test.equal(rel(findup('*.txt', opts)), path.normalize('test/fixtures/a/b/bar.txt'), 'should find the first thing that matches the glob pattern');
  26. test.equal(rel(findup(['b*.txt', 'f*.txt'], opts)), path.normalize('test/fixtures/a/b/bar.txt'), 'should find the first thing that matches any of the glob patterns');
  27. test.equal(rel(findup(['f*.txt', 'b*.txt'], opts)), path.normalize('test/fixtures/a/b/bar.txt'), 'should find the first thing that matches any of the glob patterns');
  28. test.equal(findup('not-gonna-exist-i-hope.txt', opts), null, 'should returning null if no files found');
  29. test.done();
  30. },
  31. 'cwd': function(test) {
  32. test.expect(8);
  33. process.chdir('test/fixtures/a/b');
  34. test.equal(rel(findup('foo.txt')), path.normalize('../foo.txt'), 'should find files');
  35. test.equal(rel(findup('bar.txt')), 'bar.txt', 'should find files');
  36. test.equal(rel(findup('a.txt')), path.normalize('../../a.txt'), 'should find files');
  37. test.equal(rel(findup('?.txt')), path.normalize('../../a.txt'), 'should support glob patterns');
  38. test.equal(rel(findup('*.txt')), 'bar.txt', 'should find the first thing that matches the glob pattern');
  39. test.equal(rel(findup(['b*.txt', 'f*.txt'])), 'bar.txt', 'should find the first thing that matches any of the glob patterns');
  40. test.equal(rel(findup(['f*.txt', 'b*.txt'])), 'bar.txt', 'should find the first thing that matches any of the glob patterns');
  41. test.equal(findup('not-gonna-exist-i-hope.txt'), null, 'should returning null if no files found');
  42. test.done();
  43. },
  44. };