Current Dev State

This commit is contained in:
Tim Lorsbach
2025-06-23 20:13:54 +02:00
parent b4f9bb277d
commit ded50edaa2
22617 changed files with 4345095 additions and 174 deletions

View File

@ -0,0 +1,178 @@
var convertLcovToCoveralls = require('../index').convertLcovToCoveralls;
var getOptions = require('../index').getOptions;
var should = require('should');
var fs = require('fs');
var logger = require('../lib/logger');
var path = require('path');
logger = require('log-driver')({level : false});
describe("convertLcovToCoveralls", function(){
it ("should convert a simple lcov file", function(done){
delete process.env.TRAVIS;
var lcovpath = __dirname + "/../fixtures/onefile.lcov";
var input = fs.readFileSync(lcovpath, "utf8");
var libpath = __dirname + "/../fixtures/lib";
convertLcovToCoveralls(input, {filepath: libpath}, function(err, output){
should.not.exist(err);
output.source_files[0].name.should.equal("index.js");
output.source_files[0].source.split("\n").length.should.equal(173);
output.source_files[0].coverage[54].should.equal(0);
output.source_files[0].coverage[60].should.equal(0);
done();
});
});
it ("should pass on all appropriate parameters from the environment", function(done){
delete process.env.TRAVIS;
process.env.COVERALLS_GIT_COMMIT = "GIT_HASH";
process.env.COVERALLS_GIT_BRANCH = "master";
process.env.COVERALLS_SERVICE_NAME = "SERVICE_NAME";
process.env.COVERALLS_SERVICE_JOB_ID = "SERVICE_JOB_ID";
process.env.COVERALLS_REPO_TOKEN = "REPO_TOKEN";
process.env.CI_PULL_REQUEST = "https://github.com/fake/fake/pulls/123";
process.env.COVERALLS_PARALLEL = "true";
getOptions(function(err, options){
var lcovpath = __dirname + "/../fixtures/onefile.lcov";
var input = fs.readFileSync(lcovpath, "utf8");
var libpath = "fixtures/lib";
options.filepath = libpath;
convertLcovToCoveralls(input, options, function(err, output){
should.not.exist(err);
output.service_pull_request.should.equal("123");
output.parallel.should.equal(true);
//output.git.should.equal("GIT_HASH");
done();
});
});
});
it ("should work with a relative path as well", function(done){
delete process.env.TRAVIS;
var lcovpath = __dirname + "/../fixtures/onefile.lcov";
var input = fs.readFileSync(lcovpath, "utf8");
var libpath = "fixtures/lib";
convertLcovToCoveralls(input, {filepath: libpath}, function(err, output){
should.not.exist(err);
output.source_files[0].name.should.equal("index.js");
output.source_files[0].source.split("\n").length.should.equal(173);
done();
});
});
it ("should convert absolute input paths to relative", function(done){
delete process.env.TRAVIS;
var lcovpath = __dirname + "/../fixtures/istanbul.lcov";
var input = fs.readFileSync(lcovpath, "utf8");
var libpath = "/Users/deepsweet/Dropbox/projects/svgo/lib";
var sourcepath = path.resolve(libpath, "svgo/config.js");
var originalReadFileSync = fs.readFileSync;
fs.readFileSync = function(filepath) {
if (filepath === sourcepath) {
return '';
}
return originalReadFileSync.apply(fs, arguments);
};
var originalExistsSync = fs.existsSync;
fs.existsSync = function () { return true; };
convertLcovToCoveralls(input, {filepath: libpath}, function(err, output){
fs.readFileSync = originalReadFileSync;
fs.existsSync = originalExistsSync;
should.not.exist(err);
output.source_files[0].name.should.equal(path.join("svgo", "config.js"));
done();
});
});
it ("should handle branch coverage data", function(done){
process.env.TRAVIS_JOB_ID = -1;
var lcovpath = __dirname + "/../fixtures/istanbul.lcov";
var input = fs.readFileSync(lcovpath, "utf8");
var libpath = "/Users/deepsweet/Dropbox/projects/svgo/lib";
var sourcepath = path.resolve(libpath, "svgo/config.js");
var originalReadFileSync = fs.readFileSync;
fs.readFileSync = function(filepath) {
if (filepath === sourcepath) {
return '';
}
return originalReadFileSync.apply(fs, arguments);
};
var originalExistsSync = fs.existsSync;
fs.existsSync = function () { return true; };
convertLcovToCoveralls(input, {filepath: libpath}, function(err, output){
fs.readFileSync = originalReadFileSync;
fs.existsSync = originalExistsSync;
should.not.exist(err);
output.source_files[0].branches.slice(0,8).should.eql([18,1,0,85,18,1,1,2]);
done();
});
});
it ("should ignore files that do not exists", function(done){
delete process.env.TRAVIS;
var lcovpath = __dirname + "/../fixtures/istanbul.lcov";
var input = fs.readFileSync(lcovpath, "utf8");
var libpath = "/Users/deepsweet/Dropbox/projects/svgo/lib";
var sourcepath = path.resolve(libpath, "svgo/config.js");
var originalReadFileSync = fs.readFileSync;
fs.readFileSync = function(filepath) {
if (filepath === sourcepath) {
return '';
}
return originalReadFileSync.apply(fs, arguments);
};
var originalExistsSync = fs.existsSync;
fs.existsSync = function () { return false; };
convertLcovToCoveralls(input, {filepath: libpath}, function(err, output){
fs.readFileSync = originalReadFileSync;
fs.existsSync = originalExistsSync;
should.not.exist(err);
output.source_files.should.be.empty();
done();
});
});
it ("should parse file paths concatenated by typescript and ng 2", function(done) {
process.env.TRAVIS_JOB_ID = -1;
var lcovpath = __dirname + "/../fixtures/istanbul.remap.lcov";
var input = fs.readFileSync(lcovpath, "utf8");
var libpath = "/Users/deepsweet/Dropbox/projects/svgo/lib";
var sourcepath = path.resolve(libpath, "svgo/config.js");
var originalReadFileSync = fs.readFileSync;
fs.readFileSync = function(filepath) {
if (filepath === sourcepath) {
return '';
}
return originalReadFileSync.apply(fs, arguments);
};
var originalExistsSync = fs.existsSync;
fs.existsSync = function () { return true; };
convertLcovToCoveralls(input, {filepath: libpath}, function(err, output){
fs.readFileSync = originalReadFileSync;
fs.existsSync = originalExistsSync;
should.not.exist(err);
output.source_files[0].name.should.equal(path.join("svgo", "config.js"));
done();
});
});
});

