Skip to content

Commit c28efb7

Browse files
Merge pull request microsoft#4845 from Microsoft/updateDTRWC
Fix importDefinitelyTypedTests script
2 parents f88f029 + 5a2a702 commit c28efb7

7 files changed

Lines changed: 226 additions & 131 deletions

File tree

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ tests/services/browser/typescriptServices.js
2828
scripts/configureNightly.js
2929
scripts/processDiagnosticMessages.d.ts
3030
scripts/processDiagnosticMessages.js
31-
scripts/importDefinitelyTypedTests.js
31+
scripts/importDefinitelyTypedTests/importDefinitelyTypedTests.js
3232
src/harness/*.js
3333
rwc-report.html
3434
*.swp
@@ -45,6 +45,7 @@ scripts/run.bat
4545
scripts/word2md.js
4646
scripts/ior.js
4747
scripts/*.js.map
48+
scripts/typings/
4849
coverage/
4950
internal/
5051
**/.DS_Store

Jakefile.js

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,9 @@ function concatenateFiles(destinationFile, sourceFiles) {
206206
var useDebugMode = true;
207207
var host = (process.env.host || process.env.TYPESCRIPT_HOST || "node");
208208
var compilerFilename = "tsc.js";
209+
var LKGCompiler = path.join(LKGDirectory, compilerFilename);
210+
var builtLocalCompiler = path.join(builtLocalDirectory, compilerFilename);
211+
209212
/* Compiles a file from a list of sources
210213
* @param outFile: the target file name
211214
* @param sources: an array of the names of the source files
@@ -220,7 +223,7 @@ var compilerFilename = "tsc.js";
220223
*/
221224
function compileFile(outFile, sources, prereqs, prefixes, useBuiltCompiler, noOutFile, generateDeclarations, outDir, preserveConstEnums, keepComments, noResolve, stripInternal, callback) {
222225
file(outFile, prereqs, function() {
223-
var dir = useBuiltCompiler ? builtLocalDirectory : LKGDirectory;
226+
var compilerPath = useBuiltCompiler ? builtLocalCompiler : LKGCompiler;
224227
var options = "--module commonjs --noImplicitAny --noEmitOnError";
225228

226229
// Keep comments when specifically requested
@@ -257,7 +260,7 @@ function compileFile(outFile, sources, prereqs, prefixes, useBuiltCompiler, noOu
257260
options += " --stripInternal"
258261
}
259262

260-
var cmd = host + " " + dir + compilerFilename + " " + options + " ";
263+
var cmd = host + " " + compilerPath + " " + options + " ";
261264
cmd = cmd + sources.join(" ");
262265
console.log(cmd + "\n");
263266

@@ -328,7 +331,7 @@ compileFile(processDiagnosticMessagesJs,
328331

329332
// The generated diagnostics map; built for the compiler and for the 'generate-diagnostics' task
330333
file(diagnosticInfoMapTs, [processDiagnosticMessagesJs, diagnosticMessagesJson], function () {
331-
var cmd = "node " + processDiagnosticMessagesJs + " " + diagnosticMessagesJson;
334+
var cmd = host + " " + processDiagnosticMessagesJs + " " + diagnosticMessagesJson;
332335
console.log(cmd);
333336
var ex = jake.createExec([cmd]);
334337
// Add listeners for output and error
@@ -374,7 +377,7 @@ task("setDebugMode", function() {
374377
});
375378

376379
task("configure-nightly", [configureNightlyJs], function() {
377-
var cmd = "node " + configureNightlyJs + " " + packageJson + " " + programTs;
380+
var cmd = host + " " + configureNightlyJs + " " + packageJson + " " + programTs;
378381
console.log(cmd);
379382
exec(cmd);
380383
}, { async: true });
@@ -386,6 +389,32 @@ task("publish-nightly", ["configure-nightly", "LKG", "clean", "setDebugMode", "r
386389
exec(cmd);
387390
});
388391

392+
var scriptsTsdJson = path.join(scriptsDirectory, "tsd.json");
393+
file(scriptsTsdJson);
394+
395+
task("tsd-scripts", [scriptsTsdJson], function () {
396+
var cmd = "tsd --config " + scriptsTsdJson + " install";
397+
console.log(cmd)
398+
exec(cmd);
399+
}, { async: true })
400+
401+
var importDefinitelyTypedTestsDirectory = path.join(scriptsDirectory, "importDefinitelyTypedTests");
402+
var importDefinitelyTypedTestsJs = path.join(importDefinitelyTypedTestsDirectory, "importDefinitelyTypedTests.js");
403+
var importDefinitelyTypedTestsTs = path.join(importDefinitelyTypedTestsDirectory, "importDefinitelyTypedTests.ts");
404+
405+
file(importDefinitelyTypedTestsTs);
406+
file(importDefinitelyTypedTestsJs, ["tsd-scripts", importDefinitelyTypedTestsTs], function () {
407+
var cmd = host + " " + LKGCompiler + " -p " + importDefinitelyTypedTestsDirectory;
408+
console.log(cmd);
409+
exec(cmd);
410+
}, { async: true });
411+
412+
task("importDefinitelyTypedTests", [importDefinitelyTypedTestsJs], function () {
413+
var cmd = host + " " + importDefinitelyTypedTestsJs + " ./ ../DefinitelyTyped";
414+
console.log(cmd);
415+
exec(cmd);
416+
}, { async: true });
417+
389418
// Local target to build the compiler and services
390419
var tscFile = path.join(builtLocalDirectory, compilerFilename);
391420
compileFile(tscFile, compilerSources, [builtLocalDirectory, copyright].concat(compilerSources), [copyright], /*useBuiltCompiler:*/ false);

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@
3535
"browserify": "latest",
3636
"istanbul": "latest",
3737
"mocha-fivemat-progress-reporter": "latest",
38-
"tslint": "latest"
38+
"tslint": "latest",
39+
"tsd": "latest"
3940
},
4041
"scripts": {
4142
"pretest": "jake tests",

scripts/importDefinitelyTypedTests.ts

Lines changed: 0 additions & 125 deletions
This file was deleted.
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
import * as fs from "fs";
2+
import * as path from "path";
3+
import * as child_process from "child_process";
4+
5+
6+
interface Map<T> {
7+
[key: string]: T;
8+
}
9+
10+
declare var process: {
11+
argv: string[];
12+
env: Map<string>;
13+
exit(exitCode?: number): void;
14+
}
15+
16+
main();
17+
function main() {
18+
const [, progName, tscRoot, definitelyTypedRoot] = process.argv;
19+
if (process.argv.length !== 4) {
20+
if (process.argv.length < 2) {
21+
throw "Expected at least 2 argv elements."
22+
}
23+
console.log("Usage:")
24+
console.log(` node ${path.relative(__dirname, progName)} [TypeScript Repo Root] [DefinitelyTyped Repo Root]`);
25+
return;
26+
}
27+
28+
const tscPath = path.resolve(tscRoot, "built", "local", "tsc.js");
29+
const rwcTestPath = path.resolve(tscRoot, "internal", "cases", "rwc");
30+
const resolvedDefinitelyTypedRoot = path.resolve(definitelyTypedRoot);
31+
32+
console.log(`Resolved TypeScript Compiler Path: '${tscPath}'.`);
33+
console.log(`Resolved TypeScript RWC Path: '${rwcTestPath}'.`);
34+
console.log(`Resolved DefinitelyTyped Repo Root: '${resolvedDefinitelyTypedRoot}'.`);
35+
importDefinitelyTypedTests(tscPath, rwcTestPath, resolvedDefinitelyTypedRoot);
36+
}
37+
38+
function filePathEndsWith(path: string, endingString: string): boolean {
39+
const pathLen = path.length;
40+
const extLen = endingString.length;
41+
return pathLen > extLen && path.substr(pathLen - extLen, extLen).toLocaleLowerCase() === endingString.toLocaleLowerCase();
42+
}
43+
44+
function copyFileSync(source: string, destination: string) {
45+
let text = fs.readFileSync(source);
46+
fs.writeFileSync(destination, text);
47+
}
48+
49+
function importDefinitelyTypedTest(tscPath: string, rwcTestPath: string, testCaseName: string, testFiles: string[], responseFile: string ) {
50+
let cmd = "node " + tscPath + " --module commonjs " + testFiles.join(" ");
51+
if (responseFile) {
52+
cmd += " @" + responseFile;
53+
}
54+
55+
let testDirectoryName = testCaseName + "_" + Math.floor((Math.random() * 10000) + 1);
56+
let testDirectoryPath = path.join(process.env["temp"], testDirectoryName);
57+
if (fs.existsSync(testDirectoryPath)) {
58+
throw new Error("Could not create test directory");
59+
}
60+
fs.mkdirSync(testDirectoryPath);
61+
62+
child_process.exec(cmd, {
63+
maxBuffer: 1 * 1024 * 1024,
64+
cwd: testDirectoryPath
65+
}, (error, stdout, stderr) => {
66+
console.log("importing " + testCaseName + " ...");
67+
console.log(cmd);
68+
69+
if (error) {
70+
console.log("importing " + testCaseName + " ...");
71+
console.log(cmd);
72+
console.log("==> error " + JSON.stringify(error));
73+
console.log("==> stdout " + String(stdout));
74+
console.log("==> stderr " + String(stderr));
75+
console.log("\r\n");
76+
return;
77+
}
78+
79+
// copy generated file to output location
80+
let outputFilePath = path.join(testDirectoryPath, "iocapture0.json");
81+
let testCasePath = path.join(rwcTestPath, "DefinitelyTyped_" + testCaseName + ".json");
82+
copyFileSync(outputFilePath, testCasePath);
83+
84+
//console.log("output generated at: " + outputFilePath);
85+
86+
if (!fs.existsSync(testCasePath)) {
87+
throw new Error("could not find test case at: " + testCasePath);
88+
}
89+
else {
90+
fs.unlinkSync(outputFilePath);
91+
fs.rmdirSync(testDirectoryPath);
92+
//console.log("testcase generated at: " + testCasePath);
93+
//console.log("Done.");
94+
}
95+
//console.log("\r\n");
96+
97+
})
98+
.on('error', (error: any) => {
99+
console.log("==> error " + JSON.stringify(error));
100+
console.log("\r\n");
101+
});
102+
}
103+
104+
function importDefinitelyTypedTests(tscPath: string, rwcTestPath: string, definitelyTypedRoot: string): void {
105+
fs.readdir(definitelyTypedRoot, (err, subDirectories) => {
106+
if (err) {
107+
throw err;
108+
}
109+
110+
// When you just want to test the script out on one or two files,
111+
// just add a line like the following:
112+
//
113+
// .filter(d => d.indexOf("sipml") >= 0 )
114+
subDirectories
115+
.filter(d => ["_infrastructure", "node_modules", ".git"].indexOf(d) < 0)
116+
.filter(i => fs.statSync(path.join(definitelyTypedRoot, i)).isDirectory())
117+
.forEach(d => {
118+
const directoryPath = path.join(definitelyTypedRoot, d);
119+
fs.readdir(directoryPath, function (err, files) {
120+
if (err) {
121+
throw err;
122+
}
123+
124+
let tsFiles: string[] = [];
125+
let testFiles: string[] = [];
126+
let paramFile: string;
127+
128+
for (const filePath of files.map(f => path.join(directoryPath, f))) {
129+
if (filePathEndsWith(filePath, ".ts")) {
130+
tsFiles.push(filePath);
131+
132+
if (filePathEndsWith(filePath, "-tests.ts")) {
133+
testFiles.push(filePath);
134+
}
135+
}
136+
else if (filePathEndsWith(filePath, ".tscparams")) {
137+
paramFile = filePath;
138+
}
139+
}
140+
141+
if (testFiles.length === 0) {
142+
// no test files but multiple d.ts's, e.g. winjs
143+
const regexp = new RegExp(d + "(([-][0-9])|([\.]d[\.]ts))");
144+
if (tsFiles.length > 1 && tsFiles.every(t => filePathEndsWith(t, ".d.ts") && regexp.test(t))) {
145+
for (const fileName of tsFiles) {
146+
importDefinitelyTypedTest(tscPath, rwcTestPath, path.basename(fileName, ".d.ts"), [fileName], paramFile);
147+
}
148+
}
149+
else {
150+
importDefinitelyTypedTest(tscPath, rwcTestPath, d, tsFiles, paramFile);
151+
}
152+
}
153+
else {
154+
for (const fileName of tsFiles) {
155+
importDefinitelyTypedTest(tscPath, rwcTestPath, path.basename(fileName, "-tests.ts"), [fileName], paramFile);
156+
}
157+
}
158+
});
159+
})
160+
});
161+
}

0 commit comments

Comments
 (0)