helper/args: MustNewCheckedArgs for cleaner hardcoded args
Signed-off-by: Ophestra Umiker <cat@ophivana.moe>
This commit is contained in:
parent
9647eb6a6b
commit
0fb9e40191
@ -53,3 +53,14 @@ func NewCheckedArgs(args []string) (io.WriterTo, error) {
|
|||||||
a := argsWt(args)
|
a := argsWt(args)
|
||||||
return a, a.check()
|
return a, a.check()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// MustNewCheckedArgs returns a checked argument writer for args and panics if check fails.
|
||||||
|
// Callers must not retain any references to args.
|
||||||
|
func MustNewCheckedArgs(args []string) io.WriterTo {
|
||||||
|
a, err := NewCheckedArgs(args)
|
||||||
|
if err != nil {
|
||||||
|
panic(err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
return a
|
||||||
|
}
|
||||||
|
@ -10,8 +10,6 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func Test_argsFD_String(t *testing.T) {
|
func Test_argsFD_String(t *testing.T) {
|
||||||
argsOnce.Do(prepareArgs)
|
|
||||||
|
|
||||||
wantString := strings.Join(want, " ")
|
wantString := strings.Join(want, " ")
|
||||||
if got := argsWt.(fmt.Stringer).String(); got != wantString {
|
if got := argsWt.(fmt.Stringer).String(); got != wantString {
|
||||||
t.Errorf("String(): got %v; want %v",
|
t.Errorf("String(): got %v; want %v",
|
||||||
@ -26,4 +24,17 @@ func TestNewCheckedArgs(t *testing.T) {
|
|||||||
args,
|
args,
|
||||||
err, helper.ErrContainsNull)
|
err, helper.ErrContainsNull)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
t.Run("must panic", func(t *testing.T) {
|
||||||
|
badPayload := []string{"\x00"}
|
||||||
|
defer func() {
|
||||||
|
wantPanic := "argument contains null character"
|
||||||
|
if r := recover(); r != wantPanic {
|
||||||
|
t.Errorf("MustNewCheckedArgs(%q) panic = %v, wantPanic %v",
|
||||||
|
badPayload,
|
||||||
|
r, wantPanic)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
helper.MustNewCheckedArgs(badPayload)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,6 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -23,20 +22,9 @@ var (
|
|||||||
"--talk=org.freedesktop.UPower",
|
"--talk=org.freedesktop.UPower",
|
||||||
}
|
}
|
||||||
|
|
||||||
wantPayload string
|
|
||||||
argsWt io.WriterTo
|
|
||||||
argsOnce sync.Once
|
|
||||||
)
|
|
||||||
|
|
||||||
func prepareArgs() {
|
|
||||||
wantPayload = strings.Join(want, "\x00") + "\x00"
|
wantPayload = strings.Join(want, "\x00") + "\x00"
|
||||||
|
argsWt = helper.MustNewCheckedArgs(want)
|
||||||
if a, err := helper.NewCheckedArgs(want); err != nil {
|
)
|
||||||
panic(err.Error())
|
|
||||||
} else {
|
|
||||||
argsWt = a
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func argF(argsFD int, _ int) []string {
|
func argF(argsFD int, _ int) []string {
|
||||||
return []string{"--args", strconv.Itoa(argsFD)}
|
return []string{"--args", strconv.Itoa(argsFD)}
|
||||||
@ -48,7 +36,6 @@ func argFStatus(argsFD int, statFD int) []string {
|
|||||||
|
|
||||||
func TestHelper_StartNotify_Close_Wait(t *testing.T) {
|
func TestHelper_StartNotify_Close_Wait(t *testing.T) {
|
||||||
helper.InternalReplaceExecCommand(t)
|
helper.InternalReplaceExecCommand(t)
|
||||||
argsOnce.Do(prepareArgs)
|
|
||||||
|
|
||||||
t.Run("start non-existent helper path", func(t *testing.T) {
|
t.Run("start non-existent helper path", func(t *testing.T) {
|
||||||
h := helper.New(argsWt, "/nonexistent", argF)
|
h := helper.New(argsWt, "/nonexistent", argF)
|
||||||
@ -143,7 +130,6 @@ func TestHelper_StartNotify_Close_Wait(t *testing.T) {
|
|||||||
}
|
}
|
||||||
func TestHelper_Start_Close_Wait(t *testing.T) {
|
func TestHelper_Start_Close_Wait(t *testing.T) {
|
||||||
helper.InternalReplaceExecCommand(t)
|
helper.InternalReplaceExecCommand(t)
|
||||||
argsOnce.Do(prepareArgs)
|
|
||||||
|
|
||||||
var wt io.WriterTo
|
var wt io.WriterTo
|
||||||
if a, err := helper.NewCheckedArgs(want); err != nil {
|
if a, err := helper.NewCheckedArgs(want); err != nil {
|
||||||
|
42
helper/pipe_test.go
Normal file
42
helper/pipe_test.go
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
package helper
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Test_pipes_pipe_mustClosePipes(t *testing.T) {
|
||||||
|
p := new(pipes)
|
||||||
|
|
||||||
|
t.Run("pipe without args", func(t *testing.T) {
|
||||||
|
defer func() {
|
||||||
|
wantPanic := "attempted to pipe without args"
|
||||||
|
if r := recover(); r != wantPanic {
|
||||||
|
t.Errorf("pipe() panic = %v, wantPanic %v",
|
||||||
|
r, wantPanic)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
_ = p.pipe()
|
||||||
|
})
|
||||||
|
|
||||||
|
p.args = MustNewCheckedArgs(make([]string, 0))
|
||||||
|
t.Run("obtain pipes", func(t *testing.T) {
|
||||||
|
if err := p.pipe(); err != nil {
|
||||||
|
t.Errorf("pipe() error = %v",
|
||||||
|
err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("pipe twice", func(t *testing.T) {
|
||||||
|
defer func() {
|
||||||
|
wantPanic := "attempted to pipe twice"
|
||||||
|
if r := recover(); r != wantPanic {
|
||||||
|
t.Errorf("pipe() panic = %v, wantPanic %v",
|
||||||
|
r, wantPanic)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
_ = p.pipe()
|
||||||
|
})
|
||||||
|
|
||||||
|
p.mustClosePipes()
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user