Skip to content

Commit

Permalink
merge(#36): feat: add info and verbose mode logging
Browse files Browse the repository at this point in the history
feat: add info and verbose mode logging
  • Loading branch information
403-html committed Nov 19, 2023
2 parents 9f18cde + a3e30fe commit 22d6cd8
Show file tree
Hide file tree
Showing 4 changed files with 518 additions and 42 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ that's it! You've successfuly converted json report to _slick_ md report.
| `--output` | `-o` | Path to the output markdown file | No | ./md-reports/output.md |
| `--template` | `-t` | Path to the markdown template file | No | ./sample-template.md |
| `--title` | `-T` | Title for the report | No | Test Report |
| `--verbose` | `-v` | Verbose mode | No | false |

## Templating, tags and customization

Expand Down
83 changes: 58 additions & 25 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,39 @@ const fs = require("fs");
const { program } = require("commander");
const mustache = require("mustache");
const pathPkg = require("path");
const winston = require("winston");

program
.option("-p, --path <path>", "Specify the path to the report")
.option("-o, --output <output>", "Specify the path for the markdown file", "./md-reports/output.md")
.option("-t, --template <template>", "Specify the path to the template file", "./sample-template.md")
.option("-T, --title <title>", "Specify the title for the report", "Test Report")
.option("-v, --verbose", "Enable verbose mode for debug logging")
.usage("$0 -p file/path.json [options]")
.addHelpText(
"after",
"\nFor more information, visit https://github.com/403-html/mochawesome-json-to-md"
)
.parse(process.argv);

const createLogger = (verbose) => {
const level = verbose ? 'debug' : 'info';
return winston.createLogger({
level,
format: winston.format.combine(
winston.format.timestamp(),
winston.format.colorize(),
winston.format.printf(({ timestamp, level, message }) => {
return `${timestamp} | ${level} | ${message}`;
}),
),
transports: [
new winston.transports.Console({ level, handleExceptions: true }),
],
});
};

const logger = createLogger(program.opts().verbose);

/**
* Reads the JSON file and returns its content as an object.
Expand All @@ -11,16 +44,19 @@ const pathPkg = require("path");
* @throws Will throw an error if there's an issue parsing the JSON file.
*/
const readJsonFile = (filePath) => {
logger.debug(`Reading JSON file: ${filePath}`);
if (typeof filePath !== "string") {
throw new Error(
`Provide string path for JSON file, actually you pass: ${typeof filePath}`
);
logger.error(`Invalid file path provided: ${filePath}`);
throw new Error(`Invalid file path provided: ${filePath}`);
}

let jsonObj;
try {
logger.debug(`Parsing JSON file: ${filePath}`);
jsonObj = JSON.parse(fs.readFileSync(filePath));
logger.debug(`Successfully parsed JSON file: ${filePath}`);
} catch (err) {
logger.error(`Error while parsing JSON file: ${err}`);
throw new Error(`Error while parsing JSON file: ${err}`);
}
return jsonObj;
Expand All @@ -34,21 +70,22 @@ const readJsonFile = (filePath) => {
* @returns {object} - Extracted information.
*/
const extractTestResultsInfo = ({ results, stats }) => {
const { start: startDate, duration, tests: totalTests, other: otherTests } =
stats;
logger.debug('Extracting test results information');
const { start: startDate, duration, tests: totalTests, other: otherTests } = stats;

const testTypes = ["passes", "failures", "pending", "skipped"];

const categorizedTests = testTypes.map((type) => {
return results.flatMap((result) =>
const categorizedTests = testTypes.map((type) =>
results.flatMap((result) =>
collectTestsByType({
type,
suite: result,
path: result.file,
})
);
});
)
);

logger.debug('Finished extracting test results information');
return {
startDate,
duration,
Expand Down Expand Up @@ -79,15 +116,18 @@ const extractTestResultsInfo = ({ results, stats }) => {
* @returns {Array} - List of tests with the given type.
*/
const collectTestsByType = ({ type, suite, path, cache = [] }) => {
logger.debug(`Collecting tests of type ${type}`);
const localCache = cache;
const { [type]: typeList, suites, tests } = suite;

if (typeList.length > 0) {
for (const uuid of typeList) {
const foundTestByUuid = tests.find((test) => test.uuid === uuid);
if (!foundTestByUuid) {
logger.error(`Test with uuid ${uuid} not found`);
throw new Error(`Test with uuid ${uuid} not found`);
}
logger.debug(`Found test with uuid ${uuid}`);
foundTestByUuid.path = path;
localCache.push({ path, ...foundTestByUuid });
}
Expand All @@ -108,34 +148,27 @@ const collectTestsByType = ({ type, suite, path, cache = [] }) => {
};

const convertMochaToMarkdown = () => {
logger.info('Starting Mocha to Markdown conversion');
const { path, output, template, title } = program.opts();

logger.info(`Reading test results from: ${path}`);
const testResults = readJsonFile(path);

logger.info('Extracting test results information');
const extractedInfo = extractTestResultsInfo(testResults);

// Read the template file
logger.info(`Reading template file: ${template}`);
const templateContent = fs.readFileSync(template, "utf-8");

// Render the template with the extracted information
logger.info('Rendering template with test results');
const renderedMarkdown = mustache.render(templateContent, {...extractedInfo, title});

// Ensure the directory structure exists for the output file
logger.info(`Creating directory structure: ${outputPath}`);
const outputPath = pathPkg.dirname(output);
fs.mkdirSync(outputPath, { recursive: true });

// Write the generated markdown to the specified output file
logger.info(`Writing markdown to: ${output}`);
fs.writeFileSync(output, renderedMarkdown);
};

program
.option("-p, --path <path>", "define path to the report")
.option("-o, --output <output>", "define path for the md file", "./md-reports/output.md")
.option("-t, --template <template>", "define path to the template file", "./sample-template.md")
.option("-T, --title <title>", "define title for the report", "Test Report")
.usage("$0 -p file/path.json [options]")
.addHelpText(
"after",
"\nfor more information, visit https://github.com/403-html/mochawesome-json-to-md"
)
.parse(process.argv);

convertMochaToMarkdown();
Loading

0 comments on commit 22d6cd8

Please sign in to comment.