more legible error message in config.get/set

Test added by @isaacs
Closes #4458
This commit is contained in:
Yazhong Liu
2014-01-10 15:47:40 +08:00
committed by isaacs
parent d4f6f3ff83
commit 8ebe852c8f
2 changed files with 56 additions and 1 deletions

View File

@@ -31,7 +31,15 @@ var EventEmitter = require("events").EventEmitter
, chain = slide.chain
, RegClient = require("npm-registry-client")
npm.config = {loaded: false}
npm.config = {
loaded: false,
get: function() {
throw new Error('npm.load() required')
},
set: function() {
throw new Error('npm.load() required')
}
}
// /usr/local is often a read-only fs, which is not
// well handled by node or mkdirp. Just double-check

View File

@@ -0,0 +1,47 @@
var test = require("tap").test
var npm = require("../..")
var path = require("path")
var rimraf = require("rimraf")
var npmrc = path.join(__dirname, "npmrc")
var fs = require("fs")
test("setup", function (t) {
fs.writeFileSync(npmrc, "foo = bar\n", "ascii")
t.end()
})
test("calling set/get on config pre-load should throw", function (t) {
var threw = true
try {
npm.config.get("foo")
threw = false
} catch (er) {
t.equal(er.message, "npm.load() required")
} finally {
t.ok(threw, "get before load should throw")
}
var threw = true
try {
npm.config.set("foo", "bar")
threw = false
} catch (er) {
t.equal(er.message, "npm.load() required")
} finally {
t.ok(threw, "set before load should throw")
}
npm.load({ userconfig: npmrc }, function (er) {
if (er)
throw er
t.equal(npm.config.get("foo"), "bar")
npm.config.set("foo", "baz")
t.equal(npm.config.get("foo"), "baz")
t.end()
})
})
test("cleanup", function (t) {
rimraf.sync(npmrc)
t.end()
})