Skip to content

Commit d666a5d

Browse files
committed
Test mergeReplaceDirectives
1 parent 1327168 commit d666a5d

File tree

6 files changed

+61
-2
lines changed

6 files changed

+61
-2
lines changed

pkg/commands/internal/builder.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,13 +191,17 @@ func (b Builder) mergeReplaceDirectives(ctx context.Context, pluginPath string)
191191
}
192192

193193
for _, r := range goMod.Replace {
194-
abs := filepath.Join(pluginPath, r.New.Path)
194+
abs, err := filepath.Abs(filepath.Join(pluginPath, r.New.Path))
195+
if err != nil {
196+
return fmt.Errorf("get absolute path: %w", err)
197+
}
198+
195199
stat, err := os.Stat(abs)
196200
if err != nil {
197201
return fmt.Errorf("%s: %w", abs, err)
198202
}
199203
if stat.IsDir() {
200-
r.New.Path = filepath.Join(pluginPath, r.New.Path)
204+
r.New.Path = abs
201205
}
202206

203207
replace := fmt.Sprintf("%s=%s", r.Old.Path, r.New.Path)

pkg/commands/internal/builder_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
package internal
22

33
import (
4+
"encoding/json"
5+
"os"
6+
"os/exec"
7+
"path/filepath"
48
"testing"
59

610
"github.com/stretchr/testify/assert"
11+
"github.com/stretchr/testify/require"
712
)
813

914
func Test_sanitizeVersion(t *testing.T) {
@@ -54,3 +59,39 @@ func Test_sanitizeVersion(t *testing.T) {
5459
})
5560
}
5661
}
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+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module example.com/plugin
2+
3+
go 1.24.0
4+
5+
require example.com/target v0.0.0
6+
7+
replace example.com/target => ./target
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package plugin
2+
3+
import _ "example.com/target"
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module example.com/target
2+
3+
go 1.24.0
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package target

0 commit comments

Comments
 (0)