Add more tests, most of them related with the GitResolver.

This commit is contained in:
André Cruz
2013-04-23 20:33:21 +01:00
parent 727e02b816
commit f28ac0c037
7 changed files with 774 additions and 163 deletions

View File

@@ -36,14 +36,12 @@ GitResolver.prototype.hasNew = function (canonicalPkg) {
return true;
}
// If resolved to a tag, there is new content
// if the tags are not equal
if (resolution.type === 'tag') {
return semver.neq(resolution.tag, oldResolution.tag);
// If resolved to a tag, there is new content if the tags are not equal
if (resolution.type === 'tag' && semver.neq(resolution.tag, oldResolution.tag)) {
return true;
}
// If resolved to a commit hash, just check if they are different
// Use the same strategy if it the resolution is to a branch
// As last check, we compare both commit hashes
return resolution.commit !== oldResolution.commit;
});
};
@@ -51,8 +49,12 @@ GitResolver.prototype.hasNew = function (canonicalPkg) {
// -----------------
// Abstract functions that should be implemented by concrete git resolvers
GitResolver.prototype._checkout = function () {};
GitResolver.fetchRefs = function (source) {};
GitResolver.prototype._checkout = function () {
throw new Error('_checkout not implemented');
};
GitResolver.fetchRefs = function (source) {
throw new Error('fetchRefs not implemented');
};
// -----------------
@@ -74,18 +76,18 @@ GitResolver.prototype._findResolution = function (target) {
// Find the highest one that satisfies the target
var version = mout.array.find(versions, function (version) {
return semver.satisfies(version, target);
return semver.satisfies(version.version, target);
}, this);
if (!version) {
throw createError('No tag found that was able to satisfy "' + target + '"', 'ENORESTARGET', {
details: !versions.length ?
'No tags found in "' + this._source + '"' :
'Available tags in "' + this._source + '" are: ' + versions.join(', ')
'No versions found in "' + this._source + '"' :
'Available versions in "' + this._source + '" are: ' + versions.map(function (version) { return version.version; }).join(', ')
});
}
return this._resolution = { type: 'tag', tag: version };
return this._resolution = { type: 'tag', tag: version.tag, commit: version.commit };
}.bind(this));
}
@@ -100,7 +102,7 @@ GitResolver.prototype._findResolution = function (target) {
return self.fetchHeads(this._source)
.then(function (heads) {
// Use hasOwn because a branch could have a name like "hasOwnProperty"
if (mout.object.hasOwn(heads, target)) {
if (!mout.object.hasOwn(heads, target)) {
branches = Object.keys(heads);
throw createError('Branch "' + target + '" does not exist', 'ENORESTARGET', {
details: !branches.length ?
@@ -123,6 +125,12 @@ GitResolver.prototype._cleanup = function () {
return Q.nfcall(chmodr, gitFolder, 0777)
.then(function () {
return Q.nfcall(rimraf, gitFolder);
}, function (err) {
// If .git does not exist, chmodr returns ENOENT
// so, we ignore that error code
if (err.code !== 'ENOENT') {
throw err;
}
});
} else {
return Q.nfcall(rimraf, gitFolder);
@@ -149,21 +157,23 @@ GitResolver.fetchVersions = function (source) {
// Foreach line in the refs, match only the tags
refs.forEach(function (line) {
var match = line.match(/^[a-f0-9]{40}\s+refs\/tags\/(\S+)/),
cleaned;
var match = line.match(/^([a-f0-9]{40})\s+refs\/tags\/(\S+)/),
tag,
version;
// Ensure it's valid
if (match) {
cleaned = semver.clean(match[1]);
if (cleaned) {
versions.push(cleaned);
tag = match[2];
version = semver.clean(tag);
if (version) {
versions.push({ version: version, tag: tag, commit: match[1] });
}
}
});
// Sort them by desc order
versions = versions.sort(function (a, b) {
return semver.gt(a, b) ? -1 : 1;
return semver.gt(a.version, b.version) ? -1 : 1;
});
this._versions = this._versions || {};