Skip to content

Commit

Permalink
fix: copy issues, compile target, trace app, add overwrite and debug …
Browse files Browse the repository at this point in the history
…option (#6)

* testing merged report index issues

* feat: include trace app in merge, add overwrite and debug options

* chore: add supporting reports to test

* chore: add dist for testing

* build: target node 12

* chore: remove test files
  • Loading branch information
nickofthyme authored Jul 3, 2022
1 parent 4ec4cb8 commit 0e82dc6
Show file tree
Hide file tree
Showing 36 changed files with 389 additions and 41 deletions.
77 changes: 77 additions & 0 deletions html_report-4/index.html

Large diffs are not rendered by default.

Binary file added html_report-4/trace/codicon.dcd00fb4.ttf
Binary file not shown.
Binary file added html_report-4/trace/icon-16x16.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added html_report-4/trace/icon-192x192.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added html_report-4/trace/icon-256x256.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added html_report-4/trace/icon-32x32.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added html_report-4/trace/icon-384x384.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added html_report-4/trace/icon-512x512.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
32 changes: 32 additions & 0 deletions html_report-4/trace/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<!--
Copyright (c) Microsoft Corporation.
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.
-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="icon" type="image/png" sizes="32x32" href="icon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="icon-16x16.png">
<link rel="manifest" href="manifest.webmanifest">
<title>Playwright Trace Viewer</title>
<script type="module" crossorigin src="main.ce7ea3d1.js"></script>
<link rel="stylesheet" href="main.a5d7e729.css">
</head>
<body>
<div id="root"></div>

</body>
</html>
1 change: 1 addition & 0 deletions html_report-4/trace/main.a5d7e729.css

Large diffs are not rendered by default.

46 changes: 46 additions & 0 deletions html_report-4/trace/main.ce7ea3d1.js

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions html_report-4/trace/sw.bundle.js

Large diffs are not rendered by default.

Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
77 changes: 77 additions & 0 deletions html_report-5/index.html

Large diffs are not rendered by default.

Binary file added html_report-5/trace/codicon.dcd00fb4.ttf
Binary file not shown.
Binary file added html_report-5/trace/icon-16x16.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added html_report-5/trace/icon-192x192.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added html_report-5/trace/icon-256x256.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added html_report-5/trace/icon-32x32.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added html_report-5/trace/icon-384x384.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added html_report-5/trace/icon-512x512.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
32 changes: 32 additions & 0 deletions html_report-5/trace/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<!--
Copyright (c) Microsoft Corporation.
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.
-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="icon" type="image/png" sizes="32x32" href="icon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="icon-16x16.png">
<link rel="manifest" href="manifest.webmanifest">
<title>Playwright Trace Viewer</title>
<script type="module" crossorigin src="main.ce7ea3d1.js"></script>
<link rel="stylesheet" href="main.a5d7e729.css">
</head>
<body>
<div id="root"></div>

</body>
</html>
1 change: 1 addition & 0 deletions html_report-5/trace/main.a5d7e729.css

Large diffs are not rendered by default.

46 changes: 46 additions & 0 deletions html_report-5/trace/main.ce7ea3d1.js

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions html_report-5/trace/sw.bundle.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"node": ">=12"
},
"scripts": {
"test": "rm -fr merged-html-report && ts-node --project=tsconfig.test.json ./src/merge-reports.spec.ts",
"test": "ts-node --project=tsconfig.test.json ./src/merge-reports.spec.ts",
"clean": "rm -rf dist/",
"test:js": "node ./src/merge-reports.spec.js",
"compile": "tsc",
Expand Down
5 changes: 5 additions & 0 deletions src/merge-reports.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,8 @@ mergeHTMLReports([
path.resolve(process.cwd(), "./html_report-1"),
path.resolve(process.cwd(), "./html_report-2")
]);

mergeHTMLReports([
path.resolve(process.cwd(), "./html_report-4"),
path.resolve(process.cwd(), "./html_report-5")
], { outputFolderName: './test-merge-report' });
89 changes: 57 additions & 32 deletions src/merge-reports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,18 @@
*/

import { createWriteStream, existsSync } from "fs";
import { mkdir, readFile, readdir, copyFile, appendFile, writeFile } from "fs/promises";
import { mkdir, readFile, readdir, copyFile, appendFile, writeFile, rm } from "fs/promises";
import path from "path";
import JSZip from "jszip";
import yazl from "yazl";
import Base64Encoder from "./Base64Encoder";
import type { HTMLReport, Config, ZipDataFile, Stats, FileReport } from "./types";

const SHOW_DEBUG_MESSAGES = false;

const defaultConfig = {
const defaultConfig: Required<Config> = {
outputFolderName: "merged-html-report",
outputBasePath: process.cwd()
outputBasePath: process.cwd(),
overwriteExisting: true,
debug: false,
}

const re = /<script>\nwindow\.playwrightReportBase64\s=\s"data:application\/zip;base64,(.+)<\/script>/
Expand All @@ -38,9 +38,21 @@ async function mergeHTMLReports(inputReportPaths: string[], givenConfig: Config
}

// Merge config with default values
const { outputFolderName, outputBasePath } = {...defaultConfig, ...givenConfig};
const { outputFolderName, outputBasePath, overwriteExisting, debug } = {...defaultConfig, ...givenConfig};
const outputPath = path.resolve(outputBasePath, `./${outputFolderName}`);

if (existsSync(outputPath)) {
if (overwriteExisting) {
if (debug) {
console.log('Cleaning output directory');
}
await rm(outputPath, { recursive: true, force: true })
} else {
throw new Error(`Report merge aborted. Output directory already exists and overwriteExisting set to false.\n
path: ${outputPath}\n`)
}
}

let mergedZipContent = new yazl.ZipFile();
let aggregateReportJson: HTMLReport | null = null;
let baseIndexHtml = '';
Expand Down Expand Up @@ -71,7 +83,7 @@ async function mergeHTMLReports(inputReportPaths: string[], givenConfig: Config
const fileReportJson = JSON.parse(fileContentString) as FileReport;

if (fileReportMap.has(relativePath)) {
if (SHOW_DEBUG_MESSAGES) {
if (debug) {
console.log('Merging duplicate file report: ' + relativePath);
}

Expand All @@ -86,7 +98,7 @@ async function mergeHTMLReports(inputReportPaths: string[], givenConfig: Config

} else {
const currentReportJson = JSON.parse(fileContentString);
if (SHOW_DEBUG_MESSAGES) {
if (debug) {
console.log('---------- report.json ----------');
console.log(JSON.stringify(currentReportJson, null, 2));
}
Expand Down Expand Up @@ -116,34 +128,23 @@ async function mergeHTMLReports(inputReportPaths: string[], givenConfig: Config
}));

const contentFolderName = "data";
const contentFolderPath = `${reportDir}/${contentFolderName}/`;
const contentFolderPath = path.join(reportDir, contentFolderName);
const contentOuputPath = path.join(outputPath, contentFolderName);

if (existsSync(contentFolderPath)) {
await mkdir(outputPath + "/data", { recursive: true });
const contentFiles = await readdir(contentFolderPath);
await copyDir(contentFolderPath, contentOuputPath, debug);
}

await Promise.all(
contentFiles.map(async (fileName) => {
const srcPath = path.resolve(
process.cwd(),
`${contentFolderPath}${fileName}`
);
const destPath = `${outputPath}/${contentFolderName}/${fileName}`;
if (SHOW_DEBUG_MESSAGES) {
console.log({
srcPath,
destPath
});
}
await copyFile(srcPath, destPath);
if (SHOW_DEBUG_MESSAGES) {
console.log(fileName);
}
})
);
const traceFolderName = "trace";
const traceFolderPath = path.join(reportDir, traceFolderName);
const traceOuputPath = path.join(outputPath, traceFolderName);

// only need to copy trace applicate dir once if needed
if (existsSync(traceFolderPath) && !existsSync(traceOuputPath)) {
await copyDir(traceFolderPath, traceOuputPath, debug);
}

if (SHOW_DEBUG_MESSAGES) {
if (debug) {
console.log({
reportDir
});
Expand All @@ -164,7 +165,7 @@ async function mergeHTMLReports(inputReportPaths: string[], givenConfig: Config
"report.json"
);

if (SHOW_DEBUG_MESSAGES) {
if (debug) {
console.log('---------- aggregateReportJson ----------');
console.log(JSON.stringify(aggregateReportJson, null, 2));
}
Expand Down Expand Up @@ -199,3 +200,27 @@ function mergeStats(base: Stats, added: Stats) {
base.duration += added.duration;
base.ok = base.ok && added.ok;
}

async function copyDir(srcDirPath: string, srcDirOutputPath: string, debug: boolean) {
await mkdir(srcDirOutputPath, { recursive: true });
const traceFiles = await readdir(srcDirPath);
await Promise.all(
traceFiles.map(async (fileName) => {
const srcPath = path.resolve(
process.cwd(),
path.join(srcDirPath,fileName)
);
const destPath = path.join(srcDirOutputPath, fileName);
if (debug) {
console.log({
srcPath,
destPath
});
}
await copyFile(srcPath, destPath);
if (debug) {
console.log(fileName);
}
})
);
}
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ export type HTMLReport = {
export type Config = {
outputFolderName?: string,
outputBasePath?: string
overwriteExisting?: boolean
debug?: boolean,
}

export type ZipDataFile = {
Expand Down
8 changes: 2 additions & 6 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
{
"extends": "@tsconfig/node14/tsconfig.json",
"extends": "@tsconfig/node12/tsconfig.json",
"compilerOptions": {
// "module": "commonjs",
"noImplicitAny": false,
"removeComments": true,
// "preserveConstEnums": true,
// "outFile": "../../built/local/tsc.js",
// Generate d.ts files
"declaration": true,
"sourceMap": true,
"rootDir": "./src",
"outDir": "./dist"
},
"include": ["src/**/*"],
"exclude": ["node_modules", "**/*.spec.ts"]
}
}

0 comments on commit 0e82dc6

Please sign in to comment.