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

[test] Use filenames instead of script ID when printing test results #272

Merged
merged 1 commit into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

At this point, can err be of a different type than 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)
}
Loading