Skip to content

Commit

Permalink
Use filenames instead of script ID when printing test results
Browse files Browse the repository at this point in the history
  • Loading branch information
m-Peter committed Jan 22, 2024
1 parent 0c3ffce commit f64a073
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
19 changes: 10 additions & 9 deletions test/test_framework_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
`

Expand Down Expand Up @@ -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
Expand All @@ -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) {
Expand Down
17 changes: 13 additions & 4 deletions test/test_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ type TestRunner struct {

func NewTestRunner() *TestRunner {
return &TestRunner{
logger: zerolog.Nop(),
logger: zerolog.Nop(),
contracts: baseContracts(),
}
}
Expand Down Expand Up @@ -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)
}

0 comments on commit f64a073

Please sign in to comment.