View File

@ -0,0 +1,77 @@
var should = require('should');
var fs = require('fs');
var path = require('path');
var detectLocalGit = require('../lib/detectLocalGit');
var ORIGINAL_CWD = process.cwd();
var TEST_DIR = path.resolve(__dirname);
var TEMP_GIT_DIR = path.join(TEST_DIR, '.git');
describe("detectLocalGit", function() {
before(function() {
_makeTempGitDir();
process.chdir(TEST_DIR);
});
after(function() {
_cleanTempGitDir();
process.chdir(ORIGINAL_CWD);
});
it('should get commit hash from packed-refs when refs/heads/master does not exist', function() {
var results = detectLocalGit();
should.exist(results);
(results).should.deepEqual({
git_commit: '0000000000000000ffffffffffffffffffffffff',
git_branch: 'master'
});
});
});
function _makeTempGitDir() {
_cleanTempGitDir();
var dir = TEMP_GIT_DIR;
fs.mkdirSync(dir);
var HEAD = path.join(dir, 'HEAD');
var packedRefs = path.join(dir, 'packed-refs');
fs.writeFileSync(HEAD, 'ref: refs/heads/master');
fs.writeFileSync(packedRefs, "" +
"# pack-refs with: peeled fully-peeled\n" +
"0000000000000000000000000000000000000000 refs/heads/other/ref\n" +
"0000000000000000ffffffffffffffffffffffff refs/heads/master\n" +
"ffffffffffffffffffffffffffffffffffffffff refs/remotes/origin/other\n");
}
function _cleanTempGitDir() {
_deleteFolderRecursive(TEMP_GIT_DIR);
}
function _deleteFolderRecursive(dir) {
if (!dir.match('node-coveralls/test')) {
throw new Error('Tried to clean a temp git directory that did not match path: node-coveralls/test');
}
if(fs.existsSync(dir)) {
fs.readdirSync(dir).forEach(function(file,index){
var curPath = path.join(dir, file);
if(fs.lstatSync(curPath).isDirectory()) { // recurse
_deleteFolderRecursive(curPath);
} else { // delete file
fs.unlinkSync(curPath);
}
});
fs.rmdirSync(dir);
}
}

