From 737d91099f8a646c182bd1014ba1f8ec1de21784 Mon Sep 17 00:00:00 2001 From: Zoltan Kochan Date: Sun, 15 May 2016 17:12:28 +0300 Subject: [PATCH] test: test actual md files --- README.md | 10 +-- index.js | 31 +++++++++- index.spec.js | 34 ++++++++++- lib/index.js | 10 --- lib/index.spec.js | 2 - lib/render-license.js | 25 -------- lib/render-license.spec.js | 67 --------------------- package.json | 11 ++-- test/fixtures/has-all-links/LICENSE | 21 +++++++ test/fixtures/has-all-links/input.md | 2 + test/fixtures/has-all-links/output.md | 5 ++ test/fixtures/has-all-links/package.json | 8 +++ test/fixtures/has-license-link/LICENSE | 21 +++++++ test/fixtures/has-license-link/input.md | 2 + test/fixtures/has-license-link/output.md | 5 ++ test/fixtures/has-license-link/package.json | 5 ++ test/fixtures/has-no-links/input.md | 2 + test/fixtures/has-no-links/output.md | 5 ++ test/fixtures/has-no-links/package.json | 5 ++ 19 files changed, 155 insertions(+), 116 deletions(-) delete mode 100644 lib/index.js delete mode 100644 lib/index.spec.js delete mode 100644 lib/render-license.js delete mode 100644 lib/render-license.spec.js create mode 100644 test/fixtures/has-all-links/LICENSE create mode 100644 test/fixtures/has-all-links/input.md create mode 100644 test/fixtures/has-all-links/output.md create mode 100644 test/fixtures/has-all-links/package.json create mode 100644 test/fixtures/has-license-link/LICENSE create mode 100644 test/fixtures/has-license-link/input.md create mode 100644 test/fixtures/has-license-link/output.md create mode 100644 test/fixtures/has-license-link/package.json create mode 100644 test/fixtures/has-no-links/input.md create mode 100644 test/fixtures/has-no-links/output.md create mode 100644 test/fixtures/has-no-links/package.json diff --git a/README.md b/README.md index d6b7b54..e19d165 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,8 @@ - + # mos-plugin-license - + > A mos plugin for generating a license section @@ -13,10 +13,8 @@ ## Installation -This module is installed via npm: - ```sh -npm install mos-plugin-license --save +npm install --save mos-plugin-license ``` @@ -66,6 +64,8 @@ You'll get a license section with the license and author specified in the `packa - [istanbul](https://github.com/gotwarlost/istanbul): Yet another JS code coverage tool that computes statement, line, function and branch coverage with module loader hooks to transparently add coverage when running tests. Supports all JS coverage use cases including unit tests, server side functional tests - [mocha](https://github.com/mochajs/mocha): simple, flexible, fun test framework - [mos](https://github.com/zkochan/mos): A pluggable module that injects content into your markdown files via hidden JavaScript snippets +- [mos-plugin-ejs](https://github.com/zkochan/mos-plugin-ejs): A mos plugin that executes embedded js in markdown files +- [mos-processor](https://github.com/zkochan/mos-processor): A markdown processor for mos - [semantic-release](https://github.com/semantic-release/semantic-release): automated semver compliant package publishing - [validate-commit-msg](https://github.com/kentcdodds/validate-commit-msg): Script to validate a commit message follows the conventional changelog standard diff --git a/index.js b/index.js index b26b98c..7caf09b 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,34 @@ 'use strict' -module.exports = (mos, md) => Object.assign(mos.scope, require('./lib')(md)) +const fileExists = require('file-exists') +const path = require('path') +const m = require('markdownscript') +const h2 = m.h2 +const link = m.link +const paragraph = m.paragraph + +module.exports = (mos, markdown) => { + Object.assign(mos.scope, { + license: compileLicense, + }) + + function compileLicense () { + const licensePath = path.resolve(path.dirname(markdown.filePath), 'LICENSE') + const licenseFileExists = fileExists(licensePath) + + return [ + h2(['License']), + paragraph([ + (licenseFileExists + ? link({url: './LICENSE'}, [markdown.pkg.license]) + : markdown.pkg.license), + ' © ', + (markdown.pkg.author.url + ? link({url: markdown.pkg.author.url}, [markdown.pkg.author.name]) + : markdown.pkg.author.name), + ]), + ] + } +} module.exports.attributes = { pkg: require('./package.json'), diff --git a/index.spec.js b/index.spec.js index c2720a4..fb86486 100644 --- a/index.spec.js +++ b/index.spec.js @@ -1 +1,33 @@ -require('.') +'use strict' +const describe = require('mocha').describe +const it = require('mocha').it +const expect = require('chai').expect +const path = require('path') +const fs = require('fs') +const ROOT = path.join(__dirname, 'test/fixtures') +const fixtures = fs.readdirSync(ROOT).filter(filepath => filepath.indexOf('.') !== 0) + +const mos = require('mos-processor') +const ejs = require('mos-plugin-ejs') +const license = require('.') + +describe('mos-plugin-toc', () => { + fixtures.forEach(fixture => { + const filepath = path.join(ROOT, fixture) + const output = fs.readFileSync(path.join(filepath, 'output.md'), 'utf-8') + const inputPath = path.join(filepath, 'input.md') + const input = fs.readFileSync(inputPath, 'utf-8') + const configPath = path.join(filepath, 'config.json') + const config = fs.existsSync(configPath) ? JSON.parse(fs.readFileSync(configPath, 'utf-8')) : {} + + it('should pass fixture in dir ' + filepath, done => { + mos({ content: input, filePath: inputPath }, [ejs, { register: license, options: config }]) + .then(processor => processor.process()) + .then(result => { + expect(result).to.eq(output) + done() + }) + .catch(done) + }) + }) +}) diff --git a/lib/index.js b/lib/index.js deleted file mode 100644 index 5842986..0000000 --- a/lib/index.js +++ /dev/null @@ -1,10 +0,0 @@ -'use strict' -module.exports = plugin - -const renderLicense = require('./render-license') - -function plugin (markdown) { - return { - license: () => renderLicense(markdown), - } -} diff --git a/lib/index.spec.js b/lib/index.spec.js deleted file mode 100644 index 774b1ec..0000000 --- a/lib/index.spec.js +++ /dev/null @@ -1,2 +0,0 @@ -'use strict' -require('.') diff --git a/lib/render-license.js b/lib/render-license.js deleted file mode 100644 index 990f72b..0000000 --- a/lib/render-license.js +++ /dev/null @@ -1,25 +0,0 @@ -'use strict' -const fileExists = require('file-exists') -const path = require('path') -const m = require('markdownscript') -const h2 = m.h2 -const link = m.link -const paragraph = m.paragraph - -module.exports = markdown => { - const licensePath = path.resolve(path.dirname(markdown.filePath), 'LICENSE') - const licenseFileExists = fileExists(licensePath) - - return [ - h2(['License']), - paragraph([ - (licenseFileExists - ? link({url: './LICENSE'}, [markdown.pkg.license]) - : markdown.pkg.license), - ' © ', - (markdown.pkg.author.url - ? link({url: markdown.pkg.author.url}, [markdown.pkg.author.name]) - : markdown.pkg.author.name), - ]), - ] -} diff --git a/lib/render-license.spec.js b/lib/render-license.spec.js deleted file mode 100644 index 3920069..0000000 --- a/lib/render-license.spec.js +++ /dev/null @@ -1,67 +0,0 @@ -'use strict' -const describe = require('mocha').describe -const it = require('mocha').it -const expect = require('chai').expect -const path = require('path') -const m = require('markdownscript') -const h2 = m.h2 -const p = m.paragraph -const link = m.link - -const renderLicense = require('./render-license') - -describe('renderLicense', () => { - it("should create license with link to the author's website", () => { - const pkg = { - author: { - name: 'Zoltan Kochan', - url: 'http://kochan.io', - }, - license: 'MIT', - } - const license = renderLicense({pkg}) - expect(license).to.eql([ - h2(['License']), - p([ - link({url: './LICENSE'}, ['MIT']), - ' © ', - link({url: 'http://kochan.io'}, ['Zoltan Kochan']), - ]), - ]) - }) - - it("should create license with author's name", () => { - const pkg = { - author: { - name: 'Zoltan Kochan', - }, - license: 'MIT', - } - const license = renderLicense({pkg}) - expect(license).to.eql([ - h2(['License']), - p([ - link({url: './LICENSE'}, ['MIT']), - ' © Zoltan Kochan', - ]), - ]) - }) - - it("should create license with no link to license file when license file doesn't exist", () => { - const pkg = { - author: { - name: 'Zoltan Kochan', - }, - license: 'MIT', - } - const license = renderLicense({ - pkg, - filePath: path.resolve(__filename), - }) - expect(license).to.eql([ - h2(['License']), - p(['MIT © Zoltan Kochan', - ]), - ]) - }) -}) diff --git a/package.json b/package.json index a08fb73..a679ddc 100644 --- a/package.json +++ b/package.json @@ -3,19 +3,18 @@ "version": "0.0.0-placeholder", "description": "A mos plugin for generating a license section", "files": [ - "index.js", - "lib" + "index.js" ], "main": "index.js", "scripts": { - "test": "mocha && npm run lint && mos test", + "test": "mocha && npm run lint && mos test -x=\"test/**\"", "lint": "eslint lib/**/*.js index.js", "commit": "git-cz", "coverage": "istanbul cover -x \"**/*.spec.js\" node_modules/mocha/bin/_mocha -- -R spec", "precoveralls": "istanbul cover -x \"**/*.spec.js\" node_modules/mocha/bin/_mocha --report lcovonly -- -R spec && npm i coveralls@2", "coveralls": "cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js", "postcoveralls": "rm -rf ./coverage", - "md": "mos", + "md": "mos -x=\"test/**\"", "semantic-release": "semantic-release pre && npm publish && semantic-release post" }, "repository": { @@ -51,7 +50,9 @@ "ghooks": "^1.2.1", "istanbul": "^0.4.2", "mocha": "^2.3.4", - "mos": "^0.16.1", + "mos": "^1.0.0", + "mos-plugin-ejs": "^1.0.0", + "mos-processor": "^1.1.0", "semantic-release": "^4.3.5", "validate-commit-msg": "^2.6.1" }, diff --git a/test/fixtures/has-all-links/LICENSE b/test/fixtures/has-all-links/LICENSE new file mode 100644 index 0000000..8e51fe6 --- /dev/null +++ b/test/fixtures/has-all-links/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2016 Zoltan Kochan + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/test/fixtures/has-all-links/input.md b/test/fixtures/has-all-links/input.md new file mode 100644 index 0000000..8bb5ac7 --- /dev/null +++ b/test/fixtures/has-all-links/input.md @@ -0,0 +1,2 @@ + + diff --git a/test/fixtures/has-all-links/output.md b/test/fixtures/has-all-links/output.md new file mode 100644 index 0000000..4934cc5 --- /dev/null +++ b/test/fixtures/has-all-links/output.md @@ -0,0 +1,5 @@ + +## License + +[MIT](./LICENSE) © [James Bond](http://jbond.uk) + diff --git a/test/fixtures/has-all-links/package.json b/test/fixtures/has-all-links/package.json new file mode 100644 index 0000000..aa0dcf5 --- /dev/null +++ b/test/fixtures/has-all-links/package.json @@ -0,0 +1,8 @@ +{ + "name": "foo", + "author": { + "name": "James Bond", + "url": "http://jbond.uk" + }, + "license": "MIT" +} diff --git a/test/fixtures/has-license-link/LICENSE b/test/fixtures/has-license-link/LICENSE new file mode 100644 index 0000000..8e51fe6 --- /dev/null +++ b/test/fixtures/has-license-link/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2016 Zoltan Kochan + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/test/fixtures/has-license-link/input.md b/test/fixtures/has-license-link/input.md new file mode 100644 index 0000000..8bb5ac7 --- /dev/null +++ b/test/fixtures/has-license-link/input.md @@ -0,0 +1,2 @@ + + diff --git a/test/fixtures/has-license-link/output.md b/test/fixtures/has-license-link/output.md new file mode 100644 index 0000000..f75c495 --- /dev/null +++ b/test/fixtures/has-license-link/output.md @@ -0,0 +1,5 @@ + +## License + +[MIT](./LICENSE) © James Bond + diff --git a/test/fixtures/has-license-link/package.json b/test/fixtures/has-license-link/package.json new file mode 100644 index 0000000..5553d5c --- /dev/null +++ b/test/fixtures/has-license-link/package.json @@ -0,0 +1,5 @@ +{ + "name": "foo", + "author": "James Bond", + "license": "MIT" +} diff --git a/test/fixtures/has-no-links/input.md b/test/fixtures/has-no-links/input.md new file mode 100644 index 0000000..8bb5ac7 --- /dev/null +++ b/test/fixtures/has-no-links/input.md @@ -0,0 +1,2 @@ + + diff --git a/test/fixtures/has-no-links/output.md b/test/fixtures/has-no-links/output.md new file mode 100644 index 0000000..3221d53 --- /dev/null +++ b/test/fixtures/has-no-links/output.md @@ -0,0 +1,5 @@ + +## License + +MIT © James Bond + diff --git a/test/fixtures/has-no-links/package.json b/test/fixtures/has-no-links/package.json new file mode 100644 index 0000000..5553d5c --- /dev/null +++ b/test/fixtures/has-no-links/package.json @@ -0,0 +1,5 @@ +{ + "name": "foo", + "author": "James Bond", + "license": "MIT" +}