Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add tests

  • Loading branch information...
commit 3cc39702bd29a95a09a1becc2523a0b365c3cc87 1 parent 2c297b0
@pierreinglebert authored
View
4 Gruntfile.js
@@ -20,7 +20,7 @@ module.exports = function(grunt) {
},
// Configuration to be run (and then tested).
- compress: {
+ zopfli: {
gzip: {
expand: true,
cwd: 'test/fixtures/',
@@ -76,7 +76,7 @@ module.exports = function(grunt) {
// Whenever the "test" task is run, first clean the "tmp" dir, then run this
// plugin's task(s), then test the result.
- grunt.registerTask('test', ['clean', 'compress', 'nodeunit']);
+ grunt.registerTask('test', ['clean', 'zopfli', 'nodeunit']);
// By default, lint and run all tests.
grunt.registerTask('default', ['jshint', 'test', 'build-contrib']);
View
2  tasks/lib/zopfli.js
@@ -9,7 +9,7 @@ module.exports = function(grunt) {
'use strict';
var exports = {
- options: {}
+ options: { verbose: false }
};
// 1 to 1 gziping of files
View
14 tasks/zopfli.js
@@ -7,17 +7,17 @@ module.exports = function(grunt) {
zopfli.options = this.options({
});
- zopfli.options.mode = zopfli.options.mode || compress.autoDetectMode(compress.options.archive);
- grunt.verbose.writeflags(compress.options, 'Options');
+ zopfli.options.mode = zopfli.options.mode || zopfli.autoDetectMode(zopfli.options.archive);
+ grunt.verbose.writeflags(zopfli.options, 'Options');
- if (grunt.util._.include(['gzip', 'deflate', 'deflateRaw'], compress.options.mode) === false) {
- grunt.fail.warn('Mode ' + String(compress.options.mode).cyan + ' not supported.');
+ if (grunt.util._.include(['gzip', 'deflate', 'deflateRaw'], zopfli.options.mode) === false) {
+ grunt.fail.warn('Mode ' + String(zopfli.options.mode).cyan + ' not supported.');
}
- if (compress.options.mode === 'gzip' || compress.options.mode.slice(0, 7) === 'deflate') {
- compress[compress.options.mode](this.files, this.async());
+ if (zopfli.options.mode === 'gzip' || zopfli.options.mode.slice(0, 7) === 'deflate') {
+ zopfli[zopfli.options.mode](this.files, this.async());
} else {
- compress.tar(this.files, this.async());
+ zopfli.tar(this.files, this.async());
}
});
};
View
140 test/compress_test.js
@@ -0,0 +1,140 @@
+'use strict';
+
+var grunt = require('grunt');
+var path = require('path');
+var zlib = require('zlib');
+var fs = require('fs');
+var unzip = require('unzip');
+var tar = require('tar');
+var compress = require('../tasks/lib/compress')(grunt);
+
+exports.compress = {
+ zip: function(test) {
+ test.expect(1);
+ var expected = [
+ 'folder_one/one.css', 'folder_one/one.js',
+ 'folder_two/two.css', 'folder_two/two.js',
+ 'test.css', 'test.js',
+ ];
+ var actual = [];
+ var parse = unzip.Parse();
+ fs.createReadStream(path.join('tmp', 'compress_test_files.zip')).pipe(parse);
+ parse.on('entry', function(entry) {
+ actual.push(entry.path);
+ });
+ parse.on('close', function() {
+ test.deepEqual(actual, expected, 'zip file should unzip and contain all of the expected files');
+ test.done();
+ });
+ },
+ tar: function(test) {
+ test.expect(1);
+ var expected = [
+ 'folder_one/one.css', 'folder_one/one.js',
+ 'folder_two/two.css', 'folder_two/two.js',
+ 'test.css', 'test.js',
+ ];
+ var actual = [];
+ var parse = tar.Parse();
+ fs.createReadStream(path.join('tmp', 'compress_test_files.tar')).pipe(parse);
+ parse.on('entry', function(entry) {
+ actual.push(entry.path);
+ });
+ parse.on('end', function() {
+ test.deepEqual(actual, expected, 'tar file should untar and contain all of the expected files');
+ test.done();
+ });
+ },
+ tgz: function(test) {
+ test.expect(1);
+ var expected = [
+ 'folder_one/one.css', 'folder_one/one.js',
+ 'folder_two/two.css', 'folder_two/two.js',
+ 'test.css', 'test.js',
+ ];
+ var actual = [];
+ var parse = tar.Parse();
+ fs.createReadStream(path.join('tmp', 'compress_test_files.tgz'))
+ .pipe(zlib.createGunzip())
+ .pipe(parse);
+ parse.on('entry', function(entry) {
+ actual.push(entry.path);
+ });
+ parse.on('end', function() {
+ test.deepEqual(actual, expected, 'tgz file should gunzip/untar and contain all of the expected files');
+ test.done();
+ });
+ },
+ gzip: function(test) {
+ test.expect(3);
+ grunt.util.async.forEachSeries([
+ 'test.js',
+ path.join('folder_one', 'one.css'),
+ path.join('folder_two', 'two.js'),
+ ], function(file, next) {
+ var expected = grunt.file.read(path.join('test', 'fixtures', file));
+ var actual = '';
+ fs.createReadStream(path.join('tmp', 'gzip', file + '.gz'))
+ .pipe(zlib.createGunzip())
+ .on('data', function(buf) {
+ actual += buf.toString();
+ })
+ .on('end', function() {
+ test.equal(actual, expected, 'should be equal to fixture after gunzipping');
+ next();
+ });
+ }, test.done);
+ },
+ gzipCustomExt: function(test) {
+ test.expect(3);
+ [
+ 'test',
+ 'folder_one/one',
+ 'folder_two/two',
+ ].forEach(function(file) {
+ var expected = path.join('tmp', 'gzipCustomExt', file + '.gz.js');
+ test.ok(grunt.file.exists(expected), 'should of had a correct extension.');
+ });
+ test.done();
+ },
+ deflate: function(test) {
+ test.expect(3);
+ grunt.util.async.forEachSeries([
+ 'test.js',
+ path.join('folder_one', 'one.css'),
+ path.join('folder_two', 'two.js'),
+ ], function(file, next) {
+ var expected = grunt.file.read(path.join('test', 'fixtures', file));
+ var actual = '';
+ fs.createReadStream(path.join('tmp', 'deflate', file + '.deflate'))
+ .pipe(zlib.createInflate())
+ .on('data', function(buf) {
+ actual += buf.toString();
+ })
+ .on('end', function() {
+ test.equal(actual, expected, 'should be equal to fixture after inflating');
+ next();
+ });
+ }, test.done);
+ },
+ deflateRaw: function(test) {
+ test.expect(3);
+ grunt.util.async.forEachSeries([
+ 'test.js',
+ path.join('folder_one', 'one.css'),
+ path.join('folder_two', 'two.js'),
+ ], function(file, next) {
+ var expected = grunt.file.read(path.join('test', 'fixtures', file));
+ var actual = '';
+ fs.createReadStream(path.join('tmp', 'deflateRaw', file + '.deflate'))
+ .pipe(zlib.createInflateRaw())
+ .on('data', function(buf) {
+ actual += buf.toString();
+ })
+ .on('end', function() {
+ test.equal(actual, expected, 'should be equal to fixture after inflateRaw-ing');
+ next();
+ });
+ }, test.done);
+ },
+};
View
1  test/fixtures/folder_one/one.css
@@ -0,0 +1 @@
+body,td{}a{}a:hover{}
View
1  test/fixtures/folder_one/one.js
@@ -0,0 +1 @@
+$(document).ready(function(){$.noConflict();});
View
1  test/fixtures/folder_two/two.css
@@ -0,0 +1 @@
+body,td{color:blue;}a{}a:hover{}
View
1  test/fixtures/folder_two/two.js
@@ -0,0 +1 @@
+$(document).ready(function(){jQuery});
View
1  test/fixtures/test.css
@@ -0,0 +1 @@
+body,td{background:green;}a{}a:hover{}
View
1  test/fixtures/test.js
@@ -0,0 +1 @@
+$(document).ready(function(){});
Please sign in to comment.
Something went wrong with that request. Please try again.