Skip to content

Commit

Permalink
added new option copyAttachments (#143)
Browse files Browse the repository at this point in the history
  • Loading branch information
cenfun committed Sep 1, 2024
1 parent f1b39d5 commit 9aea118
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 44 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

* 2.8.0
- (New Feature) added new merge command for CLI: `npx monocart merge path-to/*/*.json` (#142)
- added new option `copyAttachments` (#143)

* 2.7.1
- fixed custom raw dir for merging coverage
Expand Down
4 changes: 4 additions & 0 deletions lib/default/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ module.exports = () => ({
attachmentPath: null,
// attachmentPath: (currentPath, extras) => `https://cenfun.github.io/monocart-reporter/${currentPath}`,

// whether copy attachments to the output dir, defaults to true
// it is useful when there are multiple reports being output.
copyAttachments: true,

traceViewerUrl: 'https://trace.playwright.dev/?trace={traceUrl}',

// logging levels: off, error, info, debug
Expand Down
18 changes: 4 additions & 14 deletions lib/generate-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,21 +76,11 @@ const generateData = async (results) => {
trends
} = results;

// console.log(config);
const cwd = Util.formatPath(process.cwd());

const outputFile = await Util.resolveOutputFile(options.outputFile);
// init outputDir
const outputDir = path.dirname(outputFile);
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir, {
recursive: true
});
}
// for visitor relative path of attachments
options.cwd = cwd;
options.outputDir = outputDir;
const {
cwd, outputFile, outputDir
} = options;

// console.log(config);
const visitor = new Visitor(root, options);
await visitor.start();

Expand Down
27 changes: 23 additions & 4 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const path = require('path');
const EC = require('eight-colors');
const { getSystemInfo, getTickInfo } = require('./utils/system.js');
const generateData = require('./generate-data.js');
Expand Down Expand Up @@ -44,6 +45,7 @@ class MonocartReporter {
};

Util.initLoggingLevel(this.options.logging, 'reporter');
this.initDir();

this.testMap = new Map();

Expand All @@ -58,16 +60,31 @@ class MonocartReporter {
this.tickTime = this.options.tickTime || 1000;
this.tickStart();

// clean assets dir
Util.initAssetsDir(this.options);

const stateOptions = this.options.state;
if (stateOptions) {
this.bindFunctions(stateOptions);
this.stateServer = createStateServer(stateOptions);
}
}

async initDir() {

const cwd = Util.formatPath(process.cwd());
this.options.cwd = cwd;

const outputFile = await Util.resolveOutputFile(this.options.outputFile);
// init outputDir
const outputDir = path.dirname(outputFile);
Util.initDir(outputDir);
// for visitor relative path of attachments
this.options.outputDir = outputDir;

// init attachmentsDir
if (this.options.copyAttachments) {
this.options.attachmentsDir = path.resolve(outputDir, 'attachments');
}
}

// ==========================================================================

bindFunctions(obj) {
Expand Down Expand Up @@ -119,7 +136,9 @@ class MonocartReporter {
onBegin(config, suite) {
this.config = config;
this.root = suite;
// console.log(config);

// output dir for test results (not reporter)
this.options.testOutputDir = config.projects[0].outputDir;
// console.log(`onBegin: ${suite.allTests().length} tests`);
}

Expand Down
4 changes: 3 additions & 1 deletion lib/plugins/audit/audit.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,11 @@ const attachAuditReport = async (runnerResult, testInfo, options = {}) => {
return;
}

const outputDir = await Util.resolveOutputDir(testInfo);

options = {
name: `Lighthouse Report - ${testInfo.title}`,
outputDir: Util.resolveOutputDir(testInfo, options),
outputDir,
outputName: `audit-${Util.resolveTestIdWithRetry(testInfo)}`,
... options
};
Expand Down
6 changes: 2 additions & 4 deletions lib/plugins/coverage/coverage.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ const Util = require('../../utils/util.js');

const attachCoverageReport = async (data, testInfo, options = {}) => {

const reporterOptions = Util.resolveReporterOptions(testInfo);

const outputDir = Util.resolveOutputDir(testInfo, options);
const outputDir = await Util.resolveOutputDir(testInfo);
const folderName = `coverage-${Util.resolveTestIdWithRetry(testInfo)}`;

// support multiple calls
Expand All @@ -21,7 +19,7 @@ const attachCoverageReport = async (data, testInfo, options = {}) => {
}

const coverageOptions = {
logging: reporterOptions.logging,
logging: Util.resolveLogging(testInfo, options),
outputDir: htmlDir,
name: `Coverage Report - ${testInfo.title}`,
assetsPath: '../assets',
Expand Down
4 changes: 3 additions & 1 deletion lib/plugins/network/network.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,12 @@ const attachNetworkReport = async (har, testInfo, options = {}) => {
return;
}

const outputDir = await Util.resolveOutputDir(testInfo);

options = {
// default title
name: `Network Report - ${testInfo.title}`,
outputDir: Util.resolveOutputDir(testInfo, options),
outputDir,
outputName: `network-${Util.resolveTestIdWithRetry(testInfo)}`,
inline: false,
... options
Expand Down
28 changes: 10 additions & 18 deletions lib/utils/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ const Share = require('../platform/share.js');

const getDefaultOptions = require('../default/options.js');

const assetsName = 'assets';

const Util = {
... Share,

Expand Down Expand Up @@ -78,15 +76,11 @@ const Util = {
return parsed;
},

resolveOutputDir: (testInfo, options) => {
if (options && options.outputDir) {
return options.outputDir;
}
const outputDir = testInfo.project.outputDir || testInfo.config.outputDir;
if (outputDir) {
return outputDir;
}
return path.resolve(testInfo.outputDir, '../');
resolveOutputDir: async (testInfo) => {
const reporterOptions = Util.resolveReporterOptions(testInfo);
const outputFile = await Util.resolveOutputFile(reporterOptions.outputFile);
const outputDir = path.dirname(outputFile);
return outputDir;
},

resolveOutputFile: async (outputFile) => {
Expand Down Expand Up @@ -115,7 +109,11 @@ const Util = {
return reporterOptions.logging;
},

// eslint-disable-next-line complexity
resolveReporterOptions: (testInfo) => {
if (Util.reporterOptions) {
return Util.reporterOptions;
}
if (!testInfo) {
return {};
}
Expand All @@ -125,6 +123,7 @@ const Util = {
if (Array.isArray(item)) {
const [name, options] = item;
if (name && name.indexOf('monocart-reporter') !== -1) {
Util.reporterOptions = options;
return options || {};
}
}
Expand Down Expand Up @@ -158,13 +157,6 @@ const Util = {
});
},

initAssetsDir: async (options) => {
const outputFile = await Util.resolveOutputFile(options.outputFile);
const outputDir = path.dirname(outputFile);
const assetsDir = path.resolve(outputDir, assetsName);
Util.initDir(assetsDir);
},

getEOL: function(content) {
if (!content) {
return os.EOL;
Expand Down
37 changes: 36 additions & 1 deletion lib/visitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,7 @@ class Visitor {

const o = this.options;
// store relative path first
this.copyAttachmentsHandler(item);
item.path = Util.relativePath(item.path, o.outputDir);

// custom attachment path
Expand All @@ -558,6 +559,37 @@ class Visitor {
}
}

copyAttachmentsHandler(item) {

const { attachmentsDir } = this.options;
if (!attachmentsDir) {
return;
}

if (!fs.existsSync(attachmentsDir)) {
fs.mkdirSync(attachmentsDir, {
recursive: true
});
}

// custom report
if (item.report) {
return;
}

const oldPath = item.path;
const filename = Util.calculateSha1(oldPath);
const ext = path.extname(oldPath);
const newPath = path.resolve(attachmentsDir, `${filename}${ext}`);
fs.cpSync(oldPath, newPath, {
force: true,
recursive: true
});

item.path = newPath;

}

getImageCategory(item) {
if (item.contentType && item.contentType.startsWith('image/')) {
if (item.name) {
Expand Down Expand Up @@ -702,7 +734,10 @@ class Visitor {
return;
}

const attachmentsPath = path.resolve(this.options.outputDir, caseId);
// testOutputDir is for test results not reporter
const { outputDir, testOutputDir } = this.options;

const attachmentsPath = path.resolve(testOutputDir || outputDir, caseId);
if (!fs.existsSync(attachmentsPath)) {
fs.mkdirSync(attachmentsPath, {
recursive: true
Expand Down
2 changes: 1 addition & 1 deletion tests/playwright.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ module.exports = {
}
],

outputDir: '../.temp/monocart/',
outputDir: '../.temp/test-results/',
reporter: [
['list'],
['json', {
Expand Down

0 comments on commit 9aea118

Please sign in to comment.