foundry icon indicating copy to clipboard operation
foundry copied to clipboard

Include decoded logs in `forge test --json`

Open Willyham opened this issue 2 years ago • 7 comments

Component

Forge

Describe the feature you would like

forge test --json currently includes raw logging information in its output, e.g:

        "logs": [
          {
            "address": "0x0000000000000000000000000000000000000000",
            "topics": [
              "0x41304facd9323d75b11bcdd609cb38effffdb05710f7caf0e9b16c6d9d709f50"
            ],
            "data": "0x000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000033132330000000000000000000000000000000000000000000000000000000000"
          }
        ],

It would be nice if it could also include the decoded/interpreted logs, as commands consuming this json are not likely to have the ability to decode these logs.

Additional context

This is to support parsing test output information for inclusion in an editor plugin which allows forge tests to be run continuously when developing.

Willyham avatar Aug 29 '22 10:08 Willyham

Is this feasible, and if so, does the forge team agree that it would be useful? Trying to figure out if I should go down another path for testing results rather than using the json :)

Willyham avatar Sep 04 '22 15:09 Willyham

I can't think of any downside here, since this is just additional info

should be easy to add

mattsse avatar Sep 06 '22 17:09 mattsse

I will like to work on this one @mattsse as my first foundry contribution please. :grin:

@Willyham Can you give an example of the json output you want ?

The-Wary-One avatar Sep 15 '22 13:09 The-Wary-One

Good question, I hadn't really thought of that. Possibly it should just be an extra field of logLines or decodedLogs or something in the existing output?

The existing non-json command simply prints the logs for each test, so I don't think there's any more structured data available that we'd want anyway?

Willyham avatar Sep 15 '22 15:09 Willyham

Existing format looks like this:

{
  "test/Contract.t.sol:ContractTest": {
    "duration": {
      "secs": 0,
      "nanos": 287125
    },
    "test_results": {
      "testComplexFunc()": {
        "success": true,
        "reason": null,
        "counterexample": null,
        "logs": [
          {
            "address": "0x0000000000000000000000000000000000000000",
            "topics": [
              "0x41304facd9323d75b11bcdd609cb38effffdb05710f7caf0e9b16c6d9d709f50"
            ],
            "data": "0x00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000011546869732069732061206c6f6720313030000000000000000000000000000000"
          }
        ],
        "kind": {
          "Standard": 8602
        },
        "traces": [],
        "labeled_addresses": {}
      },
      "testComplexFuncAgain()": {
        "success": true,
        "reason": null,
        "counterexample": null,
        "logs": [
          {
            "address": "0x0000000000000000000000000000000000000000",
            "topics": [
              "0x41304facd9323d75b11bcdd609cb38effffdb05710f7caf0e9b16c6d9d709f50"
            ],
            "data": "0x00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000012546869732069732061206c6f6720313030300000000000000000000000000000"
          }
        ],
        "kind": {
          "Standard": 8636
        },
        "traces": [],
        "labeled_addresses": {}
      }
    },
    "warnings": []
  }
}

Maybe a decoded (or should it be deserialized?) property within the logs array?

Willyham avatar Sep 15 '22 17:09 Willyham

What should this property look like really ? Do you want to decode the event data or its hash ? :grin:

The-Wary-One avatar Sep 15 '22 17:09 The-Wary-One

We want the same log data which is printed by the CLI. In the above case the logs were generated by calling console.log from solidity. We need to turn the encoded data into a UTF-8 string (I'm assuming it's utf8 encoded).

Example of print from the CLI:

[PASS] testComplexFunc() (gas: 8602)
Logs:
  This is a log

The string "this is a log" should be included for this test in the json output. Note that there may but multiple logs per test, and each should be distinct (so we probably want an array of decoded logs)

Willyham avatar Sep 15 '22 18:09 Willyham

Sorry for the delay @Willyham ! :sweat: A decoded_logs field will be available on the tests json output once the #3543 PR is merged.

The-Wary-One avatar Oct 25 '22 09:10 The-Wary-One