|
| 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 | +} |
0 commit comments