Skip to content

Commit

Permalink
Merge pull request #658 from jspm/semver-fixes
Browse files Browse the repository at this point in the history
Fix ^ and ~ range matches on x.y patterns
  • Loading branch information
crisptrutski committed Apr 9, 2015
2 parents 198cf17 + ab60739 commit 0923d8e
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 4 deletions.
6 changes: 3 additions & 3 deletions lib/semver.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,9 @@ function matchParsed(range, version) {
// ^0.0.x falls down to exact match below
}

// check fuzzy range
// check fuzzy range (we can assume rangeVersion.minor exists, due to behaviour switch)
if (range.fuzzy)
return version.major === rangeVersion.major && version.minor < (rangeVersion.minor || 0) + 1;
return version.major === rangeVersion.major && version.minor <= rangeVersion.minor;

// exact match
// eg 001.002.003 matches 1.2.3
Expand Down Expand Up @@ -177,7 +177,7 @@ function parseRange(range) {
}

// ^0.0 behaves like ~0.0
if (rangeObj.semver && !isNaN(rangeVersion.minor) && isNaN(rangeVersion.patch)) {
if (rangeObj.semver && rangeObj.major === 0 && !isNaN(rangeVersion.minor) && isNaN(rangeVersion.patch)) {
rangeObj.semver = false;
rangeObj.fuzzy = true;
}
Expand Down
47 changes: 46 additions & 1 deletion test/semver.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,15 @@ suite('Semver Compare', function() {


suite('Semver Compatibility Ranges', function() {

test('Basic compatibility', function() {
// project.showLogs = true;
// assert.equal(semver.match('^1.5.2', '1.4.0'), false);
assert.equal(semver.match('^1', '1.4.0'), true);
assert.equal(semver.match('^0.1', '0.1.0'), true);
assert.equal(semver.match('^0.1', '0.2.0'), false);
assert.equal(semver.match('^1.1', '1.2.0'), true);
assert.equal(semver.match('^1.1', '1.1.0'), true);
assert.equal(semver.match('^0.0.2', '1.0.2'), false);
assert.equal(semver.match('^0.0.1', '0.0.1'), true);
assert.equal(semver.match('^0.0.1', '0.0.2'), false);
Expand Down Expand Up @@ -133,3 +137,44 @@ suite('Semver Compatibility Ranges', function() {
});

});

suite('Fuzzy Compatibility Ranges', function() {

test('Basic compatibility', function() {
// project.showLogs = true;
// assert.equal(semver.match('^1.5.2', '1.4.0'), false);
assert.equal(semver.match('~0', '0.1.0'), true);
assert.equal(semver.match('~0', '1.2.0'), false);
assert.equal(semver.match('~1', '1.2.0'), true);
assert.equal(semver.match('~1', '2.4.0'), false);
assert.equal(semver.match('~0.1', '0.1.0'), true);
assert.equal(semver.match('~0.1', '0.2.0'), false);
assert.equal(semver.match('~1.1', '1.2.0'), false);
assert.equal(semver.match('~1.1', '1.1.0'), true);
assert.equal(semver.match('~0.0.2', '1.0.2'), false);
assert.equal(semver.match('~0.0.1', '0.0.1'), true);
assert.equal(semver.match('~0.0.1', '0.0.2'), true);
assert.equal(semver.match('~0.0.1', '1.0.2'), false);
assert.equal(semver.match('~0.0.1', '1.0.1'), false);
assert.equal(semver.match('~0.1.0', '0.1.0'), true);
assert.equal(semver.match('~0.1.0', '0.2.0'), false);
assert.equal(semver.match('~0.1.0', '1.1.0'), false);
});

test('Semver compatibility', function() {
assert.equal(semver.match('~1.1.12', '1.1.12'), true);
assert.equal(semver.match('~1.1.12', '1.1.11'), false);
assert.equal(semver.match('~1.1.12', '1.1.345'), true);
assert.equal(semver.match('~1.1.12', '1.10.345'), false);
assert.equal(semver.match('~1.1.12', '2.10.345'), false);
});

test('Prerelease ranges', function() {
assert.equal(semver.match('~1.0.4-alpha.1', '1.0.4-alpha.1'), true);
assert.equal(semver.match('~1.0.4-alpha.1', '1.0.4-alpha.2'), true);
assert.equal(semver.match('~1.0.4-alpha.1', '1.0.4-beta'), true);
assert.equal(semver.match('~1.0.4-alpha.1', '1.0.4-beta.10'), true);
assert.equal(semver.match('~1.0.4-alpha.1', '1.0.4'), true);
});

});

0 comments on commit 0923d8e

Please sign in to comment.