-
Notifications
You must be signed in to change notification settings - Fork 0
/
environment.go
114 lines (100 loc) · 2.67 KB
/
environment.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
package flargs
import (
"bytes"
"io"
"io/fs"
"math/rand"
"os"
"strings"
"testing/fstest"
"time"
realfs "github.com/sean9999/go-real-fs"
)
// Enviroment is an execution environment for a Command.
// In the context of a CLI, these would be [os.StdIn], [os.StdOut], etc.
// In the context of a test-suite, you can use [bytes.Buffer] and [fstest.MapFS].
// For benchmarking, you can use a [NullDevice].
type Environment struct {
InputStream io.ReadWriter
OutputStream io.ReadWriter
ErrorStream io.ReadWriter
Randomness rand.Source
Filesystem fs.FS
Variables map[string]string
}
func (e Environment) GetOutput() []byte {
buf, _ := io.ReadAll(e.OutputStream)
return buf
}
func (e Environment) GetError() []byte {
buf := new(bytes.Buffer)
buf.ReadFrom(e.ErrorStream)
return buf.Bytes()
}
func (e Environment) GetInput() []byte {
buf := new(bytes.Buffer)
buf.ReadFrom(e.InputStream)
return buf.Bytes()
}
// NewCLIEnvironment produces an Environment suitable for a CLI.
// It's a helper function with sane defaults.
func NewCLIEnvironment(baseDir string) *Environment {
envAsMap := func(envs []string) map[string]string {
m := make(map[string]string)
i := 0
for _, s := range envs {
i = strings.IndexByte(s, '=')
m[s[0:i]] = s[i+1:]
}
return m
}
// import parent env vars
vars := envAsMap(os.Environ())
vars["FLARGS_EXE_ENVIRONMENT"] = "cli"
realFs := realfs.New()
env := Environment{
InputStream: os.Stdin,
OutputStream: os.Stdout,
ErrorStream: os.Stderr,
Randomness: rand.NewSource(time.Now().UnixNano()),
Filesystem: realFs,
Variables: vars,
}
return &env
}
// NewTestingEnvironment produces an [Environment] suitable for testing.
// Pass in a "randomnessProvider" that offers a level of determinism that works for you.
// For good ole fashioned regular randomness, pass in [rand.Reader]
// If your program doesn't use randomness, just pass in nil.
func NewTestingEnvironment(randomnessProvider rand.Source) *Environment {
env := Environment{
InputStream: new(bytes.Buffer),
OutputStream: new(bytes.Buffer),
ErrorStream: new(bytes.Buffer),
Randomness: randomnessProvider,
Filesystem: fstest.MapFS{},
Variables: map[string]string{
"FLARGS_EXE_ENVIRONMENT": "testing",
},
}
return &env
}
type NullDevice struct {
io.Writer
}
func (b NullDevice) Read(_ []byte) (int, error) {
return 0, nil
}
func (b NullDevice) Open(_ string) (fs.File, error) {
return nil, nil
}
func NewNullEnvironment() *Environment {
e := Environment{
InputStream: NullDevice{io.Discard},
OutputStream: NullDevice{io.Discard},
ErrorStream: NullDevice{io.Discard},
Filesystem: NullDevice{},
Variables: map[string]string{},
}
return &e
}