Skip to content

Commit

Permalink
Handle case sensitive file systems with nocase option.
Browse files Browse the repository at this point in the history
Uses detect-file
Returns actual filepath on case sensitive system when nocase is true
  • Loading branch information
doowb committed Jul 7, 2016
1 parent a569c1d commit 3bec80d
Show file tree
Hide file tree
Showing 15 changed files with 95 additions and 10 deletions.
5 changes: 4 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
sudo: false
language: node_js
os:
- linux
- osx

This comment has been minimized.

Copy link
@phated

phated Jul 11, 2016

Member

wow, didn't realize osx was available for everyone now. Happy days

This comment has been minimized.

Copy link
@jonschlinkert

jonschlinkert Jul 11, 2016

Contributor

I didn't notice that before this either. very nice

node_js:
- "stable"
- "6"
- "5"
- "4"
- "0.12"
Expand Down
24 changes: 16 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ var fs = require('fs');
var path = require('path');
var isGlob = require('is-glob');
var resolveDir = require('resolve-dir');
var exists = require('fs-exists-sync');
var detect = require('detect-file');
var mm = require('micromatch');

/**
Expand Down Expand Up @@ -46,13 +46,13 @@ function lookup(pattern, options) {
if (isGlob(pattern)) {
return matchFile(cwd, pattern, options);
} else {
return findFile(cwd, pattern);
return findFile(cwd, pattern, options);
}
}

function matchFile(cwd, pattern, opts) {
var isMatch = mm.matcher(pattern, opts);
var files = fs.readdirSync(cwd);
var files = tryReaddirSync(cwd);
var len = files.length;
var idx = -1;

Expand All @@ -71,10 +71,11 @@ function matchFile(cwd, pattern, opts) {
return matchFile(dir, pattern, opts);
}

function findFile(cwd, filename) {
function findFile(cwd, filename, options) {
var res;
var fp = cwd ? path.resolve(cwd, filename) : filename;
if (exists(fp)) {
return fp;
if (res = detect(fp, options)) {
return res;
}

var segs = cwd.split(path.sep);
Expand All @@ -83,9 +84,16 @@ function findFile(cwd, filename) {
while (len--) {
cwd = segs.slice(0, len).join(path.sep);
fp = path.resolve(cwd, filename);
if (exists(fp)) {
return fp;
if (res = detect(fp, options)) {
return res;
}
}
return null;
}

function tryReaddirSync(fp) {
try {
return fs.readdirSync(fp);
} catch(err) {}
return [];
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@
"test": "grunt && mocha"
},
"dependencies": {
"fs-exists-sync": "^0.1.0",
"detect-file": "^0.1.0",
"is-glob": "^2.0.1",
"micromatch": "^2.3.7",
"resolve-dir": "^0.1.0"
},
"devDependencies": {
"fs-exists-sync": "^0.1.0",
"grunt": "^1.0.1",
"grunt-contrib-jshint": "^0.12.0",
"is-absolute": "^0.2.3",
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/Mochafile.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
root
1 change: 1 addition & 0 deletions test/fixtures/a/Mochafile.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a
1 change: 1 addition & 0 deletions test/fixtures/a/b/Mochafile.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
b
1 change: 1 addition & 0 deletions test/fixtures/a/b/c/Mochafile.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
c
1 change: 1 addition & 0 deletions test/fixtures/a/b/c/d/Mochafile.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
d
1 change: 1 addition & 0 deletions test/fixtures/a/b/c/d/e/Mochafile.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
e
1 change: 1 addition & 0 deletions test/fixtures/a/b/c/d/e/f/Mochafile.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
f
1 change: 1 addition & 0 deletions test/fixtures/a/b/c/d/e/f/g/Mochafile.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
g
1 change: 1 addition & 0 deletions test/fixtures/a/b/c/d/e/f/g/h/Mochafile.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
h
1 change: 1 addition & 0 deletions test/fixtures/a/b/c/d/e/f/g/h/i/Mochafile.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
i
1 change: 1 addition & 0 deletions test/fixtures/a/b/c/d/e/f/g/h/i/j/Mochafile.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
j
62 changes: 62 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ var chdir = support.chdir;
var npm = support.npm;
var cwd;
var actual;
var isLinux = process.platform === 'linux';


describe('findup-sync', function () {
Expand Down Expand Up @@ -56,6 +57,17 @@ describe('findup-sync', function () {
restore();
});

it('should find case sensitive files in a child directory', function () {
var expected = path.resolve(__dirname, 'fixtures/a/b/', (isLinux ? 'Mochafile.txt' : 'mochafile.txt'));
var restore = chdir(path.resolve(__dirname, 'fixtures/a/b/c/d/e/f/g/h'));

var actual = findup('a/b/mochafile.txt', {nocase: true});
assert(actual);
assert(exists(actual));
assert.equal(actual, expected);
restore();
});

it('should find files in a child directory relative to a cwd', function () {
var expectedFile = path.resolve(__dirname, 'fixtures/a/b/file.txt');
var expectedA = path.resolve(__dirname, 'fixtures/a/a.txt');
Expand All @@ -73,6 +85,23 @@ describe('findup-sync', function () {
tempDir();
});

it('should find case sensitive files in a child directory relative to a cwd', function () {
var expectedFile = path.resolve(__dirname, 'fixtures/a/b', (isLinux ? 'Mochafile.txt' : 'mochafile.txt'));
var expectedA = path.resolve(__dirname, 'fixtures/a/a.txt');
var tempDir = chdir(path.resolve(__dirname, 'fixtures'));

var actualFile = findup('a/b/mochafile.txt', {cwd: 'a/b/c/d', nocase: true});
assert(actualFile);
assert(exists(actualFile));
assert.equal(actualFile, expectedFile);

var actualA = findup('a.txt', {cwd: 'a/b/c/d/e/f'});
assert(actualA);
assert(exists(actualA));
assert.equal(actualA, expectedA);
tempDir();
});

it('should support normal (non-glob) file paths:', function () {
var normPath = normalize(findup('package.json', {cwd: path.dirname(resolve.sync('normalize-path'))}));
assert.equal(normPath, 'node_modules/normalize-path/package.json');
Expand All @@ -95,6 +124,12 @@ describe('findup-sync', function () {
assert.basename(actual, 'package.json');
});

it('should support normal (non-glob) case sensitive file paths:', function () {
actual = findup('c/mochafile.txt', {cwd: 'test/fixtures/a/b/c/d/e/f/g', nocase: true});
assert.basename(actual, (isLinux ? 'Mochafile.txt' : 'mochafile.txt'));
assert.dirname(actual, 'test/fixtures/a/b/c');
});

it('should support glob patterns', function() {
assert.equal(normalize(findup('**/c/package.json', {cwd: 'test/fixtures/a/b/c/d/e/f/g'})), 'test/fixtures/a/b/c/package.json');
assert.equal(normalize(findup('**/one.txt', {cwd: 'test/fixtures/a/b/c/d/e/f/g'})), 'test/fixtures/a/b/c/d/one.txt');
Expand Down Expand Up @@ -127,6 +162,33 @@ describe('findup-sync', function () {
assert.basename(actual, 'package.json');
});

