From 7164f06257938379354f1ee5922efa31c8dc95d5 Mon Sep 17 00:00:00 2001 From: plocket <52798256+plocket@users.noreply.github.com> Date: Fri, 1 Nov 2024 08:55:32 -0400 Subject: [PATCH] Reformat and deduplicate reports, clarify var names --- lib/run_cucumber.js | 30 ++++++++++------- lib/steps.js | 4 +-- lib/utils/log.js | 33 ++++++++++++------- lib/utils/reports.js | 63 +++++++++++++++++++++++++++++------- tests/unit_tests/log.test.js | 6 ++-- 5 files changed, 96 insertions(+), 40 deletions(-) diff --git a/lib/run_cucumber.js b/lib/run_cucumber.js index d4b87498..ecb74839 100755 --- a/lib/run_cucumber.js +++ b/lib/run_cucumber.js @@ -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 ) { diff --git a/lib/steps.js b/lib/steps.js index a0b4be48..317e4295 100644 --- a/lib/steps.js +++ b/lib/steps.js @@ -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 ); }); diff --git a/lib/utils/log.js b/lib/utils/log.js index fb749f05..1e491a30 100644 --- a/lib/utils/log.js +++ b/lib/utils/log.js @@ -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 @@ -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 { @@ -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 = ``; @@ -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 { @@ -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. @@ -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 @@ -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. * @@ -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; } diff --git a/lib/utils/reports.js b/lib/utils/reports.js index f2a19430..ff9adf79 100644 --- a/lib/utils/reports.js +++ b/lib/utils/reports.js @@ -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 }; @@ -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 += `🔎`; } diff --git a/tests/unit_tests/log.test.js b/tests/unit_tests/log.test.js index ad770542..6ee8251f 100644 --- a/tests/unit_tests/log.test.js +++ b/tests/unit_tests/log.test.js @@ -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 }); @@ -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 ); };