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
5 changes: 3 additions & 2 deletions examples/file-transfer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,9 @@ browser. You can try to upload a few files using the web UI or API explorer.

![upload-download](upload-download.png)

The uploaded files will be stored in `.sandbox` folder under the application
root directory.
By default, the uploaded files will be stored in `.sandbox` folder under the
application root directory. The directory can be configured via
`fileStorageDirectory` of application config.

## Contributions

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ describe('file upload acceptance - multipart/form-data', () => {
});

async function givenAClient() {
({app, client} = await setupApplication());
({app, client} = await setupApplication(sandbox.path));
}

async function resetSandbox() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ import {
import path from 'path';
import {FileUploadApplication} from '../..';

export async function setupApplication(): Promise<AppWithClient> {
export async function setupApplication(
fileStorageDirectory?: string,
): Promise<AppWithClient> {
const restConfig = givenHttpServerConfig({
// Customize the server configuration here.
// Empty values (undefined, '') will be ignored by the helper.
Expand All @@ -23,6 +25,7 @@ export async function setupApplication(): Promise<AppWithClient> {

const app = new FileUploadApplication({
rest: restConfig,
fileStorageDirectory,
});

await app.boot();
Expand All @@ -38,8 +41,7 @@ export interface AppWithClient {
client: Client;
}

const SANDBOX = path.resolve(__dirname, '../../../.sandbox');
export function getSandbox() {
const sandbox = new TestSandbox(SANDBOX);
const sandbox = new TestSandbox(path.resolve(__dirname, '../../../.sandbox'));
return sandbox;
}
6 changes: 3 additions & 3 deletions examples/file-transfer/src/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export class FileUploadApplication extends BootMixin(RestApplication) {
this.component(RestExplorerComponent);

// Configure file upload with multer options
this.configureFileUpload();
this.configureFileUpload(options.fileStorageDirectory);

this.projectRoot = __dirname;
// Customize @loopback/boot Booter Conventions here
Expand All @@ -49,11 +49,11 @@ export class FileUploadApplication extends BootMixin(RestApplication) {
/**
* Configure `multer` options for file upload
*/
protected configureFileUpload() {
protected configureFileUpload(destination?: string) {
const multerOptions: multer.Options = {
storage: multer.diskStorage({
// Upload files to `.sandbox`
destination: path.join(__dirname, '../.sandbox'),
destination: destination ?? path.join(__dirname, '../.sandbox'),
// Use the original file name as is
filename: (req, file, cb) => {
cb(null, file.originalname);
Expand Down
1 change: 1 addition & 0 deletions examples/file-transfer/src/controllers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

export * from './file-download.controller';
export * from './file-upload.controller';
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ import {BooterApp} from '../fixtures/application';

describe('application metadata booter acceptance tests', () => {
let app: BooterApp;
const SANDBOX_PATH = resolve(__dirname, '../../.sandbox');
const sandbox = new TestSandbox(SANDBOX_PATH);
const sandbox = new TestSandbox(resolve(__dirname, '../../.sandbox'));

beforeEach('reset sandbox', () => sandbox.reset());
beforeEach(getApp);
Expand All @@ -36,7 +35,7 @@ describe('application metadata booter acceptance tests', () => {
'dist/application.js',
);

const MyApp = require(resolve(SANDBOX_PATH, 'dist/application.js'))
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,8 +13,7 @@ import {BooterApp} from '../fixtures/application';

describe('controller booter acceptance tests', () => {
let app: BooterApp;
const SANDBOX_PATH = resolve(__dirname, '../../.sandbox');
const sandbox = new TestSandbox(SANDBOX_PATH);
const sandbox = new TestSandbox(resolve(__dirname, '../../.sandbox'));

beforeEach('reset sandbox', () => sandbox.reset());
beforeEach(getApp);
Expand All @@ -40,7 +39,7 @@ describe('controller booter acceptance tests', () => {
'controllers/multiple.controller.js',
);

const MyApp = require(resolve(SANDBOX_PATH, 'application.js')).BooterApp;
const MyApp = require(resolve(sandbox.path, 'application.js')).BooterApp;
app = new MyApp({
rest: givenHttpServerConfig(),
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ import {ProductRepository} from '../fixtures/product.repository';

describe('CRUD rest builder acceptance tests', () => {
let app: BooterApp;
const SANDBOX_PATH = resolve(__dirname, '../../.sandbox');
const sandbox = new TestSandbox(SANDBOX_PATH);
const sandbox = new TestSandbox(resolve(__dirname, '../../.sandbox'));

beforeEach('reset sandbox', () => sandbox.reset());
beforeEach(givenAppWithDataSource);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ import {

describe('model API booter acceptance tests', () => {
let app: BooterApp;
const SANDBOX_PATH = resolve(__dirname, '../../.sandbox');
const sandbox = new TestSandbox(SANDBOX_PATH);
const sandbox = new TestSandbox(resolve(__dirname, '../../.sandbox'));

beforeEach('reset sandbox', () => sandbox.reset());
beforeEach(givenAppWithDataSource);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ import {resolve} from 'path';
import {BooterApp} from '../fixtures/application';

describe('controller booter integration tests', () => {
const SANDBOX_PATH = resolve(__dirname, '../../.sandbox');
const sandbox = new TestSandbox(SANDBOX_PATH);
const sandbox = new TestSandbox(resolve(__dirname, '../../.sandbox'));

// Remnants from Refactor -- need to add these to core
const CONTROLLERS_PREFIX = 'controllers';
Expand Down Expand Up @@ -39,7 +38,7 @@ describe('controller booter integration tests', () => {
'controllers/multiple.controller.js',
);

const MyApp = require(resolve(SANDBOX_PATH, 'application.js')).BooterApp;
const MyApp = require(resolve(sandbox.path, 'application.js')).BooterApp;
app = new MyApp();
}
});
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ import {resolve} from 'path';
import {BooterApp} from '../fixtures/application';

describe('datasource booter integration tests', () => {
const SANDBOX_PATH = resolve(__dirname, '../../.sandbox');
const sandbox = new TestSandbox(SANDBOX_PATH);
const sandbox = new TestSandbox(resolve(__dirname, '../../.sandbox'));

const DATASOURCES_PREFIX = 'datasources';
const DATASOURCES_TAG = 'datasource';
Expand All @@ -35,7 +34,7 @@ describe('datasource booter integration tests', () => {
'datasources/db.datasource.js',
);

const MyApp = require(resolve(SANDBOX_PATH, 'application.js')).BooterApp;
const MyApp = require(resolve(sandbox.path, 'application.js')).BooterApp;
app = new MyApp();
}
});
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ import {resolve} from 'path';
import {BooterApp} from '../fixtures/application';

describe('interceptor script booter integration tests', () => {
const SANDBOX_PATH = resolve(__dirname, '../../.sandbox');
const sandbox = new TestSandbox(SANDBOX_PATH);
const sandbox = new TestSandbox(resolve(__dirname, '../../.sandbox'));

let app: BooterApp;

Expand Down Expand Up @@ -76,7 +75,7 @@ describe('interceptor script booter integration tests', () => {
'interceptors/non-global-interceptor.interceptor.js',
);

const MyApp = require(resolve(SANDBOX_PATH, 'application.js')).BooterApp;
const MyApp = require(resolve(sandbox.path, 'application.js')).BooterApp;
app = new MyApp();
}
});
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ import {resolve} from 'path';
import {BooterApp} from '../fixtures/application';

describe('lifecycle script booter integration tests', () => {
const SANDBOX_PATH = resolve(__dirname, '../../.sandbox');
const sandbox = new TestSandbox(SANDBOX_PATH);
const sandbox = new TestSandbox(resolve(__dirname, '../../.sandbox'));

const OBSERVER_PREFIX = CoreBindings.LIFE_CYCLE_OBSERVERS;
const OBSERVER_TAG = CoreTags.LIFE_CYCLE_OBSERVER;
Expand Down Expand Up @@ -47,7 +46,7 @@ describe('lifecycle script booter integration tests', () => {
'observers/lifecycle-observer.observer.js',
);

const MyApp = require(resolve(SANDBOX_PATH, 'application.js')).BooterApp;
const MyApp = require(resolve(sandbox.path, 'application.js')).BooterApp;
app = new MyApp();
}
});
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ import {resolve} from 'path';
import {BooterApp} from '../fixtures/application';

describe('repository booter integration tests', () => {
const SANDBOX_PATH = resolve(__dirname, '../../.sandbox');
const sandbox = new TestSandbox(SANDBOX_PATH);
const sandbox = new TestSandbox(resolve(__dirname, '../../.sandbox'));

// Remnants from Refactor -- need to add these to core
const REPOSITORIES_PREFIX = 'repositories';
Expand Down Expand Up @@ -39,7 +38,7 @@ describe('repository booter integration tests', () => {
'repositories/multiple.repository.js',
);

const MyApp = require(resolve(SANDBOX_PATH, 'application.js')).BooterApp;
const MyApp = require(resolve(sandbox.path, 'application.js')).BooterApp;
app = new MyApp();
}
});
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ import {resolve} from 'path';
import {BooterApp} from '../fixtures/application';

describe('service booter integration tests', () => {
const SANDBOX_PATH = resolve(__dirname, '../../.sandbox');
const sandbox = new TestSandbox(SANDBOX_PATH);
const sandbox = new TestSandbox(resolve(__dirname, '../../.sandbox'));

const SERVICES_PREFIX = 'services';
const SERVICES_TAG = 'service';
Expand Down Expand Up @@ -62,7 +61,7 @@ describe('service booter integration tests', () => {
'services/bindable-classes.service.js',
);

const MyApp = require(resolve(SANDBOX_PATH, 'application.js')).BooterApp;
const MyApp = require(resolve(sandbox.path, 'application.js')).BooterApp;
app = new MyApp();
}
});
21 changes: 10 additions & 11 deletions packages/boot/src/__tests__/unit/booters/booter-utils.unit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ import {resolve} from 'path';
import {discoverFiles, isClass, loadClassesFromFiles} from '../../..';

describe('booter-utils unit tests', () => {
const SANDBOX_PATH = resolve(__dirname, '../../../.sandbox');
const sandbox = new TestSandbox(SANDBOX_PATH);
const sandbox = new TestSandbox(resolve(__dirname, '../../../.sandbox'));

beforeEach('reset sandbox', () => sandbox.reset());

Expand All @@ -18,26 +17,26 @@ describe('booter-utils unit tests', () => {

it('discovers files matching a nested glob pattern', async () => {
const expected = [
resolve(SANDBOX_PATH, 'empty.artifact.js'),
resolve(SANDBOX_PATH, 'nested/multiple.artifact.js'),
resolve(sandbox.path, 'empty.artifact.js'),
resolve(sandbox.path, 'nested/multiple.artifact.js'),
];
const glob = '/**/*.artifact.js';

const files = await discoverFiles(glob, SANDBOX_PATH);
const files = await discoverFiles(glob, sandbox.path);
expect(files.sort()).to.eql(expected.sort());
});

it('discovers files matching a non-nested glob pattern', async () => {
const expected = [resolve(SANDBOX_PATH, 'empty.artifact.js')];
const expected = [resolve(sandbox.path, 'empty.artifact.js')];
const glob = '/*.artifact.js';

const files = await discoverFiles(glob, SANDBOX_PATH);
const files = await discoverFiles(glob, sandbox.path);
expect(files).to.eql(expected);
});

it('discovers no files for a unknown glob', async () => {
const glob = '/xyz';
const files = await discoverFiles(glob, SANDBOX_PATH);
const files = await discoverFiles(glob, sandbox.path);
expect(files).to.be.eql([]);
});

Expand All @@ -64,7 +63,7 @@ describe('booter-utils unit tests', () => {
await sandbox.copyFile(
resolve(__dirname, '../../fixtures/multiple.artifact.js'),
);
const files = [resolve(SANDBOX_PATH, 'multiple.artifact.js')];
const files = [resolve(sandbox.path, 'multiple.artifact.js')];
const NUM_CLASSES = 2; // Number of classes in above file

const classes = loadClassesFromFiles(files, sandbox.path);
Expand All @@ -77,15 +76,15 @@ describe('booter-utils unit tests', () => {
await sandbox.copyFile(
resolve(__dirname, '../../fixtures/empty.artifact.js'),
);
const files = [resolve(SANDBOX_PATH, 'empty.artifact.js')];
const files = [resolve(sandbox.path, 'empty.artifact.js')];

const classes = loadClassesFromFiles(files, sandbox.path);
expect(classes).to.be.an.Array();
expect(classes).to.be.empty();
});

it('throws an error given a non-existent file', async () => {
const files = [resolve(SANDBOX_PATH, 'fake.artifact.js')];
const files = [resolve(sandbox.path, 'fake.artifact.js')];
expect(() => loadClassesFromFiles(files, sandbox.path)).to.throw(
/Cannot find module/,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ import {resolve} from 'path';
import {ControllerBooter, ControllerDefaults} from '../../..';

describe('controller booter unit tests', () => {
const SANDBOX_PATH = resolve(__dirname, '../../../.sandbox');
const sandbox = new TestSandbox(SANDBOX_PATH);
const sandbox = new TestSandbox(resolve(__dirname, '../../../.sandbox'));

const CONTROLLERS_PREFIX = 'controllers';
const CONTROLLERS_TAG = 'controller';
Expand All @@ -21,7 +20,7 @@ describe('controller booter unit tests', () => {
beforeEach(getApp);

it(`constructor uses ControllerDefaults for 'options' if none are given`, () => {
const booterInst = new ControllerBooter(app, SANDBOX_PATH);
const booterInst = new ControllerBooter(app, sandbox.path);
expect(booterInst.options).to.deepEqual(ControllerDefaults);
});

Expand All @@ -34,7 +33,7 @@ describe('controller booter unit tests', () => {
nested: ControllerDefaults.nested,
});

const booterInst = new ControllerBooter(app, SANDBOX_PATH, options);
const booterInst = new ControllerBooter(app, sandbox.path, options);
expect(booterInst.options).to.deepEqual(expected);
});

Expand All @@ -46,11 +45,11 @@ describe('controller booter unit tests', () => {
await sandbox.copyFile(
resolve(__dirname, '../../fixtures/multiple.artifact.js'),
);
const booterInst = new ControllerBooter(app, SANDBOX_PATH);
const booterInst = new ControllerBooter(app, sandbox.path);
const NUM_CLASSES = 2; // 2 classes in above file.

// Load uses discovered property
booterInst.discovered = [resolve(SANDBOX_PATH, 'multiple.artifact.js')];
booterInst.discovered = [resolve(sandbox.path, 'multiple.artifact.js')];
await booterInst.load();

const ctrls = app.findByTag(CONTROLLERS_TAG);
Expand Down
Loading