@@ -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
1419var (
@@ -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+ }
0 commit comments