From 335918f28d6cbbac2efbc25990e97549dbd96aee Mon Sep 17 00:00:00 2001 From: Christophe Porteneuve Date: Fri, 8 Nov 2013 19:05:52 +0100 Subject: Fix step 1: for dev-local uses, properly detect local Git branch and commit --- lib/detectLocalGit.js | 30 ++++++++++++++++++++++++++++++ lib/getOptions.js | 8 ++++++++ 2 files changed, 38 insertions(+) create mode 100644 lib/detectLocalGit.js diff --git a/lib/detectLocalGit.js b/lib/detectLocalGit.js new file mode 100644 index 0000000..2d0fba0 --- /dev/null +++ b/lib/detectLocalGit.js @@ -0,0 +1,30 @@ +var fs = require('fs'); +var path = require('path'); + +var REGEX_BRANCH = /^ref: refs\/heads\/(\w+)$/; + +module.exports = function detectLocalGit(knownCommit, knownBranch) { + var dir = process.cwd(), gitDir; + while ('/' !== dir) { + gitDir = path.join(dir, '.git'); + if (fs.existsSync(path.join(gitDir, 'HEAD'))) + break; + + dir = path.dirname(dir); + } + + if ('/' === dir) + return; + + var head = strip(fs.readFileSync(path.join(dir, '.git', 'HEAD'), 'utf-8')); + var branch = (head.match(REGEX_BRANCH) || [])[1]; + if (!branch) + return { git_commit: head }; + + var commit = strip(fs.readFileSync(path.join(dir, '.git', 'refs', 'heads', branch), 'utf-8')); + return { git_commit: commit, git_branch: branch }; +}; + +function strip(str) { + return (str || '').replace(/^\s+|\s+$/g, ''); +} diff --git a/lib/getOptions.js b/lib/getOptions.js index 35187b4..abd2f2e 100644 --- a/lib/getOptions.js +++ b/lib/getOptions.js @@ -43,6 +43,14 @@ var getBaseOptions = function(cb){ options.service_job_id = process.env.COVERALLS_SERVICE_JOB_ID; } + if (!git_commit || !git_branch) { + var data = require('./detectLocalGit')(git_commit, git_branch); + if (data) { + git_commit = git_commit || data.git_commit; + git_branch = git_branch || data.git_branch; + } + } + // try to get the repo token as an environment variable if (process.env.COVERALLS_REPO_TOKEN) { options.repo_token = process.env.COVERALLS_REPO_TOKEN; -- cgit v1.2.3 From 36c62aff7424b12ca9e84370afc65ecfbe73e632 Mon Sep 17 00:00:00 2001 From: Christophe Porteneuve Date: Fri, 8 Nov 2013 19:07:36 +0100 Subject: Fix step 2: properly detect local Git branch, and just keep pre-provided one if any. --- lib/fetchGitData.js | 71 ++++++++++++++++++++++++++++++----------------------- 1 file changed, 40 insertions(+), 31 deletions(-) diff --git a/lib/fetchGitData.js b/lib/fetchGitData.js index a77b4a8..f75ca82 100644 --- a/lib/fetchGitData.js +++ b/lib/fetchGitData.js @@ -25,7 +25,6 @@ var fetchGitData = function(git, cb) { "format": "'%s'", } }; - var remotes = {}; //-- Malformed/undefined git object if ('undefined' === typeof git) { @@ -36,22 +35,6 @@ var fetchGitData = function(git, cb) { return cb(new Error('You must provide the head.id')); } - function saveRemote(name, url, push) { - var key = name + "-" + url; - if ("undefined" === typeof push || "boolean" !== typeof push) { - push = true; - } - if (!remotes.hasOwnProperty(key)) { - remotes[key] = true; - if (push) { - git.remotes.push({ - "name": name, - "url": url - }); - } - } - } - //-- Set required properties of git if they weren"t provided if (!git.hasOwnProperty("branch")) { git.branch = ""; @@ -98,24 +81,50 @@ var fetchGitData = function(git, cb) { git.head[field] = response; remaining--; if (remaining === 0){ - //-- Branch - exec("git branch", function(err, branches){ - if (err) return cb(err); - git.branch = branches.split("\n")[0].replace(/^\*\ /, "").trim(); - exec("git remote -v", function(err, remotes){ - if (err) return cb(err); - remotes.split("\n").forEach(function(remote) { - remote = remote.split(/\s/); - saveRemote(remote[0], remote[1]); - }); - return cb(null, git); - }); - }); + if (git.branch) { + fetchRemotes(git, cb); + } else { + fetchBranch(git, cb); + } } }); }); }); -}; +} + +function fetchBranch(git, cb) { + exec("git branch", function(err, branches){ + if (err) + return cb(err); + + git.branch = (branches.match(/^\* (\w+)/) || [])[1]; + fetchRemotes(git, cb); + }); +} + +function fetchRemotes(git, cb) { + exec("git remote -v", function(err, remotes){ + if (err) + return cb(err); + + var processed = {}; + remotes.split("\n").forEach(function(remote) { + if (!/\s\(push\)$/.test(remote)) + return; + remote = remote.split(/\s+/); + saveRemote(processed, git, remote[0], remote[1]); + }); + cb(null, git); + }); +} + +function saveRemote(processed, git, name, url) { + var key = name + "-" + url; + if (processed.hasOwnProperty(key)) + return; + processed[key] = true; + git.remotes.push({ name: name, url: url }); +} module.exports = fetchGitData; -- cgit v1.2.3 From a49eaa7f533bb7c3338d7864e3d118d43ede861e Mon Sep 17 00:00:00 2001 From: Christophe Porteneuve Date: Fri, 8 Nov 2013 19:31:19 +0100 Subject: Fix step 3: code cleanup + faster/nimbler use of Git to obtain data (rev-parse and cat-file instead of multiple log calls) --- lib/fetchGitData.js | 78 +++++++++++++++++++---------------------------------- lib/handleInput.js | 6 ++--- 2 files changed, 31 insertions(+), 53 deletions(-) diff --git a/lib/fetchGitData.js b/lib/fetchGitData.js index f75ca82..1633bd5 100644 --- a/lib/fetchGitData.js +++ b/lib/fetchGitData.js @@ -1,37 +1,19 @@ var exec = require('child_process').exec; var logger = require('./logger')(); -var fetchGitData = function(git, cb) { +function fetchGitData(git, cb) { if (!cb){ throw new Error("fetchGitData requires a callback"); } - var i; - var execGit = true; - var head = { - "author_name": { - "format": "'%aN'", - }, - "author_email": { - "format": "'%ae'", - }, - "committer_name": { - "format": "'%cN'", - }, - "committer_email": { - "format": "'%ce'", - }, - "message": { - "format": "'%s'", - } - }; - //-- Malformed/undefined git object if ('undefined' === typeof git) { return cb(new Error('No options passed')); - } else if (!git.hasOwnProperty('head')) { + } + if (!git.hasOwnProperty('head')) { return cb(new Error('You must provide the head')); - } else if (!git.head.hasOwnProperty('id')) { + } + if (!git.head.hasOwnProperty('id')) { return cb(new Error('You must provide the head.id')); } @@ -52,7 +34,7 @@ var fetchGitData = function(git, cb) { } //-- Use git? - exec("git log -1 " + git.head.id + " --pretty=format:'%H'", function(err, response){ + exec("git rev-parse --verify " + git.head.id, function(err, response){ if (err){ // git is not available... git.head.author_name = git.head.author_name || "Unknown Author"; @@ -63,32 +45,7 @@ var fetchGitData = function(git, cb) { return cb(null, git); } - //-- Head - var commands = []; - var fields = []; - for (var field in head) { - fields.push(field); - var command = "git log -1 " + git.head.id + " --pretty=format:" + head[field].format; - commands.push(command); - } - var i = 0; - var remaining = commands.length; - commands.forEach(function(command){ - var field = fields[i]; - i++; - exec(command, function(err, response){ - if (err) return cb(err); - git.head[field] = response; - remaining--; - if (remaining === 0){ - if (git.branch) { - fetchRemotes(git, cb); - } else { - fetchBranch(git, cb); - } - } - }); - }); + fetchHeadDetails(git, cb); }); } @@ -102,6 +59,27 @@ function fetchBranch(git, cb) { }); } +var REGEX_COMMIT_DETAILS = /\nauthor (.+?) <(.+?)>.+\ncommitter (.+?) <(.+?)>.+\n\n(.*)/m; + +function fetchHeadDetails(git, cb) { + exec('git cat-file -p ' + git.head.id, function(err, response) { + if (err) + return cb(err); + + var items = response.match(REGEX_COMMIT_DETAILS).slice(1); + var fields = ['author_name', 'author_email', 'committer_name', 'committer_email', 'message']; + fields.forEach(function(field, index) { + git.head[field] = items[index]; + }); + + if (git.branch) { + fetchRemotes(git, cb); + } else { + fetchBranch(git, cb); + } + }); +} + function fetchRemotes(git, cb) { exec("git remote -v", function(err, remotes){ if (err) diff --git a/lib/handleInput.js b/lib/handleInput.js index 1435c1c..807e31d 100644 --- a/lib/handleInput.js +++ b/lib/handleInput.js @@ -1,9 +1,10 @@ var index = require('../index'); var logger = require('./logger')(); -var handleInput = function(input){ +function handleInput(input) { logger.debug(input); var options = index.getOptions(function(err, options){ + if (err){ logger.error("error from getOptions"); throw err; @@ -28,7 +29,6 @@ var handleInput = function(input){ }); }); }); - -}; +} module.exports = handleInput; -- cgit v1.2.3 From b12c8cf5f0bc11486e2b064a834ee9ff81adb0a3 Mon Sep 17 00:00:00 2001 From: Christophe Porteneuve Date: Fri, 8 Nov 2013 20:02:29 +0100 Subject: =?UTF-8?q?Ah,=20right,=20ES5=20has=20String#trim(),=20keep=20forg?= =?UTF-8?q?etting=20that=E2=80=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/detectLocalGit.js | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/lib/detectLocalGit.js b/lib/detectLocalGit.js index 2d0fba0..4fd8d1b 100644 --- a/lib/detectLocalGit.js +++ b/lib/detectLocalGit.js @@ -16,15 +16,11 @@ module.exports = function detectLocalGit(knownCommit, knownBranch) { if ('/' === dir) return; - var head = strip(fs.readFileSync(path.join(dir, '.git', 'HEAD'), 'utf-8')); + var head = fs.readFileSync(path.join(dir, '.git', 'HEAD'), 'utf-8').trim(); var branch = (head.match(REGEX_BRANCH) || [])[1]; if (!branch) return { git_commit: head }; - var commit = strip(fs.readFileSync(path.join(dir, '.git', 'refs', 'heads', branch), 'utf-8')); + var commit = fs.readFileSync(path.join(dir, '.git', 'refs', 'heads', branch), 'utf-8').trim(); return { git_commit: commit, git_branch: branch }; }; - -function strip(str) { - return (str || '').replace(/^\s+|\s+$/g, ''); -} -- cgit v1.2.3 From 6ca3aec602027774dd159c9ff41ed69e835073b0 Mon Sep 17 00:00:00 2001 From: Christophe Porteneuve Date: Thu, 14 Nov 2013 20:33:23 +0100 Subject: Massive expansion of test coverage: 100% of detectLocalGit + numerous missing lines in other files --- test/fetchGitData.js | 6 ++ test/getOptions.js | 187 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 193 insertions(+) diff --git a/test/fetchGitData.js b/test/fetchGitData.js index de5c474..9c00828 100644 --- a/test/fetchGitData.js +++ b/test/fetchGitData.js @@ -9,6 +9,12 @@ describe("fetchGitData", function(){ it("should throw an error when no data is passed", function() { fetchGitData.should.throw(/fetchGitData requires a callback/); }); + it('should throw an error when no git context is provided', function(done) { + fetchGitData(undefined, function(err){ + err.should.match(/No options passed/); + done(); + }); + }); it("should throw an error if no head is provided", function(done) { fetchGitData({ }, function(err){ diff --git a/test/getOptions.js b/test/getOptions.js index 939cedd..bab5499 100644 --- a/test/getOptions.js +++ b/test/getOptions.js @@ -16,9 +16,24 @@ describe("getBaseOptions", function(){ it ("should set git branch if it exists", function(done){ testGitBranch(getBaseOptions, done); }); + it ("should detect current git hash if not passed in", function(done) { + testGitHashDetection(getBaseOptions, done); + }); + it ("should detect current git branch if not passed in", function(done) { + testGitBranchDetection(getBaseOptions, done); + }); + it ("should detect detached git head if no hash passed in", function(done) { + testGitDetachedHeadDetection(getBaseOptions, done); + }); + it ("should fail local Git detection if no .git directory", function(done) { + testNoLocalGit(getBaseOptions, done); + }); it ("should set repo_token if it exists", function(done){ testRepoToken(getBaseOptions, done); }); + it ("should detect repo_token if not passed in", function(done){ + testRepoTokenDetection(getBaseOptions, done); + }); it ("should set service_name if it exists", function(done){ testServiceName(getBaseOptions, done); }); @@ -31,12 +46,21 @@ describe("getBaseOptions", function(){ it ("should set service_name and service_job_id if it's running on circleci", function(done){ testCircleCi(getBaseOptions, done); }); + it ("should set service_name and service_job_id if it's running on codeship", function(done){ + testCodeship(getBaseOptions, done); + }); }); describe("getOptions", function(){ beforeEach(function(){ process.env = {}; }); + it ("should require a callback", function(done) { + (function() { + getOptions(); + }).should.throw(); + done(); + }); it ("should get a filepath if there is one", function(done){ process.argv[2] = "somepath"; getOptions(function(err, options){ @@ -62,9 +86,24 @@ describe("getOptions", function(){ it ("should set git branch if it exists", function(done){ testGitBranch(getOptions, done); }); + it ("should detect current git hash if not passed in", function(done) { + testGitHashDetection(getOptions, done); + }); + it ("should detect current git branch if not passed in", function(done) { + testGitBranchDetection(getOptions, done); + }); + it ("should detect detached git head if no hash passed in", function(done) { + testGitDetachedHeadDetection(getOptions, done); + }); + it ("should fail local Git detection if no .git directory", function(done) { + testNoLocalGit(getOptions, done); + }); it ("should set repo_token if it exists", function(done){ testRepoToken(getOptions, done); }); + it ("should detect repo_token if not passed in", function(done){ + testRepoTokenDetection(getOptions, done); + }); it ("should set service_name if it exists", function(done){ testServiceName(getOptions, done); }); @@ -77,6 +116,9 @@ describe("getOptions", function(){ it ("should set service_name and service_job_id if it's running on circleci", function(done){ testCircleCi(getOptions, done); }); + it ("should set service_name and service_job_id if it's running on codeship", function(done){ + testCodeship(getOptions, done); + }); }); var testServiceJobId = function(sut, done){ @@ -95,6 +137,24 @@ var testGitHash = function(sut, done){ }); }; +var testGitDetachedHeadDetection = function(sut, done){ + var localGit = ensureLocalGitContext({ detached: true }); + sut(function(err, options) { + options.git.head.id.should.equal(localGit.id); + localGit.wrapUp(); + done(); + }); +}; + +var testGitHashDetection = function(sut, done){ + var localGit = ensureLocalGitContext(); + sut(function(err, options) { + options.git.head.id.should.equal(localGit.id); + localGit.wrapUp(); + done(); + }); +}; + var testGitBranch = function(sut, done){ process.env.COVERALLS_GIT_COMMIT = "e3e3e3e3e3e3e3e3e"; process.env.COVERALLS_GIT_BRANCH = "master"; @@ -104,6 +164,24 @@ var testGitBranch = function(sut, done){ }); }; +var testGitBranchDetection = function(sut, done){ + var localGit = ensureLocalGitContext(); + sut(function(err, options) { + options.git.branch.should.equal(localGit.branch); + localGit.wrapUp(); + done(); + }); +}; + +var testNoLocalGit = function(sut, done){ + var localGit = ensureLocalGitContext({ noGit: true }); + sut(function(err, options) { + options.should.not.have.property('git'); + localGit.wrapUp(); + done(); + }); +}; + var testRepoToken = function(sut, done){ process.env.COVERALLS_REPO_TOKEN = "REPO_TOKEN"; sut(function(err, options){ @@ -112,6 +190,28 @@ var testRepoToken = function(sut, done){ }); }; +var testRepoTokenDetection = function(sut, done) { + var fs = require('fs'); + var path = require('path'); + + var file = path.join(process.cwd(), '.coveralls.yml'), token, synthetic = false; + if (fs.exists(file)) { + var yaml = require('yaml'); + /* jshint evil:true */ + token = yaml.eval(fs.readFileSync(yml, 'utf8')).repo_token; + } else { + token = 'REPO_TOKEN'; + fs.writeFileSync(file, 'repo_token: ' + token, { encoding: 'utf-8' }); + synthetic = true; + } + sut(function(err, options) { + options.repo_token.should.equal(token); + if (synthetic) + fs.unlink(file); + done(); + }); +}; + var testServiceName = function(sut, done){ process.env.COVERALLS_SERVICE_NAME = "SERVICE_NAME"; sut(function(err, options){ @@ -171,3 +271,90 @@ var testCircleCi = function(sut, done){ done(); }); }; + +var testCodeship = function(sut, done) { + process.env.CI_NAME = 'codeship'; + process.env.CI_BUILD_NUMBER = '1234'; + process.env.CI_COMMIT_ID = "e3e3e3e3e3e3e3e3e"; + process.env.CI_COMMIT_BRANCH = "e3e3e3e3e3e3e3e3e"; + sut(function(err, options){ + options.service_name.should.equal("codeship"); + options.service_job_id.should.equal("1234"); + options.git.should.eql({ head: + { id: 'e3e3e3e3e3e3e3e3e', + author_name: 'Unknown Author', + author_email: '', + committer_name: 'Unknown Committer', + committer_email: '', + message: 'Unknown Commit Message' }, + branch: 'master', + remotes: [] }); + done(); + }); +}; + +function ensureLocalGitContext(options) { + var path = require('path'); + var fs = require('fs'); + + var baseDir = process.cwd(), dir = baseDir, gitDir; + while ('/' !== dir) { + gitDir = path.join(dir, '.git'); + if (fs.existsSync(path.join(gitDir, 'HEAD'))) + break; + + dir = path.dirname(dir); + } + + options = options || {}; + var synthetic = '/' === dir; + var gitHead, branch, id, wrapUp = function() {}; + + if (synthetic) { + branch = 'synthetic'; + id = '424242424242424242'; + gitHead = path.join('.git', 'HEAD'); + var gitBranch = path.join('.git', 'refs', 'heads', branch); + fs.mkdirSync('.git'); + if (options.detached) { + fs.writeFileSync(gitHead, id, { encoding: 'utf-8' }); + } else { + fs.mkdirSync(path.join('.git', 'refs')); + fs.mkdirSync(path.join('.git', 'refs', 'heads')); + fs.writeFileSync(gitHead, "ref: refs/heads/" + branch, { encoding: 'utf-8' }); + fs.writeFileSync(gitBranch, id, { encoding: 'utf-8' }); + } + wrapUp = function() { + fs.unlinkSync(gitHead); + if (!options.detached) { + fs.unlinkSync(gitBranch); + fs.rmdirSync(path.join('.git', 'refs', 'heads')); + fs.rmdirSync(path.join('.git', 'refs')); + } + fs.rmdirSync('.git'); + }; + } else if (options.noGit) { + fs.renameSync(gitDir, gitDir + '.bak'); + wrapUp = function() { + fs.renameSync(gitDir + '.bak', gitDir); + }; + } else if (options.detached) { + gitHead = path.join(gitDir, 'HEAD'); + var content = fs.readFileSync(gitHead, 'utf-8').trim(); + var b = content.match(/^ref: refs\/heads\/(\S+)$/)[1]; + if (!b) { + id = b; + } else { + id = fs.readFileSync(path.join(gitDir, 'refs', 'heads', b), 'utf-8').trim(); + fs.writeFileSync(gitHead, id, 'utf-8'); + wrapUp = function() { + fs.writeFileSync(gitHead, content, 'utf-8'); + }; + } + } else { + branch = fs.readFileSync(path.join(gitDir, 'HEAD'), 'utf-8').trim().match(/^ref: refs\/heads\/(\S+)$/)[1]; + id = fs.readFileSync(path.join(gitDir, 'refs', 'heads', branch), 'utf-8').trim(); + } + + return { id: id, branch: branch, wrapUp: wrapUp }; +} -- cgit v1.2.3 From 5fba035487b73bb016b9e79df6a04643c92109d0 Mon Sep 17 00:00:00 2001 From: Christophe Porteneuve Date: Thu, 14 Nov 2013 20:33:45 +0100 Subject: Makefile gardening to allow for faster, cleaner running of test-cov etc. --- Makefile | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index 938b2c0..6d761a3 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,10 @@ REPORTER = spec test: - $(MAKE) lint - echo TRAVIS_JOB_ID $(TRAVIS_JOB_ID) + @$(MAKE) lint + @echo TRAVIS_JOB_ID $(TRAVIS_JOB_ID) + @$(MAKE) core_test + +core_test: @NODE_ENV=test ./node_modules/.bin/mocha -b --require blanket --reporter $(REPORTER) lint: @@ -9,11 +12,11 @@ lint: test-cov: $(MAKE) test REPORTER=spec - $(MAKE) test REPORTER=html-cov 1> coverage.html + $(MAKE) core_test REPORTER=html-cov > coverage.html test-coveralls: $(MAKE) test REPORTER=spec - $(MAKE) test REPORTER=mocha-lcov-reporter | ./bin/coveralls.js --verbose + $(MAKE) core_test REPORTER=mocha-lcov-reporter | ./bin/coveralls.js --verbose rm -rf lib-cov .PHONY: test -- cgit v1.2.3 -- cgit v1.2.3 From 85c6bee0f6150e345f78f609b9728f82b704874f Mon Sep 17 00:00:00 2001 From: Christophe Porteneuve Date: Fri, 15 Nov 2013 08:33:28 +0100 Subject: Fix getOptions test for regular-case detached head state (e.g. Travis) --- test/getOptions.js | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/test/getOptions.js b/test/getOptions.js index bab5499..4b830be 100644 --- a/test/getOptions.js +++ b/test/getOptions.js @@ -308,7 +308,7 @@ function ensureLocalGitContext(options) { options = options || {}; var synthetic = '/' === dir; - var gitHead, branch, id, wrapUp = function() {}; + var gitHead, content, branch, id, wrapUp = function() {}; if (synthetic) { branch = 'synthetic'; @@ -340,8 +340,8 @@ function ensureLocalGitContext(options) { }; } else if (options.detached) { gitHead = path.join(gitDir, 'HEAD'); - var content = fs.readFileSync(gitHead, 'utf-8').trim(); - var b = content.match(/^ref: refs\/heads\/(\S+)$/)[1]; + ontent = fs.readFileSync(gitHead, 'utf-8').trim(); + var b = (content.match(/^ref: refs\/heads\/(\S+)$/) || [])[1]; if (!b) { id = b; } else { @@ -352,8 +352,9 @@ function ensureLocalGitContext(options) { }; } } else { - branch = fs.readFileSync(path.join(gitDir, 'HEAD'), 'utf-8').trim().match(/^ref: refs\/heads\/(\S+)$/)[1]; - id = fs.readFileSync(path.join(gitDir, 'refs', 'heads', branch), 'utf-8').trim(); + content = fs.readFileSync(path.join(gitDir, 'HEAD'), 'utf-8').trim(); + branch = (content.match(/^ref: refs\/heads\/(\S+)$/) || [])[1]; + id = branch ? fs.readFileSync(path.join(gitDir, 'refs', 'heads', branch), 'utf-8').trim() : content; } return { id: id, branch: branch, wrapUp: wrapUp }; -- cgit v1.2.3 From a591244f4467500b29ce1edb313e87dfd2e12f5b Mon Sep 17 00:00:00 2001 From: Christophe Porteneuve Date: Fri, 15 Nov 2013 08:35:56 +0100 Subject: More regular-case detached head (e.g. Travis) fixes --- test/getOptions.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/test/getOptions.js b/test/getOptions.js index 4b830be..c1abc1f 100644 --- a/test/getOptions.js +++ b/test/getOptions.js @@ -167,7 +167,10 @@ var testGitBranch = function(sut, done){ var testGitBranchDetection = function(sut, done){ var localGit = ensureLocalGitContext(); sut(function(err, options) { - options.git.branch.should.equal(localGit.branch); + if (localGit.branch) + options.git.branch.should.equal(localGit.branch); + else + options.git.should.not.have.property('branch'); localGit.wrapUp(); done(); }); -- cgit v1.2.3 From af38f79677abc820cbb24f5728672cf28c3c34c3 Mon Sep 17 00:00:00 2001 From: Christophe Porteneuve Date: Fri, 15 Nov 2013 08:38:08 +0100 Subject: =?UTF-8?q?Stupid=20typo=E2=80=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/getOptions.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/getOptions.js b/test/getOptions.js index c1abc1f..d0c4173 100644 --- a/test/getOptions.js +++ b/test/getOptions.js @@ -343,7 +343,7 @@ function ensureLocalGitContext(options) { }; } else if (options.detached) { gitHead = path.join(gitDir, 'HEAD'); - ontent = fs.readFileSync(gitHead, 'utf-8').trim(); + content = fs.readFileSync(gitHead, 'utf-8').trim(); var b = (content.match(/^ref: refs\/heads\/(\S+)$/) || [])[1]; if (!b) { id = b; -- cgit v1.2.3 From 270950f1103177b916842047c623042c715bc08e Mon Sep 17 00:00:00 2001 From: Christophe Porteneuve Date: Fri, 15 Nov 2013 09:26:56 +0100 Subject: Final fixes for detached head mode (93% cov against 94% cov in branch mode) --- test/getOptions.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/getOptions.js b/test/getOptions.js index d0c4173..5afd9e5 100644 --- a/test/getOptions.js +++ b/test/getOptions.js @@ -279,7 +279,7 @@ var testCodeship = function(sut, done) { process.env.CI_NAME = 'codeship'; process.env.CI_BUILD_NUMBER = '1234'; process.env.CI_COMMIT_ID = "e3e3e3e3e3e3e3e3e"; - process.env.CI_COMMIT_BRANCH = "e3e3e3e3e3e3e3e3e"; + process.env.CI_BRANCH = "master"; sut(function(err, options){ options.service_name.should.equal("codeship"); options.service_job_id.should.equal("1234"); @@ -346,7 +346,7 @@ function ensureLocalGitContext(options) { content = fs.readFileSync(gitHead, 'utf-8').trim(); var b = (content.match(/^ref: refs\/heads\/(\S+)$/) || [])[1]; if (!b) { - id = b; + id = content; } else { id = fs.readFileSync(path.join(gitDir, 'refs', 'heads', b), 'utf-8').trim(); fs.writeFileSync(gitHead, id, 'utf-8'); -- cgit v1.2.3 From dd9958c8ce913503d11f86ff4c80449b6ec16cf8 Mon Sep 17 00:00:00 2001 From: Christophe Porteneuve Date: Fri, 15 Nov 2013 09:30:37 +0100 Subject: Fixed encoding from utf-8 to utf8 for Travis --- test/getOptions.js | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/test/getOptions.js b/test/getOptions.js index 5afd9e5..ebb725d 100644 --- a/test/getOptions.js +++ b/test/getOptions.js @@ -204,7 +204,7 @@ var testRepoTokenDetection = function(sut, done) { token = yaml.eval(fs.readFileSync(yml, 'utf8')).repo_token; } else { token = 'REPO_TOKEN'; - fs.writeFileSync(file, 'repo_token: ' + token, { encoding: 'utf-8' }); + fs.writeFileSync(file, 'repo_token: ' + token, { encoding: 'utf8' }); synthetic = true; } sut(function(err, options) { @@ -320,12 +320,12 @@ function ensureLocalGitContext(options) { var gitBranch = path.join('.git', 'refs', 'heads', branch); fs.mkdirSync('.git'); if (options.detached) { - fs.writeFileSync(gitHead, id, { encoding: 'utf-8' }); + fs.writeFileSync(gitHead, id, { encoding: 'utf8' }); } else { fs.mkdirSync(path.join('.git', 'refs')); fs.mkdirSync(path.join('.git', 'refs', 'heads')); - fs.writeFileSync(gitHead, "ref: refs/heads/" + branch, { encoding: 'utf-8' }); - fs.writeFileSync(gitBranch, id, { encoding: 'utf-8' }); + fs.writeFileSync(gitHead, "ref: refs/heads/" + branch, { encoding: 'utf8' }); + fs.writeFileSync(gitBranch, id, { encoding: 'utf8' }); } wrapUp = function() { fs.unlinkSync(gitHead); @@ -343,21 +343,21 @@ function ensureLocalGitContext(options) { }; } else if (options.detached) { gitHead = path.join(gitDir, 'HEAD'); - content = fs.readFileSync(gitHead, 'utf-8').trim(); + content = fs.readFileSync(gitHead, 'utf8').trim(); var b = (content.match(/^ref: refs\/heads\/(\S+)$/) || [])[1]; if (!b) { id = content; } else { - id = fs.readFileSync(path.join(gitDir, 'refs', 'heads', b), 'utf-8').trim(); - fs.writeFileSync(gitHead, id, 'utf-8'); + id = fs.readFileSync(path.join(gitDir, 'refs', 'heads', b), 'utf8').trim(); + fs.writeFileSync(gitHead, id, 'utf8'); wrapUp = function() { - fs.writeFileSync(gitHead, content, 'utf-8'); + fs.writeFileSync(gitHead, content, 'utf8'); }; } } else { - content = fs.readFileSync(path.join(gitDir, 'HEAD'), 'utf-8').trim(); + content = fs.readFileSync(path.join(gitDir, 'HEAD'), 'utf8').trim(); branch = (content.match(/^ref: refs\/heads\/(\S+)$/) || [])[1]; - id = branch ? fs.readFileSync(path.join(gitDir, 'refs', 'heads', branch), 'utf-8').trim() : content; + id = branch ? fs.readFileSync(path.join(gitDir, 'refs', 'heads', branch), 'utf8').trim() : content; } return { id: id, branch: branch, wrapUp: wrapUp }; -- cgit v1.2.3 From a639b7dcb280f3b1ab6c8357fb3c8b9057543e79 Mon Sep 17 00:00:00 2001 From: Christophe Porteneuve Date: Fri, 15 Nov 2013 09:33:25 +0100 Subject: OK so Travis behaves WEIRDLY on writeFileSync with explicit utf8, let's try with implicit --- test/getOptions.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/getOptions.js b/test/getOptions.js index ebb725d..0639a3c 100644 --- a/test/getOptions.js +++ b/test/getOptions.js @@ -204,7 +204,7 @@ var testRepoTokenDetection = function(sut, done) { token = yaml.eval(fs.readFileSync(yml, 'utf8')).repo_token; } else { token = 'REPO_TOKEN'; - fs.writeFileSync(file, 'repo_token: ' + token, { encoding: 'utf8' }); + fs.writeFileSync(file, 'repo_token: ' + token); synthetic = true; } sut(function(err, options) { -- cgit v1.2.3