UNPKG

5.12 kBJavaScriptView Raw
1"use strict";
2/**
3 * @license
4 * Copyright (c) 2017 The Polymer Project Authors. All rights reserved.
5 * This code may only be used under the BSD style license found at
6 * http://polymer.github.io/LICENSE.txt
7 * The complete set of authors may be found at
8 * http://polymer.github.io/AUTHORS.txt
9 * The complete set of contributors may be found at
10 * http://polymer.github.io/CONTRIBUTORS.txt
11 * Code distributed by Google as part of the polymer project is also
12 * subject to an additional IP rights grant found at
13 * http://polymer.github.io/PATENTS.txt
14 */
15var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
16 return new (P || (P = Promise))(function (resolve, reject) {
17 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
18 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
19 function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
20 step((generator = generator.apply(thisArg, _arguments || [])).next());
21 });
22};
23Object.defineProperty(exports, "__esModule", { value: true });
24const chai_1 = require("chai");
25function getFlowingState(stream) {
26 // Cast our streams to <any> so that we can check the flowing state.
27 // _readableState is undocumented in the Node.js TypeScript definition,
28 // however it is the supported way to assert if a stream is flowing or not.
29 // See: https://nodejs.org/api/stream.html#stream_three_states
30 // tslint:disable-next-line: no-any
31 const privateReadableState = stream._readableState;
32 return privateReadableState.flowing;
33}
34exports.getFlowingState = getFlowingState;
35/**
36 * This method makes it possible to `await` a map of paths to `File` objects
37 * emitted by a stream. It returns a Promise that resolves with the map
38 * where the paths in the map exclude the optional `root` prefix.
39 */
40function emittedFiles(stream, root = '') {
41 return __awaiter(this, void 0, void 0, function* () {
42 const files = new Map();
43 return new Promise((resolve, reject) => stream
44 .on('data', (f) => files.set(f.path.substring(root.length + 1), f))
45 .on('data', () => { })
46 .on('end', () => resolve(files))
47 .on('error', (e) => reject(e)));
48 });
49}
50exports.emittedFiles = emittedFiles;
51/**
52 * Assert that two strings are equal after collapsing their whitespace.
53 */
54exports.assertEqualIgnoringWhitespace = (actual, expected) => chai_1.assert.equal(collapseWhitespace(actual), collapseWhitespace(expected));
55/**
56 * Assert that two string maps are equal, where their values have had their
57 * whitespace collapsed.
58 */
59exports.assertMapEqualIgnoringWhitespace = (actual, expected) => assertMapEqual(transformMapValues(actual, collapseWhitespace), transformMapValues(expected, collapseWhitespace));
60/**
61 * Collapse all leading whitespace, trailing whitespace, and newlines. Very
62 * lossy, but good for loose comparison of HTML, JS, etc.
63 */
64const collapseWhitespace = (s) => s.replace(/^\s+/gm, '').replace(/\s+$/gm, '').replace(/\n/gm, '');
65/**
66 * Assert that two maps are equal. Note that early versions of chai's deepEqual
67 * will always return true, and while later ones will compare correctly, they do
68 * not produce very readable output compared to this approach.
69 */
70const assertMapEqual = (actual, expected) => chai_1.assert.deepEqual([...actual.entries()], [...expected.entries()]);
71/**
72 * Return a new map where all values have been transformed with the given
73 * function.
74 */
75const transformMapValues = (map, transform) => new Map([...map.entries()].map(([key, val]) => [key, transform(val)]));
76/**
77 * Calls the given async function and captures all console.log and friends
78 * output while until the returned Promise settles.
79 *
80 * Does not capture plylog, which doesn't seem to be very easy to intercept.
81 *
82 * TODO(rictic): this function is shared across many of our packages,
83 * put it in a shared package instead.
84 */
85function interceptOutput(captured) {
86 return __awaiter(this, void 0, void 0, function* () {
87 const originalLog = console.log;
88 const originalError = console.error;
89 const originalWarn = console.warn;
90 const buffer = [];
91 // tslint:disable-next-line:no-any This is genuinely the API.
92 const capture = (...args) => {
93 buffer.push(args.join(' '));
94 };
95 console.log = capture;
96 console.error = capture;
97 console.warn = capture;
98 const restoreAndGetOutput = () => {
99 console.log = originalLog;
100 console.error = originalError;
101 console.warn = originalWarn;
102 return buffer.join('\n');
103 };
104 try {
105 yield captured();
106 }
107 catch (err) {
108 const output = restoreAndGetOutput();
109 console.error(output);
110 throw err;
111 }
112 return restoreAndGetOutput();
113 });
114}
115exports.interceptOutput = interceptOutput;
116//# sourceMappingURL=util.js.map
\No newline at end of file