Skip to content

Commit 70a99cd

Browse files
committed
Merge pull request #20 from benjamn/issue-12-test-install-package
Provide `grunt npm:test` for verifying NPM package functionality
2 parents 2d253fe + 60a6665 commit 70a99cd

4 files changed

Lines changed: 109 additions & 0 deletions

File tree

Gruntfile.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ var jsxTask = require('./grunt/tasks/jsx');
55
var browserifyTask = require('./grunt/tasks/browserify');
66
var wrapupTask = require('./grunt/tasks/wrapup');
77
var phantomTask = require('./grunt/tasks/phantom');
8+
var npmTask = require('./grunt/tasks/npm');
89
var releaseTasks = require('./grunt/tasks/release');
910

1011
module.exports = function(grunt) {
@@ -16,6 +17,7 @@ module.exports = function(grunt) {
1617
browserify: require('./grunt/config/browserify'),
1718
wrapup: require('./grunt/config/wrapup'),
1819
phantom: require('./grunt/config/phantom'),
20+
npm: require('./grunt/config/npm'),
1921
clean: ['./build', './*.gem', './docs/_site', './examples/shared/*.js'],
2022
jshint: require('./grunt/config/jshint'),
2123
compare_size: require('./grunt/config/compare_size')
@@ -44,6 +46,8 @@ module.exports = function(grunt) {
4446

4547
grunt.registerMultiTask('phantom', phantomTask);
4648

49+
grunt.registerMultiTask('npm', npmTask);
50+
4751
grunt.registerTask('build:basic', ['jsx:debug', 'browserify:basic']);
4852
grunt.registerTask('build:transformer', ['jsx:debug', 'browserify:transformer']);
4953
grunt.registerTask('build:min', ['jsx:release', 'browserify:min']);
@@ -54,6 +58,7 @@ module.exports = function(grunt) {
5458
]);
5559

5660
grunt.registerTask('test', ['build:test', 'phantom:run']);
61+
grunt.registerTask('npm:test', ['build', 'npm:pack']);
5762

5863
// Optimized build task that does all of our builds. The subtasks will be run
5964
// in order so we can take advantage of that and only run jsx:debug once.

grunt/config/npm.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
exports.pack = {};

grunt/tasks/npm.js

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
'use strict';
2+
3+
var assert = require("assert");
4+
var path = require("path");
5+
var fs = require("fs");
6+
var tmp = require("tmp");
7+
var grunt = require("grunt");
8+
var spawn = grunt.util.spawn;
9+
10+
module.exports = function() {
11+
var config = this.data;
12+
var done = this.async();
13+
14+
function run(cmd, args, opts, callback) {
15+
assert.strictEqual(typeof cmd, "string");
16+
assert.ok(args instanceof Array);
17+
18+
if (typeof opts === "function" && !callback) {
19+
callback = opts;
20+
opts = {};
21+
}
22+
23+
assert.strictEqual(typeof opts, "object");
24+
assert.strictEqual(typeof callback, "function");
25+
26+
grunt.log.writeln("> " + cmd + " " + args.join(" "));
27+
28+
var proc = spawn({
29+
cmd: cmd,
30+
args: args,
31+
opts: opts
32+
}, function(error, result, code) {
33+
if (error) {
34+
grunt.log.error(error);
35+
done(false);
36+
} else {
37+
callback(result, code);
38+
}
39+
});
40+
41+
// Uncomment these to see the output of the commands.
42+
// proc.stdout.pipe(process.stdout);
43+
// proc.stderr.pipe(process.stderr);
44+
}
45+
46+
var pkg = grunt.config.data.pkg;
47+
var tgz = pkg.name + "-" + pkg.version + ".tgz";
48+
49+
grunt.log.writeln("Packing " + tgz + " (this could take a while)...");
50+
51+
run("npm", ["pack", "--verbose", "."], function() {
52+
tmp.dir(function(err, dir) {
53+
if (err) {
54+
grunt.log.error(err);
55+
done(false);
56+
return;
57+
}
58+
59+
run("cp", [tgz, dir], function() {
60+
run("npm", [
61+
"install",
62+
"--production",
63+
tgz
64+
], { cwd: dir }, function() {
65+
var nodePath = path.join(dir, "node_modules");
66+
var pkgDir = path.join(nodePath, pkg.name);
67+
var doneCount = 2;
68+
69+
// Make sure that bin/jsx is runnable by echoing main.js.
70+
run("bin/jsx", ["main.js"], {
71+
cwd: pkgDir
72+
}, function(result) {
73+
assert.ok(result.stdout.indexOf("transform") >= 0, result.stdout);
74+
75+
if (--doneCount === 0) {
76+
done();
77+
}
78+
});
79+
80+
// Make sure the .transform package method works.
81+
run("node", [
82+
"--print",
83+
'require("react-tools").transform(' +
84+
JSON.stringify(
85+
"/** @jsx React.DOM */ <div>oyez</div>;"
86+
) + ')'
87+
], {
88+
env: { NODE_PATH: nodePath }
89+
}, function(result, code) {
90+
assert.ok(result.stdout.indexOf(
91+
'React.DOM.div(null, "oyez");'
92+
) >= 0, result.stdout);
93+
94+
if (--doneCount === 0) {
95+
done();
96+
}
97+
});
98+
});
99+
});
100+
});
101+
});
102+
};

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
"grunt-contrib-clean": "~0.4.1",
5353
"grunt-compare-size": "~0.4.0",
5454
"gzip-js": "~0.3.2",
55+
"tmp": "~0.0.18",
5556
"grunt-contrib-compress": "~0.5.1"
5657
},
5758
"preferGlobal": true

0 commit comments

Comments
 (0)