Move platform-specific code under build tags; fix corresponding tests

This commit is contained in:
Denis Borzenko
2023-02-28 22:55:03 +03:00
parent 752c6e045d
commit 2a41acb422
5 changed files with 43 additions and 17 deletions

14
ffmpeg_linux_test.go Normal file
View File

@@ -0,0 +1,14 @@
package ffmpeg_go
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestCompileWithOptions(t *testing.T) {
out := Input("dummy.mp4").Output("dummy2.mp4")
cmd := out.Compile(SeparateProcessGroup())
assert.Equal(t, cmd.SysProcAttr.Pgid, 0)
assert.True(t, cmd.SysProcAttr.Setpgid)
}

View File

@@ -277,13 +277,6 @@ func TestCompile(t *testing.T) {
assert.Equal(t, out.Compile().Args, []string{"ffmpeg", "-i", "dummy.mp4", "dummy2.mp4"})
}
func TestCompileWithOptions(t *testing.T) {
out := Input("dummy.mp4").Output("dummy2.mp4")
cmd := out.Compile(SeparateProcessGroup())
assert.Equal(t, cmd.SysProcAttr.Pgid, 0)
assert.True(t, cmd.SysProcAttr.Setpgid)
}
func TestPipe(t *testing.T) {
width, height := 32, 32

20
ffmpeg_windows_test.go Normal file
View File

@@ -0,0 +1,20 @@
package ffmpeg_go
import (
"os/exec"
"syscall"
"testing"
"github.com/stretchr/testify/assert"
)
func TestCompileWithOptions(t *testing.T) {
out := Input("dummy.mp4").Output("dummy2.mp4")
cmd := out.Compile(func(s *Stream, cmd *exec.Cmd) {
if cmd.SysProcAttr == nil {
cmd.SysProcAttr = &syscall.SysProcAttr{}
}
cmd.SysProcAttr.HideWindow = true
})
assert.Equal(t, true, cmd.SysProcAttr.HideWindow)
}

10
run.go
View File

@@ -9,7 +9,6 @@ import (
"os/exec"
"sort"
"strings"
"syscall"
"time"
)
@@ -239,15 +238,6 @@ func (s *Stream) ErrorToStdOut() *Stream {
type CompilationOption func(s *Stream, cmd *exec.Cmd)
// SeparateProcessGroup ensures that the command is run in a separate process
// group. This is useful to enable handling of signals such as SIGINT without
// propagating them to the ffmpeg process.
func SeparateProcessGroup() CompilationOption {
return func(s *Stream, cmd *exec.Cmd) {
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true, Pgid: 0}
}
}
func (s *Stream) SetFfmpegPath(path string) *Stream {
s.FfmpegPath = path
return s

View File

@@ -140,3 +140,12 @@ func (s *Stream) RunLinux() error {
return cmd.Wait()
}
// SeparateProcessGroup ensures that the command is run in a separate process
// group. This is useful to enable handling of signals such as SIGINT without
// propagating them to the ffmpeg process.
func SeparateProcessGroup() CompilationOption {
return func(s *Stream, cmd *exec.Cmd) {
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true, Pgid: 0}
}
}