forked from jmattheis/goverter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrunner_test.go
152 lines (130 loc) · 4.27 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
package goverter
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v3"
)
func TestScenario(t *testing.T) {
curPath := getCurrentPath()
scenarios := filepath.Join(curPath, "scenario")
execDir := filepath.Join(curPath, "execution")
files, err := ioutil.ReadDir(scenarios)
require.NoError(t, err)
require.NoError(t, os.MkdirAll(execDir, os.ModePerm))
require.NoError(t, clearDir(execDir))
for _, file := range files {
require.False(t, file.IsDir(), "should not be a directory")
t.Run(file.Name(), func(t *testing.T) {
scenarioFileName := filepath.Join(scenarios, file.Name())
scenarioBytes, err := ioutil.ReadFile(scenarioFileName)
require.NoError(t, err)
scenario := Scenario{}
err = yaml.Unmarshal(scenarioBytes, &scenario)
require.NoError(t, err)
for name, content := range scenario.Input {
inPath := filepath.Join(execDir, name)
err = os.MkdirAll(filepath.Dir(inPath), os.ModePerm)
require.NoError(t, err)
err = os.WriteFile(filepath.Join(execDir, name), []byte(content), os.ModePerm)
require.NoError(t, err)
}
genPkgName := "generated"
genFile := filepath.Join(execDir, genPkgName, "generated.go")
err = GenerateConverterFile(
genFile,
GenerateConfig{
PackageName: genPkgName,
WorkingDir: execDir,
PackagePath: "github.com/jmattheis/goverter/execution/" + genPkgName,
ScanDir: "github.com/jmattheis/goverter/execution",
ExtendMethods: scenario.Extends,
WrapErrors: scenario.WrapErrors,
IgnoredUnexportedFields: scenario.IgnoreUnexportedFields,
MatchFieldsIgnoreCase: scenario.MatchFieldsIgnoreCase,
})
body, _ := ioutil.ReadFile(genFile)
if os.Getenv("UPDATE_SCENARIO") == "true" && scenario.ErrorStartsWith == "" {
if err != nil {
scenario.Success = ""
scenario.Error = replaceAbsolutePath(curPath, fmt.Sprint(err))
} else {
scenario.Success = string(body)
scenario.Error = ""
}
newBytes, err := yaml.Marshal(&scenario)
if assert.NoError(t, err) {
ioutil.WriteFile(scenarioFileName, newBytes, os.ModePerm)
}
}
if scenario.ErrorStartsWith != "" {
require.Error(t, err)
strErr := replaceAbsolutePath(curPath, fmt.Sprint(err))
require.Equal(t, scenario.ErrorStartsWith, strErr[:len(scenario.ErrorStartsWith)])
return
}
if scenario.Error != "" {
require.Error(t, err)
require.Equal(t, scenario.Error, replaceAbsolutePath(curPath, fmt.Sprint(err)))
return
}
require.NoError(t, err)
require.NotEmpty(t, scenario.Success, "scenario.Success may not be empty")
require.Equal(t, scenario.Success, string(body))
require.NoError(t, compile(genFile), "generated converter doesn't build")
})
if os.Getenv("SKIP_CLEAN") != "true" {
clearDir(execDir)
}
}
}
func replaceAbsolutePath(curPath, body string) string {
return strings.ReplaceAll(body, curPath, "/ABSOLUTE")
}
func compile(file string) error {
cmd := exec.Command("go", "build", "")
cmd.Dir = filepath.Dir(file)
_, 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
}
type Scenario struct {
Input map[string]string `yaml:"input"`
Extends []string `yaml:"extends,omitempty"`
// set to test code generation with fmt.Errorf per field
WrapErrors bool `yaml:"wrapErrors,omitempty"`
IgnoreUnexportedFields bool `yaml:"ignore_unexported_fields,omitempty"`
MatchFieldsIgnoreCase bool `yaml:"match_fields_ignore_case,omitempty"`
Success string `yaml:"success,omitempty"`
// for error cases, use either Error or ErrorStartsWith, not both
Error string `yaml:"error,omitempty"`
ErrorStartsWith string `yaml:"error_starts_with,omitempty"`
}
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
}