Skip to content

Commit d3861a9

Browse files
committed
fix: check if versions are valid in diff compare (npm#553)
1 parent 503a4e5 commit d3861a9

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

functions/diff.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ const parse = require('./parse')
33
const diff = (version1, version2) => {
44
const v1 = parse(version1)
55
const v2 = parse(version2)
6+
if (v1 === null || v2 === null) {
7+
throw new TypeError(`Invalid Version: null`)
8+
}
69
const comparison = v1.compare(v2)
710

811
if (comparison === 0) {

test/functions/diff.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,22 @@ test('diff versions test', (t) => {
3232
['1.0.0-1', '2.0.0-1', 'premajor'],
3333
['1.0.0-1', '1.1.0-1', 'preminor'],
3434
['1.0.0-1', '1.0.1-1', 'prepatch'],
35+
['foo', '1.0.1-1', null, 'Invalid Version: null'],
36+
['1.0.1-1', 'foo', null, 'Invalid Version: null'],
3537
].forEach((v) => {
3638
const version1 = v[0]
3739
const version2 = v[1]
3840
const wanted = v[2]
39-
const found = diff(version1, version2)
41+
const error = v[3]
4042
const cmd = `diff(${version1}, ${version2})`
41-
t.equal(found, wanted, `${cmd} === ${wanted}`)
43+
if (!error) {
44+
const found = diff(version1, version2)
45+
t.equal(found, wanted, `${cmd} === ${wanted}`)
46+
} else {
47+
t.throws(function () {
48+
diff(version1, version2)
49+
}, new TypeError(v[3]), `${cmd} must throw an ${error} error`)
50+
}
4251
})
4352

4453
t.end()

0 commit comments

Comments
 (0)