Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions packages/boot/test/acceptance/controller.booter.acceptance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
13 changes: 0 additions & 13 deletions packages/boot/test/unit/booters/booter-utils.unit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
);
}
});

Expand All @@ -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

Expand All @@ -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);
Expand Down
23 changes: 21 additions & 2 deletions packages/testlab/src/test-sandbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<void> {
Expand All @@ -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.
Expand All @@ -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}`);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIUC, this assumes that the .js file does not contain any //# sourceMappingURL directive yet. Is it a safe assumption? Should we support the case when the .js file already contains sourceMappingURL directive?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope. A file can contain the directive but source-map only considers the last directive for the sourceMappingURL.

The moment we compile a file it gets this directive ... this adds the directive again with the absolute path of the original map file as the last line (so the previous line is ignored as just a comment) after copying the file over to it's new destination.

}
}
}
3 changes: 3 additions & 0 deletions packages/testlab/test/fixtures/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function main(): string {
return 'Hello world';
}
35 changes: 21 additions & 14 deletions packages/testlab/test/integration/test-sandbox.integration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ describe('TestSandbox integration tests', () => {
'../../../test/fixtures',
COPY_FILE,
);
let fileContent: string;

before(getCopyFileContents);
beforeEach(createSandbox);
beforeEach(givenPath);
afterEach(deleteSandbox);
Expand All @@ -37,24 +35,36 @@ 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 () => {
const rename = 'copy.me.js';
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 () => {
Expand Down Expand Up @@ -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() {
Expand All @@ -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');
}
});