|
1 | 1 | package internal
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "encoding/json" |
| 5 | + "os" |
| 6 | + "os/exec" |
| 7 | + "path/filepath" |
4 | 8 | "testing"
|
5 | 9 |
|
6 | 10 | "github.com/stretchr/testify/assert"
|
| 11 | + "github.com/stretchr/testify/require" |
7 | 12 | )
|
8 | 13 |
|
9 | 14 | func Test_sanitizeVersion(t *testing.T) {
|
@@ -54,3 +59,39 @@ func Test_sanitizeVersion(t *testing.T) {
|
54 | 59 | })
|
55 | 60 | }
|
56 | 61 | }
|
| 62 | + |
| 63 | +func TestMergeReplaceDirectives(t *testing.T) { |
| 64 | + t.Parallel() |
| 65 | + |
| 66 | + // Create a temporary module structure: |
| 67 | + // tmp/ |
| 68 | + // go.mod |
| 69 | + // golangci-lint/ |
| 70 | + tmp := t.TempDir() |
| 71 | + require.NoError(t, os.WriteFile(filepath.Join(tmp, "go.mod"), []byte(` |
| 72 | +module github.com/golangci/golangci-lint/v2 |
| 73 | +go 1.24.0 |
| 74 | +`), 0o644)) |
| 75 | + require.NoError(t, os.Mkdir(filepath.Join(tmp, "golangci-lint"), 0o755)) |
| 76 | + |
| 77 | + b := NewBuilder(nil, &Configuration{Plugins: []*Plugin{ |
| 78 | + {Module: "example.com/plugin", Path: "testdata/plugin"}, |
| 79 | + }}, tmp) |
| 80 | + |
| 81 | + err := b.mergeReplaceDirectives(t.Context(), filepath.Join("testdata", "plugin")) |
| 82 | + require.NoError(t, err) |
| 83 | + |
| 84 | + cmd := exec.Command("go", "mod", "edit", "-json") |
| 85 | + cmd.Dir = b.repo |
| 86 | + output, err := cmd.CombinedOutput() |
| 87 | + require.NoError(t, err) |
| 88 | + |
| 89 | + var goMod struct { |
| 90 | + Replace []struct{ New struct{ Path string } } |
| 91 | + } |
| 92 | + err = json.Unmarshal(output, &goMod) |
| 93 | + require.NoError(t, err) |
| 94 | + |
| 95 | + require.Len(t, goMod.Replace, 1) |
| 96 | + assert.Contains(t, goMod.Replace[0].New.Path, "testdata/plugin/target") |
| 97 | +} |
0 commit comments