Skip to content

Commit

Permalink
support for dynamically loading latest ref - refs #109 #108
Browse files Browse the repository at this point in the history
  • Loading branch information
Dane Springmeyer committed Mar 5, 2016
1 parent 2e41a68 commit 08437ab
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 24 deletions.
48 changes: 27 additions & 21 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,12 @@
var path = require('path');
var semver = require('semver');
var fs = require('fs');

var versions = [
'2.0.0',
'2.0.1',
'2.0.2',
'2.1.0',
'2.1.1',
'2.2.0',
'2.3.0',
'3.0.0',
'3.0.10',
'3.0.3',
'3.0.4',
'3.0.5',
'3.0.6',
'3.0.7',
'3.0.9'
];
var getVersions = function () {
var names = fs.readdirSync('./');
return names.filter(semver.valid);
};
var versions = semver.sort(getVersions());

// These older versions don't have the datasource info
var no_datasources = [
Expand All @@ -28,14 +18,30 @@ var no_datasources = [
'2.2.0'
]
module.exports.versions = versions;
module.exports.latest = versions[versions.length - 1];

module.exports.load = function(version) {
if (versions.indexOf(version) <= -1) {
throw new Error("Unknown mapnik-reference version: '" + version + "'");
var getSatisfyingVersion = function (wanted) {
var version = semver.maxSatisfying(versions, wanted), parsed;
if (!version) {
try {
parsed = semver(wanted);
parsed.patch = 'x';
version = semver.maxSatisfying(versions, parsed.format());
} catch (err) {
version = null;
}
}
return version;
};

module.exports.load = function(wanted) {
var version = getSatisfyingVersion(wanted);
if (!version) {
throw new Error("Unknown mapnik-reference version: '" + wanted + "'");
}
var ref = require(path.join(__dirname, version, 'reference.json'));
if (no_datasources.indexOf(version) <= -1) {
ref.datasources = require(path.join(__dirname, version, 'datasources.json')).datasources;
ref.datasources = require(path.join(__dirname, version, 'datasources.json')).datasources;
}
return ref;
}
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@
"url": "git://github.com/mapnik/mapnik-reference.git"
},
"scripts": {
"test": "mocha -R spec --timeout 50000"
"test": "mocha -R spec --timeout 50000"
},
"devDependencies": {
"mocha": "1.x",
"glob":"4.x"
"glob": "4.x"
},
"dependencies": {
"semver": "^5.1.0"
}
}
59 changes: 58 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ var assert = require('assert');
var mapnikref = require('../');
var glob = require('glob');
var path = require('path');
var semver = require('semver');

describe('datasources', function() {
mapnikref.versions.forEach(function(key) {
Expand Down Expand Up @@ -38,7 +39,63 @@ describe('versions', function() {
it('show report all versions', function() {
var versions = glob.sync("./*.*.*");
versions = versions.map(function(e) { return path.basename(e) })
assert.deepEqual(versions,mapnikref.versions);
assert.deepEqual(semver.sort(versions),mapnikref.versions);
});
});



describe('load', function() {
it('should load requested version if exists', function() {
var spec = mapnikref.load('2.0.2');
assert.equal(spec.version, '2.0.2');
});

it('should load closer patch version if requested patch does not exist', function() {
var spec = mapnikref.load('2.0.12');
assert.equal(spec.version, '2.0.2');
});

it('should support x as patch', function() {
var spec = mapnikref.load('2.0.x');
assert.equal(spec.version, '2.0.2');
});

it('should support x as minor', function() {
var spec = mapnikref.load('2.x.x');
assert.equal(spec.version, '2.3.0');
});

it('should support missing patch', function() {
var spec = mapnikref.load('2.3');
assert.equal(spec.version, '2.3.0');
});

it('should support major only', function() {
var spec = mapnikref.load('2');
assert.equal(spec.version, '2.3.0');
});

it('should support range', function() {
var spec = mapnikref.load('2.x < 3');
assert.equal(spec.version, '2.3.0');
});

it('should support *', function() {
var spec = mapnikref.load('*');
assert.equal(spec.version, mapnikref.latest);
});

it('should throw if requested minor does not exist', function() {
assert.throws(function () {
mapnikref.load('2.12.0');
}, /Unknown mapnik-reference version/);
});

it('should throw for invalid version', function() {
assert.throws(function () {
mapnikref.load('invalid');
}, /Unknown mapnik-reference version/);
});
});

Expand Down

0 comments on commit 08437ab

Please sign in to comment.