View File

@ -0,0 +1,182 @@
var should = require('should');
var fetchGitData = require('../lib/fetchGitData');
var getOptions = require('../index').getOptions;
describe("fetchGitData", function(){
beforeEach(function(){
process.env = {PATH: process.env.PATH};
});
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){
err.should.match(/You must provide the head/);
done();
});
});
it("should throw an error if no head.id is provided", function(done) {
fetchGitData({
head: {}
}, function(err){
err.should.match(/You must provide the head.id/);
done();
});
});
it("should return default values", function(done) {
var options = fetchGitData({
head: {
id: "COMMIT_HASH"
}
}, function(err, options){
options.should.eql({
"head": {
"id": "COMMIT_HASH",
"author_name": "Unknown Author",
"author_email": "",
"committer_name": "Unknown Committer",
"committer_email": "",
"message": "Unknown Commit Message"
},
"branch": "",
"remotes": []
});
done();
});
});
it("should override default values", function(done) {
var options = fetchGitData({
"head": {
"id": "COMMIT_HASH",
"author_name": "MY AUTHOR",
"author_email": "",
"committer_name": "MY COMMITTER",
"committer_email": "",
"message": "MY COMMIT MESSAGE"
},
"branch": "TEST",
"remotes": [
{
"name": "TEST",
"url": "test-url"
}
]
}, function(err, options){
options.should.eql({
"head": {
"id": "COMMIT_HASH",
"author_name": "MY AUTHOR",
"author_email": "",
"committer_name": "MY COMMITTER",
"committer_email": "",
"message": "MY COMMIT MESSAGE"
},
"branch": "TEST",
"remotes": [
{
"name": "TEST",
"url": "test-url"
}
]
});
done();
});
});
it("should convert git.branch to a string", function(done) {
fetchGitData({
"head": {
"id": "COMMIT_HASH"
},
"branch": {
"covert": "to a string"
}
}, function(err, str){
str.branch.should.be.String();
fetchGitData({
"head": {
"id": "COMMIT_HASH"
},
"branch": ["convert", "to", "a", "string"]
}, function(err, str){
str.branch.should.be.String();
done();
});
});
});
it("should convert git.remotes to an array", function(done) {
fetchGitData({
"head": {
"id": "COMMIT_HASH"
},
"remotes": "convert from string to an array"
}, function(err, arr){
arr.remotes.should.be.instanceof(Array);
fetchGitData({
"head": {
"id": "COMMIT_HASH"
},
"remotes": {
"convert": "from object to an array"
}
}, function(err, arr){
arr.remotes.should.be.instanceof(Array);
done();
});
});
});
it("should save passed remotes", function(done) {
fetchGitData({
"head": {
"id": "COMMIT_HASH"
},
"remotes": [
{
"name": "test",
"url": "https://my.test.url"
}
]
}, function(err, options){
options.should.eql({
"head": {
"id": "COMMIT_HASH",
"author_name": "Unknown Author",
"author_email": "",
"committer_name": "Unknown Committer",
"committer_email": "",
"message": "Unknown Commit Message"
},
"branch": "",
"remotes": [
{
"name": "test",
"url": "https://my.test.url"
}
]
});
done();
});
});
it("should execute git commands when a valid commit hash is given", function(done) {
process.env.COVERALLS_GIT_COMMIT = "HEAD";
process.env.COVERALLS_GIT_BRANCH = "master";
getOptions(function(err, options){
options = options.git;
options.head.should.be.Object();
options.head.author_name.should.not.equal("Unknown Author");
options.head.committer_name.should.not.equal("Unknown Committer");
options.head.message.should.not.equal("Unknown Commit Message");
options.branch.should.be.String();
options.should.have.property("remotes");
options.remotes.should.be.instanceof(Array);
options.remotes.length.should.be.above(0);
done();
});
});
});

