Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Abstract cli response logging, and add test coverage. #348

Merged
merged 4 commits into from
Jun 8, 2023
Merged

Conversation

joewagner
Copy link
Contributor

@joewagner joewagner commented Jun 1, 2023

fixes tablelandnetwork/local-tableland#347

Summary

Two main changes are included here:

  • Enable the use of console.log to debug tests by wrapping calls with a logger object.
  • Add code coverage tests.

Details

A lot of lines changed here, but the existing logic should all be preserved.
As well as making debugging via console.log possible, this will also enable us to turn on the local network logs to better understand what happened if a github test runner fails. This is particularly useful as we try to find a fix for tablelandnetwork/tableland-js#40
Code coverage is completing at 100%, but there are several c8 ignore statements. Most of these are either hard to mock error conditions or functionality that relates to the experimental ens feature.

@@ -24,17 +24,17 @@ export const handler = async (_argv: Arguments<Options>): Promise<void> => {
const { format } = _argv;

if (format === "pretty") {
console.log(JSON.stringify(chains, null, 4));
logger.log(JSON.stringify(chains, null, 4));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Much of the code changes are simply replacing console.log with logger.log etc...

if (!ens) {
logger.log(
"To use ENS, ensure you have set the enableEnsExperiment flag to true"
);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These lines include a change to wrap with a try/catch, but also include an informative message in the case the user is trying to use ens without the required flags

if (e.message.includes("in JSON at position")) {
console.log("Can't unwrap multiple rows. Use --unwrap=false");
} catch (err: any) {
if (err.message.includes("in JSON at position")) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We had a mix of caught errors being named e and err. Changing this to be consistent

@@ -64,30 +65,23 @@ async function fireFullQuery(
const { type } = await globalThis.sqlparser.normalize(statement);
if (type !== "read" && !(await confirmQuery())) return;

try {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There was a double try/catch block here, where both simply logged the error. Replaced them with a single try/catch

error: function (message: string | unknown) {
console.error(message);
},
};
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the main change. The logger Object is a simple wrapper, which lets the tests spy on it instead of directly on the global console Object.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, such a simple fix!

name.endsWith(tableId) &&
typeof transactionHash === "string" &&
transactionHash.startsWith("0x")
);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using the calledWith sinon assertion was making it hard to determine why a test was failing. I replaced this kind of single assertion with multiple checks, with multiple equal assertions from the node.js core package.
This means when one of the assertions fails the test runner will let you know which assertion it was without needing to add logging or edit the tests.

@joewagner joewagner marked this pull request as ready for review June 8, 2023 16:18
Copy link
Member

@carsonfarmer carsonfarmer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent, nice work!

error: function (message: string | unknown) {
console.error(message);
},
};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, such a simple fix!

@joewagner joewagner merged commit c6ec3c4 into main Jun 8, 2023
@joewagner joewagner deleted the joe/logger branch June 8, 2023 20:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants