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

feat: new test-main join point #394

Merged
merged 9 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion docs/layouts/shortcodes/menu.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
{{ .Title }}
</span>
<div class="hextra-card-subtitle hx-line-clamp-3 hx-text-sm hx-font-normal hx-text-gray-500 dark:hx-text-gray-400 hx-px-4 hx-mb-4 hx-mt-2">
{{ or .Params.subtitle .Summary }}
{{ or .Params.subtitle .Summary | plainify }}
</div>
</a>
{{ end }}
Expand Down
12 changes: 12 additions & 0 deletions docs/static/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,7 @@
{ "$ref": "#/$defs/join-point/package-name" },
{ "$ref": "#/$defs/join-point/struct-definition" },
{ "$ref": "#/$defs/join-point/struct-literal" },
{ "$ref": "#/$defs/join-point/test-main" },
{ "$ref": "#/$defs/join-point/value-declaration" }
]
},
Expand Down Expand Up @@ -672,6 +673,17 @@
}
]
},
"test-main": {
"required": ["test-main"],
"unevaluatedProperties": false,
"properties": {
"test-main": {
"title": "Synthetic test main package",
"markdownDescription": "The `test-main` join point can be used to only (or never) match nodes included in the synthetic main package generated by `go test`.",
"type": "boolean"
}
}
},
"value-declaration": {
"required": ["value-declaration"],
"unevaluatedProperties": false,
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/toolexec.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ var Toolexec = &cli.Command{
pin.AutoPinOrchestrion()

if proxyCmd.ShowVersion() {
log.Tracef("Toolexec version command: %q\n", proxyCmd)
log.Tracef("Toolexec version command: %q\n", proxyCmd.Args())
fullVersion, err := toolexec.ComputeVersion(proxyCmd)
if err != nil {
return err
Expand Down
4 changes: 4 additions & 0 deletions internal/injector/aspect/advice/code/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ func (m mockAdviceContext) Package() string {
assert.FailNow(m.t, "unexpected method call")
return ""
}
func (m mockAdviceContext) TestMain() bool {
assert.FailNow(m.t, "unexpected method call")
return false
}
func (m mockAdviceContext) Child(dst.Node, string, int) context.AdviceContext {
assert.FailNow(m.t, "unexpected method call")
return nil
Expand Down
12 changes: 12 additions & 0 deletions internal/injector/aspect/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ type AspectContext interface {
// Package returns the name of the package containing this node.
Package() string

// TestMain returns true if the current node is in a synthetic main package.
TestMain() bool

// Release returns this context to the memory pool so that it can be reused
// later.
Release()
Expand Down Expand Up @@ -81,6 +84,7 @@ type (
minGoLang *GoLangVersion
sourceParser SourceParser
importPath string
testMain bool
}

SourceParser interface {
Expand All @@ -106,6 +110,8 @@ type ContextArgs struct {
// MinGoLang is a pointer to the result value containing the minimum Go
// language level required by the compile unit after it has been modified.
MinGoLang *GoLangVersion
// TestMain is true when injecting into a synthetic main package.
TestMain bool
}

// Context returns a new [*context] instance that represents the ndoe at the
Expand All @@ -122,6 +128,7 @@ func (n *NodeChain) Context(args ContextArgs) *context {
minGoLang: args.MinGoLang,
sourceParser: args.SourceParser,
importPath: args.ImportPath,
testMain: args.TestMain,
}

return c
Expand Down Expand Up @@ -155,6 +162,7 @@ func (c *context) Child(node dst.Node, property string, index int) AdviceContext
minGoLang: c.minGoLang,
sourceParser: c.sourceParser,
importPath: c.importPath,
testMain: c.testMain,
}

return r
Expand Down Expand Up @@ -208,6 +216,10 @@ func (c *context) Package() string {
return c.file.Name.Name
}

func (c *context) TestMain() bool {
return c.testMain
}

func (c *context) ParseSource(bytes []byte) (*dst.File, error) {
return c.sourceParser.Parse(bytes)
}
Expand Down
42 changes: 42 additions & 0 deletions internal/injector/aspect/join/testmain.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2023-present Datadog, Inc.

package join

import (
"github.com/DataDog/orchestrion/internal/injector/aspect/context"
"github.com/dave/jennifer/jen"
"gopkg.in/yaml.v3"
)

type testMain bool

// TestMain matches only nodes in ASTs in files that either are (if true), or
// are not (if false) part of a synthetic test main package.
func TestMain(v bool) testMain {
return testMain(v)
}

func (t testMain) Matches(ctx context.AspectContext) bool {
return ctx.TestMain() == bool(t)
}

func (testMain) ImpliesImported() []string {
return nil
}

func (t testMain) AsCode() jen.Code {
return jen.Qual(pkgPath, "TestMain").Call(jen.Lit(bool(t)))
}

func init() {
unmarshalers["test-main"] = func(node *yaml.Node) (Point, error) {
var val bool
if err := node.Decode(&val); err != nil {
return nil, err
}

Check warning on line 39 in internal/injector/aspect/join/testmain.go

View check run for this annotation

Codecov / codecov/patch

internal/injector/aspect/join/testmain.go#L38-L39

Added lines #L38 - L39 were not covered by tests
return TestMain(val), nil
}
}
35 changes: 25 additions & 10 deletions internal/injector/builtin/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion internal/injector/builtin/generated_deps.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions internal/injector/builtin/generator/doc.join.test-main.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<div class="flex join-point test-main">
<span class="type">Synthetic <tt>main</tt> package</span>
<span class="value">
{{- if . -}}
Yes
{{- else -}}
No
{{- end -}}
</span>
</div>
18 changes: 2 additions & 16 deletions internal/injector/builtin/testdata/client/main.go.snap

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 2 additions & 16 deletions internal/injector/builtin/testdata/server/main.go.snap

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions internal/injector/builtin/yaml/civisibility/testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,16 @@ aspects:
template: |-
//go:linkname __dd_civisibility_instrumentTestingTFunc gopkg.in/DataDog/dd-trace-go.v1/internal/civisibility/integrations/gotesting.instrumentTestingTFunc
func __dd_civisibility_instrumentTestingTFunc(func(*T)) func(*T)

//go:linkname __dd_civisibility_instrumentSetErrorInfo gopkg.in/DataDog/dd-trace-go.v1/internal/civisibility/integrations/gotesting.instrumentSetErrorInfo
func __dd_civisibility_instrumentSetErrorInfo(tb TB, errType string, errMessage string, skip int)

//go:linkname __dd_civisibility_instrumentCloseAndSkip gopkg.in/DataDog/dd-trace-go.v1/internal/civisibility/integrations/gotesting.instrumentCloseAndSkip
func __dd_civisibility_instrumentCloseAndSkip(tb TB, skipReason string)

//go:linkname __dd_civisibility_instrumentSkipNow gopkg.in/DataDog/dd-trace-go.v1/internal/civisibility/integrations/gotesting.instrumentSkipNow
func __dd_civisibility_instrumentSkipNow(tb TB)

//go:linkname __dd_civisibility_ExitCiVisibility gopkg.in/DataDog/dd-trace-go.v1/internal/civisibility/integrations.ExitCiVisibility
func __dd_civisibility_ExitCiVisibility()

Expand Down
Loading