View File

@ -0,0 +1,516 @@
var should = require('should');
var index = require('../index');
var getOptions = index.getOptions;
var getBaseOptions = index.getBaseOptions;
describe("getBaseOptions", function(){
beforeEach(function(){
process.env = {PATH: process.env.PATH};
});
it ("should set service_job_id if it exists", function(done){
testServiceJobId(getBaseOptions, done);
});
it ("should set git hash if it exists", function(done){
testGitHash(getBaseOptions, done);
});
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);
});
it ("should set service_name and service_job_id if it's running on travis-ci", function(done){
testTravisCi(getBaseOptions, done);
});
it ("should set service_name and service_job_id if it's running on jenkins", function(done){
testJenkins(getBaseOptions, done);
});
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);
});
it ("should set service_name and service_job_id if it's running on drone", function(done){
testDrone(getBaseOptions, done);
});
it ("should set service_name and service_job_id if it's running on wercker", function(done){
testWercker(getBaseOptions, done);
});
});
describe("getOptions", function(){
beforeEach(function(){
process.env = {PATH: process.env.PATH};
});
it ("should require a callback", function(done) {
(function() {
getOptions();
}).should.throw();
done();
});
it ("should get a filepath if there is one", function(done){
index.options._ = ["somepath"];
getOptions(function(err, options){
options.filepath.should.equal("somepath");
done();
});
});
it ("should get a filepath if there is one, even in verbose mode", function(done){
index.options.verbose = "true";
index.options._ = ["somepath"];
getOptions(function(err, options){
options.filepath.should.equal("somepath");
done();
});
});
it ("should set service_job_id if it exists", function(done){
testServiceJobId(getOptions, done);
});
it ("should set git hash if it exists", function(done){
testGitHash(getOptions, done);
});
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 paralell if env var set", function(done){
testParallel(getOptions, done);
});
it ("should set service_name if it exists", function(done){
testServiceName(getOptions, done);
});
it("should set service_pull_request if it exists", function(done){
testServicePullRequest(getOptions, done);
});
it ("should set service_name and service_job_id if it's running on travis-ci", function(done){
testTravisCi(getOptions, done);
});
it ("should set service_name and service_job_id if it's running on jenkins", function(done){
testJenkins(getOptions, done);
});
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);
});
it ("should set service_name and service_job_id if it's running on drone", function(done){
testDrone(getBaseOptions, done);
});
it ("should set service_name and service_job_id if it's running on wercker", function(done){
testWercker(getOptions, done);
});
it ("should set service_name and service_job_id if it's running on Gitlab", function(done){
testGitlab(getOptions, done);
});
it ("should set service_name and service_job_id if it's running via Surf", function(done){
testSurf(getOptions, done);
});
it ("should override set options with user options", function(done){
var userOptions = {service_name: 'OVERRIDDEN_SERVICE_NAME'};
process.env.COVERALLS_SERVICE_NAME = "SERVICE_NAME";
getOptions(function(err, options){
options.service_name.should.equal("OVERRIDDEN_SERVICE_NAME");
done();
}, userOptions);
});
});
var testServiceJobId = function(sut, done){
process.env.COVERALLS_SERVICE_JOB_ID = "SERVICE_JOB_ID";
sut(function(err, options){
options.service_job_id.should.equal("SERVICE_JOB_ID");
done();
});
};
var testGitHash = function(sut, done){
process.env.COVERALLS_GIT_COMMIT = "e3e3e3e3e3e3e3e3e";
sut(function(err, options){
options.git.head.id.should.equal("e3e3e3e3e3e3e3e3e");
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";
sut(function(err, options){
options.git.branch.should.equal("master");
done();
});
};
var testGitBranchDetection = function(sut, done){
var localGit = ensureLocalGitContext();
sut(function(err, options) {
if (localGit.branch)
options.git.branch.should.equal(localGit.branch);
else
options.git.should.not.have.key('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){
options.repo_token.should.equal("REPO_TOKEN");
done();
});
};
var testParallel = function(sut, done){
process.env.COVERALLS_PARALLEL = "true";
sut(function(err, options){
options.parallel.should.equal(true);
done();
});
};
var testRepoTokenDetection = function(sut, done) {
var fs = require('fs');
var path = require('path');
var file = path.join(process.cwd(), '.coveralls.yml'), token, service_name, synthetic = false;
if (fs.exists(file)) {
var yaml = require('js-yaml');
var coveralls_yml_doc = yaml.safeLoad(fs.readFileSync(yml, 'utf8'));
token = coveralls_yml_doc.repo_token;
if(coveralls_yml_doc.service_name) {
service_name = coveralls_yml_doc.service_name;
}
} else {
token = 'REPO_TOKEN';
service_name = 'travis-pro';
fs.writeFileSync(file, 'repo_token: ' + token+'\nservice_name: ' + service_name);
synthetic = true;
}
sut(function(err, options) {
options.repo_token.should.equal(token);
if(service_name) {
options.service_name.should.equal(service_name);
}
if (synthetic)
fs.unlink(file);
done();
});
};
var testServiceName = function(sut, done){
process.env.COVERALLS_SERVICE_NAME = "SERVICE_NAME";
sut(function(err, options){
options.service_name.should.equal("SERVICE_NAME");
done();
});
};
var testServicePullRequest = function(sut, done){
process.env.CI_PULL_REQUEST = "https://github.com/fake/fake/pulls/123";
sut(function(err, options){
options.service_pull_request.should.equal("123");
done();
});
};
var testTravisCi = function(sut, done){
process.env.TRAVIS = "TRUE";
process.env.TRAVIS_JOB_ID = "1234";
process.env.TRAVIS_PULL_REQUEST = "123";
sut(function(err, options){
options.service_name.should.equal("travis-ci");
options.service_job_id.should.equal("1234");
options.service_pull_request.should.equal("123");
done();
});
};
var testJenkins = function(sut, done){
process.env.JENKINS_URL = "something";
process.env.BUILD_ID = "1234";
process.env.GIT_COMMIT = "a12s2d3df4f435g45g45g67h5g6";
process.env.GIT_BRANCH = "master";
sut(function(err, options){
options.service_name.should.equal("jenkins");
options.service_job_id.should.equal("1234");
options.git.should.eql({ head:
{ id: 'a12s2d3df4f435g45g45g67h5g6',
author_name: 'Unknown Author',
author_email: '',
committer_name: 'Unknown Committer',
committer_email: '',
message: 'Unknown Commit Message' },
branch: 'master',
remotes: [] });
done();
});
};
var testCircleCi = function(sut, done){
process.env.CIRCLECI = true;
process.env.CIRCLE_BRANCH = "master";
process.env.CIRCLE_BUILD_NUM = "1234";
process.env.CIRCLE_SHA1 = "e3e3e3e3e3e3e3e3e";
process.env.CI_PULL_REQUEST = 'http://github.com/node-coveralls/pull/3';
sut(function(err, options){
options.service_name.should.equal("circleci");
options.service_job_id.should.equal("1234");
options.service_pull_request.should.equal('3');
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();
});
};
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_BRANCH = "master";
process.env.CI_COMMITTER_NAME = "John Doe";
process.env.CI_COMMITTER_EMAIL = "jd@example.com";
process.env.CI_COMMIT_MESSAGE = "adadadadadadadadadad";
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: 'John Doe',
committer_email: 'jd@example.com',
message: 'adadadadadadadadadad' },
branch: 'master',
remotes: [] });
done();
});
};
var testDrone = function(sut, done) {
process.env.DRONE = true;
process.env.DRONE_BUILD_NUMBER = '1234';
process.env.DRONE_COMMIT = "e3e3e3e3e3e3e3e3e";
process.env.DRONE_BRANCH = "master";
process.env.DRONE_PULL_REQUEST = '3';
process.env.DRONE_COMMIT_AUTHOR = 'john doe';
process.env.DRONE_COMMIT_AUTHOR_EMAIL = 'john@doe.com';
process.env.DRONE_COMMIT_MESSAGE = 'msgmsgmsg';
sut(function(err, options){
options.service_name.should.equal("drone");
options.service_job_id.should.equal("1234");
options.git.should.eql({ head:
{ id: 'e3e3e3e3e3e3e3e3e',
author_name: 'Unknown Author',
author_email: '',
committer_name: 'john doe',
committer_email: 'john@doe.com',
message: 'msgmsgmsg' },
branch: 'master',
remotes: [] });
done();
});
};
var testWercker = function(sut, done) {
process.env.WERCKER = true;
process.env.WERCKER_BUILD_ID = '1234';
process.env.WERCKER_GIT_COMMIT = "e3e3e3e3e3e3e3e3e";
process.env.WERCKER_GIT_BRANCH = "master";
sut(function(err, options){
options.service_name.should.equal("wercker");
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();
});
};
var testGitlab = function(sut, done) {
process.env.GITLAB_CI = true;
process.env.CI_BUILD_NAME = 'spec:one';
process.env.CI_BUILD_ID = "1234";
process.env.CI_BUILD_REF = "e3e3e3e3e3e3e3e3e";
process.env.CI_BUILD_REF_NAME = "feature";
sut(function(err, options){
options.service_name.should.equal("gitlab-ci");
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: 'feature',
remotes: [] });
done();
});
};
var testSurf = function(sut, done) {
process.env.CI_NAME = 'surf';
process.env.SURF_SHA1 = "e3e3e3e3e3e3e3e3e";
process.env.SURF_REF = "feature";
sut(function(err, options){
options.service_name.should.equal("surf");
options.git.should.eql({ head:
{ id: 'e3e3e3e3e3e3e3e3e',
author_name: 'Unknown Author',
author_email: '',
committer_name: 'Unknown Committer',
committer_email: '',
message: 'Unknown Commit Message' },
branch: 'feature',
remotes: [] });
done();
});
};
function ensureLocalGitContext(options) {
var path = require('path');
var fs = require('fs');
var baseDir = process.cwd(), dir = baseDir, gitDir;
while (path.resolve('/') !== dir) {
gitDir = path.join(dir, '.git');
var existsSync = fs.existsSync || path.existsSync;
if (existsSync(path.join(gitDir, 'HEAD')))
break;
dir = path.dirname(dir);
}
options = options || {};
var synthetic = path.resolve('/') === dir;
var gitHead, content, 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: 'utf8' });
} else {
fs.mkdirSync(path.join('.git', 'refs'));
fs.mkdirSync(path.join('.git', 'refs', 'heads'));
fs.writeFileSync(gitHead, "ref: refs/heads/" + branch, { encoding: 'utf8' });
fs.writeFileSync(gitBranch, id, { encoding: 'utf8' });
}
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');
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), 'utf8').trim();
fs.writeFileSync(gitHead, id, 'utf8');
wrapUp = function() {
fs.writeFileSync(gitHead, content, 'utf8');
};
}
} else {
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), 'utf8').trim() : content;
}
return { id: id, branch: branch, wrapUp: wrapUp };
}

