test(@angular-devkit/build-angular): add harness file jasmine helpers

This change adds an `expectFile` jasmine helper function that reduces the amount of code necessary to check a file's existence, size, or content.  Additional contextual information is also displayed when an expectation fails.
This commit is contained in:
Charles Lyding 2020-12-11 16:16:45 -05:00 committed by Alan Agius
parent 97bfd46b67
commit bfd91096be
2 changed files with 32 additions and 3 deletions

View File

@ -6,4 +6,4 @@
* found in the LICENSE file at https://angular.io/license
*/
export { BuilderHarnessExecutionOptions, BuilderHarnessExecutionResult } from './builder-harness';
export { describeBuilder } from './jasmine-helpers';
export { HarnessFileMatchers, describeBuilder } from './jasmine-helpers';

View File

@ -16,14 +16,14 @@ const optionSchemaCache = new Map<string, json.schema.JsonSchema>();
export function describeBuilder<T>(
builderHandler: BuilderHandlerFn<T & json.JsonObject>,
options: { name?: string; schemaPath: string },
specDefinitions: (harness: BuilderHarness<T>) => void,
specDefinitions: (harness: JasmineBuilderHarness<T>) => void,
): void {
let optionSchema = optionSchemaCache.get(options.schemaPath);
if (optionSchema === undefined) {
optionSchema = JSON.parse(readFileSync(options.schemaPath, 'utf8')) as json.schema.JsonSchema;
optionSchemaCache.set(options.schemaPath, optionSchema);
}
const harness = new BuilderHarness<T>(builderHandler, host, {
const harness = new JasmineBuilderHarness<T>(builderHandler, host, {
builderName: options.name,
optionSchema,
});
@ -36,3 +36,32 @@ export function describeBuilder<T>(
specDefinitions(harness);
});
}
class JasmineBuilderHarness<T> extends BuilderHarness<T> {
expectFile(path: string): HarnessFileMatchers {
return expectFile(path, this);
}
}
export interface HarnessFileMatchers {
toExist(): boolean;
toNotExist(): boolean;
readonly content: jasmine.ArrayLikeMatchers<string>;
readonly size: jasmine.Matchers<number>;
}
export function expectFile<T>(path: string, harness: BuilderHarness<T>): HarnessFileMatchers {
return {
toExist: () => expect(harness.hasFile(path)).toBe(true, 'Expected file to exist: ' + path),
toNotExist: () =>
expect(harness.hasFile(path)).toBe(false, 'Expected file to not exist: ' + path),
get content() {
return expect(harness.readFile(path)).withContext(`With file content for '${path}'`);
},
get size() {
return expect(Buffer.byteLength(harness.readFile(path))).withContext(
`With file size for '${path}'`,
);
},
};
}