Bug:Fix file validation issue
[vnfsdk/refrepo.git] / vnfmarket / src / main / webapp / vnfmarket / node_modules / normalize-package-data / test / normalize.js~
1 var tap = require("tap")
2 var fs = require("fs")
3 var path = require("path")
4
5 var globals = Object.keys(global)
6
7 var normalize = require("../lib/normalize")
8 var warningMessages = require("../lib/warning_messages.json")
9 var safeFormat = require("../lib/safe_format")
10
11 var rpjPath = path.resolve(__dirname,"./fixtures/read-package-json.json")
12
13 tap.test("normalize some package data", function(t) {
14   var packageData = require(rpjPath)
15   var warnings = []
16   normalize(packageData, function(warning) {
17     warnings.push(warning)
18   })
19   // there's no readme data in this particular object
20   t.equal( warnings.length, 1, "There's exactly one warning.")
21   fs.readFile(rpjPath, function(err, data) {
22     if(err) throw err
23     // Various changes have been made
24     t.notEqual(packageData, JSON.parse(data), "Output is different from input.")
25     t.end()
26   })
27 })
28
29 tap.test("runs without passing warning function", function(t) {
30   var packageData = require(rpjPath)
31   fs.readFile(rpjPath, function(err, data) {
32     if(err) throw err
33     normalize(JSON.parse(data))
34     t.ok(true, "If you read this, this means I'm still alive.")
35     t.end()
36   })
37 })
38
39 tap.test("empty object", function(t) {
40   var packageData = {}
41   var expect =
42     { name: '',
43       version: '',
44       readme: 'ERROR: No README data found!',
45       _id: '@' }
46
47   var warnings = []
48   function warn(m) {
49     warnings.push(m)
50   }
51   normalize(packageData, warn)
52   t.same(packageData, expect)
53   t.same(warnings, [
54     warningMessages.missingDescription,
55     warningMessages.missingRepository,
56     warningMessages.missingReadme,
57     warningMessages.missingLicense
58   ])
59   t.end()
60 })
61
62 tap.test("core module name", function(t) {
63   var warnings = []
64   function warn(m) {
65     warnings.push(m)
66   }
67   var a
68   normalize(a={
69     name: "http",
70     readme: "read yourself how about",
71     homepage: 123,
72     bugs: "what is this i don't even",
73     repository: "Hello."
74   }, warn)
75
76   var expect = [
77       safeFormat(warningMessages.conflictingName, 'http'),
78       warningMessages.nonEmailUrlBugsString,
79       warningMessages.emptyNormalizedBugs,
80       warningMessages.nonUrlHomepage,
81       warningMessages.missingLicense
82       ]
83   t.same(warnings, expect)
84   t.end()
85 })
86
87 tap.test("urls required", function(t) {
88   var warnings = []
89   function warn(w) {
90     warnings.push(w)
91   }
92   normalize({
93     bugs: {
94       url: "/1",
95       email: "not an email address"
96     }
97   }, warn)
98   var a
99   normalize(a={
100     readme: "read yourself how about",
101     homepage: 123,
102     bugs: "what is this i don't even",
103     repository: "Hello."
104   }, warn)
105
106   console.error(a)
107
108   var expect =
109     [ warningMessages.missingDescription,
110       warningMessages.missingRepository,
111       warningMessages.nonUrlBugsUrlField,
112       warningMessages.nonEmailBugsEmailField,
113       warningMessages.emptyNormalizedBugs,
114       warningMessages.missingReadme,
115       warningMessages.missingLicense,
116       warningMessages.nonEmailUrlBugsString,
117       warningMessages.emptyNormalizedBugs,
118       warningMessages.nonUrlHomepage,
119       warningMessages.missingLicense]
120   t.same(warnings, expect)
121   t.end()
122 })
123
124 tap.test("homepage field must start with a protocol.", function(t) {
125   var warnings = []
126   function warn(w) {
127     warnings.push(w)
128   }
129   var a
130   normalize(a={
131     homepage: 'example.org'
132   }, warn)
133
134   console.error(a)
135
136   var expect =
137     [ warningMessages.missingDescription,
138       warningMessages.missingRepository,
139       warningMessages.missingReadme,
140       warningMessages.missingProtocolHomepage,
141       warningMessages.missingLicense]
142   t.same(warnings, expect)
143   t.same(a.homepage, 'http://example.org')
144   t.end()
145 })
146
147 tap.test("license field should be a valid SPDX expression", function(t) {
148   var warnings = []
149   function warn(w) {
150     warnings.push(w)
151   }
152   var a
153   normalize(a={
154     license: 'Apache 2'
155   }, warn)
156
157   console.error(a)
158
159   var expect =
160     [ warningMessages.missingDescription,
161       warningMessages.missingRepository,
162       warningMessages.missingReadme,
163       warningMessages.invalidLicense]
164   t.same(warnings, expect)
165   t.end()
166 })
167
168 tap.test("gist bugs url", function(t) {
169   var d = {
170     repository: "git@gist.github.com:123456.git"
171   }
172   normalize(d)
173   t.same(d.repository, { type: 'git', url: 'git+ssh://git@gist.github.com/123456.git' })
174   t.same(d.bugs, { url: 'https://gist.github.com/123456' })
175   t.end();
176 });
177
178 tap.test("singularize repositories", function(t) {
179   var d = {repositories:["git@gist.github.com:123456.git"]}
180   normalize(d)
181   t.same(d.repository, { type: 'git', url: 'git+ssh://git@gist.github.com/123456.git' })
182   t.end()
183 });
184
185 tap.test("treat visionmedia/express as github repo", function(t) {
186   var d = {repository: {type: "git", url: "visionmedia/express"}}
187   normalize(d)
188   t.same(d.repository, { type: "git", url: "git+https://github.com/visionmedia/express.git" })
189   t.end()
190 });
191
192 tap.test("treat isaacs/node-graceful-fs as github repo", function(t) {
193   var d = {repository: {type: "git", url: "isaacs/node-graceful-fs"}}
194   normalize(d)
195   t.same(d.repository, { type: "git", url: "git+https://github.com/isaacs/node-graceful-fs.git" })
196   t.end()
197 });
198
199 tap.test("homepage field will set to github url if repository is a github repo", function(t) {
200   var a
201   normalize(a={
202     repository: { type: "git", url: "https://github.com/isaacs/node-graceful-fs" }
203   })
204   t.same(a.homepage, 'https://github.com/isaacs/node-graceful-fs#readme')
205   t.end()
206 })
207
208 tap.test("homepage field will set to github gist url if repository is a gist", function(t) {
209   var a
210   normalize(a={
211     repository: { type: "git", url: "git@gist.github.com:123456.git" }
212   })
213   t.same(a.homepage, 'https://gist.github.com/123456')
214   t.end()
215 })
216
217 tap.test("homepage field will set to github gist url if repository is a shorthand reference", function(t) {
218   var a
219   normalize(a={
220     repository: { type: "git", url: "sindresorhus/chalk" }
221   })
222   t.same(a.homepage, 'https://github.com/sindresorhus/chalk#readme')
223   t.end()
224 })
225
226 tap.test("don't mangle github shortcuts in dependencies", function(t) {
227   var d = {dependencies: {"node-graceful-fs": "isaacs/node-graceful-fs"}}
228   normalize(d)
229   t.same(d.dependencies, {"node-graceful-fs": "github:isaacs/node-graceful-fs" })
230   t.end()
231 });
232
233 tap.test("deprecation warning for array in dependencies fields", function(t) {
234   var a
235   var warnings = []
236   function warn(w) {
237     warnings.push(w)
238   }
239   normalize(a={
240     dependencies: [],
241     devDependencies: [],
242     optionalDependencies: []
243   }, warn)
244   t.ok(~warnings.indexOf(safeFormat(warningMessages.deprecatedArrayDependencies, 'dependencies')), "deprecation warning")
245   t.ok(~warnings.indexOf(safeFormat(warningMessages.deprecatedArrayDependencies, 'devDependencies')), "deprecation warning")
246   t.ok(~warnings.indexOf(safeFormat(warningMessages.deprecatedArrayDependencies, 'optionalDependencies')), "deprecation warning")
247   t.end()
248 })
249
250 tap.test('no new globals', function(t) {
251   t.same(Object.keys(global), globals)
252   t.end()
253 })