microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/common/outputVerifier.ts
79lines · 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"; | |
fc602bb6Yuri Skorokhodov7 years ago | 6 | import { ErrorHelper } from "./error/errorHelper"; |
| 7 | import { InternalErrorCode } from "./error/internalErrorCode"; | |
7cc67271digeff10 years ago | 8 | |
ef902673Vladimir Kotikov9 years ago | 9 | export type PatternToFailure = { |
| 10 | pattern: string | RegExp, | |
d7d405aeYuri Skorokhodov7 years ago | 11 | errorCode: number |
ef902673Vladimir Kotikov9 years ago | 12 | }; |
7cc67271digeff10 years ago | 13 | |
e4c8f7d4digeff10 years ago | 14 | /* This class transforms a spawn process to only succeed if all defined success patterns |
7cc67271digeff10 years ago | 15 | are found on stdout, and none of the failure patterns were found on stderr */ |
f8b7022ddigeff10 years ago | 16 | export class OutputVerifier { |
7cc67271digeff10 years ago | 17 | private generatePatternsForSuccess: () => Q.Promise<string[]>; |
ef902673Vladimir Kotikov9 years ago | 18 | private generatePatternToFailure: () => Q.Promise<PatternToFailure[]>; |
77a9922aRuslan Bikkinin8 years ago | 19 | private platformName: string; |
7cc67271digeff10 years ago | 20 | |
| 21 | private output = ""; | |
| 22 | private errors = ""; | |
| 23 | | |
77a9922aRuslan Bikkinin8 years ago | 24 | constructor(generatePatternsForSuccess: () => Q.Promise<string[]>, generatePatternToFailure: () => Q.Promise<PatternToFailure[]>, platformName: string) { |
7cc67271digeff10 years ago | 25 | this.generatePatternsForSuccess = generatePatternsForSuccess; |
| 26 | this.generatePatternToFailure = generatePatternToFailure; | |
77a9922aRuslan Bikkinin8 years ago | 27 | this.platformName = platformName; |
7cc67271digeff10 years ago | 28 | } |
| 29 | | |
| 30 | public process(spawnResult: ISpawnResult): Q.Promise<void> { | |
| 31 | // Store all output | |
aab2095edigeff10 years ago | 32 | this.store(spawnResult.stdout, new_content => |
| 33 | this.output += new_content); | |
| 34 | this.store(spawnResult.stderr, new_content => | |
| 35 | this.errors += new_content); | |
7cc67271digeff10 years ago | 36 | |
| 37 | return spawnResult.outcome // Wait for the process to finish | |
| 38 | .then(this.generatePatternToFailure) // Generate the failure patterns to check | |
ef902673Vladimir Kotikov9 years ago | 39 | .then(patterns => { |
d7d405aeYuri Skorokhodov7 years ago | 40 | const failureErrorCode = this.findAnyFailurePattern(patterns); |
| 41 | if (failureErrorCode) { | |
| 42 | return Q.reject<string[]>(ErrorHelper.getInternalError(failureErrorCode)); // If at least one failure happened, we fail | |
7cc67271digeff10 years ago | 43 | } else { |
| 44 | return this.generatePatternsForSuccess(); // If not we generate the success patterns | |
| 45 | } | |
| 46 | }).then(successPatterns => { | |
| 47 | if (!this.areAllSuccessPatternsPresent(successPatterns)) { // If we don't find all the success patterns, we also fail | |
fc602bb6Yuri Skorokhodov7 years ago | 48 | return Q.reject<void>(ErrorHelper.getInternalError(InternalErrorCode.NotAllSuccessPatternsMatched, this.platformName, this.platformName)); |
7cc67271digeff10 years ago | 49 | } // else we found all the success patterns, so we succeed |
5c8365a6Artem Egorov8 years ago | 50 | return Q.resolve(void 0); |
7cc67271digeff10 years ago | 51 | }); |
| 52 | } | |
| 53 | | |
| 54 | private store(stream: NodeJS.ReadableStream, append: (new_content: string) => void) { | |
| 55 | stream.on("data", (data: Buffer) => { | |
| 56 | append(data.toString()); | |
| 57 | }); | |
| 58 | } | |
| 59 | | |
| 60 | // 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 | |
d7d405aeYuri Skorokhodov7 years ago | 61 | private findAnyFailurePattern(patterns: PatternToFailure[]): number | null { |
9ce5a776digeff10 years ago | 62 | const errorsAndOutput = this.errors + this.output; |
ef902673Vladimir Kotikov9 years ago | 63 | const patternThatAppeared = patterns.find(pattern => { |
| 64 | return pattern.pattern instanceof RegExp ? | |
| 65 | (pattern.pattern as RegExp).test(errorsAndOutput) : | |
| 66 | errorsAndOutput.indexOf(pattern.pattern as string) !== -1; | |
| 67 | }); | |
| 68 | | |
d7d405aeYuri Skorokhodov7 years ago | 69 | return patternThatAppeared ? patternThatAppeared.errorCode : null; |
7cc67271digeff10 years ago | 70 | } |
| 71 | | |
| 72 | // We check that all the patterns appeared on the output | |
| 73 | private areAllSuccessPatternsPresent(successPatterns: string[]): boolean { | |
e03193abArtem Egorov8 years ago | 74 | return successPatterns.every(pattern => { |
| 75 | let patternRe = new RegExp(pattern, "i"); | |
| 76 | return patternRe.test(this.output); | |
| 77 | }); | |
7cc67271digeff10 years ago | 78 | } |
| 79 | } |