Skip to content

Commit 2a05772

Browse files
committed
Mock env in tests to simplify run on Travis CI
1 parent 798e6c9 commit 2a05772

File tree

3 files changed

+38
-16
lines changed

3 files changed

+38
-16
lines changed

cmd/sup/main.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"flag"
55
"fmt"
6+
"io"
67
"io/ioutil"
78
"os"
89
"os/user"
@@ -12,10 +13,10 @@ import (
1213
"text/tabwriter"
1314
"time"
1415

16+
"github.com/adammck/venv"
1517
"github.com/mikkeloscar/sshconfig"
1618
"github.com/pkg/errors"
1719
"github.com/pressly/sup"
18-
"io"
1920
)
2021

2122
var (
@@ -52,6 +53,7 @@ type options struct {
5253
exceptHosts string
5354
debug bool
5455
disablePrefix bool
56+
env venv.Env
5557
}
5658

5759
func init() {
@@ -85,6 +87,7 @@ func main() {
8587
return
8688
}
8789

90+
flags.env = venv.OS()
8891
if err := runSupfile(os.Stderr, flags, flag.Args()); err != nil {
8992
fmt.Fprintln(os.Stderr, err)
9093
os.Exit(1)
@@ -99,7 +102,7 @@ func runSupfile(errStream io.Writer, options options, args []string) error {
99102
return err
100103
}
101104
// Parse network and commands to be run from flags.
102-
network, commands, err := parseArgs(errStream, args, conf)
105+
network, commands, err := parseArgs(errStream, options, args, conf)
103106
if err != nil {
104107
return err
105108
}
@@ -247,7 +250,7 @@ func resolvePath(path string) string {
247250

248251
// parseArgs parses args and returns network and commands to be run.
249252
// On error, it prints usage and exits.
250-
func parseArgs(errStream io.Writer, args []string, conf *sup.Supfile) (*sup.Network, []*sup.Command, error) {
253+
func parseArgs(errStream io.Writer, options options, args []string, conf *sup.Supfile) (*sup.Network, []*sup.Command, error) {
251254
var commands []*sup.Command
252255

253256
if len(args) < 1 {
@@ -290,15 +293,15 @@ func parseArgs(errStream io.Writer, args []string, conf *sup.Supfile) (*sup.Netw
290293

291294
// Add default nonce
292295
network.Env.Set("SUP_TIME", time.Now().UTC().Format(time.RFC3339))
293-
if os.Getenv("SUP_TIME") != "" {
294-
network.Env.Set("SUP_TIME", os.Getenv("SUP_TIME"))
296+
if options.env.Getenv("SUP_TIME") != "" {
297+
network.Env.Set("SUP_TIME", options.env.Getenv("SUP_TIME"))
295298
}
296299

297300
// Add user
298-
if os.Getenv("SUP_USER") != "" {
299-
network.Env.Set("SUP_USER", os.Getenv("SUP_USER"))
301+
if options.env.Getenv("SUP_USER") != "" {
302+
network.Env.Set("SUP_USER", options.env.Getenv("SUP_USER"))
300303
} else {
301-
network.Env.Set("SUP_USER", os.Getenv("USER"))
304+
network.Env.Set("SUP_USER", options.env.Getenv("USER"))
302305
}
303306

304307
for _, cmd := range args[1:] {

cmd/sup/main_test.go

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,16 @@ import (
44
"fmt"
55
"io/ioutil"
66
"os"
7-
"os/user"
87
"path/filepath"
98
"strings"
109
"testing"
1110
"time"
11+
12+
"github.com/adammck/venv"
13+
)
14+
15+
const (
16+
envTestUser = "sup_test_user"
1217
)
1318

1419
var (
@@ -28,6 +33,7 @@ we kp re
2833
withTmpDir(t, input, func(dirname string) {
2934
options := options{
3035
dirname: dirname,
36+
env: testEnv(),
3137
}
3238
if err := runSupfile(testErrStream, options, []string{}); err == nil {
3339
t.Fatal("Expected an error")
@@ -55,6 +61,7 @@ commands:
5561
withTmpDir(t, input, func(dirname string) {
5662
options := options{
5763
dirname: dirname,
64+
env: testEnv(),
5865
}
5966
if err := runSupfile(testErrStream, options, []string{}); err != ErrUsage {
6067
t.Fatal(err)
@@ -107,6 +114,7 @@ commands:
107114
withTmpDir(t, input, func(dirname string) {
108115
options := options{
109116
dirname: dirname,
117+
env: testEnv(),
110118
}
111119
if err := runSupfile(testErrStream, options, []string{"staging"}); err != ErrNetworkNoHosts {
112120
t.Fatal(err)
@@ -134,6 +142,7 @@ commands:
134142
withTmpDir(t, input, func(dirname string) {
135143
options := options{
136144
dirname: dirname,
145+
env: testEnv(),
137146
}
138147
if err := runSupfile(testErrStream, options, []string{"staging"}); err != ErrUsage {
139148
t.Fatal(err)
@@ -168,6 +177,7 @@ targets:
168177
withTmpDir(t, input, func(dirname string) {
169178
options := options{
170179
dirname: dirname,
180+
env: testEnv(),
171181
}
172182
if err := runSupfile(testErrStream, options, []string{"staging", "step5"}); err == nil {
173183
t.Fatal("Expected an error")
@@ -205,6 +215,7 @@ targets:
205215
withTmpDir(t, input, func(dirname string) {
206216
options := options{
207217
dirname: dirname,
218+
env: testEnv(),
208219
}
209220
if err := runSupfile(testErrStream, options, []string{"staging", "walk"}); err == nil {
210221
t.Fatal("Expected an error")
@@ -389,6 +400,7 @@ commands:
389400
options := options{
390401
dirname: dirname,
391402
onlyHosts: "server42",
403+
env: venv.Mock(),
392404
}
393405
if err := runSupfile(testErrStream, options, []string{"staging", "step1"}); err == nil {
394406
t.Fatal("Expected an error")
@@ -420,6 +432,7 @@ commands:
420432
options := options{
421433
dirname: dirname,
422434
onlyHosts: "server(",
435+
env: venv.Mock(),
423436
}
424437
if err := runSupfile(testErrStream, options, []string{"staging", "step1"}); err == nil {
425438
t.Fatal("Expected an error")
@@ -487,6 +500,7 @@ commands:
487500
options := options{
488501
dirname: dirname,
489502
exceptHosts: "server",
503+
env: venv.Mock(),
490504
}
491505
if err := runSupfile(testErrStream, options, []string{"staging", "step1"}); err == nil {
492506
t.Fatal("Expected an error")
@@ -518,6 +532,7 @@ commands:
518532
options := options{
519533
dirname: dirname,
520534
exceptHosts: "server(",
535+
env: venv.Mock(),
521536
}
522537
if err := runSupfile(testErrStream, options, []string{"staging", "step1"}); err == nil {
523538
t.Fatal("Expected an error")
@@ -578,6 +593,7 @@ commands:
578593
withTmpDir(t, input, func(dirname string) {
579594
options := options{
580595
dirname: dirname,
596+
env: testEnv(),
581597
}
582598
args := []string{"staging", "step1"}
583599
if err := runSupfile(testErrStream, options, args); err == nil {
@@ -618,15 +634,11 @@ commands:
618634
if err := runSupfile(testErrStream, options, []string{"staging", "step1"}); err != nil {
619635
t.Fatal(err)
620636
}
621-
currentUser, err := user.Current()
622-
if err != nil {
623-
t.Fatal(err)
624-
}
625637
m := newMatcher(outputs, t)
626638
m.expectActivityOnServers(0, 1)
627639
m.expectExportOnActiveServers(`SUP_NETWORK="staging"`)
628640
m.expectExportOnActiveServers(`SUP_ENV=""`)
629-
m.expectExportOnActiveServers(fmt.Sprintf(`SUP_USER="%s"`, currentUser.Name))
641+
m.expectExportOnActiveServers(fmt.Sprintf(`SUP_USER="%s"`, envTestUser))
630642
m.expectExportRegexpOnActiveServers(`SUP_HOST="localhost:\d+"`)
631643
})
632644
})
@@ -694,7 +706,7 @@ commands:
694706
if err != nil {
695707
t.Fatal(err)
696708
}
697-
os.Setenv("SUP_TIME", "now")
709+
options.env.Setenv("SUP_TIME", "now")
698710

699711
if err := runSupfile(testErrStream, options, []string{"staging", "step1"}); err != nil {
700712
t.Fatal(err)
@@ -726,7 +738,7 @@ commands:
726738
if err != nil {
727739
t.Fatal(err)
728740
}
729-
os.Setenv("SUP_USER", "sup_rules")
741+
options.env.Setenv("SUP_USER", "sup_rules")
730742

731743
if err := runSupfile(testErrStream, options, []string{"staging", "step1"}); err != nil {
732744
t.Fatal(err)
@@ -1109,3 +1121,9 @@ func writeSupfileAs(dirname, filename, input string) error {
11091121
0666,
11101122
)
11111123
}
1124+
1125+
func testEnv() venv.Env {
1126+
env := venv.Mock()
1127+
env.Setenv("USER", envTestUser)
1128+
return env
1129+
}

cmd/sup/mock_server_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ func setupMockEnv(dirname string, count int) ([]bytes.Buffer, options, error) {
5353
options := options{
5454
sshConfig: sshConfigPath,
5555
dirname: dirname,
56+
env: testEnv(),
5657
}
5758
return outputs, options, nil
5859
}

0 commit comments

Comments
 (0)