Skip to content

Commit 9eb6c47

Browse files
gabeleviAvik Chaudhuri
authored and
Avik Chaudhuri
committed
Fix newtests/lsp/queries for Windows
Summary: I was getting exceptions about bad file URIs. I found https://blogs.msdn.microsoft.com/ie/2006/12/06/file-uris-in-windows/ which seemed to indicate that an absolute file URI on Windows looks like file:///C:/Foo/Bar Tried it out and it seemed to work! Reviewed By: avikchaudhuri Differential Revision: D7602609 fbshipit-source-id: 18c8960d692a9e46db3bb5b5d65acd01b4807812
1 parent d01f06f commit 9eb6c47

File tree

1 file changed

+79
-1
lines changed

1 file changed

+79
-1
lines changed

packages/flow-dev-tools/src/test/builder.js

+79-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import {execSync, spawn} from 'child_process';
88
import {randomBytes} from 'crypto';
99
import {createWriteStream} from 'fs';
10-
import {tmpdir} from 'os';
10+
import {platform, tmpdir} from 'os';
1111
import {basename, dirname, extname, join, sep as dir_sep} from 'path';
1212
import {format} from 'util';
1313
import EventEmitter from 'events';
@@ -457,6 +457,84 @@ export class TestBuilder {
457457
}
458458
}
459459

460+
getDirUrl(): string {
461+
if (platform() === 'win32') {
462+
return 'file:///' + this.dir;
463+
} else {
464+
return 'file://' + this.dir;
465+
}
466+
}
467+
468+
// sanitizeIncomingIDEMessage: removes a few known fields from server output
469+
// that are known to be specific to an instance of a test, and replaces
470+
// them with something fixed.
471+
sanitizeIncomingIDEMessage(params: any): any {
472+
const params2 = JSON.parse(JSON.stringify(params));
473+
474+
// Legacy IDE sends back an array of objects where those objects have
475+
// a '.flowVersion' field
476+
// LSP sends back document URLs, to files within the test project
477+
const url = this.getDirUrl();
478+
function replace(obj: Object) {
479+
for (const k in obj) {
480+
if (!obj.hasOwnProperty(k)) {
481+
continue;
482+
}
483+
if (obj[k] == null) {
484+
continue;
485+
}
486+
switch (typeof obj[k]) {
487+
case 'object':
488+
replace(obj[k]);
489+
break;
490+
case 'string':
491+
if (k == 'flowVersion') {
492+
obj[k] = '<VERSION STUBBED FOR TEST>';
493+
} else if (obj[k].startsWith(url)) {
494+
obj[k] = '<PLACEHOLDER_PROJECT_URL>' + obj[k].substr(url.length);
495+
}
496+
break;
497+
}
498+
}
499+
}
500+
501+
replace(params2);
502+
return params2;
503+
}
504+
505+
// sanitizeOutoingIDEMessage: replaces some placeholders with values
506+
// that can only be computed by the builder instance
507+
sanitizeOutgoingIDEMessage(params: Array<mixed>): Array<mixed> {
508+
const params2: any = JSON.parse(JSON.stringify(params));
509+
510+
const dir = this.dir;
511+
const dirUrl = this.getDirUrl();
512+
function replace(obj: Object) {
513+
for (const k in obj) {
514+
if (!obj.hasOwnProperty(k)) {
515+
continue;
516+
}
517+
switch (typeof obj[k]) {
518+
case 'object':
519+
if (obj[k] != null) {
520+
replace(obj[k]);
521+
}
522+
break;
523+
case 'string':
524+
if (obj[k].startsWith('<PLACEHOLDER')) {
525+
obj[k] = obj[k]
526+
.replace(/^<PLACEHOLDER_PROJECT_DIR>/, dir)
527+
.replace(/^<PLACEHOLDER_PROJECT_URL>/, dirUrl);
528+
}
529+
break;
530+
}
531+
}
532+
}
533+
534+
replace(params2);
535+
return params2;
536+
}
537+
460538
async sendIDENotification(
461539
methodName: string,
462540
args: Array<mixed>,

0 commit comments

Comments
 (0)