Skip to content

Commit 75c5864

Browse files
committed
Initial Commit
0 parents  commit 75c5864

File tree

9 files changed

+363
-0
lines changed

9 files changed

+363
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_modules
2+
npm-debug.log
3+
tmp

Gruntfile.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* grunt-appc-coverage
3+
*
4+
*
5+
* Copyright (c) 2015 Appcelerator
6+
* Licensed under the MIT license.
7+
*/
8+
9+
'use strict';
10+
11+
module.exports = function (grunt) {
12+
// load all npm grunt tasks
13+
require('load-grunt-tasks')(grunt);
14+
15+
// Project configuration.
16+
grunt.initConfig({
17+
18+
mochaTest: {
19+
options: {
20+
timeout: 3000,
21+
reporter: 'spec',
22+
ignoreLeaks: false
23+
},
24+
unit: {
25+
src: ['test/**/*_test.js']
26+
},
27+
},
28+
29+
appcJs: {
30+
src: ['lib/**/*.js']
31+
},
32+
33+
// Before generating any new files, remove any previously-created files.
34+
clean: {
35+
tests: ['tmp']
36+
},
37+
38+
kahvesi: { src: ['test/**/*_test.js'] },
39+
40+
// Configuration to be run (and then tested).
41+
appc_coverage: {
42+
default_options: {
43+
src: 'coverage/lcov.info',
44+
force: true
45+
}
46+
},
47+
48+
});
49+
50+
// Actually load this plugin's task(s).
51+
grunt.loadTasks('tasks');
52+
53+
grunt.loadNpmTasks('grunt-kahvesi');
54+
grunt.loadNpmTasks('grunt-mocha-test');
55+
grunt.loadNpmTasks('grunt-appc-js');
56+
57+
// Whenever the "test" task is run, first clean the "tmp" dir, then run this
58+
// plugin's task(s), then test the result.
59+
grunt.registerTask('test', ['clean', 'appcJs','mochaTest:unit', 'kahvesi', 'appc_coverage']);
60+
61+
// By default, lint and run all tests.
62+
grunt.registerTask('default', ['jshint', 'appcJs','mochaTest:unit']);
63+
64+
};

README.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# grunt-appc-coverage
2+
3+
> Grunt task to load coverage results and submit them to coverage.appcelerator.com
4+
5+
## Getting Started
6+
This plugin requires Grunt.
7+
8+
If you haven't used [Grunt](http://gruntjs.com/) before, be sure to check out the [Getting Started](http://gruntjs.com/getting-started) guide, as it explains how to create a [Gruntfile](http://gruntjs.com/sample-gruntfile) as well as install and use Grunt plugins. Once you're familiar with that process, you may install this plugin with this command:
9+
10+
```shell
11+
npm install grunt-appc-coverage --save-dev
12+
```
13+
14+
Once the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript:
15+
16+
```js
17+
grunt.loadNpmTasks('grunt-appc-coverage');
18+
```
19+
20+
## The "appc_coverage" task
21+
22+
### Overview
23+
In your project's Gruntfile, add a section named `appc_coverage` to the data object passed into `grunt.initConfig()`.
24+
25+
```js
26+
grunt.initConfig({
27+
appc_coverage: {
28+
default_options: {
29+
src: ['paths', 'to', 'js', 'files'],
30+
force: true
31+
}
32+
}
33+
})
34+
```
35+
36+
## Contributing
37+
Add unit tests for any new or changed functionality. Run npm test to ensure your added code matches existing style standards.
38+
39+
## License
40+
Copyright (c) 2015 Appcelerator. Licensed under the MIT license.

