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

CB-11713 and CB-11961 #563

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
73 changes: 57 additions & 16 deletions spec-plugman/fetch.spec.js
Expand Up @@ -22,6 +22,7 @@ var rewire = require('rewire'),
os = require('os'),
path = require('path'),
shell = require('shelljs'),
superspawn = require('cordova-common').superspawn,
realrm = shell.rm,
TIMEOUT = 60 * 1000,
//xml_helpers = require('../src/util/xml-helpers'),
Expand Down Expand Up @@ -237,29 +238,69 @@ describe('fetch', function() {
});

describe('github plugins', function() {

// these tests actually pull a plugin from github
beforeEach(function(){
realrm('-rf',temp);
});

// this commit uses the new id
it('Test 017 : should fetch from a commit-sha', function(done) {
it('Test 017 : should fetch from master by default without ls-remote', function(done) {
var spawn=spyOn(superspawn, 'spawn').and.callThrough();
wrapper(fetch('http://github.com/apache/cordova-plugin-device.git', temp, { expected_id: 'cordova-plugin-device' }), done, function() {
expect(spawn).toHaveBeenCalledWith('git', [ 'clone', '--depth=1', '-b', 'master', 'http://github.com/apache/cordova-plugin-device.git', jasmine.any(String)]);
expect(spawn).toHaveBeenCalledWith('git', [ 'rev-parse', '--short', 'HEAD' ], Object({ cwd: jasmine.any(String) }));
expect(spawn.calls.count()).toBe(2);
done();
});
}, TIMEOUT);

it('Test 018 : should fetch from a commit-sha with checkout', function(done) {
var spawn=spyOn(superspawn, 'spawn').and.callThrough();
wrapper(fetch('http://github.com/apache/cordova-plugin-device.git#ad5f1e7bfd05ef98c01df549a0fa98036a5625db', temp, { expected_id: 'cordova-plugin-device' }), done, function() {
expect(1).toBe(1);
expect(spawn).toHaveBeenCalledWith('git', [ 'ls-remote', '--tags', '--heads', '--exit-code', 'http://github.com/apache/cordova-plugin-device.git', 'ad5f1e7bfd05ef98c01df549a0fa98036a5625db' ]);
expect(spawn).toHaveBeenCalledWith('git', [ 'clone', 'http://github.com/apache/cordova-plugin-device.git', jasmine.any(String)]);
expect(spawn).toHaveBeenCalledWith('git', [ 'checkout', 'ad5f1e7bfd05ef98c01df549a0fa98036a5625db' ], Object({ cwd: jasmine.any(String)}));
expect(spawn).toHaveBeenCalledWith('git', [ 'rev-parse', '--short', 'HEAD' ], Object({ cwd: jasmine.any(String) }));
expect(spawn.calls.count()).toBe(4);
done();
});
}, TIMEOUT);
// this branch uses the old id
it('Test 018 : should fetch from a branch', function(done) {

it('Test 019 : should shallow fetch from a branch', function(done) {
var spawn=spyOn(superspawn, 'spawn').and.callThrough();
wrapper(fetch('http://github.com/apache/cordova-plugin-device.git#cdvtest', temp, { expected_id: 'org.apache.cordova.device' }), done, function() {
expect(1).toBe(1);
expect(spawn).toHaveBeenCalledWith('git', [ 'ls-remote', '--tags', '--heads', '--exit-code', 'http://github.com/apache/cordova-plugin-device.git', 'cdvtest' ]);
expect(spawn).toHaveBeenCalledWith('git', [ 'clone', '--depth=1', '-b', 'cdvtest', 'http://github.com/apache/cordova-plugin-device.git', jasmine.any(String)]);
expect(spawn).toHaveBeenCalledWith('git', [ 'rev-parse', '--short', 'HEAD' ], Object({ cwd: jasmine.any(String) }));
expect(spawn.calls.count()).toBe(3);
done();
});
}, TIMEOUT);
// this tag uses the new id
it('Test 019 : should fetch from a tag', function(done) {

it('Test 020 : should shallow fetch from a tag', function(done) {
var spawn=spyOn(superspawn, 'spawn').and.callThrough();
wrapper(fetch('http://github.com/apache/cordova-plugin-device.git#r1.0.0', temp, { expected_id: 'cordova-plugin-device' }), done, function() {
expect(1).toBe(1);
expect(spawn).toHaveBeenCalledWith('git', [ 'ls-remote', '--tags', '--heads', '--exit-code', 'http://github.com/apache/cordova-plugin-device.git', 'r1.0.0' ]);
expect(spawn).toHaveBeenCalledWith('git', [ 'clone', '--depth=1', '-b', 'r1.0.0', 'http://github.com/apache/cordova-plugin-device.git', jasmine.any(String)]);
expect(spawn).toHaveBeenCalledWith('git', [ 'rev-parse', '--short', 'HEAD' ], Object({ cwd: jasmine.any(String) }));
expect(spawn.calls.count()).toBe(3);
done();
});
}, TIMEOUT);

it('Test 021 : should just clone and checkout if ls-remote fails', function(done) {
var old_spawn = superspawn.spawn;
var spawn=spyOn(superspawn, 'spawn').and.callFake(function() {
if (arguments[1][0] == 'ls-remote')
arguments[1][1] = '--bad-argument';
return old_spawn.apply(this, arguments);
});
wrapper(fetch('http://github.com/apache/cordova-plugin-device.git#r1.0.0', temp, { expected_id: 'cordova-plugin-device' }), done, function() {
expect(spawn).toHaveBeenCalledWith('git', [ 'ls-remote', '--bad-argument', '--heads', '--exit-code', 'http://github.com/apache/cordova-plugin-device.git', 'r1.0.0' ]);
expect(spawn).toHaveBeenCalledWith('git', [ 'clone', 'http://github.com/apache/cordova-plugin-device.git', jasmine.any(String)]);
expect(spawn).toHaveBeenCalledWith('git', [ 'checkout', 'r1.0.0' ], Object({ cwd: jasmine.any(String)}));
expect(spawn).toHaveBeenCalledWith('git', [ 'rev-parse', '--short', 'HEAD' ], Object({ cwd: jasmine.any(String) }));
expect(spawn.calls.count()).toBe(4);
done();
});
}, TIMEOUT);
Expand All @@ -271,7 +312,7 @@ describe('fetch', function() {
var appDir = path.join(__dirname, 'plugins/recursivePlug/demo');

if(/^win/.test(process.platform)) {
it('Test 020 : should copy all but the /demo/ folder',function(done) {
it('Test 022 : should copy all but the /demo/ folder',function(done) {
var cp = spyOn(shell, 'cp');
wrapper(fetch(srcDir, appDir),done, function() {
expect(cp).toHaveBeenCalledWith('-R',path.join(srcDir,'asset.txt'),path.join(appDir,'test-recursive'));
Expand All @@ -280,7 +321,7 @@ describe('fetch', function() {
});
}
else {
it('Test 021 : should skip copy to avoid recursive error', function(done) {
it('Test 023 : should skip copy to avoid recursive error', function(done) {

var cp = spyOn(shell, 'cp').and.callFake(function(){});

Expand All @@ -303,7 +344,7 @@ describe('fetch', function() {
realrm('-rf', temp);
});

it('Test 022 : should fail when the expected ID with version specified doesn\'t match', function(done) {
it('Test 024 : should fail when the expected ID with version specified doesn\'t match', function(done) {
//fetch(pluginId, temp, { expected_id: test_plugin_id + '@wrongVersion' })
fetch(pluginId, temp, { expected_id: 'wrongID' })
.then(function() {
Expand All @@ -313,23 +354,23 @@ describe('fetch', function() {
}).fin(done);
});

it('Test 023 : should succeed when the expected ID is correct', function(done) {
it('Test 025 : should succeed when the expected ID is correct', function(done) {
wrapper(fetch(pluginId, temp, { expected_id: test_plugin_id }), done, function() {
expect(1).toBe(1);
});
});
it('Test 024 : should succeed when the plugin version specified is correct', function(done) {
it('Test 026 : should succeed when the plugin version specified is correct', function(done) {
wrapper(fetch(pluginId, temp, { expected_id: test_plugin_id + '@' + test_plugin_version }), done, function() {
expect(1).toBe(1);
});
});
it('Test 025 : should fetch plugins that are scoped packages', function(done) {
it('Test 027 : should fetch plugins that are scoped packages', function(done) {
var scopedPackage = '@testcope/dummy-plugin';
wrapper(fetch(scopedPackage, temp, { expected_id: test_plugin_id }), done, function() {
expect(sFetch).toHaveBeenCalledWith([scopedPackage]);
});
});
it('Test 026 : should fetch plugins that are scoped packages and have versions specified', function(done) {
it('Test 028 : should fetch plugins that are scoped packages and have versions specified', function(done) {
var scopedPackage = '@testcope/dummy-plugin@latest';
wrapper(fetch(scopedPackage, temp, { expected_id: test_plugin_id }), done, function() {
expect(sFetch).toHaveBeenCalledWith([scopedPackage]);
Expand Down
53 changes: 37 additions & 16 deletions src/gitclone.js
Expand Up @@ -24,45 +24,66 @@ var Q = require('q'),
superspawn = require('cordova-common').superspawn,
os = require('os');


exports.clone = clone;

// clone_dir, if provided is the directory that git will clone into.
// if no clone_dir is supplied, a temp directory will be created and used by git.
function clone(git_url, git_ref, clone_dir){

var needsGitCheckout = !!git_ref;

var needsGitCheckout = false,
cloneArgs = ['clone'],
checkRemoteRef = Q.resolve(),
tmp_dir = clone_dir;

git_ref = git_ref || 'master';

if (!shell.which('git')) {
return Q.reject(new Error('"git" command line tool is not installed: make sure it is accessible on your PATH.'));
}

// If no clone_dir is specified, create a tmp dir which git will clone into.
var tmp_dir = clone_dir;
if(!tmp_dir){
if (!tmp_dir) {
tmp_dir = path.join(os.tmpdir(), 'git', String((new Date()).valueOf()));
}
shell.rm('-rf', tmp_dir);
shell.mkdir('-p', tmp_dir);
var cloneArgs = ['clone'];
if(!needsGitCheckout) {
// only get depth of 1 if there is no branch/commit specified
cloneArgs.push('--depth=1');

// if git_ref is not 'master' check if it's a branch or tag so we can shallow clone.
if (git_ref !== 'master') {
var lsRemoteArgs = ['ls-remote', '--tags', '--heads', '--exit-code', git_url, git_ref];
checkRemoteRef = superspawn.spawn('git', lsRemoteArgs);
}
cloneArgs.push(git_url, tmp_dir);
return superspawn.spawn('git', cloneArgs)

return checkRemoteRef
.then(function() {
cloneArgs.push('--depth=1', '-b', git_ref);
})
.fail(function() {
needsGitCheckout = true;
})
.then(function() {
cloneArgs.push(git_url, tmp_dir);
})
.then(function() {
if (needsGitCheckout){
return superspawn.spawn('git', cloneArgs);
})
.then(function() {
if (needsGitCheckout) {
return superspawn.spawn('git', ['checkout', git_ref], {
cwd: tmp_dir
});
}
})
.then(function(){
events.emit('log', 'Repository "' + git_url + '" checked out to git ref "' + (git_ref || 'master') + '".');
.then(function() {
return superspawn.spawn('git', ['rev-parse', '--short', 'HEAD'], {
cwd: tmp_dir
});
})
.then(function(sha) {
events.emit('log', 'Repository "' + git_url + '" cloned from git ref "' + git_ref + '" (' + sha + ').');
return tmp_dir;
})
.fail(function (err) {
.fail(function(err) {
shell.rm('-rf', tmp_dir);
return Q.reject(err);
});
Expand Down
2 changes: 1 addition & 1 deletion src/plugman/install.js
Expand Up @@ -290,7 +290,7 @@ function runInstall(actions, platform, project_dir, plugin_dir, plugins_dir, opt
// we don't need to call prepare in any way
return Q(true);
}
events.emit('log', 'Installing "' + pluginInfo.id + '" for ' + platform);
events.emit('log', 'Installing "' + pluginInfo.id + '" at "' + pluginInfo.version + '" for ' + platform);

var theEngines = getEngines(pluginInfo, platform, project_dir, plugin_dir);

Expand Down