diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 4cd250b..5d4bb5d 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -2,10 +2,10 @@ name: Test and Lint on: push: branches: - - master + - main pull_request: branches: - - master + - main jobs: test: @@ -21,7 +21,7 @@ jobs: - uses: actions/checkout@v3 - uses: actions/setup-go@v3 with: - go-version: 1.17 + go-version: 1.19 - name: Run Tests run: go test -v ./... @@ -32,9 +32,9 @@ jobs: - uses: actions/checkout@v3 - uses: actions/setup-go@v3 with: - go-version: 1.17 + go-version: 1.19 - name: Run golangci-lint uses: golangci/golangci-lint-action@v3 with: - version: v1.48.0 + version: v1.52.0 args: --enable=gofumpt,goimports,whitespace,gocritic,exportloopref,unconvert,prealloc diff --git a/go.mod b/go.mod index 14a2a21..56d73fe 100644 --- a/go.mod +++ b/go.mod @@ -1,5 +1,5 @@ module github.com/amenzhinsky/go-memexec -go 1.17 +go 1.19 require golang.org/x/sys v0.0.0-20210903071746-97244b99971b diff --git a/memexec_linux.go b/memexec_linux.go index 17de936..911336d 100644 --- a/memexec_linux.go +++ b/memexec_linux.go @@ -15,7 +15,7 @@ func open(b []byte) (*os.File, error) { if err != nil { return nil, err } - f := os.NewFile(uintptr(fd), fmt.Sprintf("/proc/self/fd/%d", fd)) + f := os.NewFile(uintptr(fd), fmt.Sprintf("/proc/%d/fd/%d", os.Getpid(), fd)) if _, err := f.Write(b); err != nil { _ = f.Close() return nil, err diff --git a/memexec_test.go b/memexec_test.go index b04bbe3..5eadca6 100644 --- a/memexec_test.go +++ b/memexec_test.go @@ -1,6 +1,7 @@ package memexec import ( + "bytes" "context" "os" "os/exec" @@ -24,6 +25,26 @@ func TestCommand(t *testing.T) { } } +func TestCommand_Shell(t *testing.T) { + if runtime.GOOS == "windows" { + return + } + + exe, err := New([]byte("#!/bin/sh\necho hello")) + if err != nil { + panic(err) + } + defer exe.Close() + + b, err := exe.Command().CombinedOutput() + if err != nil { + t.Fatalf("err = %v, output = %q", err, b) + } + if !bytes.Equal(b, []byte("hello\n")) { + t.Fatalf("output = %q, want %q", string(b), []byte("hello\n")) + } +} + func TestCommandContext(t *testing.T) { // the test is failing on windows, probably due to missing sleep command, // unfortunately I have no windows machines around to fix this @@ -104,7 +125,7 @@ func runCommand(t *testing.T, cmd *exec.Cmd) string { b, err := cmd.CombinedOutput() if err != nil { if b != nil { - t.Fatal(string(b)) + t.Fatalf("failed to run: %q", string(b)) } t.Fatal(err) }