Add more tests to the resolver factory.

This commit is contained in:
André Cruz
2013-05-02 23:45:49 +01:00
parent 4cc09e49ac
commit 409a8c2a77
3 changed files with 101 additions and 14 deletions

View File

@@ -29,7 +29,6 @@ var proxy = process.env.HTTPS_PROXY
var config;
try {
config = require('rc')('bower', {
json: 'bower.json',
directory: 'bower_components',
shorthandResolver: 'git://github.com/{{owner}}/{{package}}.git',
proxy: proxy,

View File

@@ -9,8 +9,9 @@ var UrlResolver = require('./resolvers/UrlResolver');
var config = require('../config');
var createError = require('../util/createError');
function createResolver(endpoint, options) {
function createResolver(endpoint, cfg) {
var split = endpoint.split('#'),
options = {},
source,
target;
@@ -22,10 +23,10 @@ function createResolver(endpoint, options) {
source = split[0];
target = split[1];
// Configure options
options = options || {};
options.target = options.target || target;
options.config = options.config || config;
// Setup options
cfg = cfg || config;
options.config = cfg;
options.target = target;
// Git case: git git+ssh, git+http, git+https
if (/^git(\+(ssh|https?))?:\/\//i.test(source)) {
@@ -33,7 +34,7 @@ function createResolver(endpoint, options) {
return Q.resolve(new GitRemoteResolver(source, options));
}
// Git case: .git at the end (probably ssh shortand)
// Git case: .git at the end (probably ssh shorthand)
if (/\.git$/i.test(source)) {
return Q.resolve(new GitRemoteResolver(source, options));
}
@@ -75,8 +76,8 @@ function createResolver(endpoint, options) {
throw err;
})
// If not, check against the registry
// TODO:
// TODO: if not, check against the registry
// note that the registry should also have a persistent cache for offline usage
// Finally throw a meaningful error
.fail(function () {
throw new createError('Could not find appropriate resolver for source "' + source + '"', 'ENORESOLVER');

View File

@@ -1,6 +1,9 @@
var expect = require('expect.js');
var fs = require('fs');
var path = require('path');
var mout = require('mout');
var Q = require('q');
var rimraf = require('rimraf');
var resolverFactory = require('../../lib/resolve/resolverFactory');
var FsResolver = require('../../lib/resolve/resolvers/FsResolver');
var GitFsResolver = require('../../lib/resolve/resolvers/GitFsResolver');
@@ -8,6 +11,17 @@ var GitRemoteResolver = require('../../lib/resolve/resolvers/GitRemoteResolver')
var UrlResolver = require('../../lib/resolve/resolvers/UrlResolver');
describe('resolverFactory', function () {
var tempSource;
afterEach(function (next) {
if (tempSource) {
rimraf(tempSource, next);
tempSource = null;
} else {
next();
}
});
it('should recognize git remote endpoints correctly', function (next) {
var promise = Q.resolve(),
endpoints;
@@ -79,7 +93,6 @@ describe('resolverFactory', function () {
expect(resolver).to.be.a(GitRemoteResolver);
expect(resolver.getSource()).to.equal(value.source);
expect(resolver.getTarget()).to.equal(value.target);
});
});
@@ -88,21 +101,95 @@ describe('resolverFactory', function () {
.done();
});
it.skip('should recognize local fs git endpoints correctly', function () {
it('should recognize local fs git endpoints correctly', function (next) {
var promise = Q.resolve(),
endpoints;
endpoints = [
path.resolve(__dirname, '../assets/github-test-package'),
__dirname + '/../assets/github-test-package'
];
endpoints.forEach(function (source) {
promise = promise.then(function () {
return resolverFactory(source);
})
.then(function (resolver) {
expect(resolver).to.be.a(GitFsResolver);
});
});
promise
.then(next.bind(next, null))
.done();
});
it.skip('should recognize local fs files/folder endpoints correctly', function () {
it('should recognize local fs files/folder endpoints correctly', function (next) {
tempSource = path.resolve(__dirname, '../assets/tmp');
fs.mkdirSync(tempSource);
fs.writeFileSync(path.join(tempSource, '.git'), 'foo');
var promise = Q.resolve(),
endpoints;
endpoints = [
tempSource, // folder with .git file (not folder!)
path.resolve(__dirname, '../assets/test-temp-dir'), // folder
path.resolve(__dirname, '../assets/package-zip.zip'), // file
];
endpoints.forEach(function (source) {
promise = promise.then(function () {
return resolverFactory(source);
})
.then(function (resolver) {
expect(resolver).to.be.a(FsResolver);
});
});
promise
.then(next.bind(next, null))
.done();
});
it.skip('should recognize URL endpoints correctly', function () {
it('should recognize URL endpoints correctly', function (next) {
var promise = Q.resolve(),
endpoints;
endpoints = [
'http://bower.io/foo.js',
'https://bower.io/foo.js'
];
endpoints.forEach(function (source) {
promise = promise.then(function () {
return resolverFactory(source);
})
.then(function (resolver) {
expect(resolver).to.be.a(UrlResolver);
});
});
promise
.then(next.bind(next, null))
.done();
});
it.skip('should recognize registry endpoints correctly');
it.skip('should use the configured shorthand resolver', function () {
it('should use the configured shorthand resolver', function (next) {
resolverFactory('bower/bower')
.then(function (resolver) {
expect(resolver.getSource()).to.equal('git://github.com/bower/bower.git');
return resolverFactory('IndigoUnited/promptly', {
shorthandResolver: 'git://bower.io/{{owner}}/{{package}}/{{shorthand}}'
});
})
.then(function (resolver) {
expect(resolver.getSource()).to.equal('git://bower.io/IndigoUnited/promptly/IndigoUnited/promptly.git');
next();
})
.done();
});
});