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