Skip to content

Commit 02d07a1

Browse files
committed
Make project tests run in the server
1 parent 2d3e943 commit 02d07a1

3 files changed

Lines changed: 74 additions & 23 deletions

File tree

src/harness/harness.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ namespace Utils {
424424
filtered.push(line);
425425
}
426426

427-
(<any>error).stack = filtered.join(ts.sys.newLine);
427+
(<any>error).stack = filtered.join(Harness.IO.newLine());
428428
}
429429

430430
return error;
@@ -751,7 +751,16 @@ namespace Harness {
751751
return dirPath;
752752
}
753753
export let directoryName: typeof IO.directoryName = Utils.memoize(directoryNameImpl);
754-
export const resolvePath = (path: string) => directoryName(path);
754+
755+
export function resolvePath(path: string) {
756+
const response = Http.getFileFromServerSync(serverRoot + path + "?resolve=true");
757+
if (response.status === 200) {
758+
return response.responseText;
759+
}
760+
else {
761+
return null;
762+
}
763+
}
755764

756765
export function fileExists(path: string): boolean {
757766
const response = Http.getFileFromServerSync(serverRoot + path);

src/harness/runner.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,21 +40,27 @@ let testConfigFile =
4040
Harness.IO.fileExists(mytestconfig) ? Harness.IO.readFile(mytestconfig) :
4141
(Harness.IO.fileExists(testconfig) ? Harness.IO.readFile(testconfig) : "");
4242

43+
type TestConfig = {
44+
tests?: string[];
45+
stackTraceLimit?: number | "full";
46+
light?: boolean;
47+
};
48+
4349
if (testConfigFile !== "") {
44-
const testConfig = JSON.parse(testConfigFile);
50+
const testConfig = <TestConfig>JSON.parse(testConfigFile);
4551
if (testConfig.light) {
4652
Harness.lightMode = true;
4753
}
4854

4955
if (testConfig.stackTraceLimit === "full") {
5056
(<any>Error).stackTraceLimit = Infinity;
5157
}
52-
else if ((testConfig.stackTraceLimit | 0) > 0) {
58+
else if ((+testConfig.stackTraceLimit | 0) > 0) {
5359
(<any>Error).stackTraceLimit = testConfig.stackTraceLimit;
5460
}
5561

56-
if (testConfig.test && testConfig.test.length > 0) {
57-
for (const option of testConfig.test) {
62+
if (testConfig.tests && testConfig.tests.length > 0) {
63+
for (const option of testConfig.tests) {
5864
if (!option) {
5965
continue;
6066
}

tests/webTestServer.ts

Lines changed: 53 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,53 @@ function log(msg: string) {
4848
}
4949
}
5050

51+
52+
let directorySeparator = "/";
53+
54+
function getRootLength(path: string): number {
55+
if (path.charAt(0) === directorySeparator) {
56+
if (path.charAt(1) !== directorySeparator) return 1;
57+
const p1 = path.indexOf("/", 2);
58+
if (p1 < 0) return 2;
59+
const p2 = path.indexOf("/", p1 + 1);
60+
if (p2 < 0) return p1 + 1;
61+
return p2 + 1;
62+
}
63+
if (path.charAt(1) === ":") {
64+
if (path.charAt(2) === directorySeparator) return 3;
65+
return 2;
66+
}
67+
// Per RFC 1738 'file' URI schema has the shape file://<host>/<path>
68+
// if <host> is omitted then it is assumed that host value is 'localhost',
69+
// however slash after the omitted <host> is not removed.
70+
// file:///folder1/file1 - this is a correct URI
71+
// file://folder2/file2 - this is an incorrect URI
72+
if (path.lastIndexOf("file:///", 0) === 0) {
73+
return "file:///".length;
74+
}
75+
const idx = path.indexOf("://");
76+
if (idx !== -1) {
77+
return idx + "://".length;
78+
}
79+
return 0;
80+
}
81+
82+
function getDirectoryPath(path: string): any {
83+
path = switchToForwardSlashes(path);
84+
return path.substr(0, Math.max(getRootLength(path), path.lastIndexOf(directorySeparator)));
85+
}
86+
87+
function ensureDirectoriesExist(path: string) {
88+
path = switchToForwardSlashes(path);
89+
if (path.length > getRootLength(path) && !fs.existsSync(path)) {
90+
const parentDirectory = getDirectoryPath(path);
91+
ensureDirectoriesExist(parentDirectory);
92+
if (!fs.existsSync(path)) {
93+
fs.mkdirSync(path);
94+
}
95+
}
96+
}
97+
5198
// Copied from the compiler sources
5299
function dir(path: string, spec?: string, options?: any) {
53100
options = options || <{ recursive?: boolean; }>{};
@@ -94,28 +141,16 @@ function deleteFolderRecursive(path: string) {
94141
};
95142

96143
function writeFile(path: string, data: any, opts: { recursive: boolean }) {
97-
try {
98-
fs.writeFileSync(path, data);
99-
} catch (e) {
100-
// assume file was written to a directory that exists, if not, start recursively creating them as necessary
101-
var parts = switchToForwardSlashes(path).split('/');
102-
for (var i = 0; i < parts.length; i++) {
103-
var subDir = parts.slice(0, i).join('/');
104-
if (!fs.existsSync(subDir)) {
105-
fs.mkdir(subDir);
106-
}
107-
}
108-
fs.writeFileSync(path, data);
109-
}
144+
ensureDirectoriesExist(getDirectoryPath(path));
145+
fs.writeFileSync(path, data);
110146
}
111147

112148
/// Request Handling ///
113149

114150
function handleResolutionRequest(filePath: string, res: http.ServerResponse) {
115-
var resolvedPath = path.resolve(filePath, '');
116-
resolvedPath = resolvedPath.substring(resolvedPath.indexOf('tests'));
151+
var resolvedPath = path.resolve(filePath);
117152
resolvedPath = switchToForwardSlashes(resolvedPath);
118-
send('success', res, resolvedPath);
153+
send('success', res, resolvedPath, 'text/javascript');
119154
return;
120155
}
121156

@@ -174,6 +209,7 @@ function getRequestOperation(req: http.ServerRequest, filename: string) {
174209
else return RequestType.GetDir;
175210
}
176211
else {
212+
177213
var queryData: any = url.parse(req.url, true).query;
178214
if (req.method === 'GET' && queryData.resolve !== undefined) return RequestType.ResolveFile
179215
// mocha uses ?grep=<regexp> query string as equivalent to the --grep command line option used to filter tests
@@ -223,7 +259,7 @@ function handleRequestOperation(req: http.ServerRequest, res: http.ServerRespons
223259
send('success', res, null);
224260
break;
225261
case RequestType.WriteDir:
226-
fs.mkdirSync(reqPath);
262+
ensureDirectoriesExist(reqPath);
227263
send('success', res, null);
228264
break;
229265
case RequestType.DeleteFile:

0 commit comments

Comments
 (0)