Skip to content

Commit

Permalink
fix(InputFileResolver) Fix for wrong order when globbing promisses re…
Browse files Browse the repository at this point in the history
…solve in different order (#81)
  • Loading branch information
nicojs authored and simondel committed Apr 11, 2016
1 parent 9387cbe commit f351ad4
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 10 deletions.
21 changes: 11 additions & 10 deletions src/InputFileResolver.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {InputFile} from './api/core';
import {glob, normalize} from './utils/fileUtils';
import * as _ from 'lodash';

export default class InputFileResolver {

Expand All @@ -8,12 +9,12 @@ export default class InputFileResolver {

public resolve(): Promise<InputFile[]> {
return new Promise<InputFile[]>((resolve, reject) => {
let mutateFiles: string[] = [];
let allFiles: string[] = [];
let errors: string[] = [];

Promise.all([InputFileResolver.resolveFileGlobs(this.mutateFileExpressions, mutateFiles), InputFileResolver.resolveFileGlobs(this.allFileExpressions, allFiles)])
.then(() => {
Promise.all([InputFileResolver.resolveFileGlobs(this.mutateFileExpressions), InputFileResolver.resolveFileGlobs(this.allFileExpressions)])
.then((files) => {
let mutateFiles = files[0];
let allFiles = files[1];
normalize(allFiles);
normalize(mutateFiles);

Expand All @@ -25,7 +26,7 @@ export default class InputFileResolver {
if (errors.length > 0) {
reject(errors);
} else {
resolve(allFiles.map(file => { return { path: file, shouldMutate: mutateFiles.some(mutateFile => mutateFile === file) };}))
resolve(allFiles.map(file => { return { path: file, shouldMutate: mutateFiles.some(mutateFile => mutateFile === file) }; }))
}
}, error => reject(error));
});
Expand All @@ -36,12 +37,12 @@ export default class InputFileResolver {
console.log(`WARNING: Globbing expression "${expression}" did not result in any files.`)
}

private static resolveFileGlobs(sourceExpressions: string[], resultFiles: string[]): Promise<void[]> {
return Promise.all(sourceExpressions.map((mutateFileExpression: string) => glob(mutateFileExpression).then(files => {
private static resolveFileGlobs(sourceExpressions: string[]): Promise<string[]> {
return Promise.all(sourceExpressions.map(expression => glob(expression).then(files => {
if (files.length === 0) {
this.reportEmptyGlobbingExpression(mutateFileExpression);
this.reportEmptyGlobbingExpression(expression);
}
files.forEach(f => resultFiles.push(f));
})));
return files;
}))).then(files => _.flatten(files));
}
}
19 changes: 19 additions & 0 deletions test/unit/InputFileResolverSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,25 @@ describe('InputFileResolver', () => {
});
});

describe('with file expressions that resolve in different order', () => {
let results: InputFile[];
beforeEach(() => {
let resolveFile1: (result: string[]) => void;
let resolveFile2: (result: string[]) => void;
sut = new InputFileResolver([], ['file1', 'file2']);
globStub.withArgs('file1').returns(new Promise(resolve => resolveFile1 = resolve));
globStub.withArgs('file2').returns(new Promise(resolve => resolveFile2 = resolve));
let p = sut.resolve().then(r => results = r);
resolveFile2(['file2']);
resolveFile1(['file1']);
return p;
});

it('should retain original glob order', () => {
expect(results.map(m => m.path.substr(m.path.length - 5))).to.deep.equal(['file1', 'file2'])
});
});

describe('with mutant file expressions which result in files which are not included in result of all globbing files and resolve is called', () => {
let results: InputFile[];
let error: any;
Expand Down

0 comments on commit f351ad4

Please sign in to comment.