Skip to content

Commit

Permalink
Merge branch 'master' into add-documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
vkoves committed Feb 25, 2018
2 parents 5fb4730 + dd9a5fa commit 7eba619
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 26 deletions.
8 changes: 5 additions & 3 deletions lib/output-files.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ const mkdirp = require('mkdirp')
const clone = require('clone')
const pathLib = require('path')

const storagePath = './.nyc_output/js'

class OutputFiles {
constructor (coverageInfo) {
// Clone coverageInfo to prevent mutating the passed in data
Expand All @@ -27,12 +29,12 @@ class OutputFiles {

// Special case: when html present, strip and return specialized string
if (truncatedPath.includes('.html')) {
truncatedPath = './coverage/js/puppeteerTemp-inline'
truncatedPath = pathLib.resolve(storagePath, truncatedPath) + 'puppeteerTemp-inline'
} else {
truncatedPath = truncatedPath.split('.js')[0]
truncatedPath = './coverage/js/' + truncatedPath
truncatedPath = pathLib.resolve(storagePath, truncatedPath)
}
mkdirp.sync('./coverage/js')
mkdirp.sync(storagePath)
if (fs.existsSync(truncatedPath + '.js')) {
this.iterator++
str = `${truncatedPath}-${this.iterator}.js`
Expand Down
41 changes: 41 additions & 0 deletions lib/puppeteer-to-istanbul.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const fs = require('fs')
const v8toIstanbul = require('v8-to-istanbul')
const OutputFiles = require('./output-files')
const PuppeteerToV8 = require('./puppeteer-to-v8')

class PuppeteerToIstanbul {
constructor (coverageInfo) {
this.coverageInfo = coverageInfo
this.puppeteerToConverter = OutputFiles(coverageInfo).getTransformedCoverage()
this.puppeteerToV8Info = PuppeteerToV8(this.puppeteerToConverter).convertCoverage()
}

setCoverageInfo (coverageInfo) {
this.coverageInfo = coverageInfo
}

writeIstanbulFormat () {
// TODO: We should iterate through each file based on the coverage info, and then convert to Istanbul here
// console.log(this.puppeteerToV8Info)

var fullJson = {}

this.puppeteerToV8Info.forEach(jsFile => {
const script = v8toIstanbul(jsFile.url)
script.applyCoverage(jsFile.functions)

let istanbulCoverage = script.toIstanbul()
let keys = Object.keys(istanbulCoverage)

fullJson[keys[0]] = istanbulCoverage[keys[0]]
})

// const script = v8toIstanbul(this.puppeteerToV8Info[0].url)
// script.applyCoverage(this.puppeteerToV8Info[0].functions)
fs.writeFileSync('./.nyc_output/out.json', JSON.stringify(fullJson), 'utf8')
}
}

module.exports = function (coverageInfo) {
return new PuppeteerToIstanbul(coverageInfo)
}
7 changes: 5 additions & 2 deletions lib/puppeteer-to-v8.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@ class PuppeteerToV8 {
return this.coverageInfo.map(coverageItem => {
return {
scriptId: id++,
url: coverageItem.url,
functions: [{ ranges: coverageItem.ranges.map(this.convertRange) }]
url: 'file://' + coverageItem.url,
functions: [{
ranges: coverageItem.ranges.map(this.convertRange),
isBlockCoverage: true
}]
}
})
}
Expand Down
7 changes: 7 additions & 0 deletions run.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const args = require('yargs').demandCommand(1).argv
const coverageInfo = require(args._[0])

const PuppeteerToIstanbul = require('./lib/puppeteer-to-istanbul')

const pti = PuppeteerToIstanbul(coverageInfo)
pti.writeIstanbulFormat()
30 changes: 15 additions & 15 deletions test/fixtures/function-coverage-full-duplicate.json
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
[
{
"url": "file:///C:/istanbul-puppeteer/puppeteer-to-istanbul/test/sample_js/function-coverage-100.js",
"url": "file:////Users/benjamincoe/bcoe/puppeteer-to-istanbul/test/sample_js/function-coverage-100.js",
"ranges": [
{
"start": 0,
"end": 47
"end": 46
},
{
"start": 49,
"end": 90
"start": 47,
"end": 85
},
{
"start": 92,
"end": 112
"start": 86,
"end": 102
}
],
"text": "function a (num1, num2) {\r\n return num1 + num2\r\n}\r\n\r\nfunction b (num) {\r\n return num + 1\r\n}\r\n\r\na(1, 2)\r\nb(3)\r\n"
"text": "function a (num1, num2) {\n return num1 + num2\n}\n\nfunction b (num) {\n return num + 1\n}\n\na(1, 2)\nb(3)\n"
},
{
"url": "file:///C:/istanbul-puppeteer/puppeteer-to-istanbul/test/sample_js/function-coverage-100.js",
"url": "file:////Users/benjamincoe/bcoe/puppeteer-to-istanbul/test/sample_js/function-coverage-100.js",
"ranges": [
{
"start": 0,
"end": 47
"end": 46
},
{
"start": 49,
"end": 90
"start": 47,
"end": 85
},
{
"start": 92,
"end": 112
"start": 86,
"end": 102
}
],
"text": "function a (num1, num2) {\r\n return num1 + num2\r\n}\r\n\r\nfunction b (num) {\r\n return num + 1\r\n}\r\n\r\na(1, 2)\r\nb(3)\r\n"
"text": "function a (num1, num2) {\n return num1 + num2\n}\n\nfunction b (num) {\n return num + 1\n}\n\na(1, 2)\nb(3)\n"
}
]
]
14 changes: 8 additions & 6 deletions test/output-files.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
/* globals describe, it, beforeEach, after */
/* globals describe, it, beforeEach */

const OutputFiles = require('../lib/output-files')
const rimraf = require('rimraf')
const pathLib = require('path')

const storagePath = '.nyc_output/js'
const storagePathTop = '.nyc_output'

require('chai').should()

Expand All @@ -15,7 +19,7 @@ describe('output-files', () => {
// Since block-else-not-covered was generated by the above line, this
// should make a new file with -1 appended to the name
var newPath = outputFiles.rewritePath('./sample_js/block-else-not-covered-1.js')
newPath.should.eql('./coverage/js/block-else-not-covered-1.js')
newPath.should.include(storagePath + '/block-else-not-covered-1.js')
})

it('handle multiple files with same name, and replace in json', () => {
Expand Down Expand Up @@ -55,17 +59,15 @@ describe('output-files', () => {
coverageInfo[1].url.should.include('puppeteerTemp-inline-1.js')
})

after(cleanupCoverage)

function cleanupCoverage () {
rimraf.sync('./coverage')
rimraf.sync(storagePathTop)
}

// Takes in a script and rewrites it to the path we expect in /coverage/js
function movedUrl (url) {
let splitUrl = url.split('/')

// Prepend the folder to the filename
return './coverage/js/' + splitUrl[splitUrl.length - 1]
return pathLib.resolve(storagePath, splitUrl[splitUrl.length - 1])
}
})
19 changes: 19 additions & 0 deletions test/puppeteer-to-istanbul.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// /* globals describe, it, before */

// var PuppeteerToIstanbul = require('../lib/puppeteer-to-istanbul')()

// require('chai').should()

// describe('puppeteer-to-istanbul', () => {

// const fixture = require('./fixtures/function-coverage-missing.json')

// before(() => {

// })

// it('translates ranges into lines for istanbul', () => {

// })

// })

0 comments on commit 7eba619

Please sign in to comment.