normalize.js 6.6 KB

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