npm: Upgrade to 1.3.19
[platform/upstream/nodejs.git] / deps / npm / test / tap / config-meta.js
1 // this is a weird meta test.  It verifies that all the instances of
2 // `npm.config.get(...)` are:
3 // a) Simple strings, and not variables
4 // b) Documented
5 // c) Defined in the `npmconf` package.
6
7 var test = require("tap").test
8 var fs = require("fs")
9 var path = require("path")
10 var root = path.resolve(__dirname, "..", "..")
11 var lib = path.resolve(root, "lib")
12 var nm = path.resolve(root, "node_modules")
13 var doc = path.resolve(root, "doc/misc/npm-config.md")
14 var FILES = []
15 var CONFS = {}
16 var DOC = {}
17
18 var exceptions = [
19   path.resolve(lib, "config.js"),
20   path.resolve(lib, "utils", "lifecycle.js")
21 ]
22
23 test("get files", function (t) {
24   walk(nm)
25   walk(lib)
26   t.pass("got files")
27   t.end()
28
29   function walk(lib) {
30     var files = fs.readdirSync(lib).map(function (f) {
31       return path.resolve(lib, f)
32     })
33     files.forEach(function (f) {
34       if (fs.statSync(f).isDirectory())
35         walk(f)
36       else if (f.match(/\.js$/))
37         FILES.push(f)
38     })
39   }
40 })
41
42 test("get lines", function (t) {
43   FILES.forEach(function (f) {
44     console.error(f)
45     var lines = fs.readFileSync(f, 'utf8').split('\n')
46     lines.forEach(function (l, i) {
47       var matches = l.split(/conf(?:ig)?\.get\(/g)
48       matches.shift()
49       matches.forEach(function (m) {
50         m = m.split(')').shift()
51         var literal = m.match(/^['"].+['"]$/)
52         if (literal) {
53           m = m.slice(1, -1)
54           if (!m.match(/^\_/) && m !== 'argv')
55             CONFS[m] = {
56               file: f,
57               line: i
58             }
59         } else if (exceptions.indexOf(f) === -1) {
60           t.fail("non-string-literal config used in " + f + ":" + i)
61         }
62       })
63     })
64   })
65   t.pass("got lines")
66   t.end()
67 })
68
69 test("get docs", function (t) {
70   var d = fs.readFileSync(doc, "utf8").split("\n")
71   // walk down until the "## Config Settings" section
72   for (var i = 0; i < d.length && d[i] !== "## Config Settings"; i++);
73   i++
74   // now gather up all the ^###\s lines until the next ^##\s
75   var doclines = []
76   for (; i < d.length && !d[i].match(/^## /); i++) {
77     if (d[i].match(/^### /))
78       DOC[ d[i].replace(/^### /, '').trim() ] = true
79   }
80   t.pass("read the docs")
81   console.error(DOC)
82   t.end()
83 })
84
85 test("check configs", function (t) {
86   var defs = require("npmconf/config-defs.js")
87   var types = Object.keys(defs.types)
88   var defaults = Object.keys(defs.defaults)
89
90   for (var c in CONFS) {
91     if (CONFS[c].file.indexOf(lib) === 0) {
92       t.ok(DOC[c], "should be documented " + c + " "
93           + CONFS[c].file + ":" + CONFS[c].line)
94       t.ok(types.indexOf(c) !== -1, "should be defined in npmconf " + c)
95       t.ok(defaults.indexOf(c) !== -1, "should have default in npmconf " + c)
96     }
97   }
98
99   for (var c in DOC) {
100     if (c !== "versions" && c !== "version") {
101       t.ok(CONFS[c], "config in doc should be used somewhere " + c)
102       t.ok(types.indexOf(c) !== -1, "should be defined in npmconf " + c)
103       t.ok(defaults.indexOf(c) !== -1, "should have default in npmconf " + c)
104     }
105   }
106
107   types.forEach(function(c) {
108     if (!c.match(/^\_/) && c !== 'argv' && !c.match(/^versions?$/)) {
109       t.ok(DOC[c], 'defined type should be documented ' + c)
110       t.ok(CONFS[c], 'defined type should be used ' + c)
111     }
112   })
113
114   defaults.forEach(function(c) {
115     if (!c.match(/^\_/) && c !== 'argv' && !c.match(/^versions?$/)) {
116       t.ok(DOC[c], 'defaulted type should be documented ' + c)
117       t.ok(CONFS[c], 'defaulted type should be used ' + c)
118     }
119   })
120
121   t.end()
122 })