system: improve tests of the I struct
All checks were successful
Test / Create distribution (push) Successful in 35s
Test / Sandbox (push) Successful in 2m5s
Test / Hakurei (push) Successful in 3m20s
Test / Hpkg (push) Successful in 3m57s
Test / Sandbox (race detector) (push) Successful in 4m41s
Test / Hakurei (race detector) (push) Successful in 5m25s
Test / Flake checks (push) Successful in 1m39s
All checks were successful
Test / Create distribution (push) Successful in 35s
Test / Sandbox (push) Successful in 2m5s
Test / Hakurei (push) Successful in 3m20s
Test / Hpkg (push) Successful in 3m57s
Test / Sandbox (race detector) (push) Successful in 4m41s
Test / Hakurei (race detector) (push) Successful in 5m25s
Test / Flake checks (push) Successful in 1m39s
This cleans up for the test overhaul of this package. Signed-off-by: Ophestra <cat@gensokyo.uk>
This commit is contained in:
@@ -1,28 +1,37 @@
|
||||
package system_test
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"strconv"
|
||||
"testing"
|
||||
_ "unsafe"
|
||||
|
||||
"hakurei.app/system"
|
||||
)
|
||||
|
||||
func TestNew(t *testing.T) {
|
||||
//go:linkname criteriaHasType hakurei.app/system.(*Criteria).hasType
|
||||
func criteriaHasType(_ *system.Criteria, _ system.Enablement) bool
|
||||
|
||||
func TestCriteria(t *testing.T) {
|
||||
testCases := []struct {
|
||||
uid int
|
||||
name string
|
||||
ec, t system.Enablement
|
||||
want bool
|
||||
}{
|
||||
{150},
|
||||
{149},
|
||||
{148},
|
||||
{147},
|
||||
{"nil", 0xff, system.EWayland, true},
|
||||
{"nil user", 0xff, system.User, false},
|
||||
{"all", system.EWayland | system.EX11 | system.EDBus | system.EPulse | system.User | system.Process, system.Process, true},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run("sys initialised with uid "+strconv.Itoa(tc.uid), func(t *testing.T) {
|
||||
if got := system.New(t.Context(), tc.uid); got.UID() != tc.uid {
|
||||
t.Errorf("New(%d) uid = %d, want %d",
|
||||
tc.uid,
|
||||
got.UID(), tc.uid)
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
var criteria *system.Criteria
|
||||
if tc.ec != 0xff {
|
||||
criteria = (*system.Criteria)(&tc.ec)
|
||||
}
|
||||
if got := criteriaHasType(criteria, tc.t); got != tc.want {
|
||||
t.Errorf("hasType: got %v, want %v",
|
||||
got, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -45,86 +54,108 @@ func TestTypeString(t *testing.T) {
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run("label type string "+tc.want, func(t *testing.T) {
|
||||
t.Run("label type string "+strconv.Itoa(int(tc.e)), func(t *testing.T) {
|
||||
if got := system.TypeString(tc.e); got != tc.want {
|
||||
t.Errorf("TypeString: %q, want %q",
|
||||
got, tc.want)
|
||||
t.Errorf("TypeString: %q, want %q", got, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestI_Equal(t *testing.T) {
|
||||
func TestNew(t *testing.T) {
|
||||
t.Run("panic", func(t *testing.T) {
|
||||
t.Run("ctx", func(t *testing.T) {
|
||||
defer func() {
|
||||
want := "invalid call to New"
|
||||
if r := recover(); r != want {
|
||||
t.Errorf("recover: %v, want %v", r, want)
|
||||
}
|
||||
}()
|
||||
system.New(nil, 0)
|
||||
})
|
||||
|
||||
t.Run("uid", func(t *testing.T) {
|
||||
defer func() {
|
||||
want := "invalid call to New"
|
||||
if r := recover(); r != want {
|
||||
t.Errorf("recover: %v, want %v", r, want)
|
||||
}
|
||||
}()
|
||||
system.New(t.Context(), -1)
|
||||
})
|
||||
})
|
||||
|
||||
sys := system.New(t.Context(), 0xdeadbeef)
|
||||
if got := reflect.ValueOf(sys).Elem().FieldByName("ctx"); got.IsNil() {
|
||||
t.Errorf("New: ctx = %#v", got)
|
||||
}
|
||||
if got := sys.UID(); got != 0xdeadbeef {
|
||||
t.Errorf("UID: %d", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEqual(t *testing.T) {
|
||||
testCases := []struct {
|
||||
name string
|
||||
sys *system.I
|
||||
v *system.I
|
||||
want bool
|
||||
}{
|
||||
{
|
||||
"simple UID",
|
||||
{"simple UID",
|
||||
system.New(t.Context(), 150),
|
||||
system.New(t.Context(), 150),
|
||||
true,
|
||||
},
|
||||
{
|
||||
"simple UID differ",
|
||||
true},
|
||||
|
||||
{"simple UID differ",
|
||||
system.New(t.Context(), 150),
|
||||
system.New(t.Context(), 151),
|
||||
false,
|
||||
},
|
||||
{
|
||||
"simple UID nil",
|
||||
false},
|
||||
|
||||
{"simple UID nil",
|
||||
system.New(t.Context(), 150),
|
||||
nil,
|
||||
false,
|
||||
},
|
||||
{
|
||||
"op length mismatch",
|
||||
false},
|
||||
|
||||
{"op length mismatch",
|
||||
system.New(t.Context(), 150).
|
||||
ChangeHosts("chronos"),
|
||||
system.New(t.Context(), 150).
|
||||
ChangeHosts("chronos").
|
||||
Ensure("/run", 0755),
|
||||
false,
|
||||
},
|
||||
{
|
||||
"op value mismatch",
|
||||
false},
|
||||
|
||||
{"op value mismatch",
|
||||
system.New(t.Context(), 150).
|
||||
ChangeHosts("chronos").
|
||||
Ensure("/run", 0644),
|
||||
system.New(t.Context(), 150).
|
||||
ChangeHosts("chronos").
|
||||
Ensure("/run", 0755),
|
||||
false,
|
||||
},
|
||||
{
|
||||
"op type mismatch",
|
||||
false},
|
||||
|
||||
{"op type mismatch",
|
||||
system.New(t.Context(), 150).
|
||||
ChangeHosts("chronos").
|
||||
CopyFile(new([]byte), "/home/ophestra/xdg/config/pulse/cookie", 0, 256),
|
||||
system.New(t.Context(), 150).
|
||||
ChangeHosts("chronos").
|
||||
Ensure("/run", 0755),
|
||||
false,
|
||||
},
|
||||
{
|
||||
"op equals",
|
||||
false},
|
||||
|
||||
{"op equals",
|
||||
system.New(t.Context(), 150).
|
||||
ChangeHosts("chronos").
|
||||
Ensure("/run", 0755),
|
||||
system.New(t.Context(), 150).
|
||||
ChangeHosts("chronos").
|
||||
Ensure("/run", 0755),
|
||||
true,
|
||||
},
|
||||
true},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
if tc.sys.Equal(tc.v) != tc.want {
|
||||
t.Errorf("Equal: got %v; want %v",
|
||||
!tc.want, tc.want)
|
||||
t.Errorf("Equal: %v, want %v", !tc.want, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user