lib/appc-coverage.js

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* grunt-appc-coverage
3+
*
4+
*
5+
* Copyright (c) 2015 Appcelerator
6+
* Licensed under the MIT license.
7+
*/
8+
9+
var request = require('request'),
10+
parse = require('lcov-parse');
11+
12+
const
13+
SUCCESS_UPLOAD = 'Successfully submitted coverage results to coverage.appcelerator.com',
14+
FAILED_UPLOAD = 'Failed to submit coverage results to coverage.appcelerator.com',
15+
SERVER = 'http://localhost:8080/v1';
16+
17+
/**
18+
* Get enviromental options such as service and git
19+
*/
20+
exports.getOptions = function () {
21+
var options = {
22+
service: {
23+
name: 'grunt-appc-coverage',
24+
jobId: null
25+
},
26+
git: {
27+
commit: null,
28+
branch: null,
29+
pullRequest: null,
30+
fullName: null
31+
}
32+
};
33+
34+
if (process.env.TRAVIS) {
35+
// service
36+
options.service.name = 'travis-ci';
37+
options.service.jobId = process.env.TRAVIS_JOB_ID;
38+
// git info
39+
options.git.commit = process.env.TRAVIS_COMMIT;
40+
options.git.branch = process.env.TRAVIS_BRANCH;
41+
options.git.pullRequest = process.env.TRAVIS_PULL_REQUEST;
42+
options.git.fullName = process.env.TRAVIS_REPO_SLUG;
43+
}
44+
45+
return options;
46+
};
47+
48+
/**
49+
* Parse Lcov into something processable
50+
*/
51+
exports.parseLcov = function (lcovString, callback) {
52+
parse(lcovString, function (err, data) {
53+
//process the data here
54+
callback(err, data);
55+
});
56+
};
57+
58+
/**
59+
* Upload the data to the coverage server
60+
*/
61+
exports.upload = function (data, callback) {
62+
request.post({
63+
url : SERVER + '/coverage',
64+
form : {
65+
type : 'grunt-appc-coverage',
66+
data: data
67+
},
68+
json: true
69+
}, function (err, response, body) {
70+
if (response.statusCode === 500 || err || body.hasOwnProperty('error')) {
71+
return callback(FAILED_UPLOAD);
72+
}
73+
74+
return callback(null, SUCCESS_UPLOAD);
75+
});
76+
};

package.json

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
{
2+
"name": "grunt-appc-coverage",
3+
"version": "0.1.0",
4+
"description": "Grunt task to load coverage results and submit them to coverage.appcelerator.com",
5+
"repository": "https://github.com/appcelerator-modules/grunt-appc-coverage",
6+
"author": {
7+
"name": "Muhammad Dadu",
8+
"email": "[email protected]"
9+
},
10+
"bugs": {
11+
"url": "https://github.com/appcelerator-modules/grunt-appc-coverage/issues"
12+
},
13+
"keywords": [
14+
"gruntplugin",
15+
"coverage",
16+
"appcelerator"
17+
],
18+
"main": "Gruntfile.js",
19+
"engines": {
20+
"node": ">= 0.8.0"
21+
},
22+
"license": "MIT",
23+
"dependencies": {
24+
"load-grunt-tasks": "~0.3.0",
25+
"grunt-appc-js": "*",
26+
"grunt-kahvesi": "git://github.com/appcelerator-modules/grunt-kahvesi.git",
27+
"grunt-mocha-test": "^0.12.7",
28+
"mocha": "^2.1.0",
29+
"unit.js": "*",
30+
"request": "~2.53.0",
31+
"lcov-parse": "0.0.9"
32+
},
33+
"devDependencies": {
34+
"grunt-cli": "*",
35+
"grunt-bump": "^0.3.0",
36+
"grunt-contrib-clean": "^0.5.0"
37+
},
38+
"peerDependencies": {
39+
"grunt": "~0.4.5"
40+
},
41+
"scripts": {
42+
"test": "grunt test"
43+
}
44+
}

