Skip to content

Refactor test helpers #53918

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
370 changes: 370 additions & 0 deletions src/testRunner/unittests/helpers/baseline.ts

Large diffs are not rendered by default.

21 changes: 21 additions & 0 deletions src/testRunner/unittests/helpers/contents.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import * as ts from "../../_namespaces/ts";
import { libFile } from "./virtualFileSystemWithWatch";

export function compilerOptionsToConfigJson(options: ts.CompilerOptions) {
return ts.optionMapToObject(ts.serializeCompilerOptions(options));
}

export const libContent = `${libFile.content}
interface ReadonlyArray<T> {}
declare const console: { log(msg: any): void; };`;

export const symbolLibContent = `
interface SymbolConstructor {
readonly species: symbol;
readonly toStringTag: symbol;
}
declare var Symbol: SymbolConstructor;
interface Symbol {
readonly [Symbol.toStringTag]: string;
}
`;
63 changes: 63 additions & 0 deletions src/testRunner/unittests/helpers/solutionBuilder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import * as fakes from "../../_namespaces/fakes";
import * as ts from "../../_namespaces/ts";
import { commandLineCallbacks } from "./baseline";
import {
makeSystemReadyForBaseline,
TscCompileSystem,
} from "./tsc";
import {
changeToHostTrackingWrittenFiles,
createWatchedSystem,
FileOrFolderOrSymLink,
FileOrFolderOrSymLinkMap,
TestServerHost,
TestServerHostCreationParameters,
} from "./virtualFileSystemWithWatch";

export function createSolutionBuilderHostForBaseline(
sys: TscCompileSystem | TestServerHost,
versionToWrite?: string,
originalRead?: (TscCompileSystem | TestServerHost)["readFile"]
) {
if (sys instanceof fakes.System) makeSystemReadyForBaseline(sys, versionToWrite);
const { cb } = commandLineCallbacks(sys, originalRead);
const host = ts.createSolutionBuilderHost(sys,
/*createProgram*/ undefined,
ts.createDiagnosticReporter(sys, /*pretty*/ true),
ts.createBuilderStatusReporter(sys, /*pretty*/ true)
);
host.afterProgramEmitAndDiagnostics = cb;
host.afterEmitBundle = cb;
return host;
}

export function createSolutionBuilder(system: TestServerHost, rootNames: readonly string[], originalRead?: TestServerHost["readFile"]) {
const host = createSolutionBuilderHostForBaseline(system, /*versionToWrite*/ undefined, originalRead);
return ts.createSolutionBuilder(host, rootNames, {});
}

export function ensureErrorFreeBuild(host: TestServerHost, rootNames: readonly string[]) {
// ts build should succeed
solutionBuildWithBaseline(host, rootNames);
assert.equal(host.getOutput().length, 0, JSON.stringify(host.getOutput(), /*replacer*/ undefined, " "));
}

export function solutionBuildWithBaseline(sys: TestServerHost, solutionRoots: readonly string[], originalRead?: TestServerHost["readFile"]) {
const originalReadFile = sys.readFile;
const originalWrite = sys.write;
const originalWriteFile = sys.writeFile;
ts.Debug.assert(sys.writtenFiles === undefined);
const solutionBuilder = createSolutionBuilder(changeToHostTrackingWrittenFiles(
fakes.patchHostForBuildInfoReadWrite(sys)
), solutionRoots, originalRead);
solutionBuilder.build();
sys.readFile = originalReadFile;
sys.write = originalWrite;
sys.writeFile = originalWriteFile;
sys.writtenFiles = undefined;
return sys;
}

export function createSystemWithSolutionBuild(solutionRoots: readonly string[], files: FileOrFolderOrSymLinkMap | readonly FileOrFolderOrSymLink[], params?: TestServerHostCreationParameters) {
return solutionBuildWithBaseline(createWatchedSystem(files, params), solutionRoots);
}
Loading