Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ^ and ~ range matches on x.y patterns #658

Merged
merged 2 commits into from
Apr 9, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

~0 is not a legal semver

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);
});

});