helper/args: MustNewCheckedArgs for cleaner hardcoded args

Signed-off-by: Ophestra Umiker <cat@ophivana.moe>
This commit is contained in:
Ophestra 2024-10-07 13:33:18 +09:00
parent 9647eb6a6b
commit 0fb9e40191
Signed by: cat
SSH Key Fingerprint: SHA256:gQ67O0enBZ7UdZypgtspB2FDM1g3GVw8nX0XSdcFw8Q
4 changed files with 68 additions and 18 deletions

View File

@ -53,3 +53,14 @@ func NewCheckedArgs(args []string) (io.WriterTo, error) {
a := argsWt(args)
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
}

View File

@ -10,8 +10,6 @@ import (
)
func Test_argsFD_String(t *testing.T) {
argsOnce.Do(prepareArgs)
wantString := strings.Join(want, " ")
if got := argsWt.(fmt.Stringer).String(); got != wantString {
t.Errorf("String(): got %v; want %v",
@ -26,4 +24,17 @@ func TestNewCheckedArgs(t *testing.T) {
args,
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)
})
}

View File

@ -6,7 +6,6 @@ import (
"os"
"strconv"
"strings"
"sync"
"testing"
"time"
@ -23,20 +22,9 @@ var (
"--talk=org.freedesktop.UPower",
}
wantPayload string
argsWt io.WriterTo
argsOnce sync.Once
)
func prepareArgs() {
wantPayload = strings.Join(want, "\x00") + "\x00"
if a, err := helper.NewCheckedArgs(want); err != nil {
panic(err.Error())
} else {
argsWt = a
}
}
argsWt = helper.MustNewCheckedArgs(want)
)
func argF(argsFD int, _ int) []string {
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) {
helper.InternalReplaceExecCommand(t)
argsOnce.Do(prepareArgs)
t.Run("start non-existent helper path", func(t *testing.T) {
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) {
helper.InternalReplaceExecCommand(t)
argsOnce.Do(prepareArgs)
var wt io.WriterTo
if a, err := helper.NewCheckedArgs(want); err != nil {

42
helper/pipe_test.go Normal file
View 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()
}