diff --git a/.eslintrc b/.eslintrc index a4ddaa0..c1f8661 100644 --- a/.eslintrc +++ b/.eslintrc @@ -19,6 +19,7 @@ "globals": {}, "env":{ "node": true, - "es6": true + "es6": true, + "mocha": true } } diff --git a/lib/css-ast-diff.js b/lib/css-ast-diff.js index a50a0fe..1ccea10 100644 --- a/lib/css-ast-diff.js +++ b/lib/css-ast-diff.js @@ -6,7 +6,8 @@ const parse = require('css').parse; const sort = require('./sorters/ast'); const stringify = require('css').stringify; const fs = require('fs'); -const colors = require('colors'); + +require('es6-object-assign').polyfill(); module.exports = function cssAstDiff(diff, src, callback, options) { try { diff --git a/package.json b/package.json index 609d6a6..9c574a6 100644 --- a/package.json +++ b/package.json @@ -2,9 +2,8 @@ "name": "css-ast-diff", "version": "1.0.6", "description": "Diffs CSS files by parsing into AST, sorting, and finding functional differences", - "main": "index.js", "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" + "test": "mocha --harmony" }, "repository": { "type": "git", @@ -39,6 +38,7 @@ "css": "^2.2.1", "debug": "^2.2.0", "diff": "^2.2.1", + "es6-object-assign": "^1.0.3", "fs": "0.0.2", "node-diff": "^0.2.0", "nodegit": "^0.11.8", @@ -47,5 +47,9 @@ "svn": "^0.2.0", "svn-interface": "^0.5.0", "util": "^0.10.3" + }, + "devDependencies": { + "chai": "^3.5.0", + "mocha": "^3.2.0" } } diff --git a/test/cases.js b/test/cases.js new file mode 100644 index 0000000..f84929a --- /dev/null +++ b/test/cases.js @@ -0,0 +1,65 @@ +'use strict'; + +const path = require('path'); +const async = require('async'); +const fs = require('fs'); + +const chai = require('chai'); +const expect = chai.expect; + +const cases = fs.readdirSync(path.join(__dirname, 'cases')); +const files = { + src: 'src.css', + diff: 'diff.css', + stats: 'stats.json' +}; + +const cssAstDiff = require('../lib/api'); +const options = { + type: 'hunks' +}; + +cases.forEach(function(name) { + describe('cases/' + name, function() { + var data = {}; + + before(function(done) { + this.timeout(5000); + runDiff(name, data, done); + }); + + // TODO: create more useful tests + + it('detects the expected number of changes', function(done) { + expect(data.changes).to.have.length(data.stats.changes); + done(); + }); + }); +}); + +function runDiff(name, data, callback) { + var dir = path.join(__dirname, 'cases', name); + var src = path.join(dir, files.src); + var diff = path.join(dir, files.diff); + var stats = path.join(dir, files.stats); + + async.parallel([ + function readStats(callback) { + fs.readFile(stats, 'utf8', function(err, results) { + data.stats = JSON.parse(results); + callback(); + }); + }, + function createDiff(callback) { + cssAstDiff.compareFiles(src, diff, function(err, results) { + if (err) { + throw err; + } + data.changes = results; + callback(); + }, options); + } + ], + callback + ); +} diff --git a/test/bootstrap/bootstrap-3.3.6.css b/test/cases/bootstrap/diff.css similarity index 100% rename from test/bootstrap/bootstrap-3.3.6.css rename to test/cases/bootstrap/diff.css diff --git a/test/bootstrap/bootstrap-3.3.5.css b/test/cases/bootstrap/src.css similarity index 100% rename from test/bootstrap/bootstrap-3.3.5.css rename to test/cases/bootstrap/src.css diff --git a/test/cases/bootstrap/stats.json b/test/cases/bootstrap/stats.json new file mode 100644 index 0000000..076ee43 --- /dev/null +++ b/test/cases/bootstrap/stats.json @@ -0,0 +1,4 @@ +{ + "ignore": true, + "changes": 47 +} diff --git a/test/duplicates/diff.css b/test/cases/duplicates/diff.css similarity index 100% rename from test/duplicates/diff.css rename to test/cases/duplicates/diff.css diff --git a/test/duplicates/src.css b/test/cases/duplicates/src.css similarity index 100% rename from test/duplicates/src.css rename to test/cases/duplicates/src.css diff --git a/test/cases/duplicates/stats.json b/test/cases/duplicates/stats.json new file mode 100644 index 0000000..af437a5 --- /dev/null +++ b/test/cases/duplicates/stats.json @@ -0,0 +1,3 @@ +{ + "changes": 2 +} diff --git a/test/minified/diff.css b/test/cases/minified/diff.css similarity index 100% rename from test/minified/diff.css rename to test/cases/minified/diff.css diff --git a/test/minified/src.css b/test/cases/minified/src.css similarity index 100% rename from test/minified/src.css rename to test/cases/minified/src.css diff --git a/test/cases/minified/stats.json b/test/cases/minified/stats.json new file mode 100644 index 0000000..b2b9626 --- /dev/null +++ b/test/cases/minified/stats.json @@ -0,0 +1,3 @@ +{ + "changes": 0 +} diff --git a/test/move/diff.css b/test/cases/move/diff.css similarity index 100% rename from test/move/diff.css rename to test/cases/move/diff.css diff --git a/test/move/src.css b/test/cases/move/src.css similarity index 100% rename from test/move/src.css rename to test/cases/move/src.css diff --git a/test/cases/move/stats.json b/test/cases/move/stats.json new file mode 100644 index 0000000..b2b9626 --- /dev/null +++ b/test/cases/move/stats.json @@ -0,0 +1,3 @@ +{ + "changes": 0 +}