Skip to content

Commit 0a98b32

Browse files
authored
Refactor test helpers (#53918)
1 parent 7bf0337 commit 0a98b32

File tree

144 files changed

+1784
-1729
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

144 files changed

+1784
-1729
lines changed

src/testRunner/unittests/helpers/baseline.ts

+370
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import * as ts from "../../_namespaces/ts";
2+
import { libFile } from "./virtualFileSystemWithWatch";
3+
4+
export function compilerOptionsToConfigJson(options: ts.CompilerOptions) {
5+
return ts.optionMapToObject(ts.serializeCompilerOptions(options));
6+
}
7+
8+
export const libContent = `${libFile.content}
9+
interface ReadonlyArray<T> {}
10+
declare const console: { log(msg: any): void; };`;
11+
12+
export const symbolLibContent = `
13+
interface SymbolConstructor {
14+
readonly species: symbol;
15+
readonly toStringTag: symbol;
16+
}
17+
declare var Symbol: SymbolConstructor;
18+
interface Symbol {
19+
readonly [Symbol.toStringTag]: string;
20+
}
21+
`;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import * as fakes from "../../_namespaces/fakes";
2+
import * as ts from "../../_namespaces/ts";
3+
import { commandLineCallbacks } from "./baseline";
4+
import {
5+
makeSystemReadyForBaseline,
6+
TscCompileSystem,
7+
} from "./tsc";
8+
import {
9+
changeToHostTrackingWrittenFiles,
10+
createWatchedSystem,
11+
FileOrFolderOrSymLink,
12+
FileOrFolderOrSymLinkMap,
13+
TestServerHost,
14+
TestServerHostCreationParameters,
15+
} from "./virtualFileSystemWithWatch";
16+
17+
export function createSolutionBuilderHostForBaseline(
18+
sys: TscCompileSystem | TestServerHost,
19+
versionToWrite?: string,
20+
originalRead?: (TscCompileSystem | TestServerHost)["readFile"]
21+
) {
22+
if (sys instanceof fakes.System) makeSystemReadyForBaseline(sys, versionToWrite);
23+
const { cb } = commandLineCallbacks(sys, originalRead);
24+
const host = ts.createSolutionBuilderHost(sys,
25+
/*createProgram*/ undefined,
26+
ts.createDiagnosticReporter(sys, /*pretty*/ true),
27+
ts.createBuilderStatusReporter(sys, /*pretty*/ true)
28+
);
29+
host.afterProgramEmitAndDiagnostics = cb;
30+
host.afterEmitBundle = cb;
31+
return host;
32+
}
33+
34+
export function createSolutionBuilder(system: TestServerHost, rootNames: readonly string[], originalRead?: TestServerHost["readFile"]) {
35+
const host = createSolutionBuilderHostForBaseline(system, /*versionToWrite*/ undefined, originalRead);
36+
return ts.createSolutionBuilder(host, rootNames, {});
37+
}
38+
39+
export function ensureErrorFreeBuild(host: TestServerHost, rootNames: readonly string[]) {
40+
// ts build should succeed
41+
solutionBuildWithBaseline(host, rootNames);
42+
assert.equal(host.getOutput().length, 0, JSON.stringify(host.getOutput(), /*replacer*/ undefined, " "));
43+
}
44+
45+
export function solutionBuildWithBaseline(sys: TestServerHost, solutionRoots: readonly string[], originalRead?: TestServerHost["readFile"]) {
46+
const originalReadFile = sys.readFile;
47+
const originalWrite = sys.write;
48+
const originalWriteFile = sys.writeFile;
49+
ts.Debug.assert(sys.writtenFiles === undefined);
50+
const solutionBuilder = createSolutionBuilder(changeToHostTrackingWrittenFiles(
51+
fakes.patchHostForBuildInfoReadWrite(sys)
52+
), solutionRoots, originalRead);
53+
solutionBuilder.build();
54+
sys.readFile = originalReadFile;
55+
sys.write = originalWrite;
56+
sys.writeFile = originalWriteFile;
57+
sys.writtenFiles = undefined;
58+
return sys;
59+
}
60+
61+
export function createSystemWithSolutionBuild(solutionRoots: readonly string[], files: FileOrFolderOrSymLinkMap | readonly FileOrFolderOrSymLink[], params?: TestServerHostCreationParameters) {
62+
return solutionBuildWithBaseline(createWatchedSystem(files, params), solutionRoots);
63+
}

0 commit comments

Comments
 (0)