From 9c3de2afefdad11d261e44a728de906d2f6264cd Mon Sep 17 00:00:00 2001 From: Philipp Muens Date: Sat, 14 May 2016 09:39:44 +0200 Subject: [PATCH] Refactor Utils class tests and add YamlParser class --- lib/classes/Utils.js | 33 ----- lib/classes/YamlParser.js | 42 ++++++ tests/all.js | 1 + tests/tests/classes/Utils.js | 209 +++++++++++------------------- tests/tests/classes/YamlParser.js | 94 ++++++++++++++ 5 files changed, 211 insertions(+), 168 deletions(-) create mode 100644 lib/classes/YamlParser.js create mode 100644 tests/tests/classes/YamlParser.js diff --git a/lib/classes/Utils.js b/lib/classes/Utils.js index 4012016a6..9920b781f 100644 --- a/lib/classes/Utils.js +++ b/lib/classes/Utils.js @@ -5,7 +5,6 @@ const SError = require('./Error'), path = require('path'), traverse = require('traverse'), replaceall = require('replaceall'), - resolve = require('json-refs').resolveRefs, YAML = require('js-yaml'), dotenv = require('dotenv'), rawDebug = require('debug'), @@ -20,7 +19,6 @@ module.exports = function(S) { class Utils { - constructor() { this._class = 'Utils'; } @@ -312,7 +310,6 @@ module.exports = function(S) { return servicePath; } - getFunctionsByCwd(allFunctions) { // we add a trailing slash to notate that it's the end of the folder name // this is just to avoid matching sub folder names that are substrings of other subfolder names @@ -329,36 +326,6 @@ module.exports = function(S) { return functions; } - - /* - * - Reads and parses a yaml file: - * - resolves json-ref for json files - * - resolves json-ref for yaml files - * - resolves json-ref recursively - * - * @param yamlFilePath {string} - path to the yaml file - * @returns {object} - JS object literal representing the resolved yml - */ - parseYaml(yamlFilePath) { - let parentDir = yamlFilePath.split(path.sep); - parentDir.pop(); - parentDir = parentDir.join('/'); - process.chdir(parentDir); - var root = YAML.load(this.readFileSync(yamlFilePath).toString()); - var options = { - filter : ['relative', 'remote'], - loaderOptions: { - processContent: function (res, callback) { - callback(null, YAML.load(res.text)); - } - } - }; - return resolve(root, options).then(function (res) { - return BbPromise.resolve(res.resolved) - }) - } - - } return Utils; diff --git a/lib/classes/YamlParser.js b/lib/classes/YamlParser.js new file mode 100644 index 000000000..f1bbb4ef8 --- /dev/null +++ b/lib/classes/YamlParser.js @@ -0,0 +1,42 @@ +'use strict' + +const path = require('path'); +const YAML = require('js-yaml'); +const resolve = require('json-refs').resolveRefs; +const BbPromise = require('bluebird'); +const Utils = require('../classes/Utils')({}); + +module.exports = function (S) { + + class YamlParser { + + constructor() { + this._class = 'YamlParser'; + } + + parseYaml(yamlFilePath) { + const SUtils = new Utils(); + + let parentDir = yamlFilePath.split(path.sep); + parentDir.pop(); + parentDir = parentDir.join('/'); + process.chdir(parentDir); + + const root = YAML.load(SUtils.readFileSync(yamlFilePath).toString()); + const options = { + filter : ['relative', 'remote'], + loaderOptions: { + processContent: function (res, callback) { + callback(null, YAML.load(res.text)); + }, + }, + }; + return resolve(root, options).then(function (res) { + return BbPromise.resolve(res.resolved); + }); + } + } + + return YamlParser; + +}; diff --git a/tests/all.js b/tests/all.js index e566f63fc..adc0928bf 100644 --- a/tests/all.js +++ b/tests/all.js @@ -8,6 +8,7 @@ describe('All Tests', function() { after(function() {}); require('./tests/classes/Utils'); + require('./tests/classes/YamlParser'); //require('./tests/classes/Plugin'); // require('./tests/classes/Project'); // require('./tests/classes/ProviderAws'); diff --git a/tests/tests/classes/Utils.js b/tests/tests/classes/Utils.js index 765beb3d2..a31353de7 100644 --- a/tests/tests/classes/Utils.js +++ b/tests/tests/classes/Utils.js @@ -6,178 +6,117 @@ const path = require('path'); const os = require('os'); -const YAML = require('js-yaml'); -const assert = require('chai').assert; const expect = require('chai').expect; const Utils = require('../../../lib/classes/Utils')({}); +const YamlParser = require('../../../lib/classes/YamlParser')({}); const SUtils = new Utils(); +const SYamlParser = new YamlParser(); -describe('Utils class', () => { +describe('Utils', () => { - after((done) => { - done(); - }); + describe('#exportObject()', () => { + it('should export an object', () => { + const data = { + _class: 'SampleClass', + publicProp: 'somethingPublic', + functionProp: () => { + }, + }; - it('should export an object', () => { - const data = { - _class: 'SampleClass', - publicProp: 'somethingPublic', - functionProp: () => { - } - }; - - const Obj = SUtils.exportObject(data); - assert.equal(Obj.publicProp, 'somethingPublic'); - assert.equal(typeof Obj._class, 'undefined'); - assert.equal(typeof Obj.functionProp, 'undefined'); - }); - - it('should generate a shortId', () => { - const id = SUtils.generateShortId(6); - assert.equal(typeof id, 'string'); - assert.equal(id.length, 6); - }); - - it('should check if a directory exists synchronously', () => { - const dir = SUtils.dirExistsSync(__dirname); - const noDir = SUtils.dirExistsSync(path.join(__dirname, '..', 'XYZ')); - - assert.equal(dir, true); - assert.equal(noDir, false); - }); - - it('should check if a file exists synchronously', () => { - const file = SUtils.fileExistsSync(__filename); - const noFile = SUtils.fileExistsSync(path.join(__dirname, 'XYZ.json')); - - assert.equal(file, true); - assert.equal(noFile, false); - }); - - it('should write a file synchronously', () => { - let tmpFilePath = path.join(os.tmpdir(), (new Date).getTime().toString(), 'anything.json'); - - SUtils.writeFileSync(tmpFilePath, { foo: 'bar' }); - let obj = SUtils.readFileSync(tmpFilePath); - - assert.equal(obj.foo, 'bar'); - }); - - it('should write a yaml file synchronously', () => { - let tmpFilePath = path.join(os.tmpdir(), (new Date).getTime().toString(), 'anything.yaml'); - - SUtils.writeFileSync(tmpFilePath, { foo: 'bar' }); - - return SUtils.parseYaml(tmpFilePath).then((obj) => { - expect(obj.foo).to.equal('bar'); + const Obj = SUtils.exportObject(data); + expect(Obj.publicProp).to.equal('somethingPublic'); + expect(Obj._class).to.be.an('undefined'); + expect(Obj.functionProp).to.be.an('undefined'); }); }); - it('should write a file asynchronously', () => { - let tmpFilePath = path.join(os.tmpdir(), (new Date).getTime().toString(), 'anything.json'); + describe('#generateShortId()', () => { + it('should generate a shortId', () => { + const id = SUtils.generateShortId(6); + expect(id).to.be.a('string'); + }); - // note: use return when testing promises otherwise you'll have unhandled rejection errors - return SUtils.writeFile(tmpFilePath, { foo: 'bar' }).then(() => { - let obj = SUtils.readFileSync(tmpFilePath); - - expect(obj.foo).to.equal('bar'); + it('should generate a shortId for the given length', () => { + const id = SUtils.generateShortId(6); + expect(id.length).to.equal(6); }); }); - it('should read a file synchronously', () => { - let tmpFilePath = path.join(os.tmpdir(), (new Date).getTime().toString(), 'anything.json'); + describe('#dirExistsSync()', () => { + it('should check if a directory exists synchronously', () => { + const dir = SUtils.dirExistsSync(__dirname); + const noDir = SUtils.dirExistsSync(path.join(__dirname, '..', 'XYZ')); - SUtils.writeFileSync(tmpFilePath, { foo: 'bar' }); - let obj = SUtils.readFileSync(tmpFilePath); - - assert.equal(obj.foo, 'bar'); - }); - - it('should read a file asynchronously', () => { - let tmpFilePath = path.join(os.tmpdir(), (new Date).getTime().toString(), 'anything.json'); - - SUtils.writeFileSync(tmpFilePath, { foo: 'bar' }); - - // note: use return when testing promises otherwise you'll have unhandled rejection errors - return SUtils.readFile(tmpFilePath).then((obj) => { - expect(obj.foo).to.equal('bar'); + expect(dir).to.equal(true); + expect(noDir).to.equal(false); }); }); + describe('#fileExistsSync()', () => { + it('should check if a file exists synchronously', () => { + const file = SUtils.fileExistsSync(__filename); + const noFile = SUtils.fileExistsSync(path.join(__dirname, 'XYZ.json')); - describe('YAML Parser', () => { + expect(file).to.equal(true); + expect(noFile).to.equal(false); + }); + }); - it('should parse a simple yaml file', () => { - let tmpFilePath = path.join(os.tmpdir(), (new Date).getTime().toString(), 'simple.yml'); + describe('#writeFileSync()', () => { + it('should write a .json file synchronously', () => { + const tmpFilePath = path.join(os.tmpdir(), (new Date).getTime().toString(), 'anything.json'); - SUtils.writeFileSync(tmpFilePath, YAML.dump({ foo: 'bar' })); + SUtils.writeFileSync(tmpFilePath, { foo: 'bar' }); + const obj = SUtils.readFileSync(tmpFilePath); - return SUtils.parseYaml(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'); + + SUtils.writeFileSync(tmpFilePath, { foo: 'bar' }); + + return SYamlParser.parseYaml(tmpFilePath).then((obj) => { expect(obj.foo).to.equal('bar'); }); }); + }); - it('should parse a yaml file with JSON-REF to yaml', () => { - let tmpDirPath = path.join(os.tmpdir(), (new Date).getTime().toString()); + 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 SUtils.writeFile(tmpFilePath, { foo: 'bar' }).then(() => { + const obj = SUtils.readFileSync(tmpFilePath); - SUtils.writeFileSync(path.join(tmpDirPath, 'ref.yaml'), { foo: 'bar' }); - - let testYaml = { - main: { - $ref: './ref.yaml' - } - }; - - SUtils.writeFileSync(path.join(tmpDirPath, 'test.yaml'), testYaml); - - return SUtils.parseYaml(path.join(tmpDirPath, 'test.yaml')).then((obj) => { - expect(obj.main.foo).to.equal('bar'); + expect(obj.foo).to.equal('bar'); }); }); + }); - it('should parse a yaml file with JSON-REF to json', () => { - let tmpDirPath = path.join(os.tmpdir(), (new Date).getTime().toString()); + describe('#readFileSyn()', () => { + it('should read a file synchronously', () => { + const tmpFilePath = path.join(os.tmpdir(), (new Date).getTime().toString(), 'anything.json'); - SUtils.writeFileSync(path.join(tmpDirPath, 'ref.json'), { foo: 'bar' }); + SUtils.writeFileSync(tmpFilePath, { foo: 'bar' }); + const obj = SUtils.readFileSync(tmpFilePath); - let testYaml = { - main: { - $ref: './ref.json' - } - }; - - SUtils.writeFileSync(path.join(tmpDirPath, 'test.yaml'), testYaml); - - return SUtils.parseYaml(path.join(tmpDirPath, 'test.yaml')).then((obj) => { - expect(obj.main.foo).to.equal('bar'); - }); + expect(obj.foo).to.equal('bar'); }); + }); - it('should parse yaml file with recursive JSON-REF', () => { - let tmpDirPath = path.join(os.tmpdir(), (new Date).getTime().toString()); + describe('#readFile()', () => { + it('should read a file asynchronously', () => { + const tmpFilePath = path.join(os.tmpdir(), (new Date).getTime().toString(), 'anything.json'); - SUtils.writeFileSync(path.join(tmpDirPath, 'three.yaml'), { foo: 'bar' }); + SUtils.writeFileSync(tmpFilePath, { foo: 'bar' }); - let twoYaml = { - two: { - $ref: './three.yaml' - } - }; - - SUtils.writeFileSync(path.join(tmpDirPath, 'two.yaml'), twoYaml); - - let oneYaml = { - one: { - $ref: './two.yaml' - } - }; - - SUtils.writeFileSync(path.join(tmpDirPath, 'one.yaml'), oneYaml); - - return SUtils.parseYaml(path.join(tmpDirPath, 'one.yaml')).then((obj) => { - expect(obj.one.two.foo).to.equal('bar'); + // note: use return when testing promises otherwise you'll have unhandled rejection errors + return SUtils.readFile(tmpFilePath).then((obj) => { + expect(obj.foo).to.equal('bar'); }); }); }); diff --git a/tests/tests/classes/YamlParser.js b/tests/tests/classes/YamlParser.js new file mode 100644 index 000000000..121e2b771 --- /dev/null +++ b/tests/tests/classes/YamlParser.js @@ -0,0 +1,94 @@ +'use strict'; + +/** + * Test: YamlParser Function Class + */ + +const expect = require('chai').expect; +const YAML = require('js-yaml'); +const path = require('path'); +const os = require('os'); +const YamlParser = require('../../../lib/classes/YamlParser')({}); +const Utils = require('../../../lib/classes/Utils')({}); + +const SUtils = new Utils(); +const SYamlParser = new YamlParser(); + +describe('YamlParser', () => { + + describe('#parseYaml()', () => { + + it('should parse a simple yaml file', () => { + const tmpFilePath = path.join(os.tmpdir(), (new Date).getTime().toString(), 'simple.yml'); + + SUtils.writeFileSync(tmpFilePath, YAML.dump({ foo: 'bar' })); + + return SYamlParser.parseYaml(tmpFilePath).then((obj) => { + expect(obj.foo).to.equal('bar'); + }); + }); + + it('should parse a yaml file with JSON-REF to yaml', () => { + const tmpDirPath = path.join(os.tmpdir(), (new Date).getTime().toString()); + + SUtils.writeFileSync(path.join(tmpDirPath, 'ref.yaml'), { foo: 'bar' }); + + const testYaml = { + main: { + $ref: './ref.yaml', + }, + }; + + SUtils.writeFileSync(path.join(tmpDirPath, 'test.yaml'), testYaml); + + return SYamlParser.parseYaml(path.join(tmpDirPath, 'test.yaml')).then((obj) => { + expect(obj.main.foo).to.equal('bar'); + }); + }); + + it('should parse a yaml file with JSON-REF to json', () => { + const tmpDirPath = path.join(os.tmpdir(), (new Date).getTime().toString()); + + SUtils.writeFileSync(path.join(tmpDirPath, 'ref.json'), { foo: 'bar' }); + + const testYaml = { + main: { + $ref: './ref.json', + }, + }; + + SUtils.writeFileSync(path.join(tmpDirPath, 'test.yaml'), testYaml); + + return SYamlParser.parseYaml(path.join(tmpDirPath, 'test.yaml')).then((obj) => { + expect(obj.main.foo).to.equal('bar'); + }); + }); + + it('should parse yaml file with recursive JSON-REF', () => { + const tmpDirPath = path.join(os.tmpdir(), (new Date).getTime().toString()); + + SUtils.writeFileSync(path.join(tmpDirPath, 'three.yaml'), { foo: 'bar' }); + + const twoYaml = { + two: { + $ref: './three.yaml', + }, + }; + + SUtils.writeFileSync(path.join(tmpDirPath, 'two.yaml'), twoYaml); + + const oneYaml = { + one: { + $ref: './two.yaml', + }, + }; + + SUtils.writeFileSync(path.join(tmpDirPath, 'one.yaml'), oneYaml); + + return SYamlParser.parseYaml(path.join(tmpDirPath, 'one.yaml')).then((obj) => { + expect(obj.one.two.foo).to.equal('bar'); + }); + }); + }); + +});