microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/common/outputVerifier.ts
73lines · modeblame
7cc67271digeff10 years ago | 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT license. See LICENSE file in the project root for details. | |
| 3 | | |
| 4 | import * as Q from "q"; | |
| 5 | import {ISpawnResult} from "./node/childProcess"; | |
| 6 | | |
ef902673Vladimir Kotikov9 years ago | 7 | export type PatternToFailure = { |
| 8 | pattern: string | RegExp, | |
| 9 | message: string | |
| 10 | }; | |
7cc67271digeff10 years ago | 11 | |
e4c8f7d4digeff10 years ago | 12 | /* This class transforms a spawn process to only succeed if all defined success patterns |
7cc67271digeff10 years ago | 13 | are found on stdout, and none of the failure patterns were found on stderr */ |
f8b7022ddigeff10 years ago | 14 | export class OutputVerifier { |
7cc67271digeff10 years ago | 15 | private generatePatternsForSuccess: () => Q.Promise<string[]>; |
ef902673Vladimir Kotikov9 years ago | 16 | private generatePatternToFailure: () => Q.Promise<PatternToFailure[]>; |
7cc67271digeff10 years ago | 17 | |
| 18 | private output = ""; | |
| 19 | private errors = ""; | |
| 20 | | |
ef902673Vladimir Kotikov9 years ago | 21 | constructor(generatePatternsForSuccess: () => Q.Promise<string[]>, generatePatternToFailure: () => Q.Promise<PatternToFailure[]>) { |
7cc67271digeff10 years ago | 22 | this.generatePatternsForSuccess = generatePatternsForSuccess; |
| 23 | this.generatePatternToFailure = generatePatternToFailure; | |
| 24 | } | |
| 25 | | |
| 26 | public process(spawnResult: ISpawnResult): Q.Promise<void> { | |
| 27 | // Store all output | |
aab2095edigeff10 years ago | 28 | this.store(spawnResult.stdout, new_content => |
| 29 | this.output += new_content); | |
| 30 | this.store(spawnResult.stderr, new_content => | |
| 31 | this.errors += new_content); | |
7cc67271digeff10 years ago | 32 | |
| 33 | return spawnResult.outcome // Wait for the process to finish | |
| 34 | .then(this.generatePatternToFailure) // Generate the failure patterns to check | |
ef902673Vladimir Kotikov9 years ago | 35 | .then(patterns => { |
| 36 | const failureMessage = this.findAnyFailurePattern(patterns); | |
7cc67271digeff10 years ago | 37 | if (failureMessage) { |
| 38 | return Q.reject<string[]>(new Error(failureMessage)); // If at least one failure happened, we fail | |
| 39 | } else { | |
| 40 | return this.generatePatternsForSuccess(); // If not we generate the success patterns | |
| 41 | } | |
| 42 | }).then(successPatterns => { | |
| 43 | if (!this.areAllSuccessPatternsPresent(successPatterns)) { // If we don't find all the success patterns, we also fail | |
| 44 | return Q.reject<void>(new Error("Unknown error")); | |
| 45 | } // else we found all the success patterns, so we succeed | |
5c8365a6Artem Egorov8 years ago | 46 | return Q.resolve(void 0); |
7cc67271digeff10 years ago | 47 | }); |
| 48 | } | |
| 49 | | |
| 50 | private store(stream: NodeJS.ReadableStream, append: (new_content: string) => void) { | |
| 51 | stream.on("data", (data: Buffer) => { | |
| 52 | append(data.toString()); | |
| 53 | }); | |
| 54 | } | |
| 55 | | |
| 56 | // We check the failure patterns one by one, to see if any of those appeared on the errors. If they did, we return the associated error | |
5c8365a6Artem Egorov8 years ago | 57 | private findAnyFailurePattern(patterns: PatternToFailure[]): string | null { |
9ce5a776digeff10 years ago | 58 | const errorsAndOutput = this.errors + this.output; |
ef902673Vladimir Kotikov9 years ago | 59 | const patternThatAppeared = patterns.find(pattern => { |
| 60 | return pattern.pattern instanceof RegExp ? | |
| 61 | (pattern.pattern as RegExp).test(errorsAndOutput) : | |
| 62 | errorsAndOutput.indexOf(pattern.pattern as string) !== -1; | |
| 63 | }); | |
| 64 | | |
| 65 | return patternThatAppeared ? patternThatAppeared.message : null; | |
7cc67271digeff10 years ago | 66 | } |
| 67 | | |
| 68 | // We check that all the patterns appeared on the output | |
| 69 | private areAllSuccessPatternsPresent(successPatterns: string[]): boolean { | |
| 70 | return successPatterns.every(pattern => | |
| 71 | this.output.indexOf(pattern) !== -1); | |
| 72 | } | |
| 73 | } |