1 var tap = require("tap")
3 var path = require("path")
5 var globals = Object.keys(global)
7 var normalize = require("../lib/normalize")
8 var warningMessages = require("../lib/warning_messages.json")
9 var safeFormat = require("../lib/safe_format")
11 var rpjPath = path.resolve(__dirname,"./fixtures/read-package-json.json")
13 tap.test("normalize some package data", function(t) {
14 var packageData = require(rpjPath)
16 normalize(packageData, function(warning) {
17 warnings.push(warning)
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) {
23 // Various changes have been made
24 t.notEqual(packageData, JSON.parse(data), "Output is different from input.")
29 tap.test("runs without passing warning function", function(t) {
30 var packageData = require(rpjPath)
31 fs.readFile(rpjPath, function(err, data) {
33 normalize(JSON.parse(data))
34 t.ok(true, "If you read this, this means I'm still alive.")
39 tap.test("empty object", function(t) {
44 readme: 'ERROR: No README data found!',
51 normalize(packageData, warn)
52 t.same(packageData, expect)
54 warningMessages.missingDescription,
55 warningMessages.missingRepository,
56 warningMessages.missingReadme,
57 warningMessages.missingLicense
62 tap.test("core module name", function(t) {
70 readme: "read yourself how about",
72 bugs: "what is this i don't even",
77 safeFormat(warningMessages.conflictingName, 'http'),
78 warningMessages.nonEmailUrlBugsString,
79 warningMessages.emptyNormalizedBugs,
80 warningMessages.nonUrlHomepage,
81 warningMessages.missingLicense
83 t.same(warnings, expect)
87 tap.test("urls required", function(t) {
95 email: "not an email address"
100 readme: "read yourself how about",
102 bugs: "what is this i don't even",
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)
124 tap.test("homepage field must start with a protocol.", function(t) {
131 homepage: 'example.org'
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')
147 tap.test("license field should be a valid SPDX expression", function(t) {
160 [ warningMessages.missingDescription,
161 warningMessages.missingRepository,
162 warningMessages.missingReadme,
163 warningMessages.invalidLicense]
164 t.same(warnings, expect)
168 tap.test("gist bugs url", function(t) {
170 repository: "git@gist.github.com:123456.git"
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' })
178 tap.test("singularize repositories", function(t) {
179 var d = {repositories:["git@gist.github.com:123456.git"]}
181 t.same(d.repository, { type: 'git', url: 'git+ssh://git@gist.github.com/123456.git' })
185 tap.test("treat visionmedia/express as github repo", function(t) {
186 var d = {repository: {type: "git", url: "visionmedia/express"}}
188 t.same(d.repository, { type: "git", url: "git+https://github.com/visionmedia/express.git" })
192 tap.test("treat isaacs/node-graceful-fs as github repo", function(t) {
193 var d = {repository: {type: "git", url: "isaacs/node-graceful-fs"}}
195 t.same(d.repository, { type: "git", url: "git+https://github.com/isaacs/node-graceful-fs.git" })
199 tap.test("homepage field will set to github url if repository is a github repo", function(t) {
202 repository: { type: "git", url: "https://github.com/isaacs/node-graceful-fs" }
204 t.same(a.homepage, 'https://github.com/isaacs/node-graceful-fs#readme')
208 tap.test("homepage field will set to github gist url if repository is a gist", function(t) {
211 repository: { type: "git", url: "git@gist.github.com:123456.git" }
213 t.same(a.homepage, 'https://gist.github.com/123456')
217 tap.test("homepage field will set to github gist url if repository is a shorthand reference", function(t) {
220 repository: { type: "git", url: "sindresorhus/chalk" }
222 t.same(a.homepage, 'https://github.com/sindresorhus/chalk#readme')
226 tap.test("don't mangle github shortcuts in dependencies", function(t) {
227 var d = {dependencies: {"node-graceful-fs": "isaacs/node-graceful-fs"}}
229 t.same(d.dependencies, {"node-graceful-fs": "github:isaacs/node-graceful-fs" })
233 tap.test("deprecation warning for array in dependencies fields", function(t) {
242 optionalDependencies: []
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")
250 tap.test('no new globals', function(t) {
251 t.same(Object.keys(global), globals)