forked from golangci/golangci-lint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.go
121 lines (90 loc) · 2.34 KB
/
install.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package testshared
import (
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"strconv"
"sync"
"testing"
"github.com/gofrs/flock"
"github.com/stretchr/testify/require"
)
// value: "true"
const envGolangciLintInstalled = "GOLANGCI_LINT_INSTALLED"
// Reduce the number of builds.
// The majority of tests are NOT executed inside the same process,
// then this is just to limit some cases (~60 cases).
var (
builtLock sync.RWMutex
built bool
)
func InstallGolangciLint(tb testing.TB) string {
tb.Helper()
parentPath := findMakefile(tb)
// Avoids concurrent builds and copies (before the end of the build).
f := flock.New(filepath.Join(parentPath, "test.lock"))
if ok, _ := strconv.ParseBool(os.Getenv(envGolangciLintInstalled)); !ok {
err := f.Lock()
require.NoError(tb, err)
defer func() {
errU := f.Unlock()
if errU != nil {
tb.Logf("Can't unlock test.lock: %v", errU)
}
}()
builtLock.Lock()
defer builtLock.Unlock()
if !built {
cmd := exec.Command("make", "-C", parentPath, "build")
output, err := cmd.CombinedOutput()
require.NoError(tb, err, "can't install golangci-lint %s", string(output))
built = true
}
}
// Allow tests to avoid edge-cases with concurrent runs.
binPath := filepath.Join(tb.TempDir(), binaryName)
err := copyFile(filepath.Join(parentPath, binaryName), binPath)
require.NoError(tb, err)
abs, err := filepath.Abs(binPath)
require.NoError(tb, err)
return abs
}
func findMakefile(tb testing.TB) string {
tb.Helper()
wd, _ := os.Getwd()
for wd != "/" {
_, err := os.Stat(filepath.Join(wd, "Makefile"))
if err != nil {
wd = filepath.Dir(wd)
continue
}
break
}
here, _ := os.Getwd()
rel, err := filepath.Rel(here, wd)
require.NoError(tb, err)
return rel
}
func copyFile(src, dst string) error {
source, err := os.Open(src)
if err != nil {
return fmt.Errorf("open file %s: %w", src, err)
}
defer func() { _ = source.Close() }()
info, err := source.Stat()
if err != nil {
return fmt.Errorf("file %s not found: %w", src, err)
}
destination, err := os.OpenFile(dst, os.O_RDWR|os.O_CREATE|os.O_TRUNC, info.Mode())
if err != nil {
return fmt.Errorf("create file %s: %w", dst, err)
}
defer func() { _ = destination.Close() }()
_, err = io.Copy(destination, source)
if err != nil {
return fmt.Errorf("copy file %s to %s: %w", src, dst, err)
}
return nil
}