Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #742 from Tokimon/fix/paths
General correction of glob patterns and config file locating
- Loading branch information
Showing
8 changed files
with
163 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import * as Path from 'path'; | ||
import { Minimatch, IMinimatch } from 'minimatch'; | ||
|
||
const unix = Path.sep === '/'; | ||
|
||
export function createMinimatch(patterns: string[]): IMinimatch[] { | ||
return patterns.map((pattern: string): IMinimatch => { | ||
// Ensure correct pathing on unix, by transforming `\` to `/` and remvoing any `X:/` fromt he path | ||
if (unix) { pattern = pattern.replace(/[\\]/g, '/').replace(/^\w:/, ''); } | ||
|
||
// pattern paths not starting with '**' are resolved even if it is an | ||
// absolute path, to ensure correct format for the current OS | ||
if (pattern.substr(0, 2) !== '**') { | ||
pattern = Path.resolve(pattern); | ||
} | ||
|
||
// On Windows we transform `\` to `/` to unify the way paths are intepreted | ||
if (!unix) { pattern = pattern.replace(/[\\]/g, '/'); } | ||
|
||
// Unify the path slashes before creating the minimatch, for more relyable matching | ||
return new Minimatch(pattern, { dot: true }); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import * as Path from 'path'; | ||
import { Minimatch } from 'minimatch'; | ||
|
||
import isEqual = require('lodash/isEqual'); | ||
import Assert = require('assert'); | ||
|
||
import { createMinimatch } from '..'; | ||
|
||
// Used to ensure uniform path cross OS | ||
const absolutePath = (path: string) => Path.resolve(path.replace(/^\w:/, '')).replace(/[\\]/g, '/'); | ||
|
||
describe('Paths', () => { | ||
describe('createMinimatch', () => { | ||
it('Converts an array of paths to an array of Minimatch expressions', () => { | ||
const mms = createMinimatch(['/some/path/**', '**/another/path/**', './relative/**/path']); | ||
Assert(Array.isArray(mms), 'Didn\'t return an array'); | ||
|
||
const allAreMm = mms.every((mm) => mm instanceof Minimatch); | ||
Assert(allAreMm, 'Not all paths are coverted to Minimatch'); | ||
}); | ||
|
||
it('Minimatch can match absolute paths expressions', () => { | ||
const paths = ['/unix/absolute/**/path', '\\windows\\alternative\\absolute\\path', 'C:\\Windows\\absolute\\*\\path', '**/arbitrary/path/**']; | ||
const mms = createMinimatch(paths); | ||
const patterns = mms.map(({ pattern }) => pattern); | ||
const comparePaths = [ | ||
absolutePath('/unix/absolute/**/path'), | ||
absolutePath('/windows/alternative/absolute/path'), | ||
absolutePath('/Windows/absolute/*/path'), | ||
'**/arbitrary/path/**' | ||
]; | ||
|
||
Assert(isEqual(patterns, comparePaths), `Patterns have been altered:\nMMS: ${patterns}\nPaths: ${comparePaths}`); | ||
|
||
Assert(mms[0].match(absolutePath('/unix/absolute/some/sub/dir/path')), 'Din\'t match unix path'); | ||
Assert(mms[1].match(absolutePath('/windows/alternative/absolute/path')), 'Din\'t match windows alternative path'); | ||
Assert(mms[2].match(absolutePath('/Windows/absolute/test/path')), 'Din\'t match windows path'); | ||
Assert(mms[3].match(absolutePath('/some/deep/arbitrary/path/leading/nowhere')), 'Din\'t match arbitrary path'); | ||
}); | ||
|
||
it('Minimatch can match relative to the project root', () => { | ||
const paths = ['./relative/**/path', '../parent/*/path', 'no/dot/relative/**/path/*', '*/subdir/**/path/*', '.dot/relative/**/path/*']; | ||
const absPaths = paths.map((path) => absolutePath(path)); | ||
const mms = createMinimatch(paths); | ||
const patterns = mms.map(({ pattern }) => pattern); | ||
|
||
Assert(isEqual(patterns, absPaths), `Project root have not been added to paths:\nMMS: ${patterns}\nPaths: ${absPaths}`); | ||
|
||
Assert(mms[0].match(Path.resolve('relative/some/sub/dir/path')), 'Din\'t match relative path'); | ||
Assert(mms[1].match(Path.resolve('../parent/dir/path')), 'Din\'t match parent path'); | ||
Assert(mms[2].match(Path.resolve('no/dot/relative/some/sub/dir/path/test')), 'Din\'t match no dot path'); | ||
Assert(mms[3].match(Path.resolve('some/subdir/path/here')), 'Din\'t match single star path'); | ||
Assert(mms[4].match(Path.resolve('.dot/relative/some/sub/dir/path/test')), 'Din\'t match dot path'); | ||
}); | ||
|
||
it('Minimatch matches dot files', () => { | ||
const mm = createMinimatch(['/some/path/**'])[0]; | ||
Assert(mm.match(absolutePath('/some/path/.dot/dir')), 'Didn\'t match .dot path'); | ||
Assert(mm.match(absolutePath('/some/path/normal/dir')), 'Didn\'t match normal path'); | ||
}); | ||
}); | ||
}); |