Skip to content

Add a parameter to go_test to support a custom TestMain template #3268

@shirchen

Description

@shirchen

We want to opt-in our monorepo into goroutine leak detection and we plan on using uber-go/leak. One option for us is to change template to something like

+.    {{if not .TestMain}}
+	 "go.uber.org/goleak"
+.    {{end}}
 	{{if not .TestMain}}
-	res := m.Run()
+       goleak.VerifyTestMain(m)
 	{{else}}
 	{{.TestMain}}(m)
 	{{/* See golang.org/issue/34129 and golang.org/cl/219639 */}}
 	res := int(reflect.ValueOf(m).Elem().FieldByName("exitCode").Int())
-	{{end}}
 	os.Exit(res)

But then we need to introduce goleak as a dependency to rules_go which may or may not be acceptable. One way we can think of making this optional is to allow us to pass in a custom template that we will have and will give us flexibility. A potential parameter could be

        "test_main_template": attr.label(
            default = "//go/tools/builders/templates:test_main",
            allow_file = True,
        ),

Note, we still need a way to add this dependency (deps = ["@org_uber_go_goleak//:go_default_library"]). One option is to make _testmain_additional_deps public.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions