Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🏗 Introduction of coverage-map task. #29519

Merged
merged 35 commits into from
Aug 3, 2020
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
c709f04
moved script to tasks and added package requirements
xiexr151e Jul 25, 2020
6226290
lint fix
xiexr151e Jul 25, 2020
3d8b520
progress on making task
xiexr151e Jul 25, 2020
68a0194
remove test.json
xiexr151e Jul 25, 2020
cb726e7
additional lint fixes
xiexr151e Jul 25, 2020
d3d0bf3
fixed according to comments
xiexr151e Jul 28, 2020
614b6ca
several changes in coverage-map
xiexr151e Jul 28, 2020
a0976fd
lint
xiexr151e Jul 28, 2020
aa1ebeb
clarify description
xiexr151e Jul 28, 2020
4be5973
change to use async writefile
xiexr151e Jul 29, 2020
43d3433
review flag
xiexr151e Jul 29, 2020
b73f6c3
OK in window.scrollBy()
xiexr151e Jul 29, 2020
170681d
lint
xiexr151e Jul 29, 2020
2cf7c9b
added source-map-explorer to package.json
xiexr151e Jul 30, 2020
1a3e343
Some changes based on feedback
xiexr151e Jul 30, 2020
4326fc5
additional updated based on feedback
xiexr151e Jul 30, 2020
4f731f1
small change
xiexr151e Jul 30, 2020
a9ea8e9
added separate package.json
xiexr151e Jul 30, 2020
0394351
package specific changes
xiexr151e Jul 31, 2020
63f88ef
undo changes in root package.json
xiexr151e Jul 31, 2020
629a581
added yarn.lock
xiexr151e Jul 31, 2020
1bd9856
several changes
xiexr151e Jul 31, 2020
eb2d766
resolve conflict
xiexr151e Jul 31, 2020
e35c742
revert yarn.lock
xiexr151e Jul 31, 2020
12ab917
lock file
xiexr151e Jul 31, 2020
a01a946
delete package-lock.json
xiexr151e Jul 31, 2020
5c14360
update gulpfile.js
xiexr151e Jul 31, 2020
94c11e5
lazy importing
xiexr151e Jul 31, 2020
b5e5089
revert change
xiexr151e Jul 31, 2020
5397659
remove package-lock
xiexr151e Jul 31, 2020
bb11e17
Update package.json
xiexr151e Jul 31, 2020
8dfe435
yarn.lock
xiexr151e Jul 31, 2020
300b8ec
prettify
xiexr151e Jul 31, 2020
62b8a10
excluded coverage-map for getComputedStyle
xiexr151e Jul 31, 2020
d9ed6bb
additional feedback
xiexr151e Aug 1, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 98 additions & 0 deletions build-system/tasks/coverage-map.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/**
* Copyright 2020 The AMP HTML Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const argv = require('minimist')(process.argv.slice(2));
const fs = require('fs');
const log = require('fancy-log');
const puppeteer = require('puppeteer');
const {exec} = require('../common/exec');

const coverageJsonName = argv.json || 'out.json';
const testUrl =
argv.url || 'http://localhost:8000/examples/everything.amp.html';

async function compile() {
await exec(`gulp dist`);
// The question: How to cd to 'amphtml/dist' from anywhere in 'amphtml'?
xiexr151e marked this conversation as resolved.
Show resolved Hide resolved
xiexr151e marked this conversation as resolved.
Show resolved Hide resolved
await exec(`gulp serve --compiled`);
}

async function test() {
log('Opening browser and navigating to everything.amp.html...');
const browser = await puppeteer.launch();
const page = await browser.newPage();

// Enable JavaScript coverage
await Promise.all([page.coverage.startJSCoverage()]);
xiexr151e marked this conversation as resolved.
Show resolved Hide resolved
// Navigate to page
await page.goto(testUrl);
await page.setViewport({
width: 1200,
xiexr151e marked this conversation as resolved.
Show resolved Hide resolved
height: 800,
});
log('Scrolling to the end of the page...');
await autoScroll(page);
xiexr151e marked this conversation as resolved.
Show resolved Hide resolved
log('Testing completed.');
const [jsCoverage] = await Promise.all([page.coverage.stopJSCoverage()]);
xiexr151e marked this conversation as resolved.
Show resolved Hide resolved
const data = JSON.stringify(jsCoverage);
log('Writing to test.json in working directory...');
fs.writeFileSync(coverageJsonName, data);
xiexr151e marked this conversation as resolved.
Show resolved Hide resolved
await browser.close();
}

// Source: https://github.com/chenxiaochun/blog/issues/38s
async function autoScroll(page) {
await page.evaluate(async () => {
await new Promise((resolve, opt_) => {
let totalHeight = 0;
const distance = 100;
const timer = setInterval(() => {
const {scrollHeight} = document.body;
window.scrollBy(0, distance);
xiexr151e marked this conversation as resolved.
Show resolved Hide resolved
totalHeight += distance;

if (totalHeight >= scrollHeight) {
clearInterval(timer);
resolve();
}
}, 100);
});
});
}

async function generateHeatMap() {
await exec(
`source-map-explorer v0.js v0.js.map -m --coverage ${coverageJsonName}`
);
// Maybe I can ask the user to Control+C?
xiexr151e marked this conversation as resolved.
Show resolved Hide resolved
}

async function coverageMap() {
await compile();
// I did this in another terminal, so that means I'll have to spawn another terminal...?
await test();
await generateHeatMap();
}

module.exports = {coverageMap};

coverageMap.description =
'Generates a code coverage "heat map" on v0.js based on code traversed during puppeteer test via source map explorer';

coverageMap.flags = {
json: ' Customize the name of the JSON output (test.json by default).',
url:
" Set the URL for puppeteer testing, starting with 'http://localhost:8000...' (http://localhost:8000/examples/everything.amp.html by default).",
};
2 changes: 2 additions & 0 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ const {cherryPick} = require('./build-system/tasks/cherry-pick');
const {clean} = require('./build-system/tasks/clean');
const {codecovUpload} = require('./build-system/tasks/codecov-upload');
const {compileJison} = require('./build-system/tasks/compile-jison');
const {coverageMap} = require('./build-system/tasks/coverage-map');
const {createGoldenCss} = require('./build-system/tasks/create-golden-css');
const {css} = require('./build-system/tasks/css');
const {csvifySize} = require('./build-system/tasks/csvify-size');
Expand Down Expand Up @@ -145,6 +146,7 @@ createTask('cherry-pick', cherryPick);
createTask('clean', clean);
createTask('codecov-upload', codecovUpload);
createTask('compile-jison', compileJison);
createTask('coverage-map', coverageMap);
createTask('create-golden-css', createGoldenCss);
createTask('css', css);
createTask('csvify-size', csvifySize);
Expand Down
Loading