mirror of
https://github.com/serverless/serverless.git
synced 2026-01-18 14:58:43 +00:00
* fix #1854 - added latest version of eslint-config-airbnb-base package * fix #1854 - added latest npm shrinkwrap scheme * fix #1854 - added latest npm coveralls package * fix #1854 - added latest npm eslint package * fix #1854 - added latest npm eslint-config-airbnb package * fix #1854 - added latest npm eslint-plugin-import package * fix #1854 - added latest npm eslint-plugin-jsx-a11y package * fix #1854 - added latest npm eslint-plugin-react package * fix #1854 - fixed estlint new-parens errors for integration test * fix #1854 - fixed estlint new-parens errors for yaml parsers tests * fix #1854 - fixed estlint max-len errors for util tests * fix #1854 - disabled no-extraneous-dependencies on eslintrc for NodeJS 4 incapability on this feature * fix #1854 - fixed eslint new-parens errors for Service tests * fix #1854 - fixed eslint new-parens errors for Serverless tests * fix #1854 - fixed eslint new-parens errors for plugin manager tests * fix #1854 - fixed eslint new-parens errors for plugin tracking tests * fix #1854 - fixed eslint new-parens errors for plugin package zipService lib * fix #1854 - fixed eslint new-parens errors for plugin package zipService tests * fix #1854 - fixed eslint trailing spaces errors for plugin package zipService lib * fix #1854 - fixed eslint new-parens errors for plugin package cleanup tests * fix #1854 - fixed eslint new-parens errors for plugin create tests * fix #1854 - fixed eslint dot same line expectation error on plugin aws logs index * fix #1854 - fixed eslint operator assignment error on plugin aws logs index * fix #1854 - fixed eslint dot location error on plugin aws invoke tests * fix #1854 - fixed eslint new-parens error on plugin aws invoke tests * fix #1854 - fixed eslint new-parens error on plugin aws deployFunction tests * fix #1854 - fixed eslint new-parens error on plugin aws deploy uploadDeploymentPackage tests * fix #1854 - fixed eslint new-parens error on plugin aws deploy updateStack tests * fix #1854 - fixed eslint new-parens error on plugin aws deploy createStack tests * fix #1854 - fixed eslint new-parens error on plugin aws deploy apiGateway lib deployment * fix #1854 - fixed eslint unary typeof whitespace req error on Serverless Service class * fix #1854 - fixed eslint unary typeof whitespace req error on Serverless Service class ( second fix ) * fix #1854 - fixed eslint no-lonely-if req error on Serverless Service class * fix #1854 - disabled react/require-extension on eslintrc because it's deprecated * fix #1854 - AwsCompileApigEvents #constructor() should resolve if no functions are given: Error: Resolution method is overspecified. Specify a callback *or* return a Promise; not both. * fix #1854 - createStack #postCreate() should resolve: Error: Resolution method is overspecified. Specify a callback *or* return a Promise; not both. * fix #1854 - emptyS3Bucket #deleteObjects() should resolve if objectsInBucket is empty: Error: Resolution method is overspecified. Specify a callback *or* return a Promise; not both. * fix #1854 - AwsInvoke #extendedValidate() should resolve if path is not given: Error: Resolution method is overspecified. Specify a callback *or* return a Promise; not both. * fix #1854 - #cleanup() should resolve if the .serverless directory is not present: Error: Resolution method is overspecified. Specify a callback *or* return a Promise; not both. * fix #1854 -#validate() should resolve if servicePath is given: Error: Resolution method is overspecified. Specify a callback *or* return a Promise; not both. * fix #1854 - Service #load() should resolve if no servicePath is found: Error: Resolution method is overspecified. Specify a callback *or* return a Promise; not both. * fix #1854 - added latest mocha package * fix #1854 - added latest sinon npm package * fix #1854 - serverless/lib/plugins/aws/deploy/tests/createStack.js 136:48 error Missing semicolon semi * fix #1854 - serverless/lib/plugins/package/tests/cleanup.js 35:7 error Missing semicolon semi * fix #1854 - serverless/lib/plugins/package/tests/validate.js 22:49 error Missing semicolon semi * fix #1854 - added latest npm shrinkwrap * fix #1854 - fixed no-extra-boolean-cast eslint error on aws deploy apiGateway methods * fix #1854 - fixed new-parens eslint error on serverless tests for Service class
243 lines
7.8 KiB
JavaScript
243 lines
7.8 KiB
JavaScript
'use strict';
|
|
|
|
const path = require('path');
|
|
const os = require('os');
|
|
const expect = require('chai').expect;
|
|
const Serverless = require('../../lib/Serverless');
|
|
|
|
const serverless = new Serverless();
|
|
|
|
describe('Utils', () => {
|
|
describe('#dirExistsSync()', () => {
|
|
describe('When reading a directory', () => {
|
|
it('should detect if a directory exists', () => {
|
|
const dir = serverless.utils.dirExistsSync(__dirname);
|
|
expect(dir).to.equal(true);
|
|
});
|
|
|
|
it('should detect if a directory doesn\'t exist', () => {
|
|
const noDir = serverless.utils.dirExistsSync(path.join(__dirname, '..', 'XYZ'));
|
|
expect(noDir).to.equal(false);
|
|
});
|
|
});
|
|
});
|
|
|
|
describe('#fileExistsSync()', () => {
|
|
describe('When reading a file', () => {
|
|
it('should detect if a file exists', () => {
|
|
const file = serverless.utils.fileExistsSync(__filename);
|
|
expect(file).to.equal(true);
|
|
});
|
|
|
|
it('should detect if a file doesn\'t exist', () => {
|
|
const noFile = serverless.utils.fileExistsSync(path.join(__dirname, 'XYZ.json'));
|
|
expect(noFile).to.equal(false);
|
|
});
|
|
});
|
|
});
|
|
|
|
describe('#writeFileSync()', () => {
|
|
it('should write a .json file synchronously', () => {
|
|
const tmpFilePath = path.join(
|
|
os.tmpdir(),
|
|
(new Date()).getTime().toString(),
|
|
'anything.json'
|
|
);
|
|
|
|
serverless.utils.writeFileSync(tmpFilePath, { foo: 'bar' });
|
|
const obj = serverless.utils.readFileSync(tmpFilePath);
|
|
|
|
expect(obj.foo).to.equal('bar');
|
|
});
|
|
|
|
it('should write a .yml file synchronously', () => {
|
|
const tmpFilePath = path.join(
|
|
os.tmpdir(),
|
|
(new Date()).getTime().toString(),
|
|
'anything.yml'
|
|
);
|
|
|
|
serverless.utils.writeFileSync(tmpFilePath, { foo: 'bar' });
|
|
|
|
return serverless.yamlParser.parse(tmpFilePath).then((obj) => {
|
|
expect(obj.foo).to.equal('bar');
|
|
});
|
|
});
|
|
|
|
it('should write a .yaml file synchronously', () => {
|
|
const tmpFilePath = path.join(
|
|
os.tmpdir(),
|
|
(new Date()).getTime().toString(),
|
|
'anything.yaml'
|
|
);
|
|
|
|
serverless.utils.writeFileSync(tmpFilePath, { foo: 'bar' });
|
|
|
|
return serverless.yamlParser.parse(tmpFilePath).then((obj) => {
|
|
expect(obj.foo).to.equal('bar');
|
|
});
|
|
});
|
|
|
|
it('should throw error if invalid path is provided', () => {
|
|
expect(() => { serverless.utils.writeFileSync(null); }).to.throw(Error);
|
|
});
|
|
});
|
|
|
|
describe('#writeFile()', () => {
|
|
it('should write a file asynchronously', () => {
|
|
const tmpFilePath = path.join(
|
|
os.tmpdir(),
|
|
(new Date()).getTime().toString(),
|
|
'anything.json'
|
|
);
|
|
|
|
// note: use return when testing promises otherwise you'll have unhandled rejection errors
|
|
return serverless.utils.writeFile(tmpFilePath, { foo: 'bar' }).then(() => {
|
|
const obj = serverless.utils.readFileSync(tmpFilePath);
|
|
|
|
expect(obj.foo).to.equal('bar');
|
|
});
|
|
});
|
|
});
|
|
|
|
describe('#readFileSync()', () => {
|
|
it('should read a file synchronously', () => {
|
|
const tmpFilePath = path.join(
|
|
os.tmpdir(),
|
|
(new Date()).getTime().toString(),
|
|
'anything.json'
|
|
);
|
|
|
|
serverless.utils.writeFileSync(tmpFilePath, { foo: 'bar' });
|
|
const obj = serverless.utils.readFileSync(tmpFilePath);
|
|
|
|
expect(obj.foo).to.equal('bar');
|
|
});
|
|
});
|
|
|
|
describe('#readFile()', () => {
|
|
it('should read a file asynchronously', () => {
|
|
const tmpFilePath = path.join(
|
|
os.tmpdir(),
|
|
(new Date()).getTime().toString(),
|
|
'anything.json'
|
|
);
|
|
|
|
serverless.utils.writeFileSync(tmpFilePath, { foo: 'bar' });
|
|
|
|
// note: use return when testing promises otherwise you'll have unhandled rejection errors
|
|
return serverless.utils.readFile(tmpFilePath).then((obj) => {
|
|
expect(obj.foo).to.equal('bar');
|
|
});
|
|
});
|
|
});
|
|
|
|
describe('#walkDirSync()', () => {
|
|
it('should return an array with corresponding paths to the found files', () => {
|
|
const tmpDirPath = path.join(os.tmpDir(), (new Date()).getTime().toString());
|
|
|
|
const nestedDir1 = path.join(tmpDirPath, 'foo');
|
|
const nestedDir2 = path.join(tmpDirPath, 'foo', 'bar');
|
|
const nestedDir3 = path.join(tmpDirPath, 'baz');
|
|
|
|
const tmpFilePath1 = path.join(nestedDir1, 'foo.js');
|
|
const tmpFilePath2 = path.join(nestedDir2, 'bar.js');
|
|
const tmpFilePath3 = path.join(nestedDir3, 'baz.js');
|
|
|
|
serverless.utils.writeFileSync(tmpFilePath1, 'foo');
|
|
serverless.utils.writeFileSync(tmpFilePath2, 'bar');
|
|
serverless.utils.writeFileSync(tmpFilePath3, 'baz');
|
|
|
|
const filePaths = serverless.utils.walkDirSync(tmpDirPath);
|
|
|
|
expect(filePaths).to.include(tmpFilePath1);
|
|
expect(filePaths).to.include(tmpFilePath2);
|
|
expect(filePaths).to.include(tmpFilePath3);
|
|
});
|
|
});
|
|
|
|
describe('#copyDirContentsSync()', () => {
|
|
it('recursively copy directory files', () => {
|
|
const tmpSrcDirPath = path.join(process.cwd(), 'testSrc');
|
|
const tmpDestDirPath = path.join(process.cwd(), 'testDest');
|
|
|
|
const srcFile1 = path.join(tmpSrcDirPath, 'file1.txt');
|
|
const srcFile2 = path.join(tmpSrcDirPath, 'folder', 'file2.txt');
|
|
const srcFile3 = path.join(tmpSrcDirPath, 'folder', 'folder', 'file3.txt');
|
|
|
|
const destFile1 = path.join(tmpDestDirPath, 'file1.txt');
|
|
const destFile2 = path.join(tmpDestDirPath, 'folder', 'file2.txt');
|
|
const destFile3 = path.join(tmpDestDirPath, 'folder', 'folder', 'file3.txt');
|
|
|
|
serverless.utils.writeFileSync(srcFile1, 'foo');
|
|
serverless.utils.writeFileSync(srcFile2, 'foo');
|
|
serverless.utils.writeFileSync(srcFile3, 'foo');
|
|
|
|
serverless.utils.copyDirContentsSync(tmpSrcDirPath, tmpDestDirPath);
|
|
|
|
expect(serverless.utils.fileExistsSync(destFile1)).to.equal(true);
|
|
expect(serverless.utils.fileExistsSync(destFile2)).to.equal(true);
|
|
expect(serverless.utils.fileExistsSync(destFile3)).to.equal(true);
|
|
});
|
|
});
|
|
|
|
describe('#generateShortId()', () => {
|
|
it('should generate a shortId', () => {
|
|
const id = serverless.utils.generateShortId();
|
|
expect(id).to.be.a('string');
|
|
});
|
|
|
|
it('should generate a shortId for the given length', () => {
|
|
const id = serverless.utils.generateShortId(6);
|
|
expect(id.length).to.be.equal(6);
|
|
});
|
|
});
|
|
|
|
describe('#findServicePath()', () => {
|
|
const testDir = process.cwd();
|
|
|
|
it('should detect if the CWD is a service directory when using Serverless .yaml files', () => {
|
|
const tmpDirPath = path.join(os.tmpdir(), (new Date()).getTime().toString());
|
|
const tmpFilePath = path.join(tmpDirPath, 'serverless.yaml');
|
|
|
|
serverless.utils.writeFileSync(tmpFilePath, 'foo');
|
|
process.chdir(tmpDirPath);
|
|
|
|
const servicePath = serverless.utils.findServicePath();
|
|
|
|
expect(servicePath).to.not.equal(null);
|
|
});
|
|
|
|
it('should detect if the CWD is a service directory when using Serverless .yml files', () => {
|
|
const tmpDirPath = path.join(os.tmpdir(), (new Date()).getTime().toString());
|
|
const tmpFilePath = path.join(tmpDirPath, 'serverless.yml');
|
|
|
|
serverless.utils.writeFileSync(tmpFilePath, 'foo');
|
|
process.chdir(tmpDirPath);
|
|
|
|
const servicePath = serverless.utils.findServicePath();
|
|
|
|
expect(servicePath).to.not.equal(null);
|
|
});
|
|
|
|
it('should detect if the CWD is not a service directory', () => {
|
|
// just use the root of the tmpdir because findServicePath will
|
|
// also check parent directories (and may find matching tmp dirs
|
|
// from old tests)
|
|
const tmpDirPath = os.tmpdir();
|
|
process.chdir(tmpDirPath);
|
|
|
|
const servicePath = serverless.utils.findServicePath();
|
|
|
|
expect(servicePath).to.equal(null);
|
|
});
|
|
|
|
afterEach(() => {
|
|
// always switch back to the test directory
|
|
// so that we have a clean state
|
|
process.chdir(testDir);
|
|
});
|
|
});
|
|
});
|
|
|