Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Windows build and build on Windows CI #74

Merged
merged 5 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ concurrency:
cancel-in-progress: true

env:
GOPRIVATE: github.com/stealthrocket,buf.build/gen/go
GOPRIVATE: github.com/dispatchrun,buf.build/gen/go
GOVERSION: 1.22.0

jobs:
Expand Down Expand Up @@ -39,7 +39,13 @@ jobs:
skip-pkg-cache: true

test:
runs-on: ubuntu-latest
runs-on: ${{ matrix.os }}
concurrency:
group: ${{ matrix.os }}-${{ github.workflow }}-${{ github.event.number || github.ref }}
cancel-in-progress: true
strategy:
matrix:
os: [ubuntu-latest, windows-latest]
permissions:
id-token: write
contents: read
Expand Down
4 changes: 1 addition & 3 deletions cli/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,7 @@ Run 'dispatch help run' to learn about Dispatch sessions.`, BridgeSession)
s = os.Kill
}
if cmd.Process != nil && cmd.Process.Pid > 0 {
// Sending the signal to -pid sends it to all processes
// in the process group.
_ = syscall.Kill(-cmd.Process.Pid, s.(syscall.Signal))
killProcess(cmd.Process, s.(syscall.Signal))
}
}
}
Expand Down
11 changes: 10 additions & 1 deletion cli/run_darwin.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
package cli

import "syscall"
import (
"os"
"syscall"
)

func setSysProcAttr(attr *syscall.SysProcAttr) {
attr.Setpgid = true
}

func killProcess(process *os.Process, signal os.Signal) {
// Sending the signal to -pid sends it to all processes
// in the process group.
_ = syscall.Kill(-process.Pid, signal.(syscall.Signal))
}
9 changes: 8 additions & 1 deletion cli/run_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

package cli

import "syscall"
import (
"os"
"syscall"
)

func setSysProcAttr(attr *syscall.SysProcAttr) {}

func killProcess(process *os.Process, _ os.Signal) {
process.Kill()
}
11 changes: 10 additions & 1 deletion cli/run_linux.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
package cli

import "syscall"
import (
"os"
"syscall"
)

func setSysProcAttr(attr *syscall.SysProcAttr) {
attr.Setpgid = true
attr.Pdeathsig = syscall.SIGTERM
}

func killProcess(process *os.Process, signal os.Signal) {
// Sending the signal to -pid sends it to all processes
// in the process group.
_ = syscall.Kill(-process.Pid, signal.(syscall.Signal))
}
183 changes: 97 additions & 86 deletions cli/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"os/exec"
"path/filepath"
"regexp"
"runtime"
"strings"
"testing"
Expand All @@ -26,96 +27,106 @@ func TestRunCommand(t *testing.T) {
t.Fatal(err.Error())
}

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())
})

t.Run("Run with env file", func(t *testing.T) {
t.Parallel()

envFile, err := createEnvFile(t.TempDir(), []byte("CHARACTER=rick_sanchez"))
defer os.Remove(envFile)
if err != nil {
t.Fatalf("Failed to write env file: %v", err)
errMsg := "no such file or directory\n"
path := regexp.QuoteMeta(filepath.FromSlash("/dispatch/cli/non-existent.env"))
if runtime.GOOS == "windows" {
errMsg = "The system cannot find the file specified.\n"
}

buff, err := execRunCommand(&[]string{}, "run", "--env-file", envFile, "--", "printenv", "CHARACTER")
if err != nil {
t.Fatal(err.Error())
}

result, found := findEnvVariableInLogs(&buff)
if !found {
t.Fatalf("Expected printenv in the output: %s", buff.String())
}
assert.Equal(t, "rick_sanchez", result, fmt.Sprintf("Expected 'printenv | rick_sanchez' in the output, got 'printenv | %s'", result))
assert.Regexp(t, "Error: failed to load env file from .+"+path+": open non-existent\\.env: "+errMsg, buff.String())
})

t.Run("Run with env variable", func(t *testing.T) {
t.Parallel()

// Set environment variables
envVars := []string{"CHARACTER=morty_smith"}

buff, err := execRunCommand(&envVars, "run", "--", "printenv", "CHARACTER")
if err != nil {
t.Fatal(err.Error())
}

result, found := findEnvVariableInLogs(&buff)
if !found {
t.Fatalf("Expected printenv in the output: %s", buff.String())
}
assert.Equal(t, "morty_smith", result, fmt.Sprintf("Expected 'printenv | morty_smith' in the output, got 'printenv | %s'", result))
})

t.Run("Run with env variable in command line has priority over the one in the env file", func(t *testing.T) {
t.Parallel()

envFile, err := createEnvFile(t.TempDir(), []byte("CHARACTER=rick_sanchez"))
defer os.Remove(envFile)
if err != nil {
t.Fatalf("Failed to write env file: %v", err)
}

// Set environment variables
envVars := []string{"CHARACTER=morty_smith"}
buff, err := execRunCommand(&envVars, "run", "--env-file", envFile, "--", "printenv", "CHARACTER")
if err != nil {
t.Fatal(err.Error())
}

result, found := findEnvVariableInLogs(&buff)
if !found {
t.Fatalf("Expected printenv in the output: %s", buff.String())
}
assert.Equal(t, "morty_smith", result, fmt.Sprintf("Expected 'printenv | morty_smith' in the output, got 'printenv | %s'", result))
})

t.Run("Run with env variable in local env vars has priority over the one in the env file", func(t *testing.T) {
// Do not use t.Parallel() here as we are manipulating the environment!

// Set environment variables
os.Setenv("CHARACTER", "morty_smith")
defer os.Unsetenv("CHARACTER")

envFile, err := createEnvFile(t.TempDir(), []byte("CHARACTER=rick_sanchez"))
defer os.Remove(envFile)

if err != nil {
t.Fatalf("Failed to write env file: %v", err)
}

buff, err := execRunCommand(&[]string{}, "run", "--env-file", envFile, "--", "printenv", "CHARACTER")
if err != nil {
t.Fatal(err.Error())
}
if runtime.GOOS != "windows" {
t.Run("Run with env file", func(t *testing.T) {
t.Parallel()

envFile, err := createEnvFile(t.TempDir(), []byte("CHARACTER=rick_sanchez"))
defer os.Remove(envFile)
if err != nil {
t.Fatalf("Failed to write env file: %v", err)
}

buff, err := execRunCommand(&[]string{}, "run", "--env-file", envFile, "--", "printenv", "CHARACTER")
if err != nil {
t.Fatal(err.Error())
}

result, found := findEnvVariableInLogs(&buff)
if !found {
t.Fatalf("Expected printenv in the output: %s", buff.String())
}
assert.Equal(t, "rick_sanchez", result, fmt.Sprintf("Expected 'printenv | rick_sanchez' in the output, got 'printenv | %s'", result))
})
}

result, found := findEnvVariableInLogs(&buff)
if !found {
t.Fatalf("Expected printenv in the output: %s\n\n", buff.String())
}
assert.Equal(t, "morty_smith", result, fmt.Sprintf("Expected 'printenv | morty_smith' in the output, got 'printenv | %s'", result))
})
// FIXME(@chicoxyzzy): Fix tests to work on Windows
if runtime.GOOS != "windows" {
t.Run("Run with env variable", func(t *testing.T) {
t.Parallel()

// Set environment variables
envVars := []string{"CHARACTER=morty_smith"}

buff, err := execRunCommand(&envVars, "run", "--", "printenv", "CHARACTER")
if err != nil {
t.Fatal(err.Error())
}

result, found := findEnvVariableInLogs(&buff)
if !found {
t.Fatalf("Expected printenv in the output: %s", buff.String())
}
assert.Equal(t, "morty_smith", result, fmt.Sprintf("Expected 'printenv | morty_smith' in the output, got 'printenv | %s'", result))
})

t.Run("Run with env variable in command line has priority over the one in the env file", func(t *testing.T) {
t.Parallel()

envFile, err := createEnvFile(t.TempDir(), []byte("CHARACTER=rick_sanchez"))
defer os.Remove(envFile)
if err != nil {
t.Fatalf("Failed to write env file: %v", err)
}

// Set environment variables
envVars := []string{"CHARACTER=morty_smith"}
buff, err := execRunCommand(&envVars, "run", "--env-file", envFile, "--", "printenv", "CHARACTER")
if err != nil {
t.Fatal(err.Error())
}

result, found := findEnvVariableInLogs(&buff)
if !found {
t.Fatalf("Expected printenv in the output: %s", buff.String())
}
assert.Equal(t, "morty_smith", result, fmt.Sprintf("Expected 'printenv | morty_smith' in the output, got 'printenv | %s'", result))
})

t.Run("Run with env variable in local env vars has priority over the one in the env file", func(t *testing.T) {
// Do not use t.Parallel() here as we are manipulating the environment!

// Set environment variables
os.Setenv("CHARACTER", "morty_smith")
defer os.Unsetenv("CHARACTER")

envFile, err := createEnvFile(t.TempDir(), []byte("CHARACTER=rick_sanchez"))
defer os.Remove(envFile)

if err != nil {
t.Fatalf("Failed to write env file: %v", err)
}

buff, err := execRunCommand(&[]string{}, "run", "--env-file", envFile, "--", "printenv", "CHARACTER")
if err != nil {
t.Fatal(err.Error())
}

result, found := findEnvVariableInLogs(&buff)
if !found {
t.Fatalf("Expected printenv in the output: %s\n\n", buff.String())
}
assert.Equal(t, "morty_smith", result, fmt.Sprintf("Expected 'printenv | morty_smith' in the output, got 'printenv | %s'", result))
})
}
}

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