Skip to content

Commit

Permalink
Reformat and deduplicate reports, clarify var names
Browse files Browse the repository at this point in the history
  • Loading branch information
plocket committed Nov 1, 2024
1 parent 2123101 commit 7164f06
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 40 deletions.
30 changes: 18 additions & 12 deletions lib/run_cucumber.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,22 +172,28 @@ async function main() {
);
}

// Include debug info at the bottom of the unexpected results file if needed
let debug_unexpected_content = ``;
/** Include debug info at the bottom of the unexpected results file if
* needed. Avoid creating the file if its not necessary. */
let temp_debug_unexpected_content = ``;
try {
debug_unexpected_content = fs.readFileSync(`${ log.path }/${ log.debug_unexpected_filename }`);
} catch ( error_reading_debug_unexpected ) {
// It's ok if this file doesn't exist
}
if ( debug_unexpected_content !== `` ) {
fs.appendFileSync(
`${ log.path }/${ log.unexpected_filename }`,
`\n\n\n\n\n\n\n\n
≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡
temp_debug_unexpected_content = fs.readFileSync(`${ log.path }/${ log.temp_unexpected_debug_log_filename }`);
} catch ( ignore_err ) { /* Missing debug info is ok */ }

if ( temp_debug_unexpected_content !== `` ) {
// Add spacing before the debug content if file already has content
try {
fs.readFileSync(`${ log.path }/${ log.unexpected_filename }`); // Does it have contents?
fs.appendFileSync(`${ log.path }/${ log.unexpected_filename }`, `\n\n\n\n\n` );
} catch ( ignore_err ) { /* Missing unexpected file is ok */ }

// Add the heading for debug content
fs.appendFileSync( `${ log.path }/${ log.unexpected_filename }`,
`≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡
🐛 Full DEBUG info (some repeated content) 🐛
≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡\n\n`
);
fs.appendFileSync(`${ log.path }/${ log.unexpected_filename }`, debug_unexpected_content );
// Add the debug content itself
fs.appendFileSync(`${ log.path }/${ log.unexpected_filename }`, temp_debug_unexpected_content );
}

} catch ( writing_cucumber_error ) {
Expand Down
4 changes: 2 additions & 2 deletions lib/steps.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ BeforeAll(async function() {
scope.paths.debug_log = `${ scope.paths.artifacts }/${ log.debug_log_filename }`;
scope.paths.report_log = `${ scope.paths.artifacts }/${ log.report_log_filename }`;
scope.paths.report = `${ scope.paths.artifacts }/${ log.report_filename }`;
scope.paths.debug_unexpected = `${ scope.paths.artifacts }/${ log.debug_unexpected_filename }`;
scope.paths.failures = `${ scope.paths.artifacts }/${ log.unexpected_filename }`;
scope.paths.debug_of_unexpected = `${ scope.paths.artifacts }/${ log.temp_unexpected_debug_log_filename }`;
scope.paths.report_of_unexpected = `${ scope.paths.artifacts }/${ log.unexpected_filename }`;

reports.create( scope );
});
Expand Down
33 changes: 22 additions & 11 deletions lib/utils/log.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class Log {

// Debug filenames
debug_log_filename = `debug_log.txt`; // Should always exist
debug_unexpected_filename = `debug_unexpected_results.txt`; // Sometimes exists
temp_unexpected_debug_log_filename = `temp_unexpected_results_debug_log.txt`; // Sometimes exists
unexpected_filename = `unexpected_results.txt`; // Only created if needed
report_log_filename = `report_log.txt`; // Not integrated, but used
// Regular report filename
Expand Down Expand Up @@ -300,6 +300,10 @@ class Log {
do_throw = false,
} = debug_opts;

// "warning" level and icon for for internal devs we use to avoid disrupting
// the info authors are looking for
if ( level === `note` ) { icon = `🖊️`; }

let formatted = `Skipped formatting all logs.`;
try {

Expand Down Expand Up @@ -458,14 +462,14 @@ class Log {
* @returns { string } The formatted log.
*
* @example
* // Quotes are to make the surrounding white space more clear
* // Backticks are to make the surrounding white space more clear
* log._stringify_inline({ prev_line_type: null, inline_log: "apple" });
* // "apple"
* // `apple`
* log._stringify_inline({ prev_line_type: 'inline', inline_log: "apple" });
* // " apple"
* // ` apple`
* log._stringify_inline({ prev_line_type: 'block', inline_log: "apple" });
* //
* // apple
* // `
* // apple`
* */
// An inline log's string starts with nothing if it's the first string...
let start = ``;
Expand Down Expand Up @@ -557,6 +561,13 @@ class Log {
} // Ends _combine_logs_and_possible_error()

_get_Error_safely({ maybe_error }) {
/** Given anything, return an Error. Avoid throwing any errors.
*
* @param { Object } opts
* @param { * } opts.maybe_error - Optional. Can be anything.
*
* @returns { Error }
* */
if ( maybe_error instanceof Error ) {
return maybe_error;
} else {
Expand Down Expand Up @@ -793,7 +804,7 @@ class Log {
}

try {
fs.appendFileSync( `${ this.path }/${ this.debug_log_filename }`, `\n` + text );
fs.appendFileSync( `${ this.path }/${ this.debug_log_filename }`, text + `\n` );
} catch ( fs_append_error ) {
// If this is our first time failing to write to the debug log, console
// warn about it.
Expand All @@ -804,7 +815,7 @@ class Log {

try {
if ( unexpected ) {
this._save_to_unexpected({ text });
this._save_to_temp_unexpected_file({ text });
}
} catch ( error_sending_to_unexpected ) {
// Do nothing. These values will be in the debug log
Expand All @@ -830,7 +841,7 @@ class Log {
}
}

_save_to_unexpected({ text = `` } = {}) {
_save_to_temp_unexpected_file({ text = `` } = {}) {
/** Save to the unexpected results file. Intended for errors and warnings.
* Catch ANY error.
*
Expand All @@ -841,9 +852,9 @@ class Log {
* @returns { string } The same value as given.
* */
try {
fs.appendFileSync(`${ this.path }/${ this.debug_unexpected_filename }`, `\n${ text }` );
fs.appendFileSync(`${ this.path }/${ this.temp_unexpected_debug_log_filename }`, `${ text }\n` );
} catch ( error_saving_to_unexpected_results_file ) {
// Do nothing. We should get the data in the debug file or console.
// Do nothing. At worst, we'll get the data in the debug file or console .
}
return text;
}
Expand Down
63 changes: 51 additions & 12 deletions lib/utils/reports.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,24 +121,63 @@ reports.saveFinalScenarioData = function( scope, { status=`in progress` }) {
this.setReportScenarioStatus( scope, { status });
let scenario = scope.report.get( scope.scenario_id );
let report = this.getPrintableScenario( scenario );
if ( scenario.status === `FAILED` ) { this.failures.push( report ); }
else if ( scenario.status === `PASSED` ) { this.successes.push( report ); }
// else if ( scenario.status === `WARNING` ) { passed_reports += report; this.warnings.push( report ); }
// else if ( scenario.status === `UNDEFINED` ) { passed_reports += report; this.undefineds.push( report ); }
else { this.others.push( report ); }

// Save to unexpected results files (worry about optimization later)
if ( this.failures.length > 1 || this.others.length > 1 ) {
let unexpected_results = this.title + this.getPrintableUnexpected( scope );
fs.writeFileSync( scope.paths.failures, unexpected_results );
fs.appendFileSync( scope.paths.debug_unexpected, unexpected_results );
if ( scenario.status === `PASSED` ) {
this.successes.push( report );
} else {
this._save_unexpected( scope, { status: scenario.status, report });
}
// if ( scenario.status === `FAILED` ) {
// this.failures.push( report );
// }
// else if ( scenario.status === `PASSED` ) { this.successes.push( report ); }
// // else if ( scenario.status === `WARNING` ) { passed_reports += report; this.warnings.push( report ); }
// // else if ( scenario.status === `UNDEFINED` ) { passed_reports += report; this.undefineds.push( report ); }
// else { this.others.push( report ); }

// // Save to unexpected results files (worry about optimization later)
// if ( this.failures.length > 1 || this.others.length > 1 ) {
// let unexpected_results = this.title + this.getPrintableUnexpected( scope );
// fs.writeFileSync( scope.paths.report_of_unexpected, unexpected_results );
// fs.appendFileSync( scope.paths.debug_of_unexpected, unexpected_results );
// }

// Save to reports.txt (worry about optimization later)
fs.writeFileSync( scope.paths.report, this.getPrintableReport( scope ) );

return scenario;
}; // Ends reports.saveFinalScenarioData()

reports._save_unexpected = function(scope, { status, report }) {
/** Store the unexpected report and write to the unexpected report file.
*
* @param {Object} scope - State and methods of the current tests
* @param {Object} data
* @param {Object} data.status - Status of the current scenario
* @param {string} data.report - Report for the current scenario
* */
if ( status === `FAILED` ) {
this.failures.push( report );
// } else if ( status === `WARNING` ) { passed_reports += report; this.warnings.push( report );
// } else if ( status === `UNDEFINED` ) { passed_reports += report; this.undefineds.push( report ); }
} else {
this.others.push( report );
}

// If it's our first unexpected result, add the title to the top of the page
let already_unexpected = ``;
try { already_unexpected = fs.readFileSync( scope.paths.report_of_unexpected ); }
catch ( ignore_non_critical_error ) {}
if ( already_unexpected === `` ) {
try { fs.appendFileSync( scope.paths.report_of_unexpected, this.title ); }
catch ( ignore_non_critical_error ) {}
}

try { fs.appendFileSync( scope.paths.report_of_unexpected, report ); }
catch ( ignore_non_critical_error ) {}

return this;
}

reports.progress = function ({ logs=[] }) {
// TODO: save stdout-style to report_log, object, and getting printable scenario
};
Expand Down Expand Up @@ -230,7 +269,7 @@ let getDebugLine = function ({ type=``, code=`ALK00rd`, value=`` }) {
let start = ``;

// Get all the relevant emoji
if ( type.includes(`debug`) ) { start += `🐞`; }
if ( type.includes(`debug`) ) { start += `🐛`; }
if ( type.includes(`info`) ) { start += `💡`; }
if ( type.includes(`success`) ) { start += `🌈`; }
if ( type.includes(`warning`) ) { start += `🔎`; }
Expand Down
6 changes: 3 additions & 3 deletions tests/unit_tests/log.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ describe(`An instance of log`, function () {
} catch ( error ) {
// Do nothing, go on to test the files
}
const exists = fs.existsSync(`${ temp_log3.path }/${ temp_log3.debug_unexpected_filename }`);
const exists = fs.existsSync(`${ temp_log3.path }/${ temp_log3.temp_unexpected_debug_log_filename }`);
expect( exists ).to.be.true;
// Clean up by deleting the folder
fs.rmSync(temp_log_path, { recursive: true, force: true });
Expand Down Expand Up @@ -546,11 +546,11 @@ function expect_debug_file_to_not_include( text ) {


function expect_unexpected_output_file_to_include( text ) {
let from_file = fs.readFileSync(`${path}/${log.debug_unexpected_filename}`, 'utf8');
let from_file = fs.readFileSync(`${path}/${log.temp_unexpected_debug_log_filename}`, 'utf8');
expect( from_file ).to.include( text );
};
function expect_unexpected_output_file_to_not_include( text ) {
let from_file = fs.readFileSync(`${path}/${log.debug_unexpected_filename}`, 'utf8');
let from_file = fs.readFileSync(`${path}/${log.temp_unexpected_debug_log_filename}`, 'utf8');
expect( from_file ).to.not.include( text );
};

Expand Down

0 comments on commit 7164f06

Please sign in to comment.