|
7 | 7 | import {execSync, spawn} from 'child_process';
|
8 | 8 | import {randomBytes} from 'crypto';
|
9 | 9 | import {createWriteStream} from 'fs';
|
10 |
| -import {tmpdir} from 'os'; |
| 10 | +import {platform, tmpdir} from 'os'; |
11 | 11 | import {basename, dirname, extname, join, sep as dir_sep} from 'path';
|
12 | 12 | import {format} from 'util';
|
13 | 13 | import EventEmitter from 'events';
|
@@ -457,6 +457,84 @@ export class TestBuilder {
|
457 | 457 | }
|
458 | 458 | }
|
459 | 459 |
|
| 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 | + |
460 | 538 | async sendIDENotification(
|
461 | 539 | methodName: string,
|
462 | 540 | args: Array<mixed>,
|
|
0 commit comments