Skip to content

Commit

Permalink
Sorts fetched released by semver
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnyman727 committed Feb 17, 2016
1 parent bb6fa0b commit ee1a243
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 2 deletions.
12 changes: 10 additions & 2 deletions lib/update-fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ var ProgressBar = require('progress');
var request = require('request');
var streamToBuffer = require('stream-to-buffer');
var urljoin = require('url-join');
var semver = require('semver');

// Internal
var logs = require('./logs');
Expand All @@ -32,17 +33,24 @@ function requestBuildList() {
}

var outcome = reviewResponse(response);

var builds;
// If there wasn't an issue with the request
if (outcome.success) {
// Resolve with the parsed data
try {
resolve(JSON.parse(body));
builds = JSON.parse(body);
}
// If the parse failed, reject
catch (err) {
reject(err);
}

// Sort the builds by semver version
builds.sort((a, b) => {
return semver.gt(a.version, b.version);
});

return resolve(builds);
} else {
reject(outcome.reason);
}
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
"progress": "^1.1.8",
"promzard": "^0.3.0",
"request": "^2.60.0",
"semver": "^5.1.0",
"shell-escape": "^0.2.0",
"sprintf-js": "^1.0.2",
"ssh2": "^0.4.2",
Expand Down
52 changes: 52 additions & 0 deletions test/unit/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -633,3 +633,55 @@ exports['controller.update'] = {
}.bind(this));
},
};

exports['update-fetch'] = {
setUp: function(done) {
this.sandbox = sinon.sandbox.create();
this.logsWarn = this.sandbox.stub(logs, 'warn', function() {});
this.logsInfo = this.sandbox.stub(logs, 'info', function() {});
this.logsBasic = this.sandbox.stub(logs, 'basic', function() {});

var mixedBuilds = [{
sha: 'ac4d8d8a5bfd671f7f174c2eaa258856bd82fe29',
released: '2015-05-18T02:21:57.856Z',
version: '0.0.0'
}, {
sha: '9a85c84f5a03c715908921baaaa9e7397985bc7f',
released: '2015-08-12T03:01:57.856Z',
version: '0.0.4'
}, {
sha: '789432897cd7829a988888b8843274cd8de89a98',
released: '2015-06-12T03:01:57.856Z',
version: '0.0.1'
}];

this.requestGet = this.sandbox.stub(request, 'get', function(url, cb) {
cb(null, {
statusCode: 200
}, JSON.stringify(mixedBuilds));
});
done();
},
tearDown: function(done) {
this.sandbox.restore();
done();
},
buildsSorted: function(test) {
test.expect(4);

// Request the out of order builds
updates.requestBuildList()
.then((builds) => {
// Ensure they were put back in order
test.ok(builds.length === 3);
test.ok(builds[0].version === '0.0.0');
test.ok(builds[1].version === '0.0.1');
test.ok(builds[2].version === '0.0.4');
test.done();
})
.catch(() => {
test.fail('An error was returned when the list fetch should succeed');
test.done();
});
}
};

0 comments on commit ee1a243

Please sign in to comment.