Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(InputfileResolver): exclude online files from globbing #194

Merged
merged 6 commits into from
Dec 30, 2016
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 34 additions & 10 deletions src/InputFileResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,18 @@ const log = log4js.getLogger('InputFileResolver');

const DEFAULT_INPUT_FILE_PROPERTIES = { mutated: false, included: true };

function isWebUrl(pattern: string): boolean {
return pattern.indexOf('http://') === 0 || pattern.indexOf('https://') === 0;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could also use pattern.startsWith('http://') 👍

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Turns out this is a part of ES6, thanks for the heads-up @Kattoor

}

export default class InputFileResolver {

private inputFileResolver: PatternResolver;
private mutateResolver: PatternResolver;

constructor(mutate: string[], allFileExpressions: Array<InputFileDescriptor | string>) {
this.validateFileDescriptor(allFileExpressions);
this.validateMutationArray(mutate);
this.mutateResolver = PatternResolver.parse(mutate || []);
this.inputFileResolver = PatternResolver.parse(allFileExpressions);
}
Expand All @@ -33,11 +38,26 @@ export default class InputFileResolver {
if (_.isObject(maybeInputFileDescriptor)) {
if (Object.keys(maybeInputFileDescriptor).indexOf('pattern') === -1) {
throw Error(`File descriptor ${JSON.stringify(maybeInputFileDescriptor)} is missing mandatory property 'pattern'.`);
} else {
maybeInputFileDescriptor = maybeInputFileDescriptor as InputFileDescriptor;
if (isWebUrl(maybeInputFileDescriptor.pattern) && maybeInputFileDescriptor.mutated) {
throw new Error(`Cannot mutate web url "${maybeInputFileDescriptor.pattern}".`);
}
}
}
});
}

private validateMutationArray(mutationArray: Array<string>) {
if (mutationArray) {
mutationArray.forEach(mutation => {
if (isWebUrl(mutation)) {
throw new Error(`Cannot mutate web url "${mutation}".`)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}
});
}
}

private markAdditionalFilesToMutate(allInputFiles: InputFile[], additionalMutateFiles: string[]) {
let errors: string[] = [];
additionalMutateFiles.forEach(mutateFile => {
Expand Down Expand Up @@ -70,7 +90,7 @@ class PatternResolver {
private descriptor: InputFileDescriptor;

constructor(descriptor: InputFileDescriptor | string, private previous?: PatternResolver) {
if (typeof descriptor === 'string') {
if (typeof descriptor === 'string') { // mutator array is a string array
this.descriptor = <InputFileDescriptor>_.assign({ pattern: descriptor }, DEFAULT_INPUT_FILE_PROPERTIES);
this.ignore = descriptor.indexOf('!') === 0;
if (this.ignore) {
Expand All @@ -87,7 +107,7 @@ class PatternResolver {
return Promise.resolve([]);
} else {
// Start the globbing task for the current descriptor
const globbingTask = this.resolveFileGlob(this.descriptor.pattern)
const globbingTask = this.resolveGlobbingExpression(this.descriptor.pattern)
.then(filePaths => filePaths.map(filePath => this.createInputFile(filePath)));
if (this.previous) {
// If there is a previous globbing expression, resolve that one as well
Expand Down Expand Up @@ -123,14 +143,18 @@ class PatternResolver {
return current;
}

private resolveFileGlob(pattern: string): Promise<string[]> {
return glob(pattern).then(files => {
if (files.length === 0) {
this.reportEmptyGlobbingExpression(pattern);
}
normalize(files);
return files;
});
private resolveGlobbingExpression(pattern: string): Promise<string[]> {
if (isWebUrl(pattern)) {
return Promise.resolve([pattern]);
} else {
return glob(pattern).then(files => {
if (files.length === 0) {
this.reportEmptyGlobbingExpression(pattern);
}
normalize(files);
return files;
});
}
}

private reportEmptyGlobbingExpression(expression: string) {
Expand Down
16 changes: 16 additions & 0 deletions test/unit/InputFileResolverSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,22 @@ describe('InputFileResolver', () => {

});

describe('with url as file pattern', () => {
it('should pass through the web urls without globbing', () => {
return new InputFileResolver([], ['http://www', {pattern: 'https://ok'}])
.resolve()
.then(() => expect(fileUtils.glob).to.not.have.been.called);
});

it('should fail when web url is in the mutated array', () => {
expect(() => new InputFileResolver(['http://www'], ['http://www'])).throws('Cannot mutate web url "http://www".');
});

it('should fail when web url is to be mutated', () => {
expect(() => new InputFileResolver([], [ { pattern: 'http://www', mutated: true } ])).throws('Cannot mutate web url "http://www".');
});
});

afterEach(() => {
sandbox.restore();
});
Expand Down