it('should support case sensitive glob patterns', function() {
assert.equal(normalize(findup('**/c/mochafile.txt', {cwd: 'test/fixtures/a/b/c/d/e/f/g', nocase: true})), 'test/fixtures/a/b/c/Mochafile.txt');
assert.equal(normalize(findup('**/one.txt', {cwd: 'test/fixtures/a/b/c/d/e/f/g', nocase: true})), 'test/fixtures/a/b/c/d/one.txt');
assert.equal(normalize(findup('**/two.txt', {cwd: 'test/fixtures/a/b/c/d/e/f/g', nocase: true})), 'test/fixtures/a/b/c/two.txt');

assert.equal(normalize(findup('mocha*', {cwd: 'test/fixtures/a/b/c', nocase: true})), 'test/fixtures/a/b/c/Mochafile.txt');

var opts = {cwd: 'test/fixtures/a/b/c/d/e/f/g', nocase: true};

actual = findup('**/c/mochafile.txt', opts);
assert.dirname(actual, 'test/fixtures/a/b/c');
assert.basename(actual, 'Mochafile.txt');

actual = findup('c/mochafile.txt', opts);
assert.dirname(actual, 'test/fixtures/a/b/c');
assert.basename(actual, (isLinux ? 'Mochafile.txt' : 'mochafile.txt'));

opts.nocase = false;
actual = findup('**/ONE.txt', opts);
assert.dirname(actual, 'test/fixtures/a/b/c');
assert.basename(actual, 'ONE.txt');

actual = findup('**/two.txt', opts);
assert.dirname(actual, 'test/fixtures/a/b/c');
assert.basename(actual, 'two.txt');
});

it('should support arrays of glob patterns', function() {
assert.equal(normalize(findup(['**/c/package.json'], {cwd: 'test/fixtures/a/b/c/d/e/f/g'})), 'test/fixtures/a/b/c/package.json');
assert.equal(normalize(findup(['**/one.txt'], {cwd: 'test/fixtures/a/b/c/d/e/f/g'})), 'test/fixtures/a/b/c/d/one.txt');
Expand Down

0 comments on commit 3bec80d

Please sign in to comment.