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 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
|
// Copyright 2018 Netflix, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package expect
import (
"bytes"
"io"
"os"
"regexp"
"strings"
"syscall"
"time"
)
// ExpectOpt allows settings Expect options.
type ExpectOpt func(*ExpectOpts) error
// WithTimeout sets a read timeout for an Expect statement.
func WithTimeout(timeout time.Duration) ExpectOpt {
return func(opts *ExpectOpts) error {
opts.ReadTimeout = &timeout
return nil
}
}
// ConsoleCallback is a callback function to execute if a match is found for
// the chained matcher.
type ConsoleCallback func(buf *bytes.Buffer) error
// Then returns an Expect condition to execute a callback if a match is found
// for the chained matcher.
func (eo ExpectOpt) Then(f ConsoleCallback) ExpectOpt {
return func(opts *ExpectOpts) error {
var options ExpectOpts
err := eo(&options)
if err != nil {
return err
}
for _, matcher := range options.Matchers {
opts.Matchers = append(opts.Matchers, &callbackMatcher{
f: f,
matcher: matcher,
})
}
return nil
}
}
// ExpectOpts provides additional options on Expect.
type ExpectOpts struct {
Matchers []Matcher
ReadTimeout *time.Duration
}
// Match sequentially calls Match on all matchers in ExpectOpts and returns the
// first matcher if a match exists, otherwise nil.
func (eo ExpectOpts) Match(v interface{}) Matcher {
for _, matcher := range eo.Matchers {
if matcher.Match(v) {
return matcher
}
}
return nil
}
// CallbackMatcher is a matcher that provides a Callback function.
type CallbackMatcher interface {
// Callback executes the matcher's callback with the content buffer at the
// time of match.
Callback(buf *bytes.Buffer) error
}
// Matcher provides an interface for finding a match in content read from
// Console's tty.
type Matcher interface {
// Match returns true iff a match is found.
Match(v interface{}) bool
Criteria() interface{}
}
// callbackMatcher fulfills the Matcher and CallbackMatcher interface to match
// using its embedded matcher and provide a callback function.
type callbackMatcher struct {
f ConsoleCallback
matcher Matcher
}
func (cm *callbackMatcher) Match(v interface{}) bool {
return cm.matcher.Match(v)
}
func (cm *callbackMatcher) Criteria() interface{} {
return cm.matcher.Criteria()
}
func (cm *callbackMatcher) Callback(buf *bytes.Buffer) error {
cb, ok := cm.matcher.(CallbackMatcher)
if ok {
err := cb.Callback(buf)
if err != nil {
return err
}
}
err := cm.f(buf)
if err != nil {
return err
}
return nil
}
// errorMatcher fulfills the Matcher interface to match a specific error.
type errorMatcher struct {
err error
}
func (em *errorMatcher) Match(v interface{}) bool {
err, ok := v.(error)
if !ok {
return false
}
return err == em.err
}
func (em *errorMatcher) Criteria() interface{} {
return em.err
}
// pathErrorMatcher fulfills the Matcher interface to match a specific os.PathError.
type pathErrorMatcher struct {
pathError os.PathError
}
func (em *pathErrorMatcher) Match(v interface{}) bool {
pathError, ok := v.(*os.PathError)
if !ok {
return false
}
return *pathError == em.pathError
}
func (em *pathErrorMatcher) Criteria() interface{} {
return em.pathError
}
// stringMatcher fulfills the Matcher interface to match strings against a given
// bytes.Buffer.
type stringMatcher struct {
str string
}
func (sm *stringMatcher) Match(v interface{}) bool {
buf, ok := v.(*bytes.Buffer)
if !ok {
return false
}
if strings.Contains(buf.String(), sm.str) {
return true
}
return false
}
func (sm *stringMatcher) Criteria() interface{} {
return sm.str
}
// regexpMatcher fulfills the Matcher interface to match Regexp against a given
// bytes.Buffer.
type regexpMatcher struct {
re *regexp.Regexp
}
func (rm *regexpMatcher) Match(v interface{}) bool {
buf, ok := v.(*bytes.Buffer)
if !ok {
return false
}
return rm.re.Match(buf.Bytes())
}
func (rm *regexpMatcher) Criteria() interface{} {
return rm.re
}
// allMatcher fulfills the Matcher interface to match a group of ExpectOpt
// against any value.
type allMatcher struct {
options ExpectOpts
}
func (am *allMatcher) Match(v interface{}) bool {
var matchers []Matcher
for _, matcher := range am.options.Matchers {
if matcher.Match(v) {
continue
}
matchers = append(matchers, matcher)
}
am.options.Matchers = matchers
return len(matchers) == 0
}
func (am *allMatcher) Criteria() interface{} {
var criterias []interface{}
for _, matcher := range am.options.Matchers {
criterias = append(criterias, matcher.Criteria())
}
return criterias
}
// All adds an Expect condition to exit if the content read from Console's tty
// matches all of the provided ExpectOpt, in any order.
func All(expectOpts ...ExpectOpt) ExpectOpt {
return func(opts *ExpectOpts) error {
var options ExpectOpts
for _, opt := range expectOpts {
if err := opt(&options); err != nil {
return err
}
}
opts.Matchers = append(opts.Matchers, &allMatcher{
options: options,
})
return nil
}
}
// String adds an Expect condition to exit if the content read from Console's
// tty contains any of the given strings.
func String(strs ...string) ExpectOpt {
return func(opts *ExpectOpts) error {
for _, str := range strs {
opts.Matchers = append(opts.Matchers, &stringMatcher{
str: str,
})
}
return nil
}
}
// Regexp adds an Expect condition to exit if the content read from Console's
// tty matches the given Regexp.
func Regexp(res ...*regexp.Regexp) ExpectOpt {
return func(opts *ExpectOpts) error {
for _, re := range res {
opts.Matchers = append(opts.Matchers, ®expMatcher{
re: re,
})
}
return nil
}
}
// RegexpPattern adds an Expect condition to exit if the content read from
// Console's tty matches the given Regexp patterns. Expect returns an error if
// the patterns were unsuccessful in compiling the Regexp.
func RegexpPattern(ps ...string) ExpectOpt {
return func(opts *ExpectOpts) error {
var res []*regexp.Regexp
for _, p := range ps {
re, err := regexp.Compile(p)
if err != nil {
return err
}
res = append(res, re)
}
return Regexp(res...)(opts)
}
}
// Error adds an Expect condition to exit if reading from Console's tty returns
// one of the provided errors.
func Error(errs ...error) ExpectOpt {
return func(opts *ExpectOpts) error {
for _, err := range errs {
opts.Matchers = append(opts.Matchers, &errorMatcher{
err: err,
})
}
return nil
}
}
// EOF adds an Expect condition to exit if io.EOF is returned from reading
// Console's tty.
func EOF(opts *ExpectOpts) error {
return Error(io.EOF)(opts)
}
// PTSClosed adds an Expect condition to exit if we get an
// "read /dev/ptmx: input/output error" error which can occur
// on Linux while reading from the ptm after the pts is closed.
// Further Reading:
// https://github.com/kr/pty/issues/21#issuecomment-129381749
func PTSClosed(opts *ExpectOpts) error {
opts.Matchers = append(opts.Matchers, &pathErrorMatcher{
pathError: os.PathError{
Op: "read",
Path: "/dev/ptmx",
Err: syscall.Errno(0x5),
},
})
return nil
}
|