Skip to content

PR to baseline tsc output rather than just baselining tsc --b #33566

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 8 commits into from
Sep 25, 2019
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
2 changes: 2 additions & 0 deletions src/compiler/sys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -678,6 +678,8 @@ namespace ts {
base64decode?(input: string): string;
base64encode?(input: string): string;
/*@internal*/ bufferFrom?(input: string, encoding?: string): Buffer;
// For testing
/*@internal*/ now?(): Date;
}

export interface FileWatcher {
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/tsbuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ namespace ts {
*/
export function createBuilderStatusReporter(system: System, pretty?: boolean): DiagnosticReporter {
return diagnostic => {
let output = pretty ? `[${formatColorAndReset(new Date().toLocaleTimeString(), ForegroundColorEscapeSequences.Grey)}] ` : `${new Date().toLocaleTimeString()} - `;
let output = pretty ? `[${formatColorAndReset(getLocaleTimeString(system), ForegroundColorEscapeSequences.Grey)}] ` : `${getLocaleTimeString(system)} - `;
output += `${flattenDiagnosticMessageText(diagnostic.messageText, system.newLine)}${system.newLine + system.newLine}`;
system.write(output);
};
Expand Down
13 changes: 11 additions & 2 deletions src/compiler/watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,23 @@ namespace ts {
: newLine;
}

/**
* Get locale specific time based on whether we are in test mode
*/
export function getLocaleTimeString(system: System) {
return !system.now ?
new Date().toLocaleTimeString() :
system.now().toLocaleTimeString("en-US", { timeZone: "UTC" });
}

/**
* Create a function that reports watch status by writing to the system and handles the formating of the diagnostic
*/
export function createWatchStatusReporter(system: System, pretty?: boolean): WatchStatusReporter {
return pretty ?
(diagnostic, newLine, options) => {
clearScreenIfNotWatchingForFileChanges(system, diagnostic, options);
let output = `[${formatColorAndReset(new Date().toLocaleTimeString(), ForegroundColorEscapeSequences.Grey)}] `;
let output = `[${formatColorAndReset(getLocaleTimeString(system), ForegroundColorEscapeSequences.Grey)}] `;
output += `${flattenDiagnosticMessageText(diagnostic.messageText, system.newLine)}${newLine + newLine}`;
system.write(output);
} :
Expand All @@ -72,7 +81,7 @@ namespace ts {
output += newLine;
}

output += `${new Date().toLocaleTimeString()} - `;
output += `${getLocaleTimeString(system)} - `;
output += `${flattenDiagnosticMessageText(diagnostic.messageText, system.newLine)}${getPlainDiagnosticFollowingNewLines(diagnostic, newLine)}`;

system.write(output);
Expand Down
58 changes: 37 additions & 21 deletions src/harness/fakes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ namespace fakes {
}

public createHash(data: string): string {
return data;
return `${ts.generateDjb2Hash(data)}-${data}`;
}

public realpath(path: string) {
Expand All @@ -164,6 +164,10 @@ namespace fakes {
return undefined;
}
}

now() {
return new Date(this.vfs.time());
}
}

/**
Expand Down Expand Up @@ -520,39 +524,51 @@ ${indentText}${text}`;

export const version = "FakeTSVersion";

export class SolutionBuilderHost extends CompilerHost implements ts.SolutionBuilderHost<ts.BuilderProgram> {
createProgram: ts.CreateProgram<ts.BuilderProgram>;

constructor(sys: System | vfs.FileSystem, options?: ts.CompilerOptions, setParentNodes?: boolean, createProgram?: ts.CreateProgram<ts.BuilderProgram>) {
super(sys, options, setParentNodes);
this.createProgram = createProgram || ts.createEmitAndSemanticDiagnosticsBuilderProgram;
}

readFile(path: string) {
const value = super.readFile(path);
export function patchSolutionBuilderHost(host: ts.SolutionBuilderHost<ts.BuilderProgram>, sys: System) {
const originalReadFile = host.readFile;
host.readFile = (path, encoding) => {
const value = originalReadFile.call(host, path, encoding);
if (!value || !ts.isBuildInfoFile(path)) return value;
const buildInfo = ts.getBuildInfo(value);
ts.Debug.assert(buildInfo.version === version);
buildInfo.version = ts.version;
return ts.getBuildInfoText(buildInfo);
};

if (host.writeFile) {
const originalWriteFile = host.writeFile;
host.writeFile = (fileName, content, writeByteOrderMark) => {
if (!ts.isBuildInfoFile(fileName)) return originalWriteFile.call(host, fileName, content, writeByteOrderMark);
const buildInfo = ts.getBuildInfo(content);
sanitizeBuildInfoProgram(buildInfo);
buildInfo.version = version;
originalWriteFile.call(host, fileName, ts.getBuildInfoText(buildInfo), writeByteOrderMark);
};
}

ts.Debug.assert(host.now === undefined);
host.now = () => new Date(sys.vfs.time());
ts.Debug.assertDefined(host.createHash);
}

export class SolutionBuilderHost extends CompilerHost implements ts.SolutionBuilderHost<ts.BuilderProgram> {
createProgram: ts.CreateProgram<ts.BuilderProgram>;

private constructor(sys: System | vfs.FileSystem, options?: ts.CompilerOptions, setParentNodes?: boolean, createProgram?: ts.CreateProgram<ts.BuilderProgram>) {
super(sys, options, setParentNodes);
this.createProgram = createProgram || ts.createEmitAndSemanticDiagnosticsBuilderProgram;
}

public writeFile(fileName: string, content: string, writeByteOrderMark: boolean) {
if (!ts.isBuildInfoFile(fileName)) return super.writeFile(fileName, content, writeByteOrderMark);
const buildInfo = ts.getBuildInfo(content);
sanitizeBuildInfoProgram(buildInfo);
buildInfo.version = version;
super.writeFile(fileName, ts.getBuildInfoText(buildInfo), writeByteOrderMark);
static create(sys: System | vfs.FileSystem, options?: ts.CompilerOptions, setParentNodes?: boolean, createProgram?: ts.CreateProgram<ts.BuilderProgram>) {
const host = new SolutionBuilderHost(sys, options, setParentNodes, createProgram);
patchSolutionBuilderHost(host, host.sys);
return host;
}

createHash(data: string) {
return `${ts.generateDjb2Hash(data)}-${data}`;
}

now() {
return new Date(this.sys.vfs.time());
}

diagnostics: SolutionBuilderDiagnostic[] = [];

reportDiagnostic(diagnostic: ts.Diagnostic) {
Expand Down
1 change: 1 addition & 0 deletions src/testRunner/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@

"unittests/services/extract/helpers.ts",
"unittests/tsbuild/helpers.ts",
"unittests/tsc/helpers.ts",
"unittests/tscWatch/helpers.ts",
"unittests/tsserver/helpers.ts",

Expand Down
80 changes: 32 additions & 48 deletions src/testRunner/unittests/tsbuild/amdModulesWithOut.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
namespace ts {
describe("unittests:: tsbuild:: outFile:: on amd modules with --out", () => {
let outFileFs: vfs.FileSystem;
const { time, tick } = getTime();
const enum project { lib, app }
function relName(path: string) { return path.slice(1); }
type Sources = [string, readonly string[]];
Expand All @@ -25,54 +24,52 @@ namespace ts {
]
];
before(() => {
outFileFs = loadProjectFromDisk("tests/projects/amdModulesWithOut", time);
outFileFs = loadProjectFromDisk("tests/projects/amdModulesWithOut");
});
after(() => {
outFileFs = undefined!;
});

interface VerifyOutFileScenarioInput {
scenario: string;
modifyFs: (fs: vfs.FileSystem) => void;
subScenario: string;
modifyFs?: (fs: vfs.FileSystem) => void;
modifyAgainFs?: (fs: vfs.FileSystem) => void;
}

function verifyOutFileScenario({
scenario,
subScenario,
modifyFs,
modifyAgainFs
}: VerifyOutFileScenarioInput) {
verifyTsbuildOutput({
scenario,
projFs: () => outFileFs,
time,
tick,
proj: "amdModulesWithOut",
rootNames: ["/src/app"],
verifyTscIncrementalEdits({
scenario: "amdModulesWithOut",
subScenario,
fs: () => outFileFs,
commandLineArgs: ["--b", "/src/app", "--verbose"],
baselineSourceMap: true,
initialBuild: {
modifyFs
},
incrementalDtsUnchangedBuild: {
modifyFs: fs => appendText(fs, relName(sources[project.lib][source.ts][1]), "console.log(x);")
},
incrementalHeaderChangedBuild: modifyAgainFs ? {
modifyFs: modifyAgainFs
} : undefined,
baselineOnly: true
modifyFs,
incrementalScenarios: [
{
buildKind: BuildKind.IncrementalDtsUnchanged,
modifyFs: fs => appendText(fs, relName(sources[project.lib][source.ts][1]), "console.log(x);")
},
...(modifyAgainFs ? [{
buildKind: BuildKind.IncrementalHeadersChange,
modifyFs: modifyAgainFs
}] : emptyArray),
]
});
}

describe("Prepend output with .tsbuildinfo", () => {
verifyOutFileScenario({
scenario: "modules and globals mixed in amd",
modifyFs: noop
subScenario: "modules and globals mixed in amd",
});

// Prologues
describe("Prologues", () => {
verifyOutFileScenario({
scenario: "multiple prologues in all projects",
subScenario: "multiple prologues in all projects",
modifyFs: fs => {
enableStrict(fs, sources[project.lib][source.config]);
addTestPrologue(fs, sources[project.lib][source.ts][0], `"myPrologue"`);
Expand All @@ -90,7 +87,7 @@ namespace ts {
describe("Shebang", () => {
// changes declaration because its emitted in .d.ts file
verifyOutFileScenario({
scenario: "shebang in all projects",
subScenario: "shebang in all projects",
modifyFs: fs => {
addShebang(fs, "lib", "file0");
addShebang(fs, "lib", "file1");
Expand All @@ -102,7 +99,7 @@ namespace ts {
// emitHelpers
describe("emitHelpers", () => {
verifyOutFileScenario({
scenario: "multiple emitHelpers in all projects",
subScenario: "multiple emitHelpers in all projects",
modifyFs: fs => {
addSpread(fs, "lib", "file0");
addRest(fs, "lib", "file1");
Expand All @@ -117,7 +114,7 @@ namespace ts {
describe("triple slash refs", () => {
// changes declaration because its emitted in .d.ts file
verifyOutFileScenario({
scenario: "triple slash refs in all projects",
subScenario: "triple slash refs in all projects",
modifyFs: fs => {
addTripleSlashRef(fs, "lib", "file0");
addTripleSlashRef(fs, "app", "file4");
Expand Down Expand Up @@ -161,7 +158,7 @@ ${internal} export enum internalEnum { a, b, c }`);

// Verify initial + incremental edits
verifyOutFileScenario({
scenario: "stripInternal",
subScenario: "stripInternal",
modifyFs: stripInternalScenario,
modifyAgainFs: fs => replaceText(fs, sources[project.lib][source.ts][1], `export const`, `/*@internal*/ export const`),
});
Expand All @@ -175,26 +172,13 @@ ${internal} export enum internalEnum { a, b, c }`);
replaceText(fs, sources[project.app][source.ts][0], "file1", "lib/file1");
}

verifyTsbuildOutput({
scenario: "when the module resolution finds original source file",
projFs: () => outFileFs,
time,
tick,
proj: "amdModulesWithOut",
rootNames: ["/src/app"],
verifyTsc({
scenario: "amdModulesWithOut",
subScenario: "when the module resolution finds original source file",
fs: () => outFileFs,
commandLineArgs: ["-b", "/src/app", "--verbose"],
modifyFs,
baselineSourceMap: true,
initialBuild: {
modifyFs,
expectedDiagnostics: [
getExpectedDiagnosticForProjectsInBuild("src/lib/tsconfig.json", "src/app/tsconfig.json"),
[Diagnostics.Project_0_is_out_of_date_because_output_file_1_does_not_exist, "src/lib/tsconfig.json", "src/module.js"],
[Diagnostics.Building_project_0, sources[project.lib][source.config]],
[Diagnostics.Project_0_is_out_of_date_because_output_file_1_does_not_exist, "src/app/tsconfig.json", "src/app/module.js"],
[Diagnostics.Building_project_0, sources[project.app][source.config]],
]
},
baselineOnly: true,
verifyDiagnostics: true
});
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace ts {

it("verify that subsequent builds after initial build doesnt build anything", () => {
const fs = projFs.shadow();
const host = new fakes.SolutionBuilderHost(fs);
const host = fakes.SolutionBuilderHost.create(fs);
createSolutionBuilder(host, ["/src"], { verbose: true }).build();
host.assertDiagnosticMessages(
getExpectedDiagnosticForProjectsInBuild("src/src/folder/tsconfig.json", "src/src/folder2/tsconfig.json", "src/src/tsconfig.json", "src/tests/tsconfig.json", "src/tsconfig.json"),
Expand Down
6 changes: 2 additions & 4 deletions src/testRunner/unittests/tsbuild/demo.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
namespace ts {
describe("unittests:: tsbuild:: on demo project", () => {
let projFs: vfs.FileSystem;
const { time } = getTime();

before(() => {
projFs = loadProjectFromDisk("tests/projects/demo", time);
projFs = loadProjectFromDisk("tests/projects/demo");
});

after(() => {
Expand Down Expand Up @@ -49,7 +47,7 @@ namespace ts {

function verifyBuild({ modifyDiskLayout, expectedExitStatus, expectedDiagnostics, expectedOutputs, notExpectedOutputs }: VerifyBuild) {
const fs = projFs.shadow();
const host = new fakes.SolutionBuilderHost(fs);
const host = fakes.SolutionBuilderHost.create(fs);
modifyDiskLayout(fs);
const builder = createSolutionBuilder(host, ["/src/tsconfig.json"], { verbose: true });
const exitStatus = builder.build();
Expand Down
Loading