From cfbf2aa42fe7c907e946143907f528af1b479077 Mon Sep 17 00:00:00 2001 From: "didier.amyot" Date: Mon, 20 Jan 2014 10:02:22 -0500 Subject: [PATCH] started to changed the code. Added tests. --- get_branches.py | 6 --- lib/index.js | 103 ++++++++++++++++++++++++++++++----------------- package.json | 7 ++-- test/test_lib.js | 27 +++++++++++++ 4 files changed, 97 insertions(+), 46 deletions(-) delete mode 100644 get_branches.py create mode 100644 test/test_lib.js diff --git a/get_branches.py b/get_branches.py deleted file mode 100644 index 34521fd..0000000 --- a/get_branches.py +++ /dev/null @@ -1,6 +0,0 @@ -from mercurial import ui, hg, node -from sys import argv - -peer = hg.peer(ui.ui(), {}, argv[1]) -for name, rev in peer.branchmap().items(): - print(name) \ No newline at end of file diff --git a/lib/index.js b/lib/index.js index 485e70a..a7baf2a 100644 --- a/lib/index.js +++ b/lib/index.js @@ -3,6 +3,7 @@ var gitane = require('gitane') , exec = require('child_process').exec , shellescape = require('shell-escape') , path = require('path') + , fs = require('fs') , _ = require('underscore') module.exports = { @@ -11,9 +12,29 @@ module.exports = { httpUrl: httpUrl, gitCmd: gitCmd, gitaneCmd: gitaneCmd, - processBranches: processBranches, - getBranches: getBranches, - shellEscape: shellEscape + shellEscape: shellEscape, + Config: Config, + Hg : Hg +} + +function Config(basedConfig){ + this.sanitizeConfig = function(config) { + config = config || {}; + config.auth = config.auth || {}; + + this.url= config.url || "", + this.display_url= config.display_url || "", + this.auth = { + type: config.auth.type || "", + privkey: config.auth.privkey || "", + pubkey: config.auth.pubkey || "", + username: config.auth.username || "", + password: config.auth.password || "" + } + } + + this.sanitizeConfig(basedConfig); + return this } function shellEscape(one) { @@ -51,8 +72,6 @@ function gitCmd(cmd, cwd, auth, context, done) { }, done) } - - // run a strider command with gitane function gitaneCmd(cmd, dest, privkey, context, done) { var start = new Date() @@ -77,44 +96,54 @@ function gitaneCmd(cmd, dest, privkey, context, done) { }) } -function processBranches(data, done) { - done(null, data.trim().split(/\n+/).map(function (line) { - return line.split(/\s+/)[1].split('/').slice(-1)[0] - })) -} - - - -function getBranches(config, privkey, done) { - if (config.auth.type === 'ssh') { +function Hg(){ + this.getMercurialBranchesCommandLine = function(config){ + var repoLocation = config.url; + var pythonScript = fs.realpathSync(path.join(__dirname, 'get_branches.py')); + var cmd = 'python ' + pythonScript + " " + repoLocation; + } + + this.getNameOfMercurialBranchesOutOfScriptStdout = function(stdout){ + return _.filter(stdout.split("\n"),function(v){return !!v}) + } + + this.getMercurialBranchesForHttpBasedReposetories = function(config, done){ + var cmd = this.getMercurialBranchesCommandLine(config); + exec(cmd, function (err, stdout, stderr) { + if (err){ + return done(err); + }else{ + return done(this.getNameOfMercurialBranchesOutOfScriptStdout(stdout)); + } + }) + } + + this.getMercurialBranchesForSshBasedReposetories = function(config, privkey, done){ + var cmd = this.getMercurialBranchesCommandLine(config); gitane.run({ - cmd: 'git ls-remote -h ' + gitUrl(config)[0], + cmd: cmd, baseDir: '/', privKey: config.auth.privkey || privkey, detached: true - }, function (err, stdout, stderr, exitCode) { - if (err || exitCode !== 0) return done(err || new Error(stderr)) - processBranches(stdout, done) - }) - } else { - exec('git ls-remote -h ' + httpUrl(config)[0], function (err, stdout, stderr) { - if (err) return done(err) - processBranches(stdout, done) + }, + function (err, stdout, stderr, exitCode) { + if (err || exitCode !== 0){ + return done(err || new Error(stderr)) + }else{ + return done(this.getNameOfMercurialBranchesOutOfScriptStdout(stdout)); + } }) } + + this.getMercurialBranches = function(config, privkey, done){ + if (config.auth.type === 'ssh') { + privkey = config.auth.privkey || privkey; + return this.getMercurialBranchesForSshBasedReposetories(config, privkey, done); + }else{ + return this.getMercurialBranchesForHttpBasedReposetories(config, done); + } + } } -function getMercurialBranches(repoLocation){ - var pythonScript = path.join(__dirname, 'get_branches.py'); - var cmd = 'python ' + pythonScript + " " + repoLocation; - exec(cmd, function (err, stdout, stderr) { - if (err){ - return console.log(err) - }else{ - return console.log(_.filter(stdout.split("\n"),function(v){return !!v} )) - } - }) -} -getMercurialBranches('ssh://hg@bitbucket.org/ustraca/crypto') -console.log("ok?") + diff --git a/package.json b/package.json index d02124b..70c0d20 100644 --- a/package.json +++ b/package.json @@ -20,8 +20,8 @@ "license": "MIT", "readmeFilename": "Readme.md", "strider": { - "id": "git", - "title": "Git", + "id": "hg", + "title": "Hg", "type": "provider", "webapp": "webapp.js", "worker": "worker.js", @@ -32,7 +32,8 @@ "gitane": "~0.3.1", "shell-escape": "0.0.1", "mkdirp": "~0.3.5", - "underscore": "~1.5.2" + "underscore": "~1.5.2", + "expect.js": "~0.2.0" }, "devDependencies": { "jshint": "~2.3.0", diff --git a/test/test_lib.js b/test/test_lib.js new file mode 100644 index 0000000..7b23cde --- /dev/null +++ b/test/test_lib.js @@ -0,0 +1,27 @@ +var expect = require('expect.js') + , utils = require('../lib/index.js') + +describe('The lib util interface', function () { + describe('the Hg object', function () { + it('should always pass', function(){ + expect(1).to.be(1); + }); + var hg = new utils.Hg(); + it('should get the branched name from the script stdout', function () { + var stdout = "a\nb\nc\n"; + expect(hg.getNameOfMercurialBranchesOutOfScriptStdout(stdout)).to.eql(["a", "b", "c"]); + }) + }) + + describe('the config object', function(){ + it('should be able to initiate properly', function () { + var config = new utils.Config(); + expect(config.url).to.eql(""); + expect(config.auth.username).to.eql(""); + config = new utils.Config({url: "1", auth: {username: "2"}}); + expect(config.url).to.eql("1"); + expect(config.auth.username).to.eql("2"); + }) + + }) +}) \ No newline at end of file