View File

@ -0,0 +1,78 @@
var should = require('should');
var sinon = require('sinon-restore');
var index = require('../index');
var fs = require('fs');
logger = require('log-driver')({level : false});
describe("handleInput", function(){
afterEach(function() {
sinon.restoreAll();
});
it ("returns an error when there's an error getting options", function(done){
sinon.stub(index, 'getOptions', function(cb){
return cb("some error", {});
});
var path = __dirname + "/../fixtures/onefile.lcov";
var input = fs.readFileSync(path, "utf8");
index.handleInput(input, function(err){
err.should.equal("some error");
done();
});
});
it ("returns an error when there's an error converting", function(done){
sinon.stub(index, 'getOptions', function(cb){
return cb(null, {});
});
sinon.stub(index, 'convertLcovToCoveralls', function(input, options, cb){
cb("some error");
});
var path = __dirname + "/../fixtures/onefile.lcov";
var input = fs.readFileSync(path, "utf8");
index.handleInput(input, function(err){
err.should.equal("some error");
done();
});
});
it ("returns an error when there's an error sending", function(done){
sinon.stub(index, 'getOptions', function(cb){
return cb(null, {});
});
sinon.stub(index, 'sendToCoveralls', function(postData, cb){
cb("some error");
});
var path = __dirname + "/../fixtures/onefile.lcov";
var input = fs.readFileSync(path, "utf8");
index.handleInput(input, function(err){
err.should.equal("some error");
done();
});
});
it ("returns an error when there's a bad status code", function(done){
sinon.stub(index, 'getOptions', function(cb){
return cb(null, {});
});
sinon.stub(index, 'sendToCoveralls', function(postData, cb){
cb(null, {statusCode : 500}, "body");
});
var path = __dirname + "/../fixtures/onefile.lcov";
var input = fs.readFileSync(path, "utf8");
index.handleInput(input, function(err){
err.should.equal("Bad response: 500 body");
done();
});
});
it ("completes successfully when there are no errors", function(done){
sinon.stub(index, 'getOptions', function(cb){
return cb(null, {});
});
sinon.stub(index, 'sendToCoveralls', function(postData, cb){
cb(null, {statusCode : 200}, "body");
});
var path = __dirname + "/../fixtures/onefile.lcov";
var input = fs.readFileSync(path, "utf8");
index.handleInput(input, function(err){
(err === null).should.equal(true);
done();
});
});
});