tasks/appc_coverage.js

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* grunt-appc-coverage
3+
*
4+
*
5+
* Copyright (c) 2015 Appcelerator
6+
* Licensed under the MIT license.
7+
*/
8+
9+
'use strict';
10+
11+
var NOT_FOUND = 'No src files could be found for coverage.appcelerator.com';
12+
13+
var coverage = require('../lib/appc-coverage');
14+
15+
module.exports = function (grunt) {
16+
17+
function appcCoverage() {
18+
// Force task into async mode and grab a handle to the "done" function.
19+
var done = this.async(),
20+
force = this.options().force || this.data.force || false,
21+
filesProcessed = 0;
22+
23+
if (this.filesSrc.length === 0) {
24+
if (force) {
25+
grunt.log.warn(NOT_FOUND);
26+
return done();
27+
}
28+
29+
grunt.log.error(NOT_FOUND);
30+
return done(false);
31+
}
32+
for (var i = 0, max = this.filesSrc.length; i < max; i++) {
33+
processFile(this.filesSrc[i], function (err, status) {
34+
// increment
35+
filesProcessed++;
36+
// check for error
37+
if (err && !force) {
38+
grunt.log.error(err);
39+
return done(false);
40+
} else if (err) {
41+
// warn
42+
grunt.log.warn(err);
43+
} else {
44+
// log status
45+
grunt.log.ok(status);
46+
}
47+
48+
// Check if all the files are processed
49+
if (filesProcessed === max) {
50+
return done();
51+
}
52+
});
53+
}
54+
}
55+
56+
function processFile(coverageFile, callback) {
57+
// Check if file exists
58+
if (!grunt.file.exists(coverageFile)) {
59+
return callback(NOT_FOUND);
60+
}
61+
62+
var objectToUpload = {};
63+
// Get options
64+
objectToUpload.options = coverage.getOptions();
65+
// parse Lcov
66+
coverage.parseLcov( grunt.file.read(coverageFile), function (err, data) {
67+
if (err) {
68+
return callback(err);
69+
}
70+
objectToUpload.coverage = data;
71+
// Upload Data
72+
coverage.upload(objectToUpload, callback);
73+
});
74+
}
75+
76+
grunt.registerMultiTask('appc_coverage', 'Grunt task to load coverage results and submit them to coverage.appcelerator.com', appcCoverage);
77+
78+
};

test/appc_coverage_test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
var test = require('unit.js'),
2+
coverage = require('../lib/appc-coverage.js'),
3+
fs = require('fs');
4+
5+
describe('library', function () {
6+
it('coverage.getOptions();', function () {
7+
var options = coverage.getOptions();
8+
9+
test.object(options)
10+
.hasProperty('service')
11+
.hasProperty('git');
12+
});
13+
14+
it('coverage.parseLcov();', function (done) {
15+
var lcovString = fs.readFileSync('./test/fixtures/lcov.info').toString();
16+
coverage.parseLcov(lcovString, function(err, data) {
17+
test.assert(err == null, err);
18+
test.assert(JSON.stringify(data) === fs.readFileSync('./test/expected/coverage.txt').toString(), 'Files did not match');
19+
done();
20+
});
21+
})
22+
});

test/expected/coverage.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[{"lines":{"found":16,"hit":7,"details":[{"line":9,"hit":1},{"line":15,"hit":1},{"line":16,"hit":1},{"line":29,"hit":1},{"line":31,"hit":0},{"line":32,"hit":0},{"line":34,"hit":0},{"line":35,"hit":0},{"line":36,"hit":0},{"line":37,"hit":0},{"line":40,"hit":1},{"line":46,"hit":1},{"line":47,"hit":0},{"line":49,"hit":0},{"line":56,"hit":1},{"line":57,"hit":0}]},"functions":{"hit":1,"found":4,"details":[{"name":"(anonymous_1)","line":15,"hit":1},{"name":"(anonymous_2)","line":46,"hit":0},{"name":"(anonymous_3)","line":47,"hit":0},{"name":"(anonymous_4)","line":56,"hit":0}]},"branches":{"hit":1,"found":2,"details":[{"line":29,"block":1,"branch":0,"taken":0},{"line":29,"block":1,"branch":1,"taken":1}]},"title":"","file":"/Volumes/Data/Git/DE-290/grunt-appc-coverage/lib/appc-coverage.js"}]

test/fixtures/lcov.info

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
TN:
2+
SF:/Volumes/Data/Git/DE-290/grunt-appc-coverage/lib/appc-coverage.js
3+
FN:15,(anonymous_1)
4+
FN:46,(anonymous_2)
5+
FN:47,(anonymous_3)
6+
FN:56,(anonymous_4)
7+
FNF:4
8+
FNH:1
9+
FNDA:1,(anonymous_1)
10+
FNDA:0,(anonymous_2)
11+
FNDA:0,(anonymous_3)
12+
FNDA:0,(anonymous_4)
13+
DA:9,1
14+
DA:15,1
15+
DA:16,1
16+
DA:29,1
17+
DA:31,0
18+
DA:32,0
19+
DA:34,0
20+
DA:35,0
21+
DA:36,0
22+
DA:37,0
23+
DA:40,1
24+
DA:46,1
25+
DA:47,0
26+
DA:49,0
27+
DA:56,1
28+
DA:57,0
29+
LF:16
30+
LH:7
31+
BRDA:29,1,0,0
32+
BRDA:29,1,1,1
33+
BRF:2
34+
BRH:1
35+
end_of_record

0 commit comments

Comments
 (0)