[6a3a178] | 1 | "use strict";
|
---|
| 2 | /**
|
---|
| 3 | * @license
|
---|
| 4 | * Copyright Google LLC All Rights Reserved.
|
---|
| 5 | *
|
---|
| 6 | * Use of this source code is governed by an MIT-style license that can be
|
---|
| 7 | * found in the LICENSE file at https://angular.io/license
|
---|
| 8 | */
|
---|
| 9 | Object.defineProperty(exports, "__esModule", { value: true });
|
---|
| 10 | exports.expectFile = exports.describeBuilder = void 0;
|
---|
| 11 | const fs_1 = require("fs");
|
---|
| 12 | const test_utils_1 = require("../test-utils");
|
---|
| 13 | const builder_harness_1 = require("./builder-harness");
|
---|
| 14 | const optionSchemaCache = new Map();
|
---|
| 15 | function describeBuilder(builderHandler, options, specDefinitions) {
|
---|
| 16 | let optionSchema = optionSchemaCache.get(options.schemaPath);
|
---|
| 17 | if (optionSchema === undefined) {
|
---|
| 18 | optionSchema = JSON.parse(fs_1.readFileSync(options.schemaPath, 'utf8'));
|
---|
| 19 | optionSchemaCache.set(options.schemaPath, optionSchema);
|
---|
| 20 | }
|
---|
| 21 | const harness = new JasmineBuilderHarness(builderHandler, test_utils_1.host, {
|
---|
| 22 | builderName: options.name,
|
---|
| 23 | optionSchema,
|
---|
| 24 | });
|
---|
| 25 | describe(options.name || builderHandler.name, () => {
|
---|
| 26 | beforeEach(() => test_utils_1.host.initialize().toPromise());
|
---|
| 27 | afterEach(() => test_utils_1.host.restore().toPromise());
|
---|
| 28 | specDefinitions(harness);
|
---|
| 29 | });
|
---|
| 30 | }
|
---|
| 31 | exports.describeBuilder = describeBuilder;
|
---|
| 32 | class JasmineBuilderHarness extends builder_harness_1.BuilderHarness {
|
---|
| 33 | expectFile(path) {
|
---|
| 34 | return expectFile(path, this);
|
---|
| 35 | }
|
---|
| 36 | }
|
---|
| 37 | /**
|
---|
| 38 | * Add a Jasmine expectation filter to an expectation that always fails with a message.
|
---|
| 39 | * @param base The base expectation (`expect(...)`) to use.
|
---|
| 40 | * @param message The message to provide in the expectation failure.
|
---|
| 41 | */
|
---|
| 42 | function createFailureExpectation(base, message) {
|
---|
| 43 | // Needed typings are not included in the Jasmine types
|
---|
| 44 | const expectation = base;
|
---|
| 45 | expectation.expector = expectation.expector.addFilter({
|
---|
| 46 | selectComparisonFunc() {
|
---|
| 47 | return () => ({
|
---|
| 48 | pass: false,
|
---|
| 49 | message,
|
---|
| 50 | });
|
---|
| 51 | },
|
---|
| 52 | });
|
---|
| 53 | return expectation;
|
---|
| 54 | }
|
---|
| 55 | function expectFile(path, harness) {
|
---|
| 56 | return {
|
---|
| 57 | toExist() {
|
---|
| 58 | const exists = harness.hasFile(path);
|
---|
| 59 | expect(exists).toBe(true, 'Expected file to exist: ' + path);
|
---|
| 60 | return exists;
|
---|
| 61 | },
|
---|
| 62 | toNotExist() {
|
---|
| 63 | const exists = harness.hasFile(path);
|
---|
| 64 | expect(exists).toBe(false, 'Expected file to not exist: ' + path);
|
---|
| 65 | return !exists;
|
---|
| 66 | },
|
---|
| 67 | get content() {
|
---|
| 68 | try {
|
---|
| 69 | return expect(harness.readFile(path)).withContext(`With file content for '${path}'`);
|
---|
| 70 | }
|
---|
| 71 | catch (e) {
|
---|
| 72 | if (e.code !== 'ENOENT') {
|
---|
| 73 | throw e;
|
---|
| 74 | }
|
---|
| 75 | // File does not exist so always fail the expectation
|
---|
| 76 | return createFailureExpectation(expect(''), `Expected file content but file does not exist: '${path}'`);
|
---|
| 77 | }
|
---|
| 78 | },
|
---|
| 79 | get size() {
|
---|
| 80 | try {
|
---|
| 81 | return expect(Buffer.byteLength(harness.readFile(path))).withContext(`With file size for '${path}'`);
|
---|
| 82 | }
|
---|
| 83 | catch (e) {
|
---|
| 84 | if (e.code !== 'ENOENT') {
|
---|
| 85 | throw e;
|
---|
| 86 | }
|
---|
| 87 | // File does not exist so always fail the expectation
|
---|
| 88 | return createFailureExpectation(expect(0), `Expected file size but file does not exist: '${path}'`);
|
---|
| 89 | }
|
---|
| 90 | },
|
---|
| 91 | };
|
---|
| 92 | }
|
---|
| 93 | exports.expectFile = expectFile;
|
---|