77 "os"
88 "os/exec"
99 "path/filepath"
10+ "regexp"
1011 "runtime"
1112 "strings"
1213 "testing"
@@ -26,96 +27,106 @@ func TestRunCommand(t *testing.T) {
2627 t .Fatal (err .Error ())
2728 }
2829
29- assert .Regexp (t , "Error: failed to load env file from .+/dispatch/cli/non-existent.env: open non-existent.env: no such file or directory\n " , buff .String ())
30- })
31-
32- t .Run ("Run with env file" , func (t * testing.T ) {
33- t .Parallel ()
34-
35- envFile , err := createEnvFile (t .TempDir (), []byte ("CHARACTER=rick_sanchez" ))
36- defer os .Remove (envFile )
37- if err != nil {
38- t .Fatalf ("Failed to write env file: %v" , err )
30+ errMsg := "no such file or directory\n "
31+ path := regexp .QuoteMeta (filepath .FromSlash ("/dispatch/cli/non-existent.env" ))
32+ if runtime .GOOS == "windows" {
33+ errMsg = "The system cannot find the file specified.\n "
3934 }
40-
41- buff , err := execRunCommand (& []string {}, "run" , "--env-file" , envFile , "--" , "printenv" , "CHARACTER" )
42- if err != nil {
43- t .Fatal (err .Error ())
44- }
45-
46- result , found := findEnvVariableInLogs (& buff )
47- if ! found {
48- t .Fatalf ("Expected printenv in the output: %s" , buff .String ())
49- }
50- assert .Equal (t , "rick_sanchez" , result , fmt .Sprintf ("Expected 'printenv | rick_sanchez' in the output, got 'printenv | %s'" , result ))
35+ assert .Regexp (t , "Error: failed to load env file from .+" + path + ": open non-existent\\ .env: " + errMsg , buff .String ())
5136 })
5237
53- t .Run ("Run with env variable" , func (t * testing.T ) {
54- t .Parallel ()
55-
56- // Set environment variables
57- envVars := []string {"CHARACTER=morty_smith" }
58-
59- buff , err := execRunCommand (& envVars , "run" , "--" , "printenv" , "CHARACTER" )
60- if err != nil {
61- t .Fatal (err .Error ())
62- }
63-
64- result , found := findEnvVariableInLogs (& buff )
65- if ! found {
66- t .Fatalf ("Expected printenv in the output: %s" , buff .String ())
67- }
68- assert .Equal (t , "morty_smith" , result , fmt .Sprintf ("Expected 'printenv | morty_smith' in the output, got 'printenv | %s'" , result ))
69- })
70-
71- t .Run ("Run with env variable in command line has priority over the one in the env file" , func (t * testing.T ) {
72- t .Parallel ()
73-
74- envFile , err := createEnvFile (t .TempDir (), []byte ("CHARACTER=rick_sanchez" ))
75- defer os .Remove (envFile )
76- if err != nil {
77- t .Fatalf ("Failed to write env file: %v" , err )
78- }
79-
80- // Set environment variables
81- envVars := []string {"CHARACTER=morty_smith" }
82- buff , err := execRunCommand (& envVars , "run" , "--env-file" , envFile , "--" , "printenv" , "CHARACTER" )
83- if err != nil {
84- t .Fatal (err .Error ())
85- }
86-
87- result , found := findEnvVariableInLogs (& buff )
88- if ! found {
89- t .Fatalf ("Expected printenv in the output: %s" , buff .String ())
90- }
91- assert .Equal (t , "morty_smith" , result , fmt .Sprintf ("Expected 'printenv | morty_smith' in the output, got 'printenv | %s'" , result ))
92- })
93-
94- t .Run ("Run with env variable in local env vars has priority over the one in the env file" , func (t * testing.T ) {
95- // Do not use t.Parallel() here as we are manipulating the environment!
96-
97- // Set environment variables
98- os .Setenv ("CHARACTER" , "morty_smith" )
99- defer os .Unsetenv ("CHARACTER" )
100-
101- envFile , err := createEnvFile (t .TempDir (), []byte ("CHARACTER=rick_sanchez" ))
102- defer os .Remove (envFile )
103-
104- if err != nil {
105- t .Fatalf ("Failed to write env file: %v" , err )
106- }
107-
108- buff , err := execRunCommand (& []string {}, "run" , "--env-file" , envFile , "--" , "printenv" , "CHARACTER" )
109- if err != nil {
110- t .Fatal (err .Error ())
111- }
38+ if runtime .GOOS != "windows" {
39+ t .Run ("Run with env file" , func (t * testing.T ) {
40+ t .Parallel ()
41+
42+ envFile , err := createEnvFile (t .TempDir (), []byte ("CHARACTER=rick_sanchez" ))
43+ defer os .Remove (envFile )
44+ if err != nil {
45+ t .Fatalf ("Failed to write env file: %v" , err )
46+ }
47+
48+ buff , err := execRunCommand (& []string {}, "run" , "--env-file" , envFile , "--" , "printenv" , "CHARACTER" )
49+ if err != nil {
50+ t .Fatal (err .Error ())
51+ }
52+
53+ result , found := findEnvVariableInLogs (& buff )
54+ if ! found {
55+ t .Fatalf ("Expected printenv in the output: %s" , buff .String ())
56+ }
57+ assert .Equal (t , "rick_sanchez" , result , fmt .Sprintf ("Expected 'printenv | rick_sanchez' in the output, got 'printenv | %s'" , result ))
58+ })
59+ }
11260
113- result , found := findEnvVariableInLogs (& buff )
114- if ! found {
115- t .Fatalf ("Expected printenv in the output: %s\n \n " , buff .String ())
116- }
117- assert .Equal (t , "morty_smith" , result , fmt .Sprintf ("Expected 'printenv | morty_smith' in the output, got 'printenv | %s'" , result ))
118- })
61+ // FIXME(@chicoxyzzy): Fix tests to work on Windows
62+ if runtime .GOOS != "windows" {
63+ t .Run ("Run with env variable" , func (t * testing.T ) {
64+ t .Parallel ()
65+
66+ // Set environment variables
67+ envVars := []string {"CHARACTER=morty_smith" }
68+
69+ buff , err := execRunCommand (& envVars , "run" , "--" , "printenv" , "CHARACTER" )
70+ if err != nil {
71+ t .Fatal (err .Error ())
72+ }
73+
74+ result , found := findEnvVariableInLogs (& buff )
75+ if ! found {
76+ t .Fatalf ("Expected printenv in the output: %s" , buff .String ())
77+ }
78+ assert .Equal (t , "morty_smith" , result , fmt .Sprintf ("Expected 'printenv | morty_smith' in the output, got 'printenv | %s'" , result ))
79+ })
80+
81+ t .Run ("Run with env variable in command line has priority over the one in the env file" , func (t * testing.T ) {
82+ t .Parallel ()
83+
84+ envFile , err := createEnvFile (t .TempDir (), []byte ("CHARACTER=rick_sanchez" ))
85+ defer os .Remove (envFile )
86+ if err != nil {
87+ t .Fatalf ("Failed to write env file: %v" , err )
88+ }
89+
90+ // Set environment variables
91+ envVars := []string {"CHARACTER=morty_smith" }
92+ buff , err := execRunCommand (& envVars , "run" , "--env-file" , envFile , "--" , "printenv" , "CHARACTER" )
93+ if err != nil {
94+ t .Fatal (err .Error ())
95+ }
96+
97+ result , found := findEnvVariableInLogs (& buff )
98+ if ! found {
99+ t .Fatalf ("Expected printenv in the output: %s" , buff .String ())
100+ }
101+ assert .Equal (t , "morty_smith" , result , fmt .Sprintf ("Expected 'printenv | morty_smith' in the output, got 'printenv | %s'" , result ))
102+ })
103+
104+ t .Run ("Run with env variable in local env vars has priority over the one in the env file" , func (t * testing.T ) {
105+ // Do not use t.Parallel() here as we are manipulating the environment!
106+
107+ // Set environment variables
108+ os .Setenv ("CHARACTER" , "morty_smith" )
109+ defer os .Unsetenv ("CHARACTER" )
110+
111+ envFile , err := createEnvFile (t .TempDir (), []byte ("CHARACTER=rick_sanchez" ))
112+ defer os .Remove (envFile )
113+
114+ if err != nil {
115+ t .Fatalf ("Failed to write env file: %v" , err )
116+ }
117+
118+ buff , err := execRunCommand (& []string {}, "run" , "--env-file" , envFile , "--" , "printenv" , "CHARACTER" )
119+ if err != nil {
120+ t .Fatal (err .Error ())
121+ }
122+
123+ result , found := findEnvVariableInLogs (& buff )
124+ if ! found {
125+ t .Fatalf ("Expected printenv in the output: %s\n \n " , buff .String ())
126+ }
127+ assert .Equal (t , "morty_smith" , result , fmt .Sprintf ("Expected 'printenv | morty_smith' in the output, got 'printenv | %s'" , result ))
128+ })
129+ }
119130}
120131
121132func execRunCommand (envVars * []string , arg ... string ) (bytes.Buffer , error ) {
0 commit comments