Bug:Fix file validation issue
[vnfsdk/refrepo.git] / vnfmarket / src / main / webapp / vnfmarket / node_modules / pkginfo / test / pkginfo-test.js
1 /*
2  * pkginfo-test.js: Tests for the pkginfo module.
3  *
4  * (C) 2011, Charlie Robbins
5  *
6  */
7
8 var assert = require('assert'),
9     exec = require('child_process').exec,
10     fs = require('fs'),
11     path = require('path'),
12     vows = require('vows'),
13     pkginfo = require('../lib/pkginfo');
14
15 function assertProperties (source, target) {
16   assert.lengthOf(source, target.length + 1);
17   target.forEach(function (prop) {
18     assert.isTrue(!!~source.indexOf(prop));
19   });
20 }
21
22 function compareWithExample(targetPath) {
23   var examplePaths = ['package.json'];
24   
25   if (targetPath) {
26     examplePaths.unshift(targetPath);
27   }
28   
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));
32     
33     assertProperties(exposed, keys);
34   };
35 }
36
37 function testExposes (options) {
38   return {
39     topic: function () {
40       exec('node ' + path.join(__dirname, '..', 'examples', options.script), this.callback);
41     },
42     "should expose that property correctly": function (err, stdout, stderr) {
43       assert.isNull(err);
44       
45       var exposed = stderr.match(/'(\w+)'/ig).map(function (p) { 
46         return p.substring(1, p.length - 1);
47       });
48       
49       return !options.assert 
50         ? assertProperties(exposed, options.properties)
51         : options.assert(exposed);
52     }
53   }
54 }
55
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']
61     }),
62     "and passed multiple `string` arguments": testExposes({
63       script: 'multiple-properties.js',
64       properties: ['version', 'author']
65     }),
66     "and passed an `object` argument": testExposes({
67       script: 'object-argument.js',
68       properties: ['version', 'author']
69     }),
70     "and passed an `array` argument": testExposes({
71       script: 'array-argument.js',
72       properties: ['version', 'author']
73     }),
74     "and read from a specified directory": testExposes({
75       script: 'target-dir.js',
76       assert: compareWithExample('subdir')
77     }),
78     "and passed no arguments": testExposes({
79       script: 'all-properties.js',
80       assert: compareWithExample()
81     })
82   }
83 }).export(module);