From 229ef8c9bc9973cdebacca47ad32b3efa643c3a5 Mon Sep 17 00:00:00 2001 From: Taranveer Virk Date: Thu, 1 Feb 2018 13:38:21 -0500 Subject: [PATCH] feat(testlab): update sourceMappingURL when copying a JS file When a `.ts` compled `.js` file is copied and required, a warning / error is logged to console because the accompanying `.js.map` file cannot be resolved. This PR updates the sourceMappingURL in the copied file to point to the original `.js.map` file as an absolute path so it can be resolved (only if accompanying `.js.map` file exists). --- .../controller.booter.acceptance.ts | 7 ---- .../controller.booter.integration.ts | 7 ---- .../repository.booter.integration.ts | 7 ---- .../test/unit/booters/booter-utils.unit.ts | 13 ------- packages/testlab/src/test-sandbox.ts | 23 ++++++++++-- packages/testlab/test/fixtures/test.ts | 3 ++ .../integration/test-sandbox.integration.ts | 35 +++++++++++-------- 7 files changed, 45 insertions(+), 50 deletions(-) create mode 100644 packages/testlab/test/fixtures/test.ts diff --git a/packages/boot/test/acceptance/controller.booter.acceptance.ts b/packages/boot/test/acceptance/controller.booter.acceptance.ts index 439f72ed8482..21b752e7ecce 100644 --- a/packages/boot/test/acceptance/controller.booter.acceptance.ts +++ b/packages/boot/test/acceptance/controller.booter.acceptance.ts @@ -32,17 +32,10 @@ describe('controller booter acceptance tests', () => { async function getApp() { await sandbox.copyFile(resolve(__dirname, '../fixtures/application.js')); - await sandbox.copyFile( - resolve(__dirname, '../fixtures/application.js.map'), - ); await sandbox.copyFile( resolve(__dirname, '../fixtures/multiple.artifact.js'), 'controllers/multiple.controller.js', ); - await sandbox.copyFile( - resolve(__dirname, '../fixtures/multiple.artifact.js.map'), - 'controllers/multiple.artifact.js.map', - ); const MyApp = require(resolve(SANDBOX_PATH, 'application.js')).BooterApp; app = new MyApp(); diff --git a/packages/boot/test/integration/controller.booter.integration.ts b/packages/boot/test/integration/controller.booter.integration.ts index 69d670a1b7af..26b54baa8cbb 100644 --- a/packages/boot/test/integration/controller.booter.integration.ts +++ b/packages/boot/test/integration/controller.booter.integration.ts @@ -34,17 +34,10 @@ describe('controller booter integration tests', () => { async function getApp() { await sandbox.copyFile(resolve(__dirname, '../fixtures/application.js')); - await sandbox.copyFile( - resolve(__dirname, '../fixtures/application.js.map'), - ); await sandbox.copyFile( resolve(__dirname, '../fixtures/multiple.artifact.js'), 'controllers/multiple.controller.js', ); - await sandbox.copyFile( - resolve(__dirname, '../fixtures/multiple.artifact.js.map'), - 'controllers/multiple.artifact.js.map', - ); const MyApp = require(resolve(SANDBOX_PATH, 'application.js')).BooterApp; app = new MyApp(); diff --git a/packages/boot/test/integration/repository.booter.integration.ts b/packages/boot/test/integration/repository.booter.integration.ts index 0dd39c2e7af9..a24374c1acf3 100644 --- a/packages/boot/test/integration/repository.booter.integration.ts +++ b/packages/boot/test/integration/repository.booter.integration.ts @@ -34,17 +34,10 @@ describe('repository booter integration tests', () => { async function getApp() { await sandbox.copyFile(resolve(__dirname, '../fixtures/application.js')); - await sandbox.copyFile( - resolve(__dirname, '../fixtures/application.js.map'), - ); await sandbox.copyFile( resolve(__dirname, '../fixtures/multiple.artifact.js'), 'repositories/multiple.repository.js', ); - await sandbox.copyFile( - resolve(__dirname, '../fixtures/multiple.artifact.js.map'), - 'repositories/multiple.artifact.js.map', - ); const MyApp = require(resolve(SANDBOX_PATH, 'application.js')).BooterApp; app = new MyApp(); diff --git a/packages/boot/test/unit/booters/booter-utils.unit.ts b/packages/boot/test/unit/booters/booter-utils.unit.ts index d923e1a4c1fc..734b2d4a30e4 100644 --- a/packages/boot/test/unit/booters/booter-utils.unit.ts +++ b/packages/boot/test/unit/booters/booter-utils.unit.ts @@ -45,17 +45,10 @@ describe('booter-utils unit tests', () => { await sandbox.copyFile( resolve(__dirname, '../../fixtures/empty.artifact.js'), ); - await sandbox.copyFile( - resolve(__dirname, '../../fixtures/empty.artifact.js.map'), - ); await sandbox.copyFile( resolve(__dirname, '../../fixtures/multiple.artifact.js'), 'nested/multiple.artifact.js', ); - await sandbox.copyFile( - resolve(__dirname, '../../fixtures/multiple.artifact.js.map'), - 'nested/multiple.artifact.js.map', - ); } }); @@ -71,9 +64,6 @@ describe('booter-utils unit tests', () => { await sandbox.copyFile( resolve(__dirname, '../../fixtures/multiple.artifact.js'), ); - await sandbox.copyFile( - resolve(__dirname, '../../fixtures/multiple.artifact.js.map'), - ); const files = [resolve(SANDBOX_PATH, 'multiple.artifact.js')]; const NUM_CLASSES = 2; // Number of classes in above file @@ -87,9 +77,6 @@ describe('booter-utils unit tests', () => { await sandbox.copyFile( resolve(__dirname, '../../fixtures/empty.artifact.js'), ); - await sandbox.copyFile( - resolve(__dirname, '../../fixtures/empty.artifact.js.map'), - ); const files = [resolve(SANDBOX_PATH, 'empty.artifact.js')]; const classes = loadClassesFromFiles(files); diff --git a/packages/testlab/src/test-sandbox.ts b/packages/testlab/src/test-sandbox.ts index 21732a10c7d3..c8e7723daa80 100644 --- a/packages/testlab/src/test-sandbox.ts +++ b/packages/testlab/src/test-sandbox.ts @@ -4,7 +4,15 @@ // License text available at https://opensource.org/licenses/MIT import {resolve, parse} from 'path'; -import {copy, ensureDirSync, emptyDir, remove, ensureDir} from 'fs-extra'; +import { + copy, + ensureDirSync, + emptyDir, + remove, + ensureDir, + pathExists, + appendFile, +} from 'fs-extra'; /** * TestSandbox class provides a convenient way to get a reference to a @@ -64,6 +72,7 @@ export class TestSandbox { /** * Makes a directory in the TestSandbox + * * @param dir Name of directory to create (relative to TestSandbox path) */ async mkdir(dir: string): Promise { @@ -72,7 +81,11 @@ export class TestSandbox { } /** - * Copies a file from src to the TestSandbox. + * Copies a file from src to the TestSandbox. If copying a `.js` file which + * has an accompanying `.js.map` file in the src file location, the dest file + * will have its sourceMappingURL updated to point to the original file as + * an absolute path so you don't need to copy the map file. + * * @param src Absolute path of file to be copied to the TestSandbox * @param [dest] Optional. Destination filename of the copy operation * (relative to TestSandbox). Original filename used if not specified. @@ -82,6 +95,12 @@ export class TestSandbox { dest = dest ? resolve(this.path, dest) : resolve(this.path, parse(src).base); + await copy(src, dest); + + if (parse(src).ext === '.js' && pathExists(src + '.map')) { + const srcMap = src + '.map'; + await appendFile(dest, `\n//# sourceMappingURL=${srcMap}`); + } } } diff --git a/packages/testlab/test/fixtures/test.ts b/packages/testlab/test/fixtures/test.ts new file mode 100644 index 000000000000..8dfcd4d9830a --- /dev/null +++ b/packages/testlab/test/fixtures/test.ts @@ -0,0 +1,3 @@ +export function main(): string { + return 'Hello world'; +} diff --git a/packages/testlab/test/integration/test-sandbox.integration.ts b/packages/testlab/test/integration/test-sandbox.integration.ts index 8cb75c069bcc..88a01828f9ea 100644 --- a/packages/testlab/test/integration/test-sandbox.integration.ts +++ b/packages/testlab/test/integration/test-sandbox.integration.ts @@ -16,9 +16,7 @@ describe('TestSandbox integration tests', () => { '../../../test/fixtures', COPY_FILE, ); - let fileContent: string; - before(getCopyFileContents); beforeEach(createSandbox); beforeEach(givenPath); afterEach(deleteSandbox); @@ -37,7 +35,7 @@ describe('TestSandbox integration tests', () => { it('copies a file to the sandbox', async () => { await sandbox.copyFile(COPY_FILE_PATH); expect(await pathExists(resolve(path, COPY_FILE))).to.be.True(); - await compareFiles(resolve(path, COPY_FILE)); + await expectFilesToBeIdentical(COPY_FILE_PATH, resolve(path, COPY_FILE)); }); it('copies and renames the file to the sandbox', async () => { @@ -45,16 +43,28 @@ describe('TestSandbox integration tests', () => { await sandbox.copyFile(COPY_FILE_PATH, rename); expect(await pathExists(resolve(path, COPY_FILE))).to.be.False(); expect(await pathExists(resolve(path, rename))).to.be.True(); - await compareFiles(resolve(path, rename)); + await expectFilesToBeIdentical(COPY_FILE_PATH, resolve(path, rename)); }); it('copies file to a directory', async () => { const dir = 'test'; - await sandbox.mkdir(dir); const rename = `${dir}/${COPY_FILE}`; await sandbox.copyFile(COPY_FILE_PATH, rename); expect(await pathExists(resolve(path, rename))).to.be.True(); - await compareFiles(resolve(path, rename)); + await expectFilesToBeIdentical(COPY_FILE_PATH, resolve(path, rename)); + }); + + it('updates source map path for a copied file', async () => { + const file = 'test.js'; + const resolvedFile = resolve(__dirname, '../fixtures/test.js'); + const sourceMapString = `//# sourceMappingURL=${resolvedFile}.map`; + + await sandbox.copyFile(resolvedFile); + let fileContents = (await readFile(resolve(path, file), 'utf8')).split( + '\n', + ); + + expect(fileContents.pop()).to.equal(sourceMapString); }); it('deletes the test sandbox', async () => { @@ -93,13 +103,14 @@ describe('TestSandbox integration tests', () => { await sandbox.delete(); } - async function compareFiles(path1: string) { - const file = await readFile(path1, 'utf8'); - expect(file).to.equal(fileContent); + async function expectFilesToBeIdentical(original: string, copied: string) { + const originalContent = await readFile(original, 'utf8'); + const copiedContent = await readFile(copied, 'utf8'); + expect(copiedContent).to.equal(originalContent); } function createSandbox() { - sandbox = new TestSandbox(resolve(__dirname, 'sandbox')); + sandbox = new TestSandbox(resolve(__dirname, '../../.sandbox')); } function givenPath() { @@ -110,8 +121,4 @@ describe('TestSandbox integration tests', () => { if (!await pathExists(path)) return; await remove(sandbox.getPath()); } - - async function getCopyFileContents() { - fileContent = await readFile(COPY_FILE_PATH, 'utf8'); - } });