-
-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Implemented JS local copying and renaming for conversion to v8 (#6
- Loading branch information
Showing
10 changed files
with
262 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,61 @@ | ||
// output JavaScript bundled in puppeteer output to format | ||
// that can be eaten by Istanbul. | ||
|
||
// TODO: Put function interfaces on this file | ||
|
||
const fs = require('fs') | ||
const mkdirp = require('mkdirp') | ||
const clone = require('clone') | ||
const pathLib = require('path') | ||
|
||
class OutputFiles { | ||
constructor (coverageInfo) { | ||
this.coverageInfo = coverageInfo | ||
// Clone coverageInfo to prevent mutating the passed in data | ||
this.coverageInfo = clone(coverageInfo) | ||
this.iterator = 0 | ||
this._parseAndIsolate() | ||
} | ||
|
||
rewritePath (path) { | ||
// generate a new path relative to ./coverage/js. | ||
// this would be around where you'd use mkdirp. | ||
return `${path}-hey!` | ||
|
||
var str = `` | ||
|
||
// Get the last element in the path name | ||
var truncatedPath = pathLib.basename(path) | ||
|
||
// Special case: when html present, strip and return specialized string | ||
if (truncatedPath.includes('.html')) { | ||
truncatedPath = './coverage/js/puppeteerTemp-inline' | ||
} else { | ||
truncatedPath = truncatedPath.split('.js')[0] | ||
truncatedPath = './coverage/js/' + truncatedPath | ||
} | ||
mkdirp.sync('./coverage/js') | ||
if (fs.existsSync(truncatedPath + '.js')) { | ||
this.iterator++ | ||
str = `${truncatedPath}-${this.iterator}.js` | ||
return str | ||
} else { | ||
str = `${truncatedPath}.js` | ||
return str | ||
} | ||
} | ||
|
||
_parseAndIsolate () { | ||
for (var i = 0; i < this.coverageInfo.length; i++) { | ||
var path = this.rewritePath(this.coverageInfo[i].url) | ||
this.coverageInfo[i].url = path | ||
fs.writeFileSync(path, this.coverageInfo[i].text) | ||
} | ||
} | ||
output () { | ||
|
||
getTransformedCoverage () { | ||
return this.coverageInfo | ||
} | ||
} | ||
|
||
module.exports = function (converageInfo) { | ||
return new OutputFiles(converageInfo) | ||
module.exports = function (coverageInfo) { | ||
return new OutputFiles(coverageInfo) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
[ | ||
{ | ||
"url": "file:///C:/istanbul-puppeteer/puppeteer-to-istanbul/test/sample_js/function-coverage-100.js", | ||
"ranges": [ | ||
{ | ||
"start": 0, | ||
"end": 47 | ||
}, | ||
{ | ||
"start": 49, | ||
"end": 90 | ||
}, | ||
{ | ||
"start": 92, | ||
"end": 112 | ||
} | ||
], | ||
"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" | ||
}, | ||
{ | ||
"url": "file:///C:/istanbul-puppeteer/puppeteer-to-istanbul/test/sample_js/function-coverage-100.js", | ||
"ranges": [ | ||
{ | ||
"start": 0, | ||
"end": 47 | ||
}, | ||
{ | ||
"start": 49, | ||
"end": 90 | ||
}, | ||
{ | ||
"start": 92, | ||
"end": 112 | ||
} | ||
], | ||
"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" | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
[ | ||
{ | ||
"url": "file:////home/viktor/Documents/HackIllinois2018/puppeteer-to-istanbul/test/sample_js/block-else-not-covered.js", | ||
"ranges": [ | ||
{ | ||
"start": 0, | ||
"end": 44 | ||
} | ||
], | ||
"text": "if (true) {\n console.info('hello world!')\n} else {\n function fib () {\n return 'i am the fibonacci sequence'\n }\n}" | ||
}, | ||
{ | ||
"url": "file:///tmp/puppeteerTemp.html", | ||
"ranges": [ | ||
{ | ||
"start": 0, | ||
"end": 55 | ||
}, | ||
{ | ||
"start": 60, | ||
"end": 66 | ||
}, | ||
{ | ||
"start": 109, | ||
"end": 126 | ||
} | ||
], | ||
"text": "\n function c(num1, num2) {\n return num2 * num1;\n }\n function d(num3) {\n return num3;\n }\n c(4,3);\n " | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
[ | ||
{ | ||
"url": "file:///tmp/puppeteerTemp.html", | ||
"ranges": [ | ||
{ | ||
"start": 0, | ||
"end": 59 | ||
}, | ||
{ | ||
"start": 66, | ||
"end": 74 | ||
}, | ||
{ | ||
"start": 121, | ||
"end": 140 | ||
} | ||
], | ||
"text": "\n function c(num1, num2) {\n return num2 * num1;\n }\n function d(num3) {\n return num3;\n }\n c(4,3);\n " | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
[ | ||
{ | ||
"url": "file:///tmp/puppeteerTemp.html", | ||
"ranges": [ | ||
{ | ||
"start": 0, | ||
"end": 55 | ||
}, | ||
{ | ||
"start": 60, | ||
"end": 66 | ||
}, | ||
{ | ||
"start": 109, | ||
"end": 126 | ||
} | ||
], | ||
"text": "\n function c(num1, num2) {\n return num2 * num1;\n }\n function d(num3) {\n return num3;\n }\n c(4,3);\n " | ||
}, | ||
{ | ||
"url": "file:///tmp/puppeteerTemp.html", | ||
"ranges": [ | ||
{ | ||
"start": 0, | ||
"end": 68 | ||
}, | ||
{ | ||
"start": 73, | ||
"end": 93 | ||
} | ||
], | ||
"text": "\n function e(num4, num5, num6) {\n return num4 * num5 - num6;\n }\n e(3,5,6);\n " | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,71 @@ | ||
/* globals describe, it */ | ||
/* globals describe, it, beforeEach, after */ | ||
|
||
const OutputFiles = require('../lib/output-files') | ||
const rimraf = require('rimraf') | ||
|
||
require('chai').should() | ||
|
||
describe('puppeteer-to-v8', () => { | ||
describe('filename generation', () => { | ||
// we can use path.basename(path[, ext]). | ||
it('exposes a helper for generating a filename', () => { | ||
const outputFiles = OutputFiles() // you would pass the JSON blob in here. | ||
console.info(outputFiles.rewritePath('./foo/bar/hey.txt')) | ||
}) | ||
describe('output-files', () => { | ||
beforeEach(cleanupCoverage) | ||
|
||
it('appropriately handles colliding names', () => { | ||
// we can use path.basename(path[, ext]). | ||
it('exposes a handler that appropriately handles colliding names', () => { | ||
const outputFiles = OutputFiles(require('./fixtures/block-else-not-covered.json')) | ||
|
||
}) | ||
// 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') | ||
}) | ||
|
||
it('handle multiple files with same name, and replace in json', () => { | ||
// Input from the fixture should be JSONified already | ||
const fixture = require('./fixtures/function-coverage-full-duplicate.json') | ||
const coverageInfo = OutputFiles(fixture).getTransformedCoverage() | ||
|
||
// Fixture should and output coverage should not be in the same place | ||
coverageInfo[0].url.should.not.eql(fixture[0].url) | ||
coverageInfo[1].url.should.not.eql(fixture[1].url) | ||
|
||
coverageInfo[0].url.should.eql(movedUrl(fixture[0].url)) | ||
coverageInfo[1].url.should.eql(movedUrl(fixture[0].url.replace('.js', '-1.js'))) | ||
}) | ||
|
||
// call it something like indexHTML-inline-1.js | ||
it('appropriately handles only inline JavaScript', () => { | ||
const fixture = require('./fixtures/inline-script-coverage.json') | ||
const coverageInfo = OutputFiles(fixture).getTransformedCoverage() | ||
|
||
coverageInfo[0].url.should.include('puppeteerTemp-inline.js') | ||
}) | ||
|
||
// call it something like indexHTML-inline-1.js | ||
it('appropriately handles inline JavaScript', () => { | ||
it('appropriately handles inline and external JavaScript', () => { | ||
const fixture = require('./fixtures/inline-and-external-script-coverage.json') | ||
const coverageInfo = OutputFiles(fixture).getTransformedCoverage() | ||
|
||
}) | ||
coverageInfo[0].url.should.eql(movedUrl(fixture[0].url)) | ||
coverageInfo[1].url.should.include('puppeteerTemp-inline.js') | ||
}) | ||
|
||
it('appropriately handles two cases of inline JavaScript', () => { | ||
const fixture = require('./fixtures/two-inline.json') | ||
const coverageInfo = OutputFiles(fixture).getTransformedCoverage() | ||
|
||
coverageInfo[0].url.should.include('puppeteerTemp-inline.js') | ||
coverageInfo[1].url.should.include('puppeteerTemp-inline-1.js') | ||
}) | ||
|
||
after(cleanupCoverage) | ||
|
||
function cleanupCoverage () { | ||
rimraf.sync('./coverage') | ||
} | ||
|
||
// 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] | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/* | ||
This was included as a <script> tag to generate | ||
inline-script-coverage.json and inline-and-external-script-coverage.json | ||
*/ | ||
|
||
|
||
function c(num1, num2) { | ||
return num2 * num1; | ||
} | ||
function d(num3) { | ||
return num3; | ||
} | ||
c(4,3); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/* | ||
These two blocks were included into the HTML used by the test runner | ||
to create fixtures/two-inline.json | ||
*/ | ||
|
||
// <script> block 1 | ||
function c(num1, num2) { | ||
return num2 * num1; | ||
} | ||
function d(num3) { | ||
return num3; | ||
} | ||
c(4,3); | ||
|
||
// <script> block 2 | ||
function e(num4, num5, num6) { | ||
return num4 * num5 - num6; | ||
} | ||
e(3,5,6); |