-
Notifications
You must be signed in to change notification settings - Fork 618
/
Copy pathStringBufferTerminalProvider.ts
145 lines (123 loc) · 4.04 KB
/
StringBufferTerminalProvider.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
import { StringBuilder, Text } from '@rushstack/node-core-library';
import { type ITerminalProvider, TerminalProviderSeverity } from './ITerminalProvider';
import { AnsiEscape } from './AnsiEscape';
/**
* @beta
*/
export interface IStringBufferOutputOptions {
/**
* If set to true, special characters like \\n, \\r, and the \\u001b character
* in color control tokens will get normalized to [-n-], [-r-], and [-x-] respectively
*
* This option defaults to `true`
*/
normalizeSpecialCharacters: boolean;
}
/**
* Terminal provider that stores written data in buffers separated by severity.
* This terminal provider is designed to be used when code that prints to a terminal
* is being unit tested.
*
* @beta
*/
export class StringBufferTerminalProvider implements ITerminalProvider {
private _standardBuffer: StringBuilder = new StringBuilder();
private _verboseBuffer: StringBuilder = new StringBuilder();
private _debugBuffer: StringBuilder = new StringBuilder();
private _warningBuffer: StringBuilder = new StringBuilder();
private _errorBuffer: StringBuilder = new StringBuilder();
private _supportsColor: boolean;
public constructor(supportsColor: boolean = false) {
this._supportsColor = supportsColor;
}
/**
* {@inheritDoc ITerminalProvider.write}
*/
public write(data: string, severity: TerminalProviderSeverity): void {
switch (severity) {
case TerminalProviderSeverity.warning: {
this._warningBuffer.append(data);
break;
}
case TerminalProviderSeverity.error: {
this._errorBuffer.append(data);
break;
}
case TerminalProviderSeverity.verbose: {
this._verboseBuffer.append(data);
break;
}
case TerminalProviderSeverity.debug: {
this._debugBuffer.append(data);
break;
}
case TerminalProviderSeverity.log:
default: {
this._standardBuffer.append(data);
break;
}
}
}
/**
* {@inheritDoc ITerminalProvider.eolCharacter}
*/
public get eolCharacter(): string {
return '\n';
}
/**
* {@inheritDoc ITerminalProvider.supportsColor}
*/
public get supportsColor(): boolean {
return this._supportsColor;
}
/**
* Get everything that has been written at log-level severity.
*/
public getOutput(options?: IStringBufferOutputOptions): string {
return this._normalizeOutput(this._standardBuffer.toString(), options);
}
/**
* @deprecated - use {@link StringBufferTerminalProvider.getVerboseOutput}
*/
public getVerbose(options?: IStringBufferOutputOptions): string {
return this.getVerboseOutput(options);
}
/**
* Get everything that has been written at verbose-level severity.
*/
public getVerboseOutput(options?: IStringBufferOutputOptions): string {
return this._normalizeOutput(this._verboseBuffer.toString(), options);
}
/**
* Get everything that has been written at debug-level severity.
*/
public getDebugOutput(options?: IStringBufferOutputOptions): string {
return this._normalizeOutput(this._debugBuffer.toString(), options);
}
/**
* Get everything that has been written at error-level severity.
*/
public getErrorOutput(options?: IStringBufferOutputOptions): string {
return this._normalizeOutput(this._errorBuffer.toString(), options);
}
/**
* Get everything that has been written at warning-level severity.
*/
public getWarningOutput(options?: IStringBufferOutputOptions): string {
return this._normalizeOutput(this._warningBuffer.toString(), options);
}
private _normalizeOutput(s: string, options: IStringBufferOutputOptions | undefined): string {
options = {
normalizeSpecialCharacters: true,
...(options || {})
};
s = Text.convertToLf(s);
if (options.normalizeSpecialCharacters) {
return AnsiEscape.formatForTests(s, { encodeNewlines: true });
} else {
return s;
}
}
}