Skip to content
代码片段 群组 项目
未验证 提交 07bfe304 编辑于 作者: Ash McKenzie's avatar Ash McKenzie 提交者: GitLab
浏览文件

Add basic coverage for `workhorse/internal/helper/command/command.go`

上级 0182e705
No related branches found
No related tags found
无相关合并请求
...@@ -15,9 +15,9 @@ func ExitStatus(err error) (int, bool) { ...@@ -15,9 +15,9 @@ func ExitStatus(err error) (int, bool) {
} }
} }
func KillProcessGroup(cmd *exec.Cmd) { func KillProcessGroup(cmd *exec.Cmd) error {
if cmd == nil { if cmd == nil {
return return nil
} }
if p := cmd.Process; p != nil && p.Pid > 0 { if p := cmd.Process; p != nil && p.Pid > 0 {
...@@ -26,5 +26,5 @@ func KillProcessGroup(cmd *exec.Cmd) { ...@@ -26,5 +26,5 @@ func KillProcessGroup(cmd *exec.Cmd) {
} }
// reap our child process // reap our child process
cmd.Wait() return cmd.Wait()
} }
package command
import (
"errors"
"os/exec"
"testing"
"github.com/stretchr/testify/require"
)
type ErrorWithExitCode struct {
exitCode int
}
func (e ErrorWithExitCode) Error() string {
return "Error that responds to ExitCode()"
}
func (e ErrorWithExitCode) ExitCode() int {
return e.exitCode
}
func TestExitStatus(t *testing.T) {
tests := []struct {
name string
err error
exitCode int
ok bool
}{
{
name: "error responds to ExitCode()",
err: ErrorWithExitCode{exitCode: 0},
exitCode: 0,
ok: true,
},
{
name: "error is not nil",
err: errors.New("some generic error"),
exitCode: -1,
ok: false,
},
{
name: "else",
err: nil,
exitCode: 0,
ok: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
exitCode, ok := ExitStatus(tt.err)
require.Equal(t, tt.exitCode, exitCode)
require.Equal(t, tt.ok, ok)
})
}
}
func TestKillProcessGroup(t *testing.T) {
tests := []struct {
name string
cmd *exec.Cmd
start bool
err error
}{
{
name: "command is nil",
cmd: nil,
start: false,
err: nil,
},
{
name: "command not started",
cmd: exec.Command("sleep"),
start: false,
err: errors.New(""),
},
{
name: "command started",
cmd: exec.Command("sleep"),
start: true,
err: &exec.ExitError{},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.start == true {
tt.cmd.Start()
}
err := KillProcessGroup(tt.cmd)
require.IsType(t, tt.err, err)
})
}
}
0% 加载中 .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册