onflow / cadence

Cadence, the resource-oriented smart contract programming language 🏃‍♂️
https://developers.flow.com/cadence
Apache License 2.0
526 stars 136 forks source link

[Cadence Testing Framework] Pretty Print Error Messages when running tests #3403

Open joshuahannan opened 2 weeks ago

joshuahannan commented 2 weeks ago

Issue to be solved

When running tests with the Cadence testing framework, the errors are spat out as a huge wall of text. It would be great if they were printed in a way that makes them easier to read.

Example:

joshuahannan@Joshuas-MacBook-Pro-2 flow-usdc % flow-c1 test --cover --covercode="contracts" tests/*.cdc
❌ Command Error: Execution failed:
error: assertion failed: given value is: I.Test.Test.Error(message: "[Error Code: 1101] error caused by: 1 error occurred:\n\t* transaction execute failed: [Error Code: 1101] cadence runtime error: Execution failed:\nerror: cannot deploy invalid contract\n --> 751d5fec3fe39dfac9e27973430720b5fcf70588d93503e349d5f4b88f80e0e4:4:16\n  |\n4 |                 signer.contracts.add(name: \"FiatToken\", code: \"696d706f72742043727970746f0a696d706f72742046756e6769626c65546f6b656e2066726f6d203078303030303030303030303030303030320a696d706f72742046756e6769626c65546f6b656e4d6574616461746156696577732066726f6d203078303030303030303030303030303030320a696d706f7274204d6574616461746156696577732066726f6d203078303030303030303030303030303030310a696d706f7274204f6e436861696e4d756c74695369672066726f6d203078303030303030303030303030303030370a696d706f72742049427... \n  |                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n\nerror: resource `FiatToken.Admin` does not conform to resource interface `OnChainMultiSig.PublicSigner`\n   --> 0000000000000007.FiatToken:334:22\n    |\n334 | \taccess(all)\tresource Admin: OnChainMultiSig.PublicSigner, ResourceId, AdminCapReceiver { \n    | \t           \t         ^\n   ... \n    |\n391 | \t\taccess(all) fun UUID(): UInt64{ \n    | \t\t                ---- mismatch here\n   ... \n    |\n395 | \t\taccess(all) fun getTxIndex(): UInt64{ \n    | \t\t                ---------- mismatch here\n\n\n")
  --> test_helpers.cdc:26:4

Suggested Solution

Format the output so that the errors are easy to read and understand

turbolent commented 2 weeks ago

@joshuahannan Can you please share (a link to) the test code you are running?

turbolent commented 2 weeks ago

Test already has a fun assertError(_ result: {Result}, errorMessage: String), maybe we should add something like:

fun assertNoError(_ result: {Result}) {
    if let error = result.error {
        assert(false, message: "error: ".concat(error.message))
    }
}
turbolent commented 2 weeks ago

cc @m-Peter

m-Peter commented 1 week ago

Test already has a fun assertError(_ result: {Result}, errorMessage: String), maybe we should add something like:

fun assertNoError(_ result: {Result}) {
    if let error = result.error {
        assert(false, message: "error: ".concat(error.message))
    }
}

@turbolent That's a neat idea :raised_hands:

But the issue being raised here by Joshua, is something related to the printing of a Cadence interpreter error:

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
\n\nerror: resource `FiatToken.Admin` does not conform to resource interface `OnChainMultiSig.PublicSigner`
\n   --> 0000000000000007.FiatToken:334:22
\n    |
\n334 | \taccess(all)\tresource Admin: OnChainMultiSig.PublicSigner, ResourceId, AdminCapReceiver {
\n    | \t           \t         ^\n   ... \n    |\n391 | \t\taccess(all) fun UUID(): UInt64{
\n    | \t\t                ---- mismatch here
\n   ... \n    |\n395 | \t\taccess(all) fun getTxIndex(): UInt64{
\n    | \t\t                ---------- mismatch here
\n\n\n")

I will check to see why it happens. Most likely something in the flow test command, or something related.