package testsuite_test import ( "bytes" "errors" "fmt" "io/fs" "testing" "testing/fstest" "hakurei.app/internal/testsuite" ) func TestCompare(t *testing.T) { var ( fsPasswdSample = "u0_a20:x:65534:65534:Hakurei:/var/lib/persist/module/hakurei/u0/a20:/run/current-system/sw/bin/zsh" fsGroupSample = "hakurei:x:65534:" ) testCases := []struct { name string sample fstest.MapFS want *testsuite.FS wantOut string wantErr error }{ {"skip", fstest.MapFS{}, &testsuite.FS{}, "[ OK ] s .\x00", nil}, {"simple pass", fstest.MapFS{".hakurei": {Mode: 0x800001ed}}, &testsuite.FS{Dir: map[string]*testsuite.FS{".hakurei": {Mode: 0x800001ed}}}, "[ OK ] s .hakurei\x00[ OK ] d .\x00", nil}, {"bad length", fstest.MapFS{".hakurei": {Mode: 0x800001ed}}, &testsuite.FS{Dir: make(map[string]*testsuite.FS)}, "[FAIL] d .: \".hakurei/\"\x00", testsuite.ErrFSBadLength}, {"top level bad mode", fstest.MapFS{".hakurei": {Mode: 0x800001ed}}, &testsuite.FS{Dir: map[string]*testsuite.FS{".hakurei": {Mode: 0xdeadbeef}}}, "[FAIL] m .hakurei: 020000000755, want 033653337357\x00", testsuite.ErrFSBadMode}, {"invalid entry condition", fstest.MapFS{"test": {Data: []byte{'0'}, Mode: 0644}}, &testsuite.FS{Dir: map[string]*testsuite.FS{"test": {Dir: make(map[string]*testsuite.FS)}}}, "[FAIL] d .: \"test\"\x00", testsuite.ErrFSInvalidEnt}, {"nonexistent", fstest.MapFS{"test": {Data: []byte{'0'}, Mode: 0644}}, &testsuite.FS{Dir: map[string]*testsuite.FS{".test": {}}}, "[FAIL] d .: \"test\"\x00", fs.ErrNotExist}, {"file", fstest.MapFS{"etc": {Mode: 0x800001c0}, "etc/passwd": {Data: []byte(fsPasswdSample), Mode: 0644}, "etc/group": {Data: []byte(fsGroupSample), Mode: 0644}, }, &testsuite.FS{Dir: map[string]*testsuite.FS{"etc": {Mode: 0x800001c0, Dir: map[string]*testsuite.FS{ "passwd": {Mode: 0x1a4, Data: &fsPasswdSample}, "group": {Mode: 0x1a4, Data: &fsGroupSample}, }}}}, "[ OK ] f etc/group\x00[ OK ] f etc/passwd\x00[ OK ] d etc\x00[ OK ] d .\x00", nil}, {"file differ", fstest.MapFS{"etc": {Mode: 0x800001c0}, "etc/passwd": {Data: []byte(fsPasswdSample), Mode: 0644}, "etc/group": {Data: []byte(fsGroupSample), Mode: 0644}, }, &testsuite.FS{Dir: map[string]*testsuite.FS{"etc": {Mode: 0x800001c0, Dir: map[string]*testsuite.FS{ "passwd": {Mode: 0x1a4, Data: &fsGroupSample}, "group": {Mode: 0x1a4, Data: &fsGroupSample}, }}}}, "[ OK ] f etc/group\x00[FAIL] f etc/passwd\n\t got: u0_a20:x:65534:65534:Hakurei:/var/lib/persist/module/hakurei/u0/a20:/run/current-system/sw/bin/zsh\n\twant: hakurei:x:65534:\x00", testsuite.ErrFSBadData}, } for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { var buf bytes.Buffer err := tc.want.Compare( func(format string, a ...any) { _, _ = fmt.Fprintf(&buf, format+"\x00", a...) }, ".", tc.sample, ) if !errors.Is(err, tc.wantErr) { t.Errorf( "Compare: error = %v; wantErr %v", err, tc.wantErr, ) } if buf.String() != tc.wantOut { t.Errorf( "Compare: output %q; want %q", &buf, tc.wantOut, ) } }) } }