Skip to content

Commit 63cdc08

Browse files
committed
test setupPhase.addDeps
1 parent a880790 commit 63cdc08

File tree

4 files changed

+173
-3
lines changed

4 files changed

+173
-3
lines changed

tool/internal/setup/add.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ package setup
55

66
import (
77
"fmt"
8+
"maps"
89
"path/filepath"
10+
"slices"
911

1012
"github.com/dave/dst"
1113

@@ -28,9 +30,10 @@ func genImportDecl(matched []*rule.InstFuncRule) []dst.Decl {
2830
for _, m := range matched {
2931
requiredImports[m.Path] = ast.IdentIgnore
3032
}
31-
importDecls := make([]dst.Decl, 0)
32-
for k, v := range requiredImports {
33-
importDecls = append(importDecls, ast.ImportDecl(v, k))
33+
importDecls := make([]dst.Decl, 0, len(requiredImports))
34+
// Sort the keys to ensure deterministic order
35+
for _, k := range slices.Sorted(maps.Keys(requiredImports)) {
36+
importDecls = append(importDecls, ast.ImportDecl(requiredImports[k], k))
3437
}
3538
return importDecls
3639
}

tool/internal/setup/add_test.go

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
// Package setup tests verify that the addDeps function generates
5+
// the expected otel.runtime.go file by comparing against golden files.
6+
//
7+
// To update golden files after intentional changes:
8+
//
9+
// go test -update ./tool/internal/setup/...
10+
11+
package setup
12+
13+
import (
14+
"io"
15+
"log/slog"
16+
"os"
17+
"path/filepath"
18+
"testing"
19+
20+
"github.com/open-telemetry/opentelemetry-go-compile-instrumentation/tool/internal/rule"
21+
"github.com/stretchr/testify/assert"
22+
"github.com/stretchr/testify/require"
23+
"gotest.tools/v3/golden"
24+
)
25+
26+
func TestAddDeps(t *testing.T) {
27+
tests := []struct {
28+
name string
29+
matched []*rule.InstRuleSet
30+
goldenFile string // Empty means no file should be generated
31+
}{
32+
{
33+
name: "empty_matched_rules",
34+
matched: []*rule.InstRuleSet{},
35+
goldenFile: "",
36+
},
37+
{
38+
name: "single_func_rule",
39+
matched: []*rule.InstRuleSet{
40+
newTestRuleSet(
41+
"github.com/example/pkg",
42+
newTestFuncRule("github.com/example/pkg", "github.com/example/pkg"),
43+
),
44+
},
45+
goldenFile: "single_func_rule.otel.runtime.go.golden",
46+
},
47+
{
48+
name: "no_func_rules",
49+
matched: []*rule.InstRuleSet{
50+
newTestRuleSet("github.com/example/pkg"),
51+
},
52+
goldenFile: "",
53+
},
54+
{
55+
name: "multiple_rule_sets",
56+
matched: []*rule.InstRuleSet{
57+
newTestRuleSet(
58+
"github.com/example/pkg1",
59+
newTestFuncRule("github.com/example/pkg1", "github.com/example/pkg1"),
60+
),
61+
newTestRuleSet(
62+
"github.com/example/pkg2",
63+
newTestFuncRule("github.com/example/pkg2", "github.com/example/pkg2"),
64+
),
65+
},
66+
goldenFile: "multiple_rule_sets.otel.runtime.go.golden",
67+
},
68+
}
69+
70+
for _, tt := range tests {
71+
t.Run(tt.name, func(t *testing.T) {
72+
tmpDir := t.TempDir()
73+
sp := newTestSetupPhase()
74+
75+
err := sp.addDeps(tt.matched, tmpDir)
76+
require.NoError(t, err)
77+
78+
runtimeFilePath := filepath.Join(tmpDir, OtelRuntimeFile)
79+
80+
if tt.goldenFile == "" {
81+
assert.NoFileExists(t, runtimeFilePath)
82+
return
83+
}
84+
85+
assert.FileExists(t, runtimeFilePath)
86+
actual, err := os.ReadFile(runtimeFilePath)
87+
require.NoError(t, err)
88+
89+
golden.Assert(t, string(actual), tt.goldenFile)
90+
})
91+
}
92+
}
93+
94+
func TestAddDeps_FileWriteError(t *testing.T) {
95+
matched := []*rule.InstRuleSet{
96+
newTestRuleSet(
97+
"github.com/example/pkg",
98+
newTestFuncRule("github.com/example/pkg", "github.com/example/pkg"),
99+
),
100+
}
101+
102+
// Use a non-existent parent directory to cause write error
103+
invalidPath := filepath.Join(t.TempDir(), "nonexistent", "subdir")
104+
sp := newTestSetupPhase()
105+
106+
err := sp.addDeps(matched, invalidPath)
107+
assert.Error(t, err)
108+
}
109+
110+
// Helper functions for constructing test data
111+
112+
func newTestSetupPhase() *SetupPhase {
113+
return &SetupPhase{
114+
logger: slog.New(slog.NewTextHandler(io.Discard, nil)),
115+
}
116+
}
117+
118+
func newTestFuncRule(path, target string) *rule.InstFuncRule {
119+
return &rule.InstFuncRule{
120+
InstBaseRule: rule.InstBaseRule{
121+
Target: target,
122+
},
123+
Path: path,
124+
}
125+
}
126+
127+
func newTestRuleSet(modulePath string, funcRules ...*rule.InstFuncRule) *rule.InstRuleSet {
128+
rs := rule.NewInstRuleSet(modulePath)
129+
for _, fr := range funcRules {
130+
rs.AddFuncRule("/path/to/file.go", fr)
131+
}
132+
return rs
133+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// This file is generated by the opentelemetry-go-compile-instrumentation tool. DO NOT EDIT.
2+
package main
3+
4+
import _ "github.com/example/pkg"
5+
import _ "github.com/example/pkg1"
6+
import _ "github.com/example/pkg2"
7+
import _otel_log "log"
8+
import _otel_debug "runtime/debug"
9+
import _ "unsafe"
10+
11+
//go:linkname _getstatck0 github.com/example/pkg1.OtelGetStackImpl
12+
var _getstatck0 = _otel_debug.Stack
13+
14+
//go:linkname _printstack0 github.com/example/pkg1.OtelPrintStackImpl
15+
var _printstack0 = func(bt []byte) { _otel_log.Printf(string(bt)) }
16+
17+
//go:linkname _getstatck1 github.com/example/pkg2.OtelGetStackImpl
18+
var _getstatck1 = _otel_debug.Stack
19+
20+
//go:linkname _printstack1 github.com/example/pkg2.OtelPrintStackImpl
21+
var _printstack1 = func(bt []byte) { _otel_log.Printf(string(bt)) }
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// This file is generated by the opentelemetry-go-compile-instrumentation tool. DO NOT EDIT.
2+
package main
3+
4+
import _ "github.com/example/pkg"
5+
import _otel_log "log"
6+
import _otel_debug "runtime/debug"
7+
import _ "unsafe"
8+
9+
//go:linkname _getstatck0 github.com/example/pkg.OtelGetStackImpl
10+
var _getstatck0 = _otel_debug.Stack
11+
12+
//go:linkname _printstack0 github.com/example/pkg.OtelPrintStackImpl
13+
var _printstack0 = func(bt []byte) { _otel_log.Printf(string(bt)) }

0 commit comments

Comments
 (0)