Skip to content

Commit c92deef

Browse files
authored
Fix runtests-browser with latest node (#17735)
* Fixes for running tests in the browser with the latest node * Make memoize safe * Integrate PR feedback, require key func on memoize
1 parent c110ecb commit c92deef

File tree

3 files changed

+23
-23
lines changed

3 files changed

+23
-23
lines changed

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@
9090
"setup-hooks": "node scripts/link-hooks.js"
9191
},
9292
"browser": {
93-
"buffer": false,
9493
"fs": false,
9594
"os": false,
9695
"path": false

src/harness/harness.ts

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -67,17 +67,16 @@ namespace Utils {
6767

6868
export let currentExecutionEnvironment = getExecutionEnvironment();
6969

70-
const Buffer: typeof global.Buffer = currentExecutionEnvironment !== ExecutionEnvironment.Browser
71-
? require("buffer").Buffer
72-
: undefined;
70+
// Thanks to browserify, Buffer is always available nowadays
71+
const Buffer: typeof global.Buffer = require("buffer").Buffer;
7372

7473
export function encodeString(s: string): string {
75-
return Buffer ? (new Buffer(s)).toString("utf8") : s;
74+
return Buffer.from(s).toString("utf8");
7675
}
7776

7877
export function byteLength(s: string, encoding?: string): number {
7978
// stub implementation if Buffer is not available (in-browser case)
80-
return Buffer ? Buffer.byteLength(s, encoding) : s.length;
79+
return Buffer.byteLength(s, encoding);
8180
}
8281

8382
export function evalFile(fileContents: string, fileName: string, nodeContext?: any) {
@@ -133,17 +132,18 @@ namespace Utils {
133132
return content;
134133
}
135134

136-
export function memoize<T extends Function>(f: T): T {
137-
const cache: { [idx: string]: any } = {};
135+
export function memoize<T extends Function>(f: T, memoKey: (...anything: any[]) => string): T {
136+
const cache = ts.createMap<any>();
138137

139-
return <any>(function(this: any) {
140-
const key = Array.prototype.join.call(arguments);
141-
const cachedResult = cache[key];
142-
if (cachedResult) {
143-
return cachedResult;
138+
return <any>(function(this: any, ...args: any[]) {
139+
const key = memoKey(...args);
140+
if (cache.has(key)) {
141+
return cache.get(key);
144142
}
145143
else {
146-
return cache[key] = f.apply(this, arguments);
144+
const value = f.apply(this, args);
145+
cache.set(key, value);
146+
return value;
147147
}
148148
});
149149
}
@@ -686,7 +686,7 @@ namespace Harness {
686686

687687
return dirPath;
688688
}
689-
export let directoryName: typeof IO.directoryName = Utils.memoize(directoryNameImpl);
689+
export let directoryName: typeof IO.directoryName = Utils.memoize(directoryNameImpl, path => path);
690690

691691
export function resolvePath(path: string) {
692692
const response = Http.getFileFromServerSync(serverRoot + path + "?resolve=true");
@@ -703,21 +703,22 @@ namespace Harness {
703703
return response.status === 200;
704704
}
705705

706-
export let listFiles = Utils.memoize((path: string, spec?: RegExp): string[] => {
706+
export const listFiles = Utils.memoize((path: string, spec?: RegExp, options?: { recursive?: boolean }): string[] => {
707707
const response = Http.getFileFromServerSync(serverRoot + path);
708708
if (response.status === 200) {
709-
const results = response.responseText.split(",");
709+
let results = response.responseText.split(",");
710710
if (spec) {
711-
return results.filter(file => spec.test(file));
711+
results = results.filter(file => spec.test(file));
712712
}
713-
else {
714-
return results;
713+
if (options && !options.recursive) {
714+
results = results.filter(file => (ts.getDirectoryPath(ts.normalizeSlashes(file)) === path));
715715
}
716+
return results;
716717
}
717718
else {
718719
return [""];
719720
}
720-
});
721+
}, (path: string, spec?: RegExp, options?: { recursive?: boolean }) => `${path}|${spec}|${options ? options.recursive : undefined}`);
721722

722723
export function readFile(file: string): string | undefined {
723724
const response = Http.getFileFromServerSync(serverRoot + file);
@@ -1989,7 +1990,7 @@ namespace Harness {
19891990
IO.writeFile(actualFileName + ".delete", "");
19901991
}
19911992
else {
1992-
IO.writeFile(actualFileName, actual);
1993+
IO.writeFile(actualFileName, encoded_actual);
19931994
}
19941995
throw new Error(`The baseline file ${relativeFileName} has changed.`);
19951996
}

tests/webTestServer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ console.log(`Static file server running at\n => http://localhost:${port}/\nCTRL
288288

289289
http.createServer((req: http.ServerRequest, res: http.ServerResponse) => {
290290
log(`${req.method} ${req.url}`);
291-
const uri = url.parse(req.url).pathname;
291+
const uri = decodeURIComponent(url.parse(req.url).pathname);
292292
const reqPath = path.join(process.cwd(), uri);
293293
const operation = getRequestOperation(req);
294294
handleRequestOperation(req, res, operation, reqPath);

0 commit comments

Comments
 (0)