-
Notifications
You must be signed in to change notification settings - Fork 5
/
sol_test.go
187 lines (165 loc) · 4.59 KB
/
sol_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
package sol
import (
"os"
"reflect"
"strings"
"testing"
"github.com/noperator/jqfmt"
)
func TestExplode(t *testing.T) {
cases := []struct {
inFile string
outFile string
}{
{"testdata/args-in.sh", "testdata/args-out.sh"},
{"testdata/bincmd-and-in.sh", "testdata/bincmd-and-out.sh"},
{"testdata/bincmd-or-in.sh", "testdata/bincmd-or-out.sh"},
{"testdata/bincmd-pipe-in.sh", "testdata/bincmd-pipe-out.sh"},
{"testdata/bincmd-pipestderr-in.sh", "testdata/bincmd-pipestderr-out.sh"},
{"testdata/clause-case-in.sh", "testdata/clause-case-out.sh"},
{"testdata/clause-for-in.sh", "testdata/clause-for-out.sh"},
{"testdata/clause-if-in.sh", "testdata/clause-if-out.sh"},
{"testdata/clause-while-in.sh", "testdata/clause-while-out.sh"},
{"testdata/cmdsubst-backtick-in.sh", "testdata/cmdsubst-backtick-out.sh"}, // `` is deprecated, switches to $()
{"testdata/cmdsubst-paren-in.sh", "testdata/cmdsubst-paren-out.sh"},
{"testdata/jq_jqarr-in.sh", "testdata/jq_jqarr-out.sh"},
{"testdata/jq_jqobj-in.sh", "testdata/jq_jqobj-out.sh"},
{"testdata/jq_jqop-add-in.sh", "testdata/jq_jqop-add-out.sh"},
{"testdata/jq_jqop-comma-in.sh", "testdata/jq_jqop-comma-out.sh"},
{"testdata/jq_jqop-pipe-in.sh", "testdata/jq_jqop-pipe-out.sh"},
{"testdata/procsubst-input-in.sh", "testdata/procsubst-input-out.sh"},
{"testdata/procsubst-output-in.sh", "testdata/procsubst-output-out.sh"},
{"testdata/redir-herestring-in.sh", "testdata/redir-herestring-out.sh"},
{"testdata/redir-stdall-in.sh", "testdata/redir-stdall-out.sh"},
{"testdata/redir-stdin-in.sh", "testdata/redir-stdin-out.sh"},
{"testdata/redir-stdout-in.sh", "testdata/redir-stdout-out.sh"},
{"testdata/sh_args-parallel-in.sh", "testdata/sh_args-parallel-out.sh"},
{"testdata/sh_bincmd-xargs-in.sh", "testdata/sh_bincmd-xargs-out.sh"},
}
for _, c := range cases {
cfgTypeStr := strings.Split(strings.TrimPrefix(c.inFile, "testdata/"), "-")[0]
cfgTypes := []string{}
jqOps := []string{}
if strings.HasPrefix(cfgTypeStr, "sh") || strings.HasPrefix(cfgTypeStr, "jq") {
cfgTypes = strings.Split(cfgTypeStr, "_")
for _, cfgType := range cfgTypes {
if cfgType == "jqop" {
jqOpsStr := strings.Split(strings.TrimPrefix(c.inFile, "testdata/"), "-")[1]
jqOps = strings.Split(jqOpsStr, "-")
}
}
} else {
cfgTypes = []string{cfgTypeStr}
}
inBytes, err := os.ReadFile(c.inFile)
if err != nil {
t.Fatalf("failed to open input file: %s", err)
}
in := string(inBytes)
wantBytes, err := os.ReadFile(c.outFile)
if err != nil {
t.Fatalf("failed to open want file: %s", err)
}
want := string(wantBytes)
Cfg = SolCfg{}
for _, cfgType := range cfgTypes {
if cfgType == "clause" {
Cfg.Clause = true
}
if cfgType == "bincmd" {
Cfg.BinCmd = true
}
if cfgType == "redir" {
Cfg.Redir = true
}
if cfgType == "cmdsubst" {
Cfg.CmdSubst = true
}
if cfgType == "procsubst" {
Cfg.ProcSubst = true
}
if cfgType == "args" {
Cfg.Args = true
}
if cfgType == "sh" {
Cfg.Sh = true
}
if cfgType == "jq" {
Cfg.Jq = true
}
if cfgType == "jqobj" {
Cfg.JqFmtCfg.Obj = true
}
if cfgType == "jqarr" {
Cfg.JqFmtCfg.Arr = true
}
if cfgType == "jqop" {
Cfg.JqFmtCfg.Ops = jqOps
}
}
out, err := Format(in)
if err != nil {
t.Fatalf("could not format program: %v", err)
}
if !reflect.DeepEqual(want, out) {
t.Logf("want: %s", want)
t.Logf("have: %s", out)
t.Errorf("%s does not match %s", c.inFile, c.outFile)
}
}
}
func TestComplex(t *testing.T) {
cases := []struct {
inFile string
cfg SolCfg
outFile string
}{
{
"testdata/complex-1-in.sh",
SolCfg{
Sh: true,
BinCmd: true,
Args: true,
Redir: true,
Jq: true,
JqFmtCfg: jqfmt.JqFmtCfg{
Obj: true,
Arr: true,
Ops: []string{"pipe", "add"},
},
},
"testdata/complex-1-out.sh",
},
{
"testdata/complex-2-in.sh",
SolCfg{
BinCmd: true,
Args: true,
MaxWidth: 30,
},
"testdata/complex-2-out.sh",
},
}
for _, c := range cases {
inBytes, err := os.ReadFile(c.inFile)
if err != nil {
t.Fatalf("failed to open input file: %s", err)
}
in := string(inBytes)
wantBytes, err := os.ReadFile(c.outFile)
if err != nil {
t.Fatalf("failed to open want file: %s", err)
}
want := string(wantBytes)
Cfg = c.cfg
out, err := Format(in)
if err != nil {
t.Fatalf("could not format program: %v", err)
}
if !reflect.DeepEqual(want, out) {
t.Logf("want: %s", want)
t.Logf("have: %s", out)
t.Errorf("%s does not match %s", c.inFile, c.outFile)
}
}
}