2 * pkginfo-test.js: Tests for the pkginfo module.
4 * (C) 2011, Charlie Robbins
8 var assert = require('assert'),
9 exec = require('child_process').exec,
11 path = require('path'),
12 vows = require('vows'),
13 pkginfo = require('../lib/pkginfo');
15 function assertProperties (source, target) {
16 assert.lengthOf(source, target.length + 1);
17 target.forEach(function (prop) {
18 assert.isTrue(!!~source.indexOf(prop));
22 function compareWithExample(targetPath) {
23 var examplePaths = ['package.json'];
26 examplePaths.unshift(targetPath);
29 return function(exposed) {
30 var pkg = fs.readFileSync(path.join.apply(null, [__dirname, '..', 'examples'].concat(examplePaths))).toString(),
31 keys = Object.keys(JSON.parse(pkg));
33 assertProperties(exposed, keys);
37 function testExposes (options) {
40 exec('node ' + path.join(__dirname, '..', 'examples', options.script), this.callback);
42 "should expose that property correctly": function (err, stdout, stderr) {
45 var exposed = stderr.match(/'(\w+)'/ig).map(function (p) {
46 return p.substring(1, p.length - 1);
49 return !options.assert
50 ? assertProperties(exposed, options.properties)
51 : options.assert(exposed);
56 vows.describe('pkginfo').addBatch({
57 "When using the pkginfo module": {
58 "and passed a single `string` argument": testExposes({
59 script: 'single-property.js',
60 properties: ['version']
62 "and passed multiple `string` arguments": testExposes({
63 script: 'multiple-properties.js',
64 properties: ['version', 'author']
66 "and passed an `object` argument": testExposes({
67 script: 'object-argument.js',
68 properties: ['version', 'author']
70 "and passed an `array` argument": testExposes({
71 script: 'array-argument.js',
72 properties: ['version', 'author']
74 "and read from a specified directory": testExposes({
75 script: 'target-dir.js',
76 assert: compareWithExample('subdir')
78 "and passed no arguments": testExposes({
79 script: 'all-properties.js',
80 assert: compareWithExample()