-
-
Notifications
You must be signed in to change notification settings - Fork 356
/
Copy pathtest.go
208 lines (200 loc) · 5.08 KB
/
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
205
206
207
208
// Copyright (c) 2017, Daniel Martí <[email protected]>
// See LICENSE for licensing information
package interp
import (
"context"
"fmt"
"os"
"os/exec"
"regexp"
"golang.org/x/term"
"mvdan.cc/sh/v3/expand"
"mvdan.cc/sh/v3/syntax"
)
// non-empty string is true, empty string is false
func (r *Runner) bashTest(ctx context.Context, expr syntax.TestExpr, classic bool) string {
switch x := expr.(type) {
case *syntax.Word:
if classic {
// In the classic "test" mode, we already expanded and
// split the list of words, so don't redo that work.
return r.document(x)
}
return r.literal(x)
case *syntax.ParenTest:
return r.bashTest(ctx, x.X, classic)
case *syntax.BinaryTest:
switch x.Op {
case syntax.TsMatchShort, syntax.TsMatch, syntax.TsNoMatch:
str := r.literal(x.X.(*syntax.Word))
yw := x.Y.(*syntax.Word)
if classic { // test, [
lit := r.literal(yw)
if (str == lit) == (x.Op != syntax.TsNoMatch) {
return "1"
}
} else { // [[
pattern := r.pattern(yw)
if match(pattern, str) == (x.Op != syntax.TsNoMatch) {
return "1"
}
}
return ""
}
if r.binTest(ctx, x.Op, r.bashTest(ctx, x.X, classic), r.bashTest(ctx, x.Y, classic)) {
return "1"
}
return ""
case *syntax.UnaryTest:
if r.unTest(ctx, x.Op, r.bashTest(ctx, x.X, classic)) {
return "1"
}
return ""
}
return ""
}
func (r *Runner) binTest(ctx context.Context, op syntax.BinTestOperator, x, y string) bool {
switch op {
case syntax.TsReMatch:
re, err := regexp.Compile(y)
if err != nil {
r.exit = 2
return false
}
return re.MatchString(x)
case syntax.TsNewer:
info1, err1 := r.stat(ctx, x)
info2, err2 := r.stat(ctx, y)
if err1 != nil || err2 != nil {
return false
}
return info1.ModTime().After(info2.ModTime())
case syntax.TsOlder:
info1, err1 := r.stat(ctx, x)
info2, err2 := r.stat(ctx, y)
if err1 != nil || err2 != nil {
return false
}
return info1.ModTime().Before(info2.ModTime())
case syntax.TsDevIno:
info1, err1 := r.stat(ctx, x)
info2, err2 := r.stat(ctx, y)
if err1 != nil || err2 != nil {
return false
}
return os.SameFile(info1, info2)
case syntax.TsEql:
return atoi(x) == atoi(y)
case syntax.TsNeq:
return atoi(x) != atoi(y)
case syntax.TsLeq:
return atoi(x) <= atoi(y)
case syntax.TsGeq:
return atoi(x) >= atoi(y)
case syntax.TsLss:
return atoi(x) < atoi(y)
case syntax.TsGtr:
return atoi(x) > atoi(y)
case syntax.AndTest:
return x != "" && y != ""
case syntax.OrTest:
return x != "" || y != ""
case syntax.TsBefore:
return x < y
default: // syntax.TsAfter
return x > y
}
}
func (r *Runner) statMode(ctx context.Context, name string, mode os.FileMode) bool {
info, err := r.stat(ctx, name)
return err == nil && info.Mode()&mode != 0
}
func (r *Runner) unTest(ctx context.Context, op syntax.UnTestOperator, x string) bool {
switch op {
case syntax.TsExists:
_, err := r.stat(ctx, x)
return err == nil
case syntax.TsRegFile:
info, err := r.stat(ctx, x)
return err == nil && info.Mode().IsRegular()
case syntax.TsDirect:
return r.statMode(ctx, x, os.ModeDir)
case syntax.TsCharSp:
return r.statMode(ctx, x, os.ModeCharDevice)
case syntax.TsBlckSp:
info, err := r.stat(ctx, x)
return err == nil && info.Mode()&os.ModeDevice != 0 &&
info.Mode()&os.ModeCharDevice == 0
case syntax.TsNmPipe:
return r.statMode(ctx, x, os.ModeNamedPipe)
case syntax.TsSocket:
return r.statMode(ctx, x, os.ModeSocket)
case syntax.TsSmbLink:
info, err := r.lstat(ctx, x)
return err == nil && info.Mode()&os.ModeSymlink != 0
case syntax.TsSticky:
return r.statMode(ctx, x, os.ModeSticky)
case syntax.TsUIDSet:
return r.statMode(ctx, x, os.ModeSetuid)
case syntax.TsGIDSet:
return r.statMode(ctx, x, os.ModeSetgid)
// case syntax.TsGrpOwn:
// case syntax.TsUsrOwn:
// case syntax.TsModif:
case syntax.TsRead:
f, err := r.open(ctx, x, os.O_RDONLY, 0, false)
if err == nil {
f.Close()
}
return err == nil
case syntax.TsWrite:
f, err := r.open(ctx, x, os.O_WRONLY, 0, false)
if err == nil {
f.Close()
}
return err == nil
case syntax.TsExec:
_, err := exec.LookPath(r.absPath(x))
return err == nil
case syntax.TsNoEmpty:
info, err := r.stat(ctx, x)
return err == nil && info.Size() > 0
case syntax.TsFdTerm:
fd := atoi(x)
var f any
switch fd {
case 0:
f = r.stdin
case 1:
f = r.stdout
case 2:
f = r.stderr
}
if f, ok := f.(interface{ Fd() uintptr }); ok {
// Support Fd methods such as the one on *os.File.
return term.IsTerminal(int(f.Fd()))
}
// TODO: allow term.IsTerminal here too if running in the
// "single process" mode.
return false
case syntax.TsEmpStr:
return x == ""
case syntax.TsNempStr:
return x != ""
case syntax.TsOptSet:
if _, opt := r.optByName(x, false); opt != nil {
return *opt
}
return false
case syntax.TsVarSet:
return r.lookupVar(x).IsSet()
case syntax.TsRefVar:
return r.lookupVar(x).Kind == expand.NameRef
case syntax.TsNot:
return x == ""
case syntax.TsUsrOwn, syntax.TsGrpOwn:
return r.unTestOwnOrGrp(ctx, op, x)
default:
panic(fmt.Sprintf("unhandled unary test op: %v", op))
}
}