1 var common = require("../common-tap.js")
2 var test = require("tap").test
3 var osenv = require("osenv")
4 var path = require("path")
6 var rimraf = require("rimraf")
7 var mkdirp = require("mkdirp")
8 var tmp = osenv.tmpdir()
9 var t1dir = path.resolve(tmp, "view-local-no-pkg")
10 var t2dir = path.resolve(tmp, "view-local-notmine")
11 var t3dir = path.resolve(tmp, "view-local-mine")
12 var mr = require("npm-registry-mock")
14 test("setup", function (t) {
19 fs.writeFileSync(t2dir + "/package.json", JSON.stringify({
21 , name: "test-repo-url-https"
25 fs.writeFileSync(t3dir + "/package.json", JSON.stringify({
31 t.pass("created fixtures")
35 function plugin (server) {
39 .reply(404, {"error": "version not found"})
42 test("npm view . in global mode", function (t) {
47 , "--registry=" + common.registry
49 ], { cwd: t1dir }, function (err, code, stdout, stderr) {
50 t.ifError(err, "view command finished successfully")
51 t.equal(code, 1, "exit not ok")
52 t.similar(stderr, /Cannot use view command in global mode./m)
57 test("npm view --global", function(t) {
61 , "--registry=" + common.registry
63 ], { cwd: t1dir }, function(err, code, stdout, stderr) {
64 t.ifError(err, "view command finished successfully")
65 t.equal(code, 1, "exit not ok")
66 t.similar(stderr, /Cannot use view command in global mode./m)
71 test("npm view . with no package.json", function(t) {
76 , "--registry=" + common.registry
77 ], { cwd: t1dir }, function (err, code, stdout, stderr) {
78 t.ifError(err, "view command finished successfully")
79 t.equal(code, 1, "exit not ok")
80 t.similar(stderr, /Invalid package.json/m)
85 test("npm view . with no published package", function (t) {
87 mr({port : common.port, plugin : plugin}, function (er, s) {
91 , "--registry=" + common.registry
92 ], { cwd: t3dir }, function (err, code, stdout, stderr) {
93 t.ifError(err, "view command finished successfully")
94 t.equal(code, 1, "exit not ok")
95 t.similar(stderr, /version not found/m)
102 test("npm view .", function (t) {
104 mr({port : common.port, plugin : plugin}, function (er, s) {
108 , "--registry=" + common.registry
109 ], { cwd: t2dir }, function (err, code, stdout) {
110 t.ifError(err, "view command finished successfully")
111 t.equal(code, 0, "exit ok")
112 var re = new RegExp("name: 'test-repo-url-https'")
113 t.similar(stdout, re)
120 test("npm view . select fields", function (t) {
122 mr({port : common.port, plugin : plugin}, function (er, s) {
127 , "--registry=" + common.registry
128 ], { cwd: t2dir }, function (err, code, stdout) {
129 t.ifError(err, "view command finished successfully")
130 t.equal(code, 0, "exit ok")
131 t.equal(stdout.trim(), "index.js", "should print `index.js`")
138 test("npm view .@<version>", function (t) {
140 mr({port : common.port, plugin : plugin}, function (er, s) {
145 , "--registry=" + common.registry
146 ], { cwd: t2dir }, function (err, code, stdout) {
147 t.ifError(err, "view command finished successfully")
148 t.equal(code, 0, "exit ok")
149 t.equal(stdout.trim(), "0.0.0", "should print `0.0.0`")
156 test("npm view .@<version> --json", function (t) {
158 mr({port : common.port, plugin : plugin}, function (er, s) {
164 , "--registry=" + common.registry
165 ], { cwd: t2dir }, function (err, code, stdout) {
166 t.ifError(err, "view command finished successfully")
167 t.equal(code, 0, "exit ok")
168 t.equal(stdout.trim(), "\"0.0.0\"", "should print `\"0.0.0\"`")
175 test("npm view <package name>", function (t) {
176 mr({port : common.port, plugin : plugin}, function (er, s) {
180 , "--registry=" + common.registry
181 ], { cwd: t2dir }, function (err, code, stdout) {
182 t.ifError(err, "view command finished successfully")
183 t.equal(code, 0, "exit ok")
184 var re = new RegExp("name: 'underscore'")
185 t.similar(stdout, re, "should have name `underscore`")
192 test("npm view <package name> --global", function(t) {
193 mr({port : common.port, plugin : plugin}, function (er, s) {
198 , "--registry=" + common.registry
199 ], { cwd: t2dir }, function(err, code, stdout) {
200 t.ifError(err, "view command finished successfully")
201 t.equal(code, 0, "exit ok")
202 var re = new RegExp("name: 'underscore'")
203 t.similar(stdout, re, "should have name `underscore`")
210 test("npm view <package name> --json", function(t) {
212 mr({port : common.port, plugin : plugin}, function (er, s) {
217 , "--registry=" + common.registry
218 ], { cwd: t2dir }, function (err, code, stdout) {
219 t.ifError(err, "view command finished successfully")
220 t.equal(code, 0, "exit ok")
223 var out = JSON.parse(stdout.trim())
225 maintainers: "jashkenas <jashkenas@gmail.com>"
226 }, "should have the same maintainer")
229 t.fail("Unable to parse JSON")
235 test("npm view <package name> <field>", function (t) {
236 mr({port : common.port, plugin : plugin}, function (er, s) {
241 , "--registry=" + common.registry
242 ], { cwd: t2dir }, function (err, code, stdout) {
243 t.ifError(err, "view command finished successfully")
244 t.equal(code, 0, "exit ok")
245 t.equal(stdout.trim(), "http://underscorejs.org",
246 "homepage should equal `http://underscorejs.org`")
253 test("npm view with invalid package name", function (t) {
254 var invalidName = "InvalidPackage"
256 obj["/" + invalidName] = [404, {"error": "not found"}]
258 mr({port : common.port, mocks: {"get": obj}}, function (er, s) {
262 , "--registry=" + common.registry
263 ], {}, function (err, code, stdout, stderr) {
264 t.ifError(err, "view command finished successfully")
265 t.equal(code, 1, "exit not ok")
267 t.similar(stderr, new RegExp("is not in the npm registry"),
268 "Package should NOT be found")
270 t.dissimilar(stderr, new RegExp("use the name yourself!"),
271 "Suggestion should not be there")
273 t.similar(stderr, new RegExp("name can no longer contain capital letters"),
274 "Suggestion about Capital letter should be there")
283 test("npm view with valid but non existent package name", function (t) {
284 mr({port : common.port, mocks: {
286 "/valid-but-non-existent-package" : [404, {"error": "not found"}]
288 }}, function (er, s) {
291 , "valid-but-non-existent-package"
292 , "--registry=" + common.registry
293 ], {}, function (err, code, stdout, stderr) {
294 t.ifError(err, "view command finished successfully")
295 t.equal(code, 1, "exit not ok")
298 new RegExp("'valid-but-non-existent-package' is not in the npm registry\."),
299 "Package should NOT be found")
301 t.similar(stderr, new RegExp("use the name yourself!"),
302 "Suggestion should be there")
310 test("cleanup", function (t) {
311 process.chdir(osenv.tmpdir())