microsoft/vscode-react-native

Public

mirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
efbe1ba61ca71887b3fa9a2d1ae3afb9a78cb87e

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/common/outputVerifier.ts

79lines · modecode

1// 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";
6import { ErrorHelper } from "./error/errorHelper";
7import { InternalErrorCode } from "./error/internalErrorCode";
8
9export type PatternToFailure = {
10 pattern: string | RegExp,
11 errorCode: number
12};
13
14/* This class transforms a spawn process to only succeed if all defined success patterns
15 are found on stdout, and none of the failure patterns were found on stderr */
16export class OutputVerifier {
17 private generatePatternsForSuccess: () => Q.Promise<string[]>;
18 private generatePatternToFailure: () => Q.Promise<PatternToFailure[]>;
19 private platformName: string;
20
21 private output = "";
22 private errors = "";
23
24 constructor(generatePatternsForSuccess: () => Q.Promise<string[]>, generatePatternToFailure: () => Q.Promise<PatternToFailure[]>, platformName: string) {
25 this.generatePatternsForSuccess = generatePatternsForSuccess;
26 this.generatePatternToFailure = generatePatternToFailure;
27 this.platformName = platformName;
28 }
29
30 public process(spawnResult: ISpawnResult): Q.Promise<void> {
31 // Store all output
32 this.store(spawnResult.stdout, new_content =>
33 this.output += new_content);
34 this.store(spawnResult.stderr, new_content =>
35 this.errors += new_content);
36
37 return spawnResult.outcome // Wait for the process to finish
38 .then(this.generatePatternToFailure) // Generate the failure patterns to check
39 .then(patterns => {
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
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
48 return Q.reject<void>(ErrorHelper.getInternalError(InternalErrorCode.NotAllSuccessPatternsMatched, this.platformName, this.platformName));
49 } // else we found all the success patterns, so we succeed
50 return Q.resolve(void 0);
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
61 private findAnyFailurePattern(patterns: PatternToFailure[]): number | null {
62 const errorsAndOutput = this.errors + this.output;
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
69 return patternThatAppeared ? patternThatAppeared.errorCode : null;
70 }
71
72 // We check that all the patterns appeared on the output
73 private areAllSuccessPatternsPresent(successPatterns: string[]): boolean {
74 return successPatterns.every(pattern => {
75 let patternRe = new RegExp(pattern, "i");
76 return patternRe.test(this.output);
77 });
78 }
79}
80