View File

@ -0,0 +1,32 @@
var should = require('should');
var sinon = require('sinon-restore');
var index = require('../index');
describe("logger", function(){
it ("should log at debug level when --verbose is set", function(){
index.options.verbose = true;
var logger = require('../index').logger();
logger.level.should.equal('warn');
});
it ("should log at debug level when NODE_COVERALLS_DEBUG is set in env", function(){
index.options.verbose = false;
process.env.NODE_COVERALLS_DEBUG = 1;
var logger = require('../index').logger();
logger.level.should.equal('warn');
});
it ("should log at debug level when NODE_COVERALLS_DEBUG is set in env as a string", function(){
index.options.verbose = false;
process.env.NODE_COVERALLS_DEBUG = '1';
var logger = require('../index').logger();
logger.level.should.equal('warn');
});
it ("should log at warn level when NODE_COVERALLS_DEBUG not set and no --verbose", function(){
index.options.verbose = false;
process.env.NODE_COVERALLS_DEBUG = 0;
var logger = require('../index').logger();
logger.level.should.equal('error');
});
});

View File

@ -0,0 +1,75 @@
var should = require('should');
var request = require('request');
var sinon = require('sinon-restore');
var index = require('../index');
logger = require('log-driver')({level : false});
describe("sendToCoveralls", function(){
var realCoverallsHost;
beforeEach(function() {
realCoverallsHost = process.env.COVERALLS_ENDPOINT;
});
afterEach(function() {
sinon.restoreAll();
if (realCoverallsHost !== undefined) {
process.env.COVERALLS_ENDPOINT = realCoverallsHost;
} else {
delete process.env.COVERALLS_ENDPOINT;
}
});
it ("passes on the correct params to request.post", function(done){
sinon.stub(request, 'post', function(obj, cb){
obj.url.should.equal('https://coveralls.io/api/v1/jobs');
obj.form.should.eql({json : '{"some":"obj"}'});
cb('err', 'response', 'body');
});
var obj = {"some":"obj"};
index.sendToCoveralls(obj, function(err, response, body){
err.should.equal('err');
response.should.equal('response');
body.should.equal('body');
done();
});
});
it ("allows sending to enterprise url", function(done){
process.env.COVERALLS_ENDPOINT = 'https://coveralls-ubuntu.domain.com';
sinon.stub(request, 'post', function(obj, cb){
obj.url.should.equal('https://coveralls-ubuntu.domain.com/api/v1/jobs');
obj.form.should.eql({json : '{"some":"obj"}'});
cb('err', 'response', 'body');
});
var obj = {"some":"obj"};
index.sendToCoveralls(obj, function(err, response, body){
err.should.equal('err');
response.should.equal('response');
body.should.equal('body');
done();
});
});
it ("writes output to stdout when --stdout is passed", function(done) {
var obj = {"some":"obj"};
// set up mock process.stdout.write temporarily
var origStdoutWrite = process.stdout.write;
process.stdout.write = function(string) {
if (string == JSON.stringify(obj)) {
process.stdout.write = origStdoutWrite;
return done();
}
origStdoutWrite.apply(this, arguments);
};
index.options.stdout = true;
index.sendToCoveralls(obj, function(err, response, body) {
response.statusCode.should.equal(200);
});
});
});