From 5c9a607987374b4bf5d937afe25543410bb26e1f Mon Sep 17 00:00:00 2001 From: Philipp Muens Date: Wed, 3 May 2017 12:31:28 +0200 Subject: [PATCH] Improve create plugin tests --- lib/plugins/create/create.test.js | 93 +++---------------------------- 1 file changed, 7 insertions(+), 86 deletions(-) diff --git a/lib/plugins/create/create.test.js b/lib/plugins/create/create.test.js index 084854003..2d41558b1 100644 --- a/lib/plugins/create/create.test.js +++ b/lib/plugins/create/create.test.js @@ -37,9 +37,16 @@ describe('Create', () => { describe('#create()', () => { let tmpDir; + let cwd; beforeEach(() => { tmpDir = testUtils.getTmpDirPath(); + fse.mkdirsSync(tmpDir); + cwd = process.cwd(); + }); + + afterEach(() => { + process.chdir(cwd); }); it('should throw error if user passed unsupported template', () => { @@ -48,8 +55,6 @@ describe('Create', () => { }); it('should overwrite the name for the service if user passed name', () => { - const cwd = process.cwd(); - fse.mkdirsSync(tmpDir); process.chdir(tmpDir); create.options.template = 'aws-nodejs'; create.options.name = 'my_service'; @@ -59,37 +64,28 @@ describe('Create', () => { path.join(tmpDir, 'serverless.yml') ).then((obj) => { expect(obj.service).to.equal('my_service'); - process.chdir(cwd); }) ); }); it('should set servicePath based on cwd', () => { - const cwd = process.cwd(); - fse.mkdirsSync(tmpDir); process.chdir(tmpDir); create.options.template = 'aws-nodejs'; return create.create().then(() => { expect(create.serverless.config.servicePath).to.be.equal(process.cwd()); - process.chdir(cwd); }); }); it('should display ascii greeting', () => { - const cwd = process.cwd(); - fse.mkdirsSync(tmpDir); process.chdir(tmpDir); const greetingStub = sinon.stub(create.serverless.cli, 'asciiGreeting'); return create.create().then(() => { expect(greetingStub.callCount).to.be.equal(1); - process.chdir(cwd); }); }); it('should generate scaffolding for "aws-nodejs" template', () => { - const cwd = process.cwd(); - fse.mkdirsSync(tmpDir); process.chdir(tmpDir); create.options.template = 'aws-nodejs'; @@ -100,14 +96,10 @@ describe('Create', () => { .to.be.equal(true); expect(create.serverless.utils.fileExistsSync(path.join(tmpDir, '.gitignore'))) .to.be.equal(true); - - process.chdir(cwd); }); }); it('should generate scaffolding for "aws-csharp" template', () => { - const cwd = process.cwd(); - fse.mkdirsSync(tmpDir); process.chdir(tmpDir); create.options.template = 'aws-csharp'; @@ -126,14 +118,10 @@ describe('Create', () => { .to.be.equal(true); expect(create.serverless.utils.fileExistsSync(path.join(tmpDir, 'project.json'))) .to.be.equal(true); - - process.chdir(cwd); }); }); it('should generate scaffolding for "aws-python" template', () => { - const cwd = process.cwd(); - fse.mkdirsSync(tmpDir); process.chdir(tmpDir); create.options.template = 'aws-python'; @@ -144,14 +132,10 @@ describe('Create', () => { .to.be.equal(true); expect(create.serverless.utils.fileExistsSync(path.join(tmpDir, '.gitignore'))) .to.be.equal(true); - - process.chdir(cwd); }); }); it('should generate scaffolding for "aws-python3" template', () => { - const cwd = process.cwd(); - fse.mkdirsSync(tmpDir); process.chdir(tmpDir); create.options.template = 'aws-python3'; @@ -162,14 +146,10 @@ describe('Create', () => { .to.be.equal(true); expect(create.serverless.utils.fileExistsSync(path.join(tmpDir, '.gitignore'))) .to.be.equal(true); - - process.chdir(cwd); }); }); it('should generate scaffolding for "aws-java-maven" template', () => { - const cwd = process.cwd(); - fse.mkdirsSync(tmpDir); process.chdir(tmpDir); create.options.template = 'aws-java-maven'; @@ -196,14 +176,10 @@ describe('Create', () => { .to.be.equal(true); expect(create.serverless.utils.fileExistsSync(path.join(tmpDir, '.gitignore'))) .to.be.equal(true); - - process.chdir(cwd); }); }); it('should generate scaffolding for "aws-java-gradle" template', () => { - const cwd = process.cwd(); - fse.mkdirsSync(tmpDir); process.chdir(tmpDir); create.options.template = 'aws-java-gradle'; @@ -240,14 +216,10 @@ describe('Create', () => { .to.be.equal(true); expect(create.serverless.utils.fileExistsSync(path.join(tmpDir, '.gitignore'))) .to.be.equal(true); - - process.chdir(cwd); }); }); it('should generate scaffolding for "aws-groovy-gradle" template', () => { - const cwd = process.cwd(); - fse.mkdirsSync(tmpDir); process.chdir(tmpDir); create.options.template = 'aws-groovy-gradle'; @@ -284,14 +256,10 @@ describe('Create', () => { .to.be.equal(true); expect(create.serverless.utils.fileExistsSync(path.join(tmpDir, '.gitignore'))) .to.be.equal(true); - - process.chdir(cwd); }); }); it('should generate scaffolding for "aws-scala-sbt" template', () => { - const cwd = process.cwd(); - fse.mkdirsSync(tmpDir); process.chdir(tmpDir); create.options.template = 'aws-scala-sbt'; @@ -314,14 +282,10 @@ describe('Create', () => { .to.be.equal(true); expect(create.serverless.utils.fileExistsSync(path.join(tmpDir, '.gitignore'))) .to.be.equal(true); - - process.chdir(cwd); }); }); it('should generate scaffolding for "openwhisk-nodejs" template', () => { - const cwd = process.cwd(); - fse.mkdirsSync(tmpDir); process.chdir(tmpDir); create.options.template = 'openwhisk-nodejs'; @@ -334,14 +298,10 @@ describe('Create', () => { .to.be.equal(true); expect(create.serverless.utils.fileExistsSync(path.join(tmpDir, '.gitignore'))) .to.be.equal(true); - - process.chdir(cwd); }); }); it('should generate scaffolding for "openwhisk-python" template', () => { - const cwd = process.cwd(); - fse.mkdirsSync(tmpDir); process.chdir(tmpDir); create.options.template = 'openwhisk-python'; @@ -354,14 +314,10 @@ describe('Create', () => { .to.be.equal(true); expect(create.serverless.utils.fileExistsSync(path.join(tmpDir, '.gitignore'))) .to.be.equal(true); - - process.chdir(cwd); }); }); it('should generate scaffolding for "openwhisk-swift" template', () => { - const cwd = process.cwd(); - fse.mkdirsSync(tmpDir); process.chdir(tmpDir); create.options.template = 'openwhisk-swift'; @@ -374,14 +330,10 @@ describe('Create', () => { .to.be.equal(true); expect(create.serverless.utils.fileExistsSync(path.join(tmpDir, '.gitignore'))) .to.be.equal(true); - - process.chdir(cwd); }); }); it('should generate scaffolding for "azure-nodejs" template', () => { - const cwd = process.cwd(); - fse.mkdirsSync(tmpDir); process.chdir(tmpDir); create.options.template = 'azure-nodejs'; @@ -394,28 +346,20 @@ describe('Create', () => { .to.be.equal(true); expect(create.serverless.utils.fileExistsSync(path.join(tmpDir, '.gitignore'))) .to.be.equal(true); - - process.chdir(cwd); }); }); it('should generate scaffolding for "plugin" template', () => { - const cwd = process.cwd(); - fse.mkdirsSync(tmpDir); process.chdir(tmpDir); create.options.template = 'plugin'; return create.create().then(() => { expect(create.serverless.utils.fileExistsSync(path.join(tmpDir, 'index.js'))) .to.be.equal(true); - - process.chdir(cwd); }); }); it('should generate scaffolding for "hello-world" template', () => { - const cwd = process.cwd(); - fse.mkdirsSync(tmpDir); process.chdir(tmpDir); create.options.template = 'hello-world'; @@ -426,16 +370,12 @@ describe('Create', () => { .to.be.equal(true); expect(create.serverless.utils.fileExistsSync(path.join(tmpDir, '.gitignore'))) .to.be.equal(true); - - process.chdir(cwd); }); }); // this test should live here because of process.cwd() which might cause trouble when using // nested dirs like its done here it('should create a plugin in the current directory', () => { - const cwd = process.cwd(); - fse.mkdirsSync(tmpDir); process.chdir(tmpDir); // using the nodejs template (this test is completely be independent from the template) @@ -447,16 +387,12 @@ describe('Create', () => { // check if files are created in the correct directory expect(create.serverless.utils.fileExistsSync( path.join(serviceDir, 'index.js'))).to.be.equal(true); - - process.chdir(cwd); }); }); // this test should live here because of process.cwd() which might cause trouble when using // nested dirs like its done here it('should create a renamed service in the directory if using the "path" option', () => { - const cwd = process.cwd(); - fse.mkdirsSync(tmpDir); process.chdir(tmpDir); create.options.path = 'my-new-service'; @@ -479,14 +415,10 @@ describe('Create', () => { .readFileSync(path.join(serviceDir, 'serverless.yml')).toString(); expect((/service: my-new-service/).test(serverlessYmlfileContent)).to.equal(true); - - process.chdir(cwd); }); }); it('should create a service in the directory if using the "path" option with digits', () => { - const cwd = process.cwd(); - fse.mkdirsSync(tmpDir); process.chdir(tmpDir); create.options.path = 123; @@ -503,15 +435,11 @@ describe('Create', () => { path.join(serviceDir, 'serverless.yml'))).to.be.equal(true); expect(create.serverless.utils.fileExistsSync( path.join(serviceDir, 'handler.js'))).to.be.equal(true); - - process.chdir(cwd); }); }); it('should create a custom renamed service in the directory if using ' + 'the "path" and "name" option', () => { - const cwd = process.cwd(); - fse.mkdirsSync(tmpDir); process.chdir(tmpDir); create.options.path = 'my-new-service'; @@ -534,14 +462,10 @@ describe('Create', () => { .readFileSync(path.join(serviceDir, 'serverless.yml')).toString(); expect((/service: my-custom-new-service/).test(serverlessYmlfileContent)).to.equal(true); - - process.chdir(cwd); }); }); it('should throw error if there are existing template files in cwd', () => { - const cwd = process.cwd(); - fse.mkdirsSync(tmpDir); process.chdir(tmpDir); // create existing files from nodejs template @@ -553,20 +477,17 @@ describe('Create', () => { expect(create.serverless.utils.fileExistsSync( path.join(tmpDir, 'serverless.yml'))).to.be.equal(true); expect(() => create.create()).to.throw(Error); - process.chdir(cwd); }); it('should throw error if the directory for the service already exists in cwd', () => { create.options.path = 'my-service'; create.options.template = 'aws-nodejs'; - const cwd = process.cwd(); // create a directory with the pathname fse.mkdirsSync(path.join(tmpDir, create.options.path)); process.chdir(tmpDir); expect(() => create.create()).to.throw(Error); - process.chdir(cwd); }); }); });