From be052bb1fe8830c467b7dd7546c53690698f4375 Mon Sep 17 00:00:00 2001 From: Raymond Feng Date: Mon, 15 Jun 2020 11:38:55 -0700 Subject: [PATCH 1/2] feat(testlab): allow env var KEEP_TEST_SANDBOX to keep files during delete --- .../integration/test-sandbox.integration.ts | 12 +++++++++++- packages/testlab/src/test-sandbox.ts | 7 +++++-- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/packages/testlab/src/__tests__/integration/test-sandbox.integration.ts b/packages/testlab/src/__tests__/integration/test-sandbox.integration.ts index 0981c842fea8..1c6e37647d1c 100644 --- a/packages/testlab/src/__tests__/integration/test-sandbox.integration.ts +++ b/packages/testlab/src/__tests__/integration/test-sandbox.integration.ts @@ -126,6 +126,16 @@ describe('TestSandbox integration tests', () => { expect(await pathExists(path)).to.be.False(); }); + it('keeps the test sandbox with KEEP_TEST_SANDBOX env var', async () => { + process.env.KEEP_TEST_SANDBOX = '1'; + try { + await sandbox.delete(); + expect(await pathExists(path)).to.be.true(); + } finally { + delete process.env.KEEP_TEST_SANDBOX; + } + }); + describe('after deleting sandbox', () => { const ERR = 'TestSandbox instance was deleted. Create a new instance.'; @@ -172,6 +182,6 @@ describe('TestSandbox integration tests', () => { async function deleteSandbox() { if (!(await pathExists(path))) return; - await remove(sandbox.path); + await remove(path); } }); diff --git a/packages/testlab/src/test-sandbox.ts b/packages/testlab/src/test-sandbox.ts index efe7d09bedac..8d8431fc8b36 100644 --- a/packages/testlab/src/test-sandbox.ts +++ b/packages/testlab/src/test-sandbox.ts @@ -107,10 +107,13 @@ export class TestSandbox { } /** - * Deletes the TestSandbox. + * Deletes the TestSandbox. If `KEEP_TEST_SANDBOX` env variable is set, we + * leave the sandbox directory as-is on the file system. */ async delete(): Promise { - await remove(this.path); + if (!process.env.KEEP_TEST_SANDBOX) { + await remove(this.path); + } delete this._path; } From 5d8f1f141f8c0ef0ffe00b0af17060621140ca41 Mon Sep 17 00:00:00 2001 From: Raymond Feng Date: Mon, 15 Jun 2020 11:45:32 -0700 Subject: [PATCH 2/2] fix: delete sandbox after tests --- .../application-metadata.booter.acceptance.ts | 3 + ...component-application.booter.acceptance.ts | 2 + .../controller.booter.acceptance.ts | 2 + .../crud-rest.api-builder.acceptance.ts | 2 + .../acceptance/model-api.booter.acceptance.ts | 2 + .../controller.booter.integration.ts | 2 + .../datasource.booter.integration.ts | 2 + .../interceptor.booter.integration.ts | 2 + .../lifecycle-observer.booter.integration.ts | 2 + .../integration/model.booter.integration.ts | 2 + .../repository.booter.integration.ts | 2 + .../integration/service.booter.integration.ts | 2 + .../unit/booters/booter-utils.unit.ts | 2 + .../unit/booters/controller.booter.unit.ts | 2 + .../unit/booters/datasource.booter.unit.ts | 2 + .../unit/booters/repository.booter.unit.ts | 2 + .../unit/booters/service.booter.unit.ts | 2 + .../generators/clone-example.integration.js | 2 + .../generators/controller.integration.js | 2 + .../generators/datasource.integration.js | 2 + .../import-lb3-models.integration.js | 2 + .../generators/model.integration.js | 72 ++++++++++--------- packages/cli/test/unit/update-index.unit.js | 2 + 23 files changed, 84 insertions(+), 33 deletions(-) diff --git a/packages/boot/src/__tests__/acceptance/application-metadata.booter.acceptance.ts b/packages/boot/src/__tests__/acceptance/application-metadata.booter.acceptance.ts index 3de1c8be196a..5a031775d6bf 100644 --- a/packages/boot/src/__tests__/acceptance/application-metadata.booter.acceptance.ts +++ b/packages/boot/src/__tests__/acceptance/application-metadata.booter.acceptance.ts @@ -11,6 +11,9 @@ import {BooterApp} from '../fixtures/application'; describe('application metadata booter acceptance tests', () => { let app: BooterApp; const sandbox = new TestSandbox(resolve(__dirname, '../../.sandbox')); + + after('delete sandbox', () => sandbox.delete()); + beforeEach('reset sandbox', () => sandbox.reset()); beforeEach(getApp); diff --git a/packages/boot/src/__tests__/acceptance/component-application.booter.acceptance.ts b/packages/boot/src/__tests__/acceptance/component-application.booter.acceptance.ts index 7ccce707386f..416c53a8b31b 100644 --- a/packages/boot/src/__tests__/acceptance/component-application.booter.acceptance.ts +++ b/packages/boot/src/__tests__/acceptance/component-application.booter.acceptance.ts @@ -15,6 +15,8 @@ describe('component application booter acceptance tests', () => { let app: BooterApp; const sandbox = new TestSandbox(resolve(__dirname, '../../.sandbox')); + after('delete sandbox', () => sandbox.delete()); + beforeEach('reset sandbox', () => sandbox.reset()); beforeEach(getApp); diff --git a/packages/boot/src/__tests__/acceptance/controller.booter.acceptance.ts b/packages/boot/src/__tests__/acceptance/controller.booter.acceptance.ts index dc8d2258164d..03762500ba06 100644 --- a/packages/boot/src/__tests__/acceptance/controller.booter.acceptance.ts +++ b/packages/boot/src/__tests__/acceptance/controller.booter.acceptance.ts @@ -15,6 +15,8 @@ describe('controller booter acceptance tests', () => { let app: BooterApp; const sandbox = new TestSandbox(resolve(__dirname, '../../.sandbox')); + after('delete sandbox', () => sandbox.delete()); + beforeEach('reset sandbox', () => sandbox.reset()); beforeEach(getApp); diff --git a/packages/boot/src/__tests__/acceptance/crud-rest.api-builder.acceptance.ts b/packages/boot/src/__tests__/acceptance/crud-rest.api-builder.acceptance.ts index 5cee86b9b78b..dde89167bdd4 100644 --- a/packages/boot/src/__tests__/acceptance/crud-rest.api-builder.acceptance.ts +++ b/packages/boot/src/__tests__/acceptance/crud-rest.api-builder.acceptance.ts @@ -16,6 +16,8 @@ describe('CRUD rest builder acceptance tests', () => { let app: BooterApp; const sandbox = new TestSandbox(resolve(__dirname, '../../.sandbox')); + after('delete sandbox', () => sandbox.delete()); + beforeEach('reset sandbox', () => sandbox.reset()); beforeEach(givenAppWithDataSource); diff --git a/packages/boot/src/__tests__/acceptance/model-api.booter.acceptance.ts b/packages/boot/src/__tests__/acceptance/model-api.booter.acceptance.ts index c610e517fbcd..e90dd79757a9 100644 --- a/packages/boot/src/__tests__/acceptance/model-api.booter.acceptance.ts +++ b/packages/boot/src/__tests__/acceptance/model-api.booter.acceptance.ts @@ -28,6 +28,8 @@ describe('model API booter acceptance tests', () => { let app: BooterApp; const sandbox = new TestSandbox(resolve(__dirname, '../../.sandbox')); + after('delete sandbox', () => sandbox.delete()); + beforeEach('reset sandbox', () => sandbox.reset()); beforeEach(givenAppWithDataSource); diff --git a/packages/boot/src/__tests__/integration/controller.booter.integration.ts b/packages/boot/src/__tests__/integration/controller.booter.integration.ts index 8f10b3fb47c9..653e29d8cbe7 100644 --- a/packages/boot/src/__tests__/integration/controller.booter.integration.ts +++ b/packages/boot/src/__tests__/integration/controller.booter.integration.ts @@ -16,6 +16,8 @@ describe('controller booter integration tests', () => { let app: BooterApp; + after('delete sandbox', () => sandbox.delete()); + beforeEach('reset sandbox', () => sandbox.reset()); beforeEach(getApp); diff --git a/packages/boot/src/__tests__/integration/datasource.booter.integration.ts b/packages/boot/src/__tests__/integration/datasource.booter.integration.ts index c7eb0d86d2cb..841c36b54327 100644 --- a/packages/boot/src/__tests__/integration/datasource.booter.integration.ts +++ b/packages/boot/src/__tests__/integration/datasource.booter.integration.ts @@ -15,6 +15,8 @@ describe('datasource booter integration tests', () => { let app: BooterApp; + after('delete sandbox', () => sandbox.delete()); + beforeEach('reset sandbox', () => sandbox.reset()); beforeEach(getApp); diff --git a/packages/boot/src/__tests__/integration/interceptor.booter.integration.ts b/packages/boot/src/__tests__/integration/interceptor.booter.integration.ts index 58a608eba188..b148776f7578 100644 --- a/packages/boot/src/__tests__/integration/interceptor.booter.integration.ts +++ b/packages/boot/src/__tests__/integration/interceptor.booter.integration.ts @@ -17,6 +17,8 @@ describe('interceptor script booter integration tests', () => { let app: BooterApp; + after('delete sandbox', () => sandbox.delete()); + beforeEach('reset sandbox', () => sandbox.reset()); beforeEach(buildAppWithInterceptors); diff --git a/packages/boot/src/__tests__/integration/lifecycle-observer.booter.integration.ts b/packages/boot/src/__tests__/integration/lifecycle-observer.booter.integration.ts index 933b3755c54b..020cc4741120 100644 --- a/packages/boot/src/__tests__/integration/lifecycle-observer.booter.integration.ts +++ b/packages/boot/src/__tests__/integration/lifecycle-observer.booter.integration.ts @@ -21,6 +21,8 @@ describe('lifecycle script booter integration tests', () => { let app: BooterApp; + after('delete sandbox', () => sandbox.delete()); + beforeEach('reset sandbox', () => sandbox.reset()); beforeEach(getApp); diff --git a/packages/boot/src/__tests__/integration/model.booter.integration.ts b/packages/boot/src/__tests__/integration/model.booter.integration.ts index a34c1d5aa6b5..27a03a0d66a6 100644 --- a/packages/boot/src/__tests__/integration/model.booter.integration.ts +++ b/packages/boot/src/__tests__/integration/model.booter.integration.ts @@ -14,6 +14,8 @@ describe('repository booter integration tests', () => { let app: BooterApp; + after('delete sandbox', () => sandbox.delete()); + beforeEach('reset sandbox', () => sandbox.reset()); beforeEach(getApp); diff --git a/packages/boot/src/__tests__/integration/repository.booter.integration.ts b/packages/boot/src/__tests__/integration/repository.booter.integration.ts index 3669512287fa..9cabd0321ec6 100644 --- a/packages/boot/src/__tests__/integration/repository.booter.integration.ts +++ b/packages/boot/src/__tests__/integration/repository.booter.integration.ts @@ -16,6 +16,8 @@ describe('repository booter integration tests', () => { let app: BooterApp; + after('delete sandbox', () => sandbox.delete()); + beforeEach('reset sandbox', () => sandbox.reset()); beforeEach(getApp); diff --git a/packages/boot/src/__tests__/integration/service.booter.integration.ts b/packages/boot/src/__tests__/integration/service.booter.integration.ts index a3ed37f0d473..e6c88e1cee00 100644 --- a/packages/boot/src/__tests__/integration/service.booter.integration.ts +++ b/packages/boot/src/__tests__/integration/service.booter.integration.ts @@ -15,6 +15,8 @@ describe('service booter integration tests', () => { let app: BooterApp; + after('delete sandbox', () => sandbox.delete()); + beforeEach('reset sandbox', () => sandbox.reset()); beforeEach(getApp); diff --git a/packages/boot/src/__tests__/unit/booters/booter-utils.unit.ts b/packages/boot/src/__tests__/unit/booters/booter-utils.unit.ts index 44d5ef53886d..09533baf6fe0 100644 --- a/packages/boot/src/__tests__/unit/booters/booter-utils.unit.ts +++ b/packages/boot/src/__tests__/unit/booters/booter-utils.unit.ts @@ -10,6 +10,8 @@ import {discoverFiles, isClass, loadClassesFromFiles} from '../../..'; describe('booter-utils unit tests', () => { const sandbox = new TestSandbox(resolve(__dirname, '../../../.sandbox')); + after('delete sandbox', () => sandbox.delete()); + beforeEach('reset sandbox', () => sandbox.reset()); describe('discoverFiles()', () => { diff --git a/packages/boot/src/__tests__/unit/booters/controller.booter.unit.ts b/packages/boot/src/__tests__/unit/booters/controller.booter.unit.ts index bfae7be3caf2..904885b321fb 100644 --- a/packages/boot/src/__tests__/unit/booters/controller.booter.unit.ts +++ b/packages/boot/src/__tests__/unit/booters/controller.booter.unit.ts @@ -16,6 +16,8 @@ describe('controller booter unit tests', () => { let app: Application; + after('delete sandbox', () => sandbox.delete()); + beforeEach('reset sandbox', () => sandbox.reset()); beforeEach(getApp); diff --git a/packages/boot/src/__tests__/unit/booters/datasource.booter.unit.ts b/packages/boot/src/__tests__/unit/booters/datasource.booter.unit.ts index b627a012bbf9..9d5d4eef3460 100644 --- a/packages/boot/src/__tests__/unit/booters/datasource.booter.unit.ts +++ b/packages/boot/src/__tests__/unit/booters/datasource.booter.unit.ts @@ -24,6 +24,8 @@ describe('datasource booter unit tests', () => { // eslint-disable-next-line @typescript-eslint/no-explicit-any let stub: sinon.SinonStub<[any?, ...any[]], void>; + after('delete sandbox', () => sandbox.delete()); + beforeEach('reset sandbox', () => sandbox.reset()); beforeEach(getApp); beforeEach(createStub); diff --git a/packages/boot/src/__tests__/unit/booters/repository.booter.unit.ts b/packages/boot/src/__tests__/unit/booters/repository.booter.unit.ts index 7ca4d349c9e5..28a16da94fc5 100644 --- a/packages/boot/src/__tests__/unit/booters/repository.booter.unit.ts +++ b/packages/boot/src/__tests__/unit/booters/repository.booter.unit.ts @@ -24,6 +24,8 @@ describe('repository booter unit tests', () => { // eslint-disable-next-line @typescript-eslint/no-explicit-any let stub: sinon.SinonStub<[any?, ...any[]], void>; + after('delete sandbox', () => sandbox.delete()); + beforeEach('reset sandbox', () => sandbox.reset()); beforeEach(getApp); beforeEach(createStub); diff --git a/packages/boot/src/__tests__/unit/booters/service.booter.unit.ts b/packages/boot/src/__tests__/unit/booters/service.booter.unit.ts index 60b2c77055e1..06e6a8638d30 100644 --- a/packages/boot/src/__tests__/unit/booters/service.booter.unit.ts +++ b/packages/boot/src/__tests__/unit/booters/service.booter.unit.ts @@ -21,6 +21,8 @@ describe('service booter unit tests', () => { // eslint-disable-next-line @typescript-eslint/no-explicit-any let stub: sinon.SinonStub<[any?, ...any[]], void>; + after('delete sandbox', () => sandbox.delete()); + beforeEach('reset sandbox', () => sandbox.reset()); beforeEach(getApp); beforeEach(createStub); diff --git a/packages/cli/test/integration/generators/clone-example.integration.js b/packages/cli/test/integration/generators/clone-example.integration.js index 3ae50709d88e..5ac438bacba0 100644 --- a/packages/cli/test/integration/generators/clone-example.integration.js +++ b/packages/cli/test/integration/generators/clone-example.integration.js @@ -22,6 +22,8 @@ const sandbox = new TestSandbox(path.resolve(__dirname, '../.sandbox')); describe('cloneExampleFromGitHub (SLOW)', /** @this {Mocha.Suite} */ function () { this.timeout(20000); + after('delete sandbox', () => sandbox.delete()); + beforeEach('reset sandbox', () => sandbox.reset()); it('extracts project files', async () => { diff --git a/packages/cli/test/integration/generators/controller.integration.js b/packages/cli/test/integration/generators/controller.integration.js index 685c3016ab94..5493385c253f 100644 --- a/packages/cli/test/integration/generators/controller.integration.js +++ b/packages/cli/test/integration/generators/controller.integration.js @@ -45,6 +45,8 @@ describe('controller-generator extending BaseGenerator', baseTests); describe('generator-loopback4:controller', tests); describe('lb4 controller', () => { + after('delete sandbox', () => sandbox.delete()); + beforeEach('reset sandbox', () => sandbox.reset()); it('does not run without package.json', () => { diff --git a/packages/cli/test/integration/generators/datasource.integration.js b/packages/cli/test/integration/generators/datasource.integration.js index ea5f2bae59f6..480b44c6f821 100644 --- a/packages/cli/test/integration/generators/datasource.integration.js +++ b/packages/cli/test/integration/generators/datasource.integration.js @@ -61,6 +61,8 @@ describe('datasource-generator extending BaseGenerator', baseTests); describe('generator-loopback4:datasource', tests); describe('lb4 datasource integration', () => { + after('delete sandbox', () => sandbox.delete()); + beforeEach('reset sandbox', () => sandbox.reset()); it('does not run without package.json', () => { diff --git a/packages/cli/test/integration/generators/import-lb3-models.integration.js b/packages/cli/test/integration/generators/import-lb3-models.integration.js index 999ac3fe5dcc..5e344000d935 100644 --- a/packages/cli/test/integration/generators/import-lb3-models.integration.js +++ b/packages/cli/test/integration/generators/import-lb3-models.integration.js @@ -43,6 +43,8 @@ describe('lb4 import-lb3-models', function () { return loadLb3App(COFFEE_SHOP_EXAMPLE); } + after('delete sandbox', () => sandbox.delete()); + beforeEach('reset sandbox', () => sandbox.reset()); it('imports CoffeeShop model from lb3-example app', async () => { diff --git a/packages/cli/test/integration/generators/model.integration.js b/packages/cli/test/integration/generators/model.integration.js index ce3c8e234ddf..e565145f8995 100644 --- a/packages/cli/test/integration/generators/model.integration.js +++ b/packages/cli/test/integration/generators/model.integration.js @@ -36,6 +36,8 @@ describe('model-generator extending BaseGenerator', baseTests); describe('generator-loopback4:model', tests); describe('lb4 model integration', () => { + after('delete sandbox', () => sandbox.delete()); + beforeEach('reset sandbox', () => sandbox.reset()); it('does not run without package.json', () => { @@ -261,52 +263,56 @@ describe('lb4 model integration', () => { basicModelFileChecks(expectedModelFile, expectedIndexFile); }); }); -}); - -describe('model generator using --config option', () => { - it('create models with valid json', async () => { - await testUtils - .executeGenerator(generator) - .inDir(sandbox.path, () => testUtils.givenLBProject(sandbox.path)) - .withArguments(['--config', '{"name":"test", "base":"Entity"}', '--yes']); - - basicModelFileChecks(expectedModelFile, expectedIndexFile); - }); - it('does not run if pass invalid json', () => { - return expect( - testUtils + describe('model generator using --config option', () => { + it('create models with valid json', async () => { + await testUtils .executeGenerator(generator) .inDir(sandbox.path, () => testUtils.givenLBProject(sandbox.path)) .withArguments([ '--config', - '{"name":"test", "base":"InvalidBaseModel"}', + '{"name":"test", "base":"Entity"}', '--yes', - ]), - ).to.be.rejectedWith(/Model was not found in/); - }); + ]); - describe('model generator using --config option with model settings', () => { - it('creates a model with valid settings', async () => { - await testUtils - .executeGenerator(generator) - .inDir(sandbox.path, () => testUtils.givenLBProject(sandbox.path)) - .withArguments([ - '--config', - '{"name":"test", "base":"Entity", \ + basicModelFileChecks(expectedModelFile, expectedIndexFile); + }); + + it('does not run if pass invalid json', () => { + return expect( + testUtils + .executeGenerator(generator) + .inDir(sandbox.path, () => testUtils.givenLBProject(sandbox.path)) + .withArguments([ + '--config', + '{"name":"test", "base":"InvalidBaseModel"}', + '--yes', + ]), + ).to.be.rejectedWith(/Model was not found in/); + }); + + describe('model generator using --config option with model settings', () => { + it('creates a model with valid settings', async () => { + await testUtils + .executeGenerator(generator) + .inDir(sandbox.path, () => testUtils.givenLBProject(sandbox.path)) + .withArguments([ + '--config', + '{"name":"test", "base":"Entity", \ "modelSettings": {"annotations": \ [{"destinationClass": "class1","argument": 0}],\ "foreignKeys": {"fk_destination": {"name": "fk_destination"}}},\ "allowAdditionalProperties":true}', - '--yes', - ]); + '--yes', + ]); - basicModelFileChecks(expectedModelFile, expectedIndexFile); + basicModelFileChecks(expectedModelFile, expectedIndexFile); - assert.fileContent( - expectedModelFile, - /@model\({\n {2}settings: {\n {4}annotations: \[{destinationClass: 'class1', argument: 0}],\n {4}foreignKeys: {fk_destination: {name: 'fk_destination'}},\n {4}strict: false\n {2}}\n}\)/, - ); + assert.fileContent( + expectedModelFile, + /@model\({\n {2}settings: {\n {4}annotations: \[{destinationClass: 'class1', argument: 0}],\n {4}foreignKeys: {fk_destination: {name: 'fk_destination'}},\n {4}strict: false\n {2}}\n}\)/, + ); + }); }); }); }); diff --git a/packages/cli/test/unit/update-index.unit.js b/packages/cli/test/unit/update-index.unit.js index 9053991becdf..dcbeaf976d1f 100644 --- a/packages/cli/test/unit/update-index.unit.js +++ b/packages/cli/test/unit/update-index.unit.js @@ -19,6 +19,8 @@ const sandbox = new TestSandbox(path.resolve(__dirname, '.sandbox')); const expectedFile = path.join(sandbox.path, 'index.ts'); describe('update-index unit tests', () => { + after('delete sandbox', () => sandbox.delete()); + beforeEach('reset sandbox', () => sandbox.reset()); it('creates index.ts when not present', async () => {