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