Skip to content
This repository was archived by the owner on Aug 11, 2022. It is now read-only.

Commit 4ef1412

Browse files
smikesothiym23
authored andcommitted
publish, config: move tag-semver test
add usage info to publish move tag-semver check to publish test publish instead of config
1 parent ad53d0f commit 4ef1412

4 files changed

Lines changed: 91 additions & 37 deletions

File tree

lib/config/defaults.js

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,6 @@ nopt.typeDefs.semver = { type: semver, validate: validateSemver }
4747
nopt.typeDefs.Stream = { type: Stream, validate: validateStream }
4848
nopt.typeDefs.Umask = { type: Umask, validate: validateUmask }
4949

50-
// Don't let --tag=1.2.3 ever be a thing
51-
var tag = {}
52-
nopt.typeDefs.tag = { type: tag, validate: validateTag }
53-
5450
nopt.invalidHandler = function (k, val, type) {
5551
log.warn("invalid config", k + "=" + JSON.stringify(val))
5652

@@ -60,9 +56,6 @@ nopt.invalidHandler = function (k, val, type) {
6056
}
6157

6258
switch (type) {
63-
case tag:
64-
log.warn("invalid config", "Tag must not be a SemVer range")
65-
break
6659
case Umask:
6760
log.warn("invalid config", "Must be umask, octal number in range 0000..0777")
6861
break
@@ -312,7 +305,7 @@ exports.types =
312305
, "sign-git-tag": Boolean
313306
, spin: ["always", Boolean]
314307
, "strict-ssl": Boolean
315-
, tag : tag
308+
, tag : String
316309
, tmp : path
317310
, unicode : Boolean
318311
, "unsafe-perm" : Boolean

lib/publish.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@ var npm = require("./npm.js")
1313
, cachedPackageRoot = require("./cache/cached-package-root.js")
1414
, createReadStream = require("graceful-fs").createReadStream
1515
, npa = require("npm-package-arg")
16+
, semver = require('semver')
1617

17-
publish.usage = "npm publish <tarball>"
18-
+ "\nnpm publish <folder>"
18+
publish.usage = "npm publish <tarball> [--tag <tagname>]"
19+
+ "\nnpm publish <folder> [--tag <tagname>]"
1920
+ "\n\nPublishes '.' if no argument supplied"
21+
+ "\n\nSets tag `latest` if no --tag specified"
2022

2123
publish.completion = function (opts, cb) {
2224
// publish can complete to a folder with a package.json
@@ -34,6 +36,13 @@ function publish (args, isRetry, cb) {
3436
if (args.length !== 1) return cb(publish.usage)
3537

3638
log.verbose("publish", args)
39+
40+
var t = npm.config.get('tag').trim()
41+
if (semver.validRange(t)) {
42+
var er = new Error("Tag name must not be a valid SemVer range: " + t)
43+
return cb(er)
44+
}
45+
3746
var arg = args[0]
3847
// if it's a local folder, then run the prepublish there, first.
3948
readJson(path.resolve(arg, "package.json"), function (er, data) {

test/tap/config-semver-tag.js

Lines changed: 0 additions & 27 deletions
This file was deleted.
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
var common = require('../common-tap.js')
2+
var test = require('tap').test
3+
var npm = require('../../lib/npm.js')
4+
var mkdirp = require('mkdirp')
5+
var rimraf = require('rimraf')
6+
var path = require('path')
7+
var fs = require('fs')
8+
var mr = require('npm-registry-mock')
9+
10+
var osenv = require('osenv')
11+
12+
var PKG_DIR = path.resolve(__dirname, 'publish-invalid-semver-tag')
13+
var CACHE_DIR = path.resolve(PKG_DIR, 'cache')
14+
15+
var DEFAULT_PKG = {
16+
'name': 'examples',
17+
'version': '1.2.3'
18+
}
19+
20+
var mockServer
21+
22+
function resetPackage (options) {
23+
rimraf.sync(CACHE_DIR)
24+
mkdirp.sync(CACHE_DIR)
25+
26+
fs.writeFileSync(path.resolve(PKG_DIR, 'package.json'), DEFAULT_PKG)
27+
}
28+
29+
test('setup', function (t) {
30+
process.chdir(osenv.tmpdir())
31+
mkdirp.sync(PKG_DIR)
32+
process.chdir(PKG_DIR)
33+
34+
resetPackage({})
35+
36+
mr({ port: common.port }, function (er, server) {
37+
npm.load({
38+
cache: CACHE_DIR,
39+
registry: common.registry,
40+
cwd: PKG_DIR
41+
}, function (err) {
42+
t.ifError(err, 'started server')
43+
mockServer = server
44+
45+
t.end()
46+
})
47+
})
48+
})
49+
50+
test('attempt publish with semver-like version', function (t) {
51+
resetPackage({})
52+
53+
npm.config.set('tag', 'v1.x')
54+
npm.commands.publish([], function (err) {
55+
t.notEqual(err, null)
56+
t.same(err.message, 'Tag name must not be a valid SemVer range: v1.x')
57+
t.end()
58+
})
59+
})
60+
61+
test('attempt publish with semver-like version', function (t) {
62+
resetPackage({})
63+
64+
npm.config.set('tag', '1.2.3')
65+
npm.commands.publish([], function (err) {
66+
t.notEqual(err, null)
67+
t.same(err.message, 'Tag name must not be a valid SemVer range: 1.2.3')
68+
t.end()
69+
})
70+
})
71+
72+
test('cleanup', function (t) {
73+
mockServer.close()
74+
75+
process.chdir(osenv.tmpdir())
76+
rimraf.sync(PKG_DIR)
77+
78+
t.end()
79+
})

0 commit comments

Comments
 (0)