-
-
Notifications
You must be signed in to change notification settings - Fork 50
/
runner_test.go
204 lines (170 loc) · 5.17 KB
/
runner_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
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package goverter
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"
"sort"
"strings"
"testing"
"github.com/jmattheis/goverter/config"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v3"
)
var (
UpdateScenario = os.Getenv("UPDATE_SCENARIO") == "true"
SkipVersionDependent = os.Getenv("SKIP_VERSION_DEPENDENT") == "true"
NoParallel = os.Getenv("NO_PARALLEL") == "true"
)
func TestScenario(t *testing.T) {
rootDir := getCurrentPath()
scenarioDir := filepath.Join(rootDir, "scenario")
workDir := filepath.Join(rootDir, "execution")
scenarioFiles, err := os.ReadDir(scenarioDir)
require.NoError(t, err)
require.NoError(t, clearDir(workDir))
for _, file := range scenarioFiles {
require.False(t, file.IsDir(), "should not be a directory")
file := file
testName := strings.TrimSuffix(file.Name(), filepath.Ext(file.Name()))
t.Run(testName, func(t *testing.T) {
if !NoParallel {
t.Parallel()
}
testWorkDir := filepath.Join(workDir, testName)
require.NoError(t, os.MkdirAll(testWorkDir, 0o755))
require.NoError(t, clearDir(testWorkDir))
scenarioFilePath := filepath.Join(scenarioDir, file.Name())
scenarioFileBytes, err := os.ReadFile(scenarioFilePath)
require.NoError(t, err)
scenario := Scenario{}
err = yaml.Unmarshal(scenarioFileBytes, &scenario)
require.NoError(t, err)
if SkipVersionDependent && scenario.VersionDependent {
t.SkipNow()
return
}
err = os.WriteFile(filepath.Join(testWorkDir, "go.mod"), []byte("module github.com/jmattheis/goverter/execution\ngo 1.18"), 0o644)
require.NoError(t, err)
for name, content := range scenario.Input {
inPath := filepath.Join(testWorkDir, name)
err = os.MkdirAll(filepath.Dir(inPath), 0o755)
require.NoError(t, err)
err = os.WriteFile(filepath.Join(testWorkDir, name), []byte(content), 0o644)
require.NoError(t, err)
}
patterns := scenario.Patterns
if len(patterns) == 0 {
patterns = append(patterns, "github.com/jmattheis/goverter/execution")
}
files, err := generateConvertersRaw(
&GenerateConfig{
WorkingDir: testWorkDir,
PackagePatterns: patterns,
OutputBuildConstraint: scenario.BuildConstraint,
BuildTags: "goverter",
Global: config.RawLines{
Lines: scenario.Global,
Location: "scenario global",
},
})
actualOutputFiles := toOutputFiles(testWorkDir, files)
if UpdateScenario {
if err != nil {
scenario.Success = []*OutputFile{}
scenario.Error = replaceAbsolutePath(testWorkDir, fmt.Sprint(err))
} else {
scenario.Success = toOutputFiles(testWorkDir, files)
scenario.Error = ""
}
newBytes, err := yaml.Marshal(&scenario)
if assert.NoError(t, err) {
os.WriteFile(scenarioFilePath, newBytes, 0o644)
}
}
if scenario.Error != "" {
require.Error(t, err)
require.Equal(t, scenario.Error, replaceAbsolutePath(testWorkDir, fmt.Sprint(err)))
return
}
require.NoError(t, err)
require.NotEmpty(t, scenario.Success, "scenario.Success may not be empty")
require.Equal(t, scenario.Success, actualOutputFiles)
err = writeFiles(files)
require.NoError(t, err)
require.NoError(t, compile(testWorkDir), "generated converter doesn't build")
})
}
}
func replaceAbsolutePath(curPath, body string) string {
return strings.ReplaceAll(body, curPath, "@workdir")
}
func compile(dir string) error {
cmd := exec.Command("go", "build", "./...")
cmd.Dir = dir
_, err := cmd.Output()
if err != nil {
if exit, ok := err.(*exec.ExitError); ok {
return fmt.Errorf("Process exited with %d:\n%s", exit.ExitCode(), string(exit.Stderr))
}
}
return err
}
func toOutputFiles(execDir string, files map[string][]byte) []*OutputFile {
output := []*OutputFile{}
for fileName, content := range files {
rel, err := filepath.Rel(execDir, fileName)
if err != nil {
panic("could not create relpath")
}
output = append(output, &OutputFile{Name: rel, Content: string(content)})
}
sort.Slice(output, func(i, j int) bool {
return output[i].Name < output[j].Name
})
return output
}
type Scenario struct {
VersionDependent bool `yaml:"version_dependent,omitempty"`
Input map[string]string `yaml:"input"`
Global []string `yaml:"global,omitempty"`
BuildConstraint string `yaml:"build_constraint,omitempty"`
Patterns []string `yaml:"patterns,omitempty"`
Success []*OutputFile `yaml:"success,omitempty"`
Error string `yaml:"error,omitempty"`
}
type OutputFile struct {
Name string
Content string
}
func (f *OutputFile) MarshalYAML() (interface{}, error) {
return map[string]string{f.Name: f.Content}, nil
}
func (f *OutputFile) UnmarshalYAML(value *yaml.Node) error {
v := map[string]string{}
err := value.Decode(&v)
for name, content := range v {
f.Name = name
f.Content = content
}
return err
}
func getCurrentPath() string {
_, filename, _, _ := runtime.Caller(1)
return filepath.Dir(filename)
}
func clearDir(dir string) error {
files, err := filepath.Glob(filepath.Join(dir, "*"))
if err != nil {
return err
}
for _, file := range files {
err = os.RemoveAll(file)
if err != nil {
return err
}
}
return nil
}