From 4d8c08ae7a260d758e6c42612e6c051f0e1f47d2 Mon Sep 17 00:00:00 2001 From: Simon Lydell Date: Tue, 9 Dec 2014 19:44:26 +0100 Subject: [PATCH] Add unit tests infrastructure --- CONTRIBUTING.md | 8 +++++- extension/lib/main.coffee | 3 +++ extension/test/index.coffee | 43 +++++++++++++++++++++++++++++++ extension/test/test-utils.coffee | 26 +++++++++++++++++++ extension/test/tests-list.js.tmpl | 1 + gulpfile.coffee | 24 ++++++++++++++--- package.json | 1 - 7 files changed, 101 insertions(+), 5 deletions(-) create mode 100644 extension/test/index.coffee create mode 100644 extension/test/test-utils.coffee create mode 100644 extension/test/tests-list.js.tmpl diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 9557e9c..105ae24 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -81,6 +81,8 @@ Code! Try to follow the following simple rules: Please lint your code. See below. +Run the tests and make sure that all pass. See below. Add tests if possible. + Break up your pull request in several commits if necessary. The first line of commit messages should be a short summary. Add a blank line and then a nicely formatted markdown description after it if needed. @@ -105,8 +107,12 @@ Finally send a pull request to same branch as you based your topic branch on automatically install it. (No need to restart Firefox.) - `gulp clean` removes the `build/` directory. - `gulp lint` lints your code. +- Use the `--test` or `-t` option to include the unit test files. The output of + the tests are `console.log`ed. See the browser console, or start Firefox from + the command line to see it. -An easy workflow is code, `gulp`, test, repeat. +An easy workflow is code, `gulp`, test, repeat. (Use `gulp -t` to also run the +unit tests.) [Node.js]: http://nodejs.org/ [gulp]: https://github.com/gulpjs/gulp diff --git a/extension/lib/main.coffee b/extension/lib/main.coffee index 43fe9d5..ba3fad5 100644 --- a/extension/lib/main.coffee +++ b/extension/lib/main.coffee @@ -27,8 +27,11 @@ , addToolbarButton } = require('./button') options = require('./options') { watchWindows } = require('./window-utils') +test = try require('../test/index') module.exports = (data, reason) -> + test?() + setDefaultPrefs() if reason == ADDON_INSTALL diff --git a/extension/test/index.coffee b/extension/test/index.coffee new file mode 100644 index 0000000..513a89f --- /dev/null +++ b/extension/test/index.coffee @@ -0,0 +1,43 @@ +### +# Copyright Simon Lydell 2014. +# +# This file is part of VimFx. +# +# VimFx is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# VimFx is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with VimFx. If not, see . +### + +list = require('./tests-list') + +Components.utils.import('resource://specialpowers/Assert.jsm') +assert = new Assert() + +module.exports = -> + report = [] + passed = 0 + total = 0 + + for name in list + tests = require("./#{ name }") + report.push(name) + for key, fn of tests when key.startsWith('test') + total++ + try + fn(assert) + passed++ + catch error + report.push(" #{ if error then '\u2718' else '\u2714' } #{ key }") + report.push(error.toString().replace(/^/gm, ' ')) if error + + report.push("#{ passed }/#{ total } tests passed.") + console.log("\n#{ report.join('\n') }") diff --git a/extension/test/test-utils.coffee b/extension/test/test-utils.coffee new file mode 100644 index 0000000..6092af6 --- /dev/null +++ b/extension/test/test-utils.coffee @@ -0,0 +1,26 @@ +### +# Copyright Simon Lydell 2014. +# +# This file is part of VimFx. +# +# VimFx is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# VimFx is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with VimFx. If not, see . +### + +utils = require('../lib/utils') + +exports['test removeDuplicates'] = (assert) -> + assert.deepEqual(utils.removeDuplicates([1, 1, 2, 1, 3, 2]), + [1, 2, 3]) + assert.deepEqual(utils.removeDuplicates(['a', 'b', 'c', 'b', 'd', 'a']), + ['a', 'b', 'c', 'd']) diff --git a/extension/test/tests-list.js.tmpl b/extension/test/tests-list.js.tmpl new file mode 100644 index 0000000..6996770 --- /dev/null +++ b/extension/test/tests-list.js.tmpl @@ -0,0 +1 @@ +module.exports = {{{list}}} diff --git a/gulpfile.coffee b/gulpfile.coffee index 2aaab0c..bc3c85e 100644 --- a/gulpfile.coffee +++ b/gulpfile.coffee @@ -26,7 +26,6 @@ git = require('gulp-git') header = require('gulp-header') merge = require('gulp-merge') mustache = require('gulp-mustache') -util = require('gulp-util') zip = require('gulp-zip') precompute = require('require-precompute') request = require('request') @@ -37,6 +36,10 @@ pkg = require('./package.json') DEST = 'build' XPI = 'VimFx.xpi' LOCALE = 'extension/locale' +TEST = 'extension/test' + +test = '--test' in process.argv or '-t' in process.argv +ifTest = (value) -> if test then [value] else [] read = (filepath) -> fs.readFileSync(filepath).toString() template = (data) -> mustache(data, {extension: ''}) @@ -61,8 +64,12 @@ gulp.task('node_modules', -> ) gulp.task('coffee', -> - gulp.src('extension/**/*.coffee') - .pipe(coffee({bare: true}).on('error', util.log)) + gulp.src([ + 'extension/bootstrap.coffee' + 'extension/lib/**/*.coffee' + ifTest('extension/test/**/*.coffee')... + ], {base: 'extension'}) + .pipe(coffee({bare: true})) .pipe(gulp.dest(DEST)) ) @@ -104,10 +111,21 @@ gulp.task('require-data', ['node_modules'], -> .pipe(gulp.dest(DEST)) ) +gulp.task('tests-list', -> + list = JSON.stringify(fs.readdirSync(TEST) + .map((name) -> name.match(/^(test-.+)\.coffee$/)?[1]) + .filter(Boolean) + ) + gulp.src("#{ TEST }/tests-list.js.tmpl", {base: 'extension'}) + .pipe(template({list})) + .pipe(gulp.dest(DEST)) +) + gulp.task('templates', [ 'chrome.manifest' 'install.rdf' 'require-data' + ifTest('tests-list')... ]) gulp.task('build', (callback) -> diff --git a/package.json b/package.json index 0f64bfc..5083bd1 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,6 @@ "gulp-header": "^1.2.2", "gulp-merge": "^0.1.0", "gulp-mustache": "^1.0.0", - "gulp-util": "^3.0.1", "gulp-zip": "^2.0.2", "request": "^2.49.0", "require-precompute": "~0.2.0", -- 2.39.3