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
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,7 @@ import {BooterApp} from '../fixtures/application';

describe('application metadata booter acceptance tests', () => {
let app: BooterApp;
const sandbox = new TestSandbox(resolve(__dirname, '../../.sandbox'), {
// We intentionally use this flag so that `dist/application.js` can keep
// its relative path to satisfy import statements
subdir: false,
});

const sandbox = new TestSandbox(resolve(__dirname, '../../.sandbox'));
beforeEach('reset sandbox', () => sandbox.reset());
beforeEach(getApp);

Expand All @@ -33,14 +28,19 @@ describe('application metadata booter acceptance tests', () => {
// Add the following files
// - package.json
// - dist/application.js
await sandbox.copyFile(resolve(__dirname, '../fixtures/package.json'));

await sandbox.copyFile(
resolve(__dirname, '../fixtures/application.js'),
'dist/application.js',
// Adjust the relative path for `import`
content => content.replace('../..', '../../..'),
);

await sandbox.copyFile(resolve(__dirname, '../fixtures/package.json'));

const MyApp = require(resolve(sandbox.path, 'dist/application.js'))
.BooterApp;

app = new MyApp({
rest: givenHttpServerConfig(),
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,7 @@ import {BooterApp} from '../fixtures/application';

describe('component application booter acceptance tests', () => {
let app: BooterApp;
const sandbox = new TestSandbox(resolve(__dirname, '../../.sandbox'), {
// We intentionally use this flag so that `dist/application.js` can keep
// its relative path to satisfy import statements
subdir: false,
});
const sandbox = new TestSandbox(resolve(__dirname, '../../.sandbox'));

beforeEach('reset sandbox', () => sandbox.reset());
beforeEach(getApp);
Expand Down Expand Up @@ -87,8 +83,14 @@ describe('component application booter acceptance tests', () => {
}

async function getApp() {
await sandbox.copyFile(
resolve(__dirname, '../fixtures/application.js'),
'application.js',
// Adjust the relative path for `import`
content => content.replace('../..', '../../..'),
);

await sandbox.copyFile(resolve(__dirname, '../fixtures/package.json'));
await sandbox.copyFile(resolve(__dirname, '../fixtures/application.js'));
await sandbox.copyFile(
resolve(__dirname, '../fixtures/multiple.artifact.js'),
'controllers/multiple.controller.js',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,6 @@ describe('controller booter acceptance tests', () => {
}

async function stopApp() {
try {
await app.stop();
} catch (err) {
console.log(`Stopping the app threw an error: ${err}`);
}
await app?.stop();
Copy link
Member

Choose a reason for hiding this comment

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

Should we check the state first, for consistency with other stopApp implementations?

Suggested change
await app?.stop();
if (app?.state === 'started') await app?.stop();

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In this case, app.start() is called.

}
});
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,6 @@ module.exports = {
}

async function stopApp() {
if (app.state !== 'started') return;
await app.stop();
if (app?.state === 'started') await app?.stop();
}
});
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,6 @@ module.exports = {
}

async function stopApp() {
try {
await app.stop();
} catch (err) {
// console.error('Cannot stop the app, ignoring the error.', err);
}
if (app?.state === 'started') await app?.stop();
}
});
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,27 @@ describe('TestSandbox integration tests', () => {
await expectFilesToBeIdentical(COPY_FILE_PATH, resolve(path, COPY_FILE));
});

it('copies a file to the sandbox with transform', async () => {
await sandbox.copyFile(COPY_FILE_PATH, undefined, content =>
content.toUpperCase(),
);
const dest = resolve(path, COPY_FILE);
expect(await pathExists(dest)).to.be.True();
const content = await readFile(dest, 'utf-8');
expect(content).to.equal('HELLO WORLD!');
});

it('copies a file to the sandbox with dest and transform', async () => {
const rename = 'copy.me.js';
await sandbox.copyFile(COPY_FILE_PATH, rename, content =>
content.toUpperCase(),
);
const dest = resolve(path, rename);
expect(await pathExists(dest)).to.be.True();
const content = await readFile(dest, 'utf-8');
expect(content).to.equal('HELLO WORLD!');
});

it('copies and renames the file to the sandbox', async () => {
const rename = 'copy.me.js';
await sandbox.copyFile(COPY_FILE_PATH, rename);
Expand Down
28 changes: 18 additions & 10 deletions packages/testlab/src/test-sandbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ import {
ensureDir,
ensureDirSync,
mkdtempSync,
outputFile,
outputJson,
pathExists,
readFile,
remove,
writeFile,
writeJson,
} from 'fs-extra';
import {join, parse, resolve} from 'path';

Expand Down Expand Up @@ -131,13 +132,24 @@ export class TestSandbox {
* @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.
* @param transform - Optional. A function to transform the file content.
*/
async copyFile(src: string, dest?: string): Promise<void> {
async copyFile(
src: string,
dest?: string,
transform?: (content: string) => string,
): Promise<void> {
dest = dest
? resolve(this.path, dest)
: resolve(this.path, parse(src).base);

await copy(src, dest);
if (transform == null) {
await copy(src, dest);
} else {
let content = await readFile(src, 'utf-8');
content = transform(content);
await outputFile(dest, content, {encoding: 'utf-8'});
}

if (parse(src).ext === '.js' && pathExists(src + '.map')) {
const srcMap = src + '.map';
Expand All @@ -153,9 +165,7 @@ export class TestSandbox {
*/
async writeJsonFile(dest: string, data: unknown): Promise<void> {
dest = resolve(this.path, dest);
const destDir = parse(dest).dir;
await ensureDir(destDir);
return writeJson(dest, data, {spaces: 2});
return outputJson(dest, data, {spaces: 2});
}

/**
Expand All @@ -166,8 +176,6 @@ export class TestSandbox {
*/
async writeTextFile(dest: string, data: string): Promise<void> {
dest = resolve(this.path, dest);
const destDir = parse(dest).dir;
await ensureDir(destDir);
return writeFile(dest, data, {encoding: 'utf-8'});
return outputFile(dest, data, 'utf-8');
}
}