Skip to content

Commit

Permalink
zip report files
Browse files Browse the repository at this point in the history
  • Loading branch information
cenfun committed Oct 1, 2024
1 parent 8cc1b08 commit cf240b4
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 28 deletions.
3 changes: 3 additions & 0 deletions lib/default/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ module.exports = () => ({
// the output html file path (relative process.cwd)
outputFile: './monocart-report/index.html',

json: true,
zip: false,

// whether to copy attachments to the reporter output dir, defaults to true
// it is useful when there are multiple html reports being output.
copyAttachments: true,
Expand Down
124 changes: 96 additions & 28 deletions lib/generate-report.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,92 @@
const fs = require('fs');
const path = require('path');
const EC = require('eight-colors');
const nodemailer = require('nodemailer');
const Util = require('./utils/util.js');
const emailPlugin = require('./plugins/email.js');
const Assets = require('./assets.js');

const { ZipFile } = require('./packages/monocart-reporter-vendor.js');
// ===========================================================================

const generateJson = (outputDir, htmlFile, reportData) => {
const filename = path.basename(htmlFile, '.html');
let jsonPath = path.resolve(outputDir, `${filename}.json`);
const generateJson = (outputDir, filename, reportData, options) => {
if (!options.json) {
return;
}

const jsonPath = path.resolve(outputDir, `${filename}.json`);
Util.writeJSONSync(jsonPath, reportData);
jsonPath = Util.relativePath(jsonPath);
return jsonPath;
reportData.jsonPath = Util.relativePath(jsonPath);
};

const forEachFile = (dir, callback) => {
if (!fs.existsSync(dir)) {
return;
}
const dirs = [];
fs.readdirSync(dir, {
withFileTypes: true
}).forEach((it) => {

if (it.isFile()) {
callback(it.name, dir);
return;
}

if (it.isDirectory()) {
dirs.push(path.resolve(dir, it.name));
}
});

for (const subDir of dirs) {
forEachFile(subDir, callback);
}

};

const generateZip = (outputDir, filename, reportData, options) => {

if (!options.zip) {
return;
}

const zipPath = path.resolve(outputDir, `${filename}.zip`);
const reportFiles = [];
return new Promise((resolve) => {
const zipFile = new ZipFile();
zipFile.outputStream.pipe(fs.createWriteStream(zipPath)).on('close', function() {

// remove files after zip
reportData.reportFiles = reportFiles;
reportData.zipPath = Util.relativePath(zipPath);

resolve();
});

forEachFile(outputDir, (name, dir) => {
const absPath = path.resolve(dir, name);
const relPath = Util.relativePath(absPath, outputDir);
reportFiles.push(relPath);
// console.log(relPath);
zipFile.addFile(absPath, relPath);
});

zipFile.end();
});
};

const generateHtml = async (outputDir, htmlFile, reportData, inline) => {
const generateHtml = async (outputDir, filename, reportData, options) => {

// generate html
let inline = true;
if (typeof options.inline === 'boolean') {
inline = options.inline;
}

// deps
const jsFiles = ['monocart-reporter-app'];
const htmlFile = `${filename}.html`;

const options = {
const htmlPath = await Assets.saveHtmlReport({
inline,
reportData,
jsFiles,
Expand All @@ -29,11 +95,10 @@ const generateHtml = async (outputDir, htmlFile, reportData, inline) => {
htmlFile,

reportDataFile: 'report-data.js'
};
});

const htmlPath = await Assets.saveHtmlReport(options);
reportData.htmlPath = htmlPath;

return htmlPath;
};

const showTestResults = (reportData) => {
Expand Down Expand Up @@ -221,30 +286,33 @@ const generateReport = async (reportData, options, rawData) => {
await onDataHandler(reportData, options, rawData);

// console.log(reportData);
const htmlFile = path.basename(outputFile);
const filename = path.basename(outputFile, '.html');

// generate json
const jsonPath = await generateJson(outputDir, htmlFile, reportData);

// generate html
let inline = true;
if (typeof options.inline === 'boolean') {
inline = options.inline;
}
const htmlPath = await generateHtml(outputDir, htmlFile, reportData, inline);

// for onEnd after saved
Object.assign(reportData, {
htmlPath,
jsonPath
});
await generateHtml(outputDir, filename, reportData, options);
await generateJson(outputDir, filename, reportData, options);
await generateZip(outputDir, filename, reportData, options);

await onEndHandler(reportData, options);

// after onEnd for summary changes
showTestResults(reportData);

Util.logInfo(`html report: ${EC.cyan(htmlPath)} (json: ${jsonPath})`);
const {
htmlPath, jsonPath, zipPath
} = reportData;

const assets = [];
if (jsonPath) {
assets.push(`json: ${EC.cyan(jsonPath)}`);
}
if (zipPath) {
assets.push(`zip: ${EC.cyan(zipPath)}`);
}

if (assets.length) {
Util.logInfo(assets.join(' '));
}

Util.logInfo(`view report: ${EC.cyan(`npx monocart show-report ${htmlPath}`)}`);

};
Expand Down
5 changes: 5 additions & 0 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ export type MonocartReporterOptions = {
/** the output file path (relative process.cwd) */
outputFile?: string;

/** output json file for data only */
json?: boolean;
/** output zip file for all report files */
zip?: boolean;

/**
* whether to copy attachments to the reporter output dir, defaults to true
* it is useful when there are multiple html reports being output
Expand Down
2 changes: 2 additions & 0 deletions tests/playwright.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ module.exports = {
name: 'My Test Report',
outputFile: '.temp/monocart/index.html',

zip: true,

attachmentPath: (currentPath, extras) => {
// console.log(currentPath, extras);
// return `https://cenfun.github.io/monocart-reporter/${relativePath}`;
Expand Down

0 comments on commit cf240b4

Please sign in to comment.