From f64a0739b339b73ab1885153126f24eddab9e9e4 Mon Sep 17 00:00:00 2001 From: Ardit Marku Date: Mon, 22 Jan 2024 10:48:38 +0200 Subject: [PATCH] Use filenames instead of script ID when printing test results --- test/test_framework_test.go | 19 ++++++++++--------- test/test_runner.go | 17 +++++++++++++---- 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/test/test_framework_test.go b/test/test_framework_test.go index d02ad7d2..602fa073 100644 --- a/test/test_framework_test.go +++ b/test/test_framework_test.go @@ -2164,20 +2164,20 @@ func TestPrettyPrintTestResults(t *testing.T) { results, err := runner.RunTests(code) require.NoError(t, err) - resultsStr := PrettyPrintResults(results, "test_script.cdc") + resultsStr := PrettyPrintResults(results, "tests/test_script.cdc") - const expected = `Test results: "test_script.cdc" + const expected = `Test results: "tests/test_script.cdc" - PASS: testFunc1 - FAIL: testFunc2 Execution failed: error: assertion failed: unexpected error occurred - --> 7465737400000000000000000000000000000000000000000000000000000000:9:12 + --> tests/test_script.cdc:9:12 - PASS: testFunc3 - FAIL: testFunc4 Execution failed: error: panic: runtime error - --> 7465737400000000000000000000000000000000000000000000000000000000:17:12 + --> tests/test_script.cdc:17:12 ` @@ -4831,9 +4831,10 @@ func TestWithLogger(t *testing.T) { t.Parallel() const code = ` - access(all) fun testWithLogger() { - log("Hello, world!") - } + access(all) + fun testWithLogger() { + log("Hello, world!") + } ` var buf bytes.Buffer @@ -4845,8 +4846,8 @@ func TestWithLogger(t *testing.T) { require.NoError(t, err) require.NoError(t, result.Error) - expectedPattern := `{"level":"info","time":"[0-9TZ:.-]+","message":"\\u001b\[1;34mLOG:\\u001b\[0m \\"Hello, world!\\""}` - assert.Regexp(t, expectedPattern, buf.String()) + assert.Contains(t, buf.String(), "Hello, world!") + assert.Equal(t, []string{"Hello, world!"}, runner.Logs()) } func TestGetEventsFromIntegrationTests(t *testing.T) { diff --git a/test/test_runner.go b/test/test_runner.go index 1fbd5503..9f266c64 100644 --- a/test/test_runner.go +++ b/test/test_runner.go @@ -148,7 +148,7 @@ type TestRunner struct { func NewTestRunner() *TestRunner { return &TestRunner{ - logger: zerolog.Nop(), + logger: zerolog.Nop(), contracts: baseContracts(), } } @@ -821,19 +821,28 @@ func PrettyPrintResults(results Results, scriptPath string) string { var sb strings.Builder fmt.Fprintf(&sb, "Test results: %q\n", scriptPath) for _, result := range results { - sb.WriteString(PrettyPrintResult(result.TestName, result.Error)) + sb.WriteString(PrettyPrintResult(scriptPath, result.TestName, result.Error)) sb.WriteRune('\n') } return sb.String() } -func PrettyPrintResult(funcName string, err error) string { +func PrettyPrintResult(scriptPath, funcName string, err error) string { if err == nil { return fmt.Sprintf("- PASS: %s", funcName) } + interErr := err.(interpreter.Error) + + // Replace script ID with actual file path + errString := strings.ReplaceAll( + err.Error(), + interErr.Location.String(), + scriptPath, + ) + // Indent the error messages - errString := strings.ReplaceAll(err.Error(), "\n", "\n\t\t\t") + errString = strings.ReplaceAll(errString, "\n", "\n\t\t\t") return fmt.Sprintf("- FAIL: %s\n\t\t%s", funcName, errString) }