From d065b4602578ccc797bbcb09c1a1d3fc70916d31 Mon Sep 17 00:00:00 2001 From: Gabriel Csapo Date: Wed, 13 Sep 2017 19:51:52 -0700 Subject: [PATCH] fixes tests and updates readme and docs --- README.md | 22 +-- bin/lcov-server-cli.js | 8 +- docs/code/Coverage.html | 4 +- docs/code/coverage.js.html | 4 +- docs/code/git.js.html | 4 +- docs/code/index.html | 8 +- docs/code/lcov.js.html | 10 +- docs/code/module-util_git.html | 156 +---------------- docs/code/module-util_lcov.html | 90 ++++++++++ lib/lcov.js | 214 +++++++++++------------ package.json | 3 +- test/fixtures/sample-module/package.json | 5 +- test/index.js | 92 +++++----- test/util/git.js | 34 ++-- test/util/lcov.js | 47 ++--- 15 files changed, 321 insertions(+), 380 deletions(-) create mode 100644 docs/code/module-util_lcov.html diff --git a/README.md b/README.md index 1b7f1de..787773d 100644 --- a/README.md +++ b/README.md @@ -36,13 +36,14 @@ tap test --coverage-report=text-lcov | lcov-server-cli > cli:help ``` -Usage: lcov-server-cli [options] + Usage: lcov-server-cli [options] -Options: - -h, --help output usage information - -V, --version output the version number - -u, --url [db] Set the url to upload lcov data too + Options: + + -V, --version output the version number + -u, --url [server] Set the url to upload lcov data too + -h, --help output usage information ``` > server @@ -54,11 +55,12 @@ lcov-server > server:help ``` -Usage: lcov-server [options] + Usage: lcov-server [options] + -Options: + Options: - -h, --help output usage information - -V, --version output the version number - -d, --db Set the db connection + -V, --version output the version number + -d, --db [db] Set the db connection + -h, --help output usage information ``` diff --git a/bin/lcov-server-cli.js b/bin/lcov-server-cli.js index f536212..dd499f0 100755 --- a/bin/lcov-server-cli.js +++ b/bin/lcov-server-cli.js @@ -18,7 +18,7 @@ updateNotifier({pkg}).notify(); program .version(pkg.version) - .option('-u, --url [db]', 'Set the url to upload lcov data too', 'http://localhost:8080') + .option('-u, --url [server]', 'Set the url to upload lcov data too', 'http://localhost:8080') .parse(process.argv); const parsedUrl = url.parse(program.url); @@ -96,7 +96,11 @@ process.stdin.on('end', () => { }) .catch((err) => { console.log(err); // eslint-disable-line + process.exit(1); }); }) - .catch((err) => { throw err; } ); + .catch((err) => { + console.log(`could not parse lcov report correctly: ${err}`); // eslint-disable-line + process.exit(1); + }); }); diff --git a/docs/code/Coverage.html b/docs/code/Coverage.html index 7817340..e0e99e1 100644 --- a/docs/code/Coverage.html +++ b/docs/code/Coverage.html @@ -22,7 +22,7 @@
@@ -1582,7 +1582,7 @@
Returns:

- Documentation generated by JSDoc 3.5.4 on Tue Sep 12 2017 23:59:34 GMT-0700 (PDT) using the docdash theme. + Documentation generated by JSDoc 3.5.4 on Wed Sep 13 2017 18:27:42 GMT-0700 (PDT) using the docdash theme.
diff --git a/docs/code/coverage.js.html b/docs/code/coverage.js.html index 494d8d3..ee55203 100644 --- a/docs/code/coverage.js.html +++ b/docs/code/coverage.js.html @@ -22,7 +22,7 @@
@@ -199,7 +199,7 @@

coverage.js


- Documentation generated by JSDoc 3.5.4 on Tue Sep 12 2017 23:59:34 GMT-0700 (PDT) using the docdash theme. + Documentation generated by JSDoc 3.5.4 on Wed Sep 13 2017 18:27:42 GMT-0700 (PDT) using the docdash theme.
diff --git a/docs/code/git.js.html b/docs/code/git.js.html index da94e7b..037d0c3 100644 --- a/docs/code/git.js.html +++ b/docs/code/git.js.html @@ -22,7 +22,7 @@
@@ -139,7 +139,7 @@

git.js


- Documentation generated by JSDoc 3.5.4 on Tue Sep 12 2017 23:59:34 GMT-0700 (PDT) using the docdash theme. + Documentation generated by JSDoc 3.5.4 on Wed Sep 13 2017 18:27:42 GMT-0700 (PDT) using the docdash theme.
diff --git a/docs/code/index.html b/docs/code/index.html index be5bd0d..6693ec7 100644 --- a/docs/code/index.html +++ b/docs/code/index.html @@ -22,7 +22,7 @@
@@ -57,7 +57,6 @@

Home

Classes

  • npm npm

    What is this?

    It is a lcov server! It stores lcov reports and categorizes them based on their origin repo.

    -

    main screenshot

    Prerequisites

    • mongodb installed
    • nodejs installed
    • @@ -77,6 +76,9 @@

      Install

      npm install lcov-server -g
         -u, --url [db]  Set the url to upload lcov data too

      server

      +
      lcov-server
      +

      server:help

      +
      Usage: lcov-server [options]
       
       Options:
      @@ -96,7 +98,7 @@ 

      Install

      npm install lcov-server -g
       
      - Documentation generated by JSDoc 3.5.4 on Tue Sep 12 2017 23:59:34 GMT-0700 (PDT) using the docdash theme. + Documentation generated by JSDoc 3.5.4 on Wed Sep 13 2017 18:27:42 GMT-0700 (PDT) using the docdash theme.
      diff --git a/docs/code/lcov.js.html b/docs/code/lcov.js.html index 875f5ee..ac91085 100644 --- a/docs/code/lcov.js.html +++ b/docs/code/lcov.js.html @@ -22,7 +22,7 @@
      @@ -40,15 +40,15 @@

      lcov.js

      'use strict';
       
       /**
      - * @module util/git
      + * @module util/lcov
        */
       
       const fs = require('fs');
       
       const lcov = {
         walkFile: function(str, cb) {
      -      let data = [],
      -          item;
      +      let data = [];
      +      let item;
       
             ['end_of_record'].concat(str.split('\n')).forEach((line) => {
                 const allparts = line.trim().split(':');
      @@ -179,7 +179,7 @@ 

      lcov.js


      - Documentation generated by JSDoc 3.5.4 on Tue Sep 12 2017 23:59:34 GMT-0700 (PDT) using the docdash theme. + Documentation generated by JSDoc 3.5.4 on Wed Sep 13 2017 18:27:42 GMT-0700 (PDT) using the docdash theme.
      diff --git a/docs/code/module-util_git.html b/docs/code/module-util_git.html index 6805740..892f761 100644 --- a/docs/code/module-util_git.html +++ b/docs/code/module-util_git.html @@ -22,7 +22,7 @@
      @@ -106,158 +106,6 @@

      (inner) parse - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      Returns:
      - - -
      -
        -
      • returns a promise that resolves with the git information needed
      • -
      -
      - - - -
      -
      - Type -
      -
      - -Promise - - -
      -
      - - - - - - - - - - - - - - - - - - - - -
      - -
      - - - -
      - -
      -
      - - - - - -
      - - - - - - - - - - - - - - -

      Methods

      - - - - - - -

      (inner) parse() → {Promise}

      - - - - - - -
      - - -
      Source:
      -
      - - - - - - - - - - - - - - - - - - - - @@ -341,7 +189,7 @@
      Returns:

      - Documentation generated by JSDoc 3.5.4 on Tue Sep 12 2017 23:59:34 GMT-0700 (PDT) using the docdash theme. + Documentation generated by JSDoc 3.5.4 on Wed Sep 13 2017 18:27:42 GMT-0700 (PDT) using the docdash theme.
      diff --git a/docs/code/module-util_lcov.html b/docs/code/module-util_lcov.html new file mode 100644 index 0000000..c3a4eed --- /dev/null +++ b/docs/code/module-util_lcov.html @@ -0,0 +1,90 @@ + + + + + util/lcov - Documentation + + + + + + + + + + + + + + + + +
      + +

      util/lcov

      + + + + + + + +
      + +
      + + + +
      + +
      +
      + + + + + +
      + + + + + + + + + + + + + + + + + + +
      + +
      + + + + +
      + +
      + +
      + Documentation generated by JSDoc 3.5.4 on Wed Sep 13 2017 18:27:42 GMT-0700 (PDT) using the docdash theme. +
      + + + + + \ No newline at end of file diff --git a/lib/lcov.js b/lib/lcov.js index 71d7c10..d6eeefc 100644 --- a/lib/lcov.js +++ b/lib/lcov.js @@ -1,128 +1,128 @@ 'use strict'; /** - * @module util/git + * @module util/lcov */ const fs = require('fs'); const lcov = { walkFile: function(str, cb) { - let data = [], - item; + let data = []; + let item; - ['end_of_record'].concat(str.split('\n')).forEach((line) => { - const allparts = line.trim().split(':'); - const parts = [allparts.shift(), allparts.join(':')]; - let lines, fn; + ['end_of_record'].concat(str.split('\n')).forEach((line) => { + const allparts = line.trim().split(':'); + const parts = [allparts.shift(), allparts.join(':')]; + let lines, fn; - switch (parts[0].toUpperCase()) { - case 'TN': - item.title = parts[1].trim(); - break; - case 'SF': - item.file = parts.slice(1).join(':').trim(); - break; - case 'FNF': - item.functions.found = Number(parts[1].trim()); - break; - case 'FNH': - item.functions.hit = Number(parts[1].trim()); - break; - case 'LF': - item.lines.found = Number(parts[1].trim()); - break; - case 'LH': - item.lines.hit = Number(parts[1].trim()); - break; - case 'DA': - lines = parts[1].split(','); - item.lines.details.push({ - line: Number(lines[0]), - hit: Number(lines[1]) - }); - break; - case 'FN': - fn = parts[1].split(','); - item.functions.details.push({ - name: fn[1], - line: Number(fn[0]) - }); - break; - case 'FNDA': - fn = parts[1].split(','); - item.functions.details.some((i, k) => { - if (i.name === fn[1] && i.hit === undefined) { - item.functions.details[k].hit = Number(fn[0]); - return true; - } - }); - break; - case 'BRDA': - fn = parts[1].split(','); - item.branches.details.push({ - line: Number(fn[0]), - block: Number(fn[1]), - branch: Number(fn[2]), - taken: ((fn[3] === '-') ? 0 : Number(fn[3])) - }); - break; - case 'BRF': - item.branches.found = Number(parts[1]); - break; - case 'BRH': - item.branches.hit = Number(parts[1]); - break; - } + switch (parts[0].toUpperCase()) { + case 'TN': + item.title = parts[1].trim(); + break; + case 'SF': + item.file = parts.slice(1).join(':').trim(); + break; + case 'FNF': + item.functions.found = Number(parts[1].trim()); + break; + case 'FNH': + item.functions.hit = Number(parts[1].trim()); + break; + case 'LF': + item.lines.found = Number(parts[1].trim()); + break; + case 'LH': + item.lines.hit = Number(parts[1].trim()); + break; + case 'DA': + lines = parts[1].split(','); + item.lines.details.push({ + line: Number(lines[0]), + hit: Number(lines[1]) + }); + break; + case 'FN': + fn = parts[1].split(','); + item.functions.details.push({ + name: fn[1], + line: Number(fn[0]) + }); + break; + case 'FNDA': + fn = parts[1].split(','); + item.functions.details.some((i, k) => { + if (i.name === fn[1] && i.hit === undefined) { + item.functions.details[k].hit = Number(fn[0]); + return true; + } + }); + break; + case 'BRDA': + fn = parts[1].split(','); + item.branches.details.push({ + line: Number(fn[0]), + block: Number(fn[1]), + branch: Number(fn[2]), + taken: ((fn[3] === '-') ? 0 : Number(fn[3])) + }); + break; + case 'BRF': + item.branches.found = Number(parts[1]); + break; + case 'BRH': + item.branches.hit = Number(parts[1]); + break; + } - if (line.indexOf('end_of_record') > -1) { - data.push(item); - item = { - lines: { - found: 0, - hit: 0, - details: [] - }, - functions: { - hit: 0, - found: 0, - details: [] - }, - branches: { - hit: 0, - found: 0, - details: [] - } - }; + if (line.indexOf('end_of_record') > -1) { + data.push(item); + item = { + lines: { + found: 0, + hit: 0, + details: [] + }, + functions: { + hit: 0, + found: 0, + details: [] + }, + branches: { + hit: 0, + found: 0, + details: [] } - }); + }; + } + }); - data.shift(); + data.shift(); - if (data.length) { - cb(undefined, data); - } else { - cb('Failed to parse string'); - } + if (data.length) { + cb(undefined, data); + } else { + cb('Failed to parse string'); + } }, parse: (file) => { - return new Promise(function(resolve, reject) { - if(fs.existsSync(file)) { - fs.readFile(file, 'utf8', (err, str) => { - if(err) { - reject(err); - } - return lcov.walkFile(str, function(err, result) { - if(err) return reject(err); - resolve(result); - }); - }); - } else { - return lcov.walkFile(file, function(err, result) { - if(err) return reject(err); - resolve(result); - }); + return new Promise(function(resolve, reject) { + if (fs.existsSync(file)) { + fs.readFile(file, 'utf8', (err, str) => { + if (err) { + reject(err); } + return lcov.walkFile(str, function(err, result) { + if (err) return reject(err); + resolve(result); + }); + }); + } else { + return lcov.walkFile(file, function(err, result) { + if (err) return reject(err); + resolve(result); + }); + } }); } }; diff --git a/package.json b/package.json index 06a76f8..d9f0473 100644 --- a/package.json +++ b/package.json @@ -17,7 +17,7 @@ "scripts": { "lint": "eslint .", "test": "tape test/util/**/*.js test/index.js", - "coverage": "tap test/**/*.js --coverage --coverage-report=lcov", + "coverage": "tap test/util/**.js test/index.js --coverage --coverage-report=lcov", "build": "webpack --progress", "start": "./bin/lcov-server.js", "dev": "NODE_ENV=development webpack-dev-server --hot --port 5000", @@ -52,7 +52,6 @@ "react-router": "^4.2.0", "react-router-dom": "^4.2.2", "shelljs": "^0.7.8", - "storybook": "0.0.1", "style-loader": "^0.18.2", "tap": "^10.7.2", "tape": "^4.8.0", diff --git a/test/fixtures/sample-module/package.json b/test/fixtures/sample-module/package.json index 58103bf..0026581 100644 --- a/test/fixtures/sample-module/package.json +++ b/test/fixtures/sample-module/package.json @@ -18,8 +18,7 @@ }, "homepage": "https://github.com/gabrielcsapo/sample-module#readme", "devDependencies": { - "shelljs": "^0.7.7", - "tap": "^10.3.2", - "tape": "^4.6.3" + "tap": "^10.7.2", + "tape": "^4.8.0" } } diff --git a/test/index.js b/test/index.js index b48654f..925e73d 100644 --- a/test/index.js +++ b/test/index.js @@ -1,54 +1,52 @@ const test = require('tape'); const path = require('path'); -const http = require('http'); +const express = require('express'); const shell = require('shelljs'); -test('node-coverage-cli', (t) => { - t.plan(1); - - t.test('successfully send coverage to server', (t) => { - const port = 8080; - const wdir = process.cwd(); - const server = http.createServer((request, response) => { - let data = ''; - - request.on('data', function(d) { - data += d; - }); - - request.on('end', function() { - const output = JSON.parse(data); - t.deepEqual(Object.keys(output), ['service_job_id', 'service_pull_request', 'service_name', 'source_files', 'git', 'run_at']); - t.equal(Array.isArray(output['source_files']), true); - t.equal(typeof output['git'], 'object'); - t.deepEqual(Object.keys(output['git']), ['commit', 'author_name', 'author_email', 'author_date', 'committer_name', 'committer_email', 'committer_date', 'message', 'branch', 'remotes']); - - shell.exec('rm -rf .git'); - - response.end(); - server.close(); - process.chdir(wdir); - t.end(); - }); - }).listen(port, (err) => { - if (err) { - t.fail(err); - } - console.log(`node-coverage-server is listening on http://localhost:${port}`); // eslint-disable-line - - process.chdir(path.resolve(__dirname, 'fixtures', 'sample-module')); - - shell.exec('rm -rf .git'); - shell.exec('git init'); - shell.exec('git add -A'); - shell.exec('git commit -m "Initial Commit"'); - shell.exec('git remote add origin http://github.com/gabrielcsapo/sample-module'); - - shell.exec(`./node_modules/.bin/tap test/**.js --coverage --coverage-report=text-lcov | ../../../bin/lcov-server-cli.js --url http://localhost:${port}`, { - async: true - }); - }); +const { parseBody } = require('../lib/util'); + +test('lcov-server-cli', (t) => { + t.plan(1); + + t.test('successfully send coverage to server', (t) => { + const port = 8080; + const wdir = process.cwd(); + const app = express(); + + app.post('/api/v1/upload', parseBody, (req, res) => { + t.deepEqual(Object.keys(req.body), ['service_job_id', 'service_pull_request', 'service_name', 'source_files', 'git', 'run_at']); + t.equal(Array.isArray(req.body['source_files']), true); + t.equal(typeof req.body['git'], 'object'); + t.deepEqual(Object.keys(req.body['git']), ['commit', 'author_name', 'author_email', 'author_date', 'committer_name', 'committer_email', 'committer_date', 'message', 'branch', 'remotes']); + + res.status(200).end(); + }); + + + const server = app.listen(port, (err) => { + if (err) { + t.fail(err); + return; + } + console.log(`node-coverage-server is listening on http://localhost:${port}`); // eslint-disable-line + + process.chdir(path.resolve(__dirname, 'fixtures', 'sample-module')); + + shell.exec('npm install'); + shell.exec('rm -rf .git'); + shell.exec('git init'); + shell.exec('git add -A'); + shell.exec('git commit -m "Initial Commit"'); + shell.exec('git remote add origin http://github.com/gabrielcsapo/sample-module'); + + shell.exec(`./node_modules/.bin/tap test/**.js --coverage --coverage-report=text-lcov | ../../../bin/lcov-server-cli.js --url http://localhost:${port}`, () => { + shell.exec('rm -rf .git'); + server.close(); + process.chdir(wdir); + + t.end(); + }); }); + }); - t.end(); }); diff --git a/test/util/git.js b/test/util/git.js index ec83f5c..7ec8be5 100644 --- a/test/util/git.js +++ b/test/util/git.js @@ -38,26 +38,22 @@ test('git', (t) => { }); t.test('should fail when no remote is present', (t) => { - // make sure the tests are finished before this one is run - setTimeout(() => { - const root = process.cwd(); - process.chdir(path.resolve(__dirname, '..', 'fixtures', 'sample-module')); + const root = process.cwd(); + process.chdir(path.resolve(__dirname, '..', 'fixtures', 'sample-module')); - shell.exec('git init'); - shell.exec('git add -A'); - shell.exec('git commit -m "testtest"'); - git.parse() - .then(() => { - t.fail('should fail, but doesn\'t'); - process.chdir(root); - }) - .catch((err) => { - t.equal(err, 'no remote found'); - t.end(); - process.chdir(root); - }); - }, 3000); + shell.exec('git init'); + shell.exec('git add -A'); + shell.exec('git commit -m "testtest"'); + git.parse() + .then(() => { + t.fail('should fail, but doesn\'t'); + process.chdir(root); + }) + .catch((err) => { + t.equal(err, 'no remote found'); + t.end(); + process.chdir(root); + }); }); - t.end(); }); diff --git a/test/util/lcov.js b/test/util/lcov.js index 9f7c92b..7b0b8ea 100644 --- a/test/util/lcov.js +++ b/test/util/lcov.js @@ -16,38 +16,40 @@ test('lcov', function(t) { t.ok(data !== undefined); t.fail('should not return error'); t.end(); - }).catch(function(err) { + }) + .catch(function(err) { t.ok(typeof err !== 'undefined', 'error is not undefined'); t.end(); }); }); t.test('should be able to parse string', function(t) { - var data = 'TN:TestName\nSF:foobar.js\nend_of_record\n'; - var expected = [{ - lines: { - found: 0, - hit: 0, - details: [] - }, - functions: { - hit: 0, - found: 0, - details: [] - }, - branches: { - hit: 0, - found: 0, - details: [] - }, - title: 'TestName', - file: 'foobar.js' + const data = 'TN:TestName\nSF:foobar.js\nend_of_record\n'; + const expected = [{ + lines: { + found: 0, + hit: 0, + details: [] + }, + functions: { + hit: 0, + found: 0, + details: [] + }, + branches: { + hit: 0, + found: 0, + details: [] + }, + title: 'TestName', + file: 'foobar.js' }]; lcov.parse(data) .then(function(data) { t.deepEqual(data, expected, 'data is correctly formed'); t.end(); - }).catch(function(err) { + }) + .catch(function(err) { t.ok(typeof err !== 'undefined', 'error is not undefined'); t.fail('should not return error'); t.end(); @@ -61,7 +63,8 @@ test('lcov', function(t) { .then(function(data) { t.deepEqual(data, expected, 'data is correctly formed'); t.end(); - }).catch(function(err) { + }) + .catch(function(err) { t.ok(typeof err !== 'undefined', 'error is not undefined'); t.fail('should not return error'); t.end();