1
1
package envbuilder_test
2
2
3
3
import (
4
+ "bytes"
4
5
"testing"
5
6
6
7
"github.com/coder/envbuilder"
7
8
"github.com/coder/serpent"
8
9
"github.com/stretchr/testify/require"
9
10
)
10
11
11
- // TestStrEnvOptions tests that string env variables can be handled as expected.
12
- func TestStrEnvOptions (t * testing.T ) {
13
- t .Setenv ("SETUP_SCRIPT" , "setup.sh" )
14
- t .Setenv ("INIT_COMMAND" , "sleep infinity" )
12
+ // TestEnvOptionParsing tests that given environment variables of different types are handled as expected.
13
+ func TestTestEnvOptionParsing (t * testing.T ) {
14
+ t .Run ("string" , func (t * testing.T ) {
15
+ t .Setenv ("SETUP_SCRIPT" , "setup.sh" )
16
+ t .Setenv ("INIT_COMMAND" , "sleep infinity" )
15
17
16
- var o envbuilder.Options
17
- err := runCLI (& o )
18
- require .NoError (t , err )
18
+ var o envbuilder.Options
19
+ err := runCLI (& o )
20
+ require .NoError (t , err )
19
21
20
- require .Equal (t , o .SetupScript , "setup.sh" )
21
- require .Equal (t , o .InitCommand , "sleep infinity" )
22
- }
22
+ require .Equal (t , o .SetupScript , "setup.sh" )
23
+ require .Equal (t , o .InitCommand , "sleep infinity" )
24
+ })
23
25
24
- // TestIntEnvOptions tests that numeric env variables can be handled as expected.
25
- func TestIntEnvOptions (t * testing.T ) {
26
- t .Setenv ("CACHE_TTL_DAYS" , "7" )
26
+ t .Run ("int" , func (t * testing.T ) {
27
+ t .Setenv ("CACHE_TTL_DAYS" , "7" )
27
28
28
- var o envbuilder.Options
29
- err := runCLI (& o )
30
- require .NoError (t , err )
29
+ var o envbuilder.Options
30
+ err := runCLI (& o )
31
+ require .NoError (t , err )
31
32
32
- require .Equal (t , o .CacheTTLDays , 7 )
33
- }
33
+ require .Equal (t , o .CacheTTLDays , int64 ( 7 ) )
34
+ })
34
35
35
- // TestMultipleStrEnvOptions tests that numeric env variables can be handled as expected.
36
- func TestMultipleStrEnvOptions (t * testing.T ) {
37
- t .Setenv ("CACHE_TTL_DAYS" , "7" )
36
+ t .Run ("string array" , func (t * testing.T ) {
37
+ t .Setenv ("IGNORE_PATHS" , "/var,/temp" )
38
38
39
- var o envbuilder.Options
40
- err := runCLI (& o )
41
- require .NoError (t , err )
39
+ var o envbuilder.Options
40
+ err := runCLI (& o )
41
+ require .NoError (t , err )
42
42
43
- require .Equal (t , o .CacheTTLDays , 7 )
44
- }
43
+ require .Equal (t , o .IgnorePaths , []string {"/var" , "/temp" })
44
+ })
45
+
46
+ t .Run ("bool" , func (t * testing.T ) {
47
+ t .Setenv ("SKIP_REBUILD" , "true" )
48
+ t .Setenv ("GIT_CLONE_SINGLE_BRANCH" , "false" )
49
+ t .Setenv ("EXIT_ON_BUILD_FAILURE" , "true" )
50
+ t .Setenv ("FORCE_SAFE" , "false" )
51
+ t .Setenv ("INSECURE" , "true" )
52
+
53
+ var o envbuilder.Options
54
+ err := runCLI (& o )
55
+ require .NoError (t , err )
45
56
46
- // TestBoolEnvOptions tests that boolean env variables can be handled as expected.
47
- func TestBoolEnvOptions (t * testing.T ) {
48
- t .Setenv ("SKIP_REBUILD" , "true" )
49
- t .Setenv ("GIT_CLONE_SINGLE_BRANCH" , "" )
50
- t .Setenv ("EXIT_ON_BUILD_FAILURE" , "false" )
51
- t .Setenv ("FORCE_SAFE" , "TRUE" )
52
- t .Setenv ("INSECURE" , "FALSE" )
53
-
54
- var o envbuilder.Options
55
- err := runCLI (& o )
56
- require .NoError (t , err )
57
-
58
- require .True (t , o .SkipRebuild )
59
- require .False (t , o .GitCloneSingleBranch )
60
- require .False (t , o .ExitOnBuildFailure )
61
- require .True (t , o .ForceSafe )
62
- require .False (t , o .Insecure )
57
+ require .True (t , o .SkipRebuild )
58
+ require .False (t , o .GitCloneSingleBranch )
59
+ require .True (t , o .ExitOnBuildFailure )
60
+ require .False (t , o .ForceSafe )
61
+ require .True (t , o .Insecure )
62
+ })
63
63
}
64
64
65
65
func runCLI (o * envbuilder.Options ) error {
@@ -69,6 +69,22 @@ func runCLI(o *envbuilder.Options) error {
69
69
return nil
70
70
},
71
71
}
72
- err := cmd .Invoke ().WithOS ().Run ()
72
+ i := cmd .Invoke ().WithOS ()
73
+ fakeIO (i )
74
+ err := i .Run ()
73
75
return err
74
76
}
77
+
78
+ type ioBufs struct {
79
+ Stdin bytes.Buffer
80
+ Stdout bytes.Buffer
81
+ Stderr bytes.Buffer
82
+ }
83
+
84
+ func fakeIO (i * serpent.Invocation ) * ioBufs {
85
+ var b ioBufs
86
+ i .Stdout = & b .Stdout
87
+ i .Stderr = & b .Stderr
88
+ i .Stdin = & b .Stdin
89
+ return & b
90
+ }
0 commit comments