Skip to content

Commit 5ba89d3

Browse files
authored
Merge pull request #74 from dispatchrun/run_tests_win
Fix Windows build and build on Windows CI
2 parents dbce3be + 457d6e1 commit 5ba89d3

File tree

6 files changed

+134
-94
lines changed

6 files changed

+134
-94
lines changed

.github/workflows/build.yml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ concurrency:
1111
cancel-in-progress: true
1212

1313
env:
14-
GOPRIVATE: github.com/stealthrocket,buf.build/gen/go
14+
GOPRIVATE: github.com/dispatchrun,buf.build/gen/go
1515
GOVERSION: 1.22.0
1616

1717
jobs:
@@ -39,7 +39,13 @@ jobs:
3939
skip-pkg-cache: true
4040

4141
test:
42-
runs-on: ubuntu-latest
42+
runs-on: ${{ matrix.os }}
43+
concurrency:
44+
group: ${{ matrix.os }}-${{ github.workflow }}-${{ github.event.number || github.ref }}
45+
cancel-in-progress: true
46+
strategy:
47+
matrix:
48+
os: [ubuntu-latest, windows-latest]
4349
permissions:
4450
id-token: write
4551
contents: read

cli/run.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -205,9 +205,7 @@ Run 'dispatch help run' to learn about Dispatch sessions.`, BridgeSession)
205205
s = os.Kill
206206
}
207207
if cmd.Process != nil && cmd.Process.Pid > 0 {
208-
// Sending the signal to -pid sends it to all processes
209-
// in the process group.
210-
_ = syscall.Kill(-cmd.Process.Pid, s.(syscall.Signal))
208+
killProcess(cmd.Process, s.(syscall.Signal))
211209
}
212210
}
213211
}

cli/run_darwin.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
11
package cli
22

3-
import "syscall"
3+
import (
4+
"os"
5+
"syscall"
6+
)
47

58
func setSysProcAttr(attr *syscall.SysProcAttr) {
69
attr.Setpgid = true
710
}
11+
12+
func killProcess(process *os.Process, signal os.Signal) {
13+
// Sending the signal to -pid sends it to all processes
14+
// in the process group.
15+
_ = syscall.Kill(-process.Pid, signal.(syscall.Signal))
16+
}

cli/run_default.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22

33
package cli
44

5-
import "syscall"
5+
import (
6+
"os"
7+
"syscall"
8+
)
69

710
func setSysProcAttr(attr *syscall.SysProcAttr) {}
11+
12+
func killProcess(process *os.Process, _ os.Signal) {
13+
process.Kill()
14+
}

cli/run_linux.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
11
package cli
22

3-
import "syscall"
3+
import (
4+
"os"
5+
"syscall"
6+
)
47

58
func setSysProcAttr(attr *syscall.SysProcAttr) {
69
attr.Setpgid = true
710
attr.Pdeathsig = syscall.SIGTERM
811
}
12+
13+
func killProcess(process *os.Process, signal os.Signal) {
14+
// Sending the signal to -pid sends it to all processes
15+
// in the process group.
16+
_ = syscall.Kill(-process.Pid, signal.(syscall.Signal))
17+
}

cli/run_test.go

Lines changed: 97 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
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

121132
func execRunCommand(envVars *[]string, arg ...string) (bytes.Buffer, error) {

0 commit comments

Comments
 (0)