-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherrstack_test.go
95 lines (75 loc) · 2.45 KB
/
errstack_test.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
package errstack_test
import (
"os"
"path"
"path/filepath"
"testing"
"github.com/AdamBrianBright/errstack/internal/config"
"github.com/AdamBrianBright/errstack/internal/helpers"
"github.com/AdamBrianBright/errstack/internal/passes/errstack"
"github.com/stretchr/testify/require"
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/analysistest"
_ "golang.org/x/tools/go/analysis/passes/ctrlflow"
)
func TestAnalyzer(t *testing.T) {
// Load the dirs under ./testdata
testdata := analysistest.TestData()
t.Chdir(testdata + "/src")
files, err := os.ReadDir(testdata + "/src")
require.NoError(t, err)
_ = config.Analyzer.Flags.Set(config.Debug, "true")
t.Cleanup(func() {
_ = config.Analyzer.Flags.Set(config.Debug, "")
})
for _, f := range files {
if !f.IsDir() || f.Name() == "vendor" {
continue
}
t.Run(f.Name(), func(t *testing.T) {
dirPath, err := filepath.Abs(path.Join(testdata, "./src", f.Name()))
require.NoError(t, err)
configPath := path.Join(dirPath, ".errstack.yaml")
_, err = os.Stat(configPath)
if err == nil {
// A config file exists, use it
configFile, err := os.ReadFile(configPath)
require.NoError(t, err)
err = config.Analyzer.Flags.Set(config.YamlConfig, string(configFile))
require.NoError(t, err)
} else if !os.IsNotExist(err) {
require.FailNow(t, err.Error())
}
r := analysistest.Run(t, testdata, errstack.Analyzer, f.Name())
res := r[0].Result
result := res.(*helpers.Result[*errstack.Result])
require.NoError(t, result.Err)
})
}
}
func TestConfig(t *testing.T) {
pass := &analysis.Pass{Analyzer: config.Analyzer}
res, err := config.Analyzer.Run(pass)
require.NoError(t, err)
_ = config.Analyzer.Flags.Set(config.Debug, "true")
t.Cleanup(func() {
_ = config.Analyzer.Flags.Set(config.Debug, "")
})
result := res.(*helpers.Result[*config.Config])
require.NoError(t, result.Err)
conf := result.Res
require.ElementsMatch(t, conf.WrapperFunctions, config.DefaultWrapperFunctions)
require.ElementsMatch(t, conf.CleanFunctions, config.DefaultCleanFunctions)
}
func TestSingle(t *testing.T) {
testdata := analysistest.TestData()
t.Chdir(testdata + "/src")
_ = config.Analyzer.Flags.Set(config.Debug, "true")
t.Cleanup(func() {
_ = config.Analyzer.Flags.Set(config.Debug, "")
})
r := analysistest.Run(t, testdata, errstack.Analyzer, "cfgs_branches")
res := r[0].Result
result := res.(*helpers.Result[*errstack.Result])
require.NoError(t, result.Err)
}