-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathopts.go
66 lines (54 loc) · 1.52 KB
/
opts.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
// Copyright 2020 ActiveState Software. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file
package termtest
import (
"io/ioutil"
"os"
"time"
expect "github.com/ActiveState/termtest/expect"
)
// SendObserver is function that is called when text is send to the console
// Arguments are the message, number of bytes written and an error message
// See TestSendObserveFn for an example
type SendObserver func(msg string, num int, err error)
// Options contain optional values for ConsoleProcess construction and usage.
type Options struct {
DefaultTimeout time.Duration
WorkDirectory string
RetainWorkDir bool
Environment []string
ObserveSend SendObserver
ObserveExpect expect.ExpectObserver
CmdName string
Args []string
HideCmdLine bool
ExtraOpts []expect.ConsoleOpt
}
// Normalize fills in default options
func (opts *Options) Normalize() error {
if opts.DefaultTimeout == 0 {
opts.DefaultTimeout = time.Second * 20
}
if opts.WorkDirectory == "" {
tmpDir, err := ioutil.TempDir("", "")
if err != nil {
return err
}
opts.WorkDirectory = tmpDir
}
if opts.ObserveSend == nil {
opts.ObserveSend = func(string, int, error) {}
}
if opts.ObserveExpect == nil {
opts.ObserveExpect = func([]expect.Matcher, *expect.MatchState, error) {}
}
return nil
}
// CleanUp cleans up the environment
func (opts *Options) CleanUp() error {
if !opts.RetainWorkDir {
return os.RemoveAll(opts.WorkDirectory)
}
return nil
}