microsoft/vscode-react-native

Public

mirrored from https://github.com/microsoft/vscode-react-nativeAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
0.8.0

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

src/common/outputVerifier.ts

79lines · modeblame

7cc67271digeff10 years ago1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the MIT license. See LICENSE file in the project root for details.
3
4import * as Q from "q";
5import {ISpawnResult} from "./node/childProcess";
fc602bb6Yuri Skorokhodov7 years ago6import { ErrorHelper } from "./error/errorHelper";
7import { InternalErrorCode } from "./error/internalErrorCode";
7cc67271digeff10 years ago8
ef902673Vladimir Kotikov9 years ago9export type PatternToFailure = {
10pattern: string | RegExp,
d7d405aeYuri Skorokhodov7 years ago11errorCode: number
ef902673Vladimir Kotikov9 years ago12};
7cc67271digeff10 years ago13
e4c8f7d4digeff10 years ago14/* This class transforms a spawn process to only succeed if all defined success patterns
7cc67271digeff10 years ago15are found on stdout, and none of the failure patterns were found on stderr */
f8b7022ddigeff10 years ago16export class OutputVerifier {
7cc67271digeff10 years ago17private generatePatternsForSuccess: () => Q.Promise<string[]>;
ef902673Vladimir Kotikov9 years ago18private generatePatternToFailure: () => Q.Promise<PatternToFailure[]>;
77a9922aRuslan Bikkinin8 years ago19private platformName: string;
7cc67271digeff10 years ago20
21private output = "";
22private errors = "";
23
77a9922aRuslan Bikkinin8 years ago24constructor(generatePatternsForSuccess: () => Q.Promise<string[]>, generatePatternToFailure: () => Q.Promise<PatternToFailure[]>, platformName: string) {
7cc67271digeff10 years ago25this.generatePatternsForSuccess = generatePatternsForSuccess;
26this.generatePatternToFailure = generatePatternToFailure;
77a9922aRuslan Bikkinin8 years ago27this.platformName = platformName;
7cc67271digeff10 years ago28}
29
30public process(spawnResult: ISpawnResult): Q.Promise<void> {
31// Store all output
aab2095edigeff10 years ago32this.store(spawnResult.stdout, new_content =>
33this.output += new_content);
34this.store(spawnResult.stderr, new_content =>
35this.errors += new_content);
7cc67271digeff10 years ago36
37return spawnResult.outcome // Wait for the process to finish
38.then(this.generatePatternToFailure) // Generate the failure patterns to check
ef902673Vladimir Kotikov9 years ago39.then(patterns => {
d7d405aeYuri Skorokhodov7 years ago40const failureErrorCode = this.findAnyFailurePattern(patterns);
41if (failureErrorCode) {
42return Q.reject<string[]>(ErrorHelper.getInternalError(failureErrorCode)); // If at least one failure happened, we fail
7cc67271digeff10 years ago43} else {
44return this.generatePatternsForSuccess(); // If not we generate the success patterns
45}
46}).then(successPatterns => {
47if (!this.areAllSuccessPatternsPresent(successPatterns)) { // If we don't find all the success patterns, we also fail
fc602bb6Yuri Skorokhodov7 years ago48return Q.reject<void>(ErrorHelper.getInternalError(InternalErrorCode.NotAllSuccessPatternsMatched, this.platformName, this.platformName));
7cc67271digeff10 years ago49} // else we found all the success patterns, so we succeed
5c8365a6Artem Egorov8 years ago50return Q.resolve(void 0);
7cc67271digeff10 years ago51});
52}
53
54private store(stream: NodeJS.ReadableStream, append: (new_content: string) => void) {
55stream.on("data", (data: Buffer) => {
56append(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 ago61private findAnyFailurePattern(patterns: PatternToFailure[]): number | null {
9ce5a776digeff10 years ago62const errorsAndOutput = this.errors + this.output;
ef902673Vladimir Kotikov9 years ago63const patternThatAppeared = patterns.find(pattern => {
64return pattern.pattern instanceof RegExp ?
65(pattern.pattern as RegExp).test(errorsAndOutput) :
66errorsAndOutput.indexOf(pattern.pattern as string) !== -1;
67});
68
d7d405aeYuri Skorokhodov7 years ago69return patternThatAppeared ? patternThatAppeared.errorCode : null;
7cc67271digeff10 years ago70}
71
72// We check that all the patterns appeared on the output
73private areAllSuccessPatternsPresent(successPatterns: string[]): boolean {
e03193abArtem Egorov8 years ago74return successPatterns.every(pattern => {
75let patternRe = new RegExp(pattern, "i");
76return patternRe.test(this.output);
77});
7cc67271digeff10 years ago78}
79}