diff options
author | Evan Lucas <evanlucas@me.com> | 2014-07-25 21:07:46 +0400 |
---|---|---|
committer | Forrest L Norvell <forrest@npmjs.com> | 2014-09-21 11:26:18 +0400 |
commit | 36356011b6f2e6a5a81490e85a0a44eb27199dd7 (patch) | |
tree | 95c25071673b6f4db07a49e47005c62f161c81cd /test | |
parent | b90a91a6d2ea962f39d018eb2c6d745d4bfdb2b3 (diff) |
add `npm view .`
Fixes #5520
This adds back the ability to run `npm view .`
Includes multiple tests for `npm view`
Diffstat (limited to 'test')
-rw-r--r-- | test/tap/view.js | 221 |
1 files changed, 221 insertions, 0 deletions
diff --git a/test/tap/view.js b/test/tap/view.js new file mode 100644 index 000000000..568e603a8 --- /dev/null +++ b/test/tap/view.js @@ -0,0 +1,221 @@ +var common = require("../common-tap.js") +var test = require("tap").test +var osenv = require("osenv") +var path = require("path") +var fs = require("fs") +var rimraf = require("rimraf") +var mkdirp = require("mkdirp") +var tmp = osenv.tmpdir() +var t1dir = path.resolve(tmp, "view-local-no-pkg") +var t2dir = path.resolve(tmp, "view-local-notmine") +var t3dir = path.resolve(tmp, "view-local-mine") +var mr = require("npm-registry-mock") + +test("setup", function(t) { + mkdirp.sync(t1dir) + mkdirp.sync(t2dir) + mkdirp.sync(t3dir) + + fs.writeFileSync(t2dir + "/package.json", JSON.stringify({ + author: "Evan Lucas" + , name: "test-repo-url-https" + , version: "0.0.1" + }), "utf8") + + fs.writeFileSync(t3dir + "/package.json", JSON.stringify({ + author: "Evan Lucas" + , name: "biscuits" + , version: "0.0.1" + }), "utf8") + + t.pass("created fixtures") + t.end() +}) + +test("npm view . in global mode", function(t) { + process.chdir(t1dir) + common.npm([ + "view" + , "." + , "--registry=" + common.registry + , "--global" + ], { cwd: t1dir }, function(err, code, stdout, stderr) { + t.ifError(err, "error should not exist") + t.equal(code, 1, "exit not ok") + t.similar(stderr, /Cannot use view command in global mode./m) + t.end() + }) +}) + +test("npm view . with no package.json", function(t) { + process.chdir(t1dir) + common.npm([ + "view" + , "." + , "--registry=" + common.registry + ], { cwd: t1dir }, function(err, code, stdout, stderr) { + t.ifError(err, "error should not exist") + t.equal(code, 1, "exit not ok") + t.similar(stderr, /Invalid package.json/m) + t.end() + }) +}) + +test("npm view . with no published package", function(t) { + process.chdir(t3dir) + mr(common.port, function(s) { + common.npm([ + "view" + , "." + , "--registry=" + common.registry + ], { cwd: t3dir }, function(err, code, stdout, stderr) { + t.ifError(err, "error should not exist") + t.equal(code, 1, "exit not ok") + t.similar(stderr, /version not found/m) + s.close() + t.end() + }) + }) +}) + +test("npm view .", function(t) { + process.chdir(t2dir) + mr(common.port, function(s) { + common.npm([ + "view" + , "." + , "--registry=" + common.registry + ], { cwd: t2dir }, function(err, code, stdout) { + t.ifError(err, "error should not exist") + t.equal(code, 0, "exit ok") + var re = new RegExp("name: 'test-repo-url-https'") + t.similar(stdout, re) + s.close() + t.end() + }) + }) +}) + +test("npm view . select fields", function(t) { + process.chdir(t2dir) + mr(common.port, function(s) { + common.npm([ + "view" + , "." + , "main" + , "--registry=" + common.registry + ], { cwd: t2dir }, function(err, code, stdout) { + t.ifError(err, "error should not exist") + t.equal(code, 0, "exit ok") + t.equal(stdout.trim(), "index.js", "should print `index.js`") + s.close() + t.end() + }) + }) +}) + +test("npm view .@<version>", function(t) { + process.chdir(t2dir) + mr(common.port, function(s) { + common.npm([ + "view" + , ".@0.0.0" + , "version" + , "--registry=" + common.registry + ], { cwd: t2dir }, function(err, code, stdout) { + t.ifError(err, "error should not exist") + t.equal(code, 0, "exit ok") + t.equal(stdout.trim(), "0.0.0", "should print `0.0.0`") + s.close() + t.end() + }) + }) +}) + +test("npm view .@<version> --json", function(t) { + process.chdir(t2dir) + mr(common.port, function(s) { + common.npm([ + "view" + , ".@0.0.0" + , "version" + , "--json" + , "--registry=" + common.registry + ], { cwd: t2dir }, function(err, code, stdout) { + t.ifError(err, "error should not exist") + t.equal(code, 0, "exit ok") + t.equal(stdout.trim(), "\"0.0.0\"", "should print `\"0.0.0\"`") + s.close() + t.end() + }) + }) +}) + +test("npm view <package name>", function(t) { + mr(common.port, function(s) { + common.npm([ + "view" + , "underscore" + , "--registry=" + common.registry + ], { cwd: t2dir }, function(err, code, stdout) { + t.ifError(err, "error should not exist") + t.equal(code, 0, "exit ok") + var re = new RegExp("name: 'underscore'") + t.similar(stdout, re, "should have name `underscore`") + s.close() + t.end() + }) + }) +}) + +test("npm view <package name> --json", function(t) { + t.plan(3) + mr(common.port, function(s) { + common.npm([ + "view" + , "underscore" + , "--json" + , "--registry=" + common.registry + ], { cwd: t2dir }, function(err, code, stdout) { + s.close() + t.ifError(err, "error should not exist") + t.equal(code, 0, "exit ok") + try { + var out = JSON.parse(stdout.trim()) + t.similar(out, { + maintainers: "jashkenas <jashkenas@gmail.com>" + }, "should have the same maintainer") + } + catch (er) { + t.fail("Unable to parse JSON") + } + }) + }) +}) + +test("npm view <package name> <field>", function(t) { + mr(common.port, function(s) { + common.npm([ + "view" + , "underscore" + , "homepage" + , "--registry=" + common.registry + ], { cwd: t2dir }, function(err, code, stdout) { + t.ifError(err, "error should not exist") + t.equal(code, 0, "exit ok") + t.equal(stdout.trim(), "http://underscorejs.org", + "homepage should equal `http://underscorejs.org`") + s.close() + t.end() + }) + }) +}) + +test("cleanup", function(t) { + process.chdir(osenv.tmpdir()) + rimraf.sync(t1dir) + rimraf.sync(t2dir) + rimraf.sync(t3dir) + t.pass("cleaned up") + t.end() +}) |