From c168362c3c27dd11023ec31b431482f7d590bec9 Mon Sep 17 00:00:00 2001 From: g026r Date: Sat, 14 Sep 2024 17:08:25 -0400 Subject: [PATCH] Some refactoring around tests to remove embedding. --- cmd/main.go | 10 +++++-- pkg/model/entry_test.go | 27 ++++++++---------- pkg/model/playtimes_test.go | 22 +++++++------- .../count_mismatch/list.bin | Bin .../count_mismatch/ngp_thumbs.bin | Bin .../count_mismatch/playtimes.bin | Bin .../invalid_header/list.bin | Bin .../invalid_header/ngp_thumbs.bin | Bin .../invalid_header/playtimes.bin | Bin pkg/model/{tests => testdata}/valid/list.bin | Bin .../{tests => testdata}/valid/ngp_thumbs.bin | Bin .../{tests => testdata}/valid/playtimes.bin | Bin pkg/model/thumbnail.go | 2 +- pkg/model/thumbnail_test.go | 22 +++++++------- 14 files changed, 41 insertions(+), 42 deletions(-) rename pkg/model/{tests => testdata}/count_mismatch/list.bin (100%) rename pkg/model/{tests => testdata}/count_mismatch/ngp_thumbs.bin (100%) rename pkg/model/{tests => testdata}/count_mismatch/playtimes.bin (100%) rename pkg/model/{tests => testdata}/invalid_header/list.bin (100%) rename pkg/model/{tests => testdata}/invalid_header/ngp_thumbs.bin (100%) rename pkg/model/{tests => testdata}/invalid_header/playtimes.bin (100%) rename pkg/model/{tests => testdata}/valid/list.bin (100%) rename pkg/model/{tests => testdata}/valid/ngp_thumbs.bin (100%) rename pkg/model/{tests => testdata}/valid/playtimes.bin (100%) diff --git a/cmd/main.go b/cmd/main.go index 0f11c06..0281cb7 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -71,7 +71,10 @@ func loadPocketDir(d string) (pkg.Application, error) { root := os.DirFS(d) - pg, _ := fs.Sub(root, "System/Played Games") + pg, err := fs.Sub(root, "System/Played Games") + if err != nil { + return pkg.Application{}, nil + } entries, err := model.ReadEntries(pg) if err != nil { return pkg.Application{}, err @@ -85,7 +88,10 @@ func loadPocketDir(d string) (pkg.Application, error) { return pkg.Application{}, fmt.Errorf("entry count mismatch between list.bin [%d] & playtimes.bin [%d]", len(entries), len(playtimes)) } - tb, _ := fs.Sub(root, "System/Library/Images") + tb, err := fs.Sub(root, "System/Library/Images") + if err != nil { + return pkg.Application{}, nil + } thumbs, err := model.LoadThumbnails(tb) if err != nil { return pkg.Application{}, err diff --git a/pkg/model/entry_test.go b/pkg/model/entry_test.go index 1b4ae11..083feac 100644 --- a/pkg/model/entry_test.go +++ b/pkg/model/entry_test.go @@ -2,8 +2,7 @@ package model import ( "bytes" - "embed" - "io/fs" + "os" "slices" "testing" @@ -15,8 +14,8 @@ import ( // invalid_header: header magic number is invalid; should not parse // valid: contains 229 entries, all valid // -//go:embed tests -var files embed.FS +////go:embed testdata +//var files embed.FS // rawEntry is the binary representation of entry, as copied from a valid list.bin var rawEntry = []byte{0x1C, 0x00, 0x00, 0x07, 0x6D, 0x8D, 0xE0, 0xFD, 0x3E, 0x1A, 0xCD, 0x79, 0x94, 0x1B, 0x00, 0x00, 0x31, 0x39, 0x34, 0x33, 0x20, 0x4B, 0x61, 0x69, 0x00, 0x45, 0x00, 0xA0} @@ -124,24 +123,22 @@ func TestReadEntries(t *testing.T) { cases := map[string]struct { count int err bool - }{"tests/count_mismatch": { - count: 4, - }, - "tests/invalid_header": { + }{ + "testdata/count_mismatch": { + count: 4, + }, + "testdata/invalid_header": { err: true, }, - "tests/valid": { + "testdata/valid": { count: 229, - }} + }, + } for k, v := range cases { t.Run(k, func(t *testing.T) { t.Parallel() - fsys, err := fs.Sub(files, k) - if err != nil { - t.Fatal(err) - } - pt, err := ReadEntries(fsys) + pt, err := ReadEntries(os.DirFS(k)) if (err != nil) != v.err { t.Error(err) } else if len(pt) != v.count { diff --git a/pkg/model/playtimes_test.go b/pkg/model/playtimes_test.go index 1124cf1..9626f22 100644 --- a/pkg/model/playtimes_test.go +++ b/pkg/model/playtimes_test.go @@ -3,7 +3,7 @@ package model import ( "bytes" "encoding/binary" - "io/fs" + "os" "slices" "testing" "time" @@ -57,24 +57,22 @@ func TestReadPlayTimes(t *testing.T) { cases := map[string]struct { count int err bool - }{"tests/count_mismatch": { - count: 4, - }, - "tests/invalid_header": { + }{ + "testdata/count_mismatch": { + count: 4, + }, + "testdata/invalid_header": { err: true, }, - "tests/valid": { + "testdata/valid": { count: 229, - }} + }, + } for k, v := range cases { t.Run(k, func(t *testing.T) { t.Parallel() - fsys, err := fs.Sub(files, k) - if err != nil { - t.Fatal(err) - } - pt, err := ReadPlayTimes(fsys) + pt, err := ReadPlayTimes(os.DirFS(k)) if (err != nil) != v.err { t.Error(err) } else if len(pt) != v.count { diff --git a/pkg/model/tests/count_mismatch/list.bin b/pkg/model/testdata/count_mismatch/list.bin similarity index 100% rename from pkg/model/tests/count_mismatch/list.bin rename to pkg/model/testdata/count_mismatch/list.bin diff --git a/pkg/model/tests/count_mismatch/ngp_thumbs.bin b/pkg/model/testdata/count_mismatch/ngp_thumbs.bin similarity index 100% rename from pkg/model/tests/count_mismatch/ngp_thumbs.bin rename to pkg/model/testdata/count_mismatch/ngp_thumbs.bin diff --git a/pkg/model/tests/count_mismatch/playtimes.bin b/pkg/model/testdata/count_mismatch/playtimes.bin similarity index 100% rename from pkg/model/tests/count_mismatch/playtimes.bin rename to pkg/model/testdata/count_mismatch/playtimes.bin diff --git a/pkg/model/tests/invalid_header/list.bin b/pkg/model/testdata/invalid_header/list.bin similarity index 100% rename from pkg/model/tests/invalid_header/list.bin rename to pkg/model/testdata/invalid_header/list.bin diff --git a/pkg/model/tests/invalid_header/ngp_thumbs.bin b/pkg/model/testdata/invalid_header/ngp_thumbs.bin similarity index 100% rename from pkg/model/tests/invalid_header/ngp_thumbs.bin rename to pkg/model/testdata/invalid_header/ngp_thumbs.bin diff --git a/pkg/model/tests/invalid_header/playtimes.bin b/pkg/model/testdata/invalid_header/playtimes.bin similarity index 100% rename from pkg/model/tests/invalid_header/playtimes.bin rename to pkg/model/testdata/invalid_header/playtimes.bin diff --git a/pkg/model/tests/valid/list.bin b/pkg/model/testdata/valid/list.bin similarity index 100% rename from pkg/model/tests/valid/list.bin rename to pkg/model/testdata/valid/list.bin diff --git a/pkg/model/tests/valid/ngp_thumbs.bin b/pkg/model/testdata/valid/ngp_thumbs.bin similarity index 100% rename from pkg/model/tests/valid/ngp_thumbs.bin rename to pkg/model/testdata/valid/ngp_thumbs.bin diff --git a/pkg/model/tests/valid/playtimes.bin b/pkg/model/testdata/valid/playtimes.bin similarity index 100% rename from pkg/model/tests/valid/playtimes.bin rename to pkg/model/testdata/valid/playtimes.bin diff --git a/pkg/model/thumbnail.go b/pkg/model/thumbnail.go index 08b0cc3..b6ae720 100644 --- a/pkg/model/thumbnail.go +++ b/pkg/model/thumbnail.go @@ -93,7 +93,7 @@ func LoadThumbnails(fs fs.FS) (map[util.System]Thumbnails, error) { } else { // This does present the problem that a file with the wrong number of entries in the count will wind up with one really weird // entry. But not sure that can really be helped, since there isn't a terminator or image size field for the entries - end, _ := f.Seek(0, io.SeekEnd) // fs.FS is terrible & I wouldn't be using it if it wasn't easier to test this way + end, _ := f.Seek(0, io.SeekEnd) // since a fs.File doesn't have a Size() func, we have to do it this way. t.Images[i].Image = make([]byte, end-int64(t.Images[i].address)) _, _ = f.Seek(int64(t.Images[i].address), io.SeekStart) } diff --git a/pkg/model/thumbnail_test.go b/pkg/model/thumbnail_test.go index 1da1cc1..28a5ba0 100644 --- a/pkg/model/thumbnail_test.go +++ b/pkg/model/thumbnail_test.go @@ -2,7 +2,7 @@ package model import ( "image" - "io/fs" + "os" "testing" "github.com/disintegration/imaging" @@ -61,24 +61,22 @@ func TestLoadThumbnails(t *testing.T) { cases := map[string]struct { count int err bool - }{"tests/count_mismatch": { - count: 2, - }, - "tests/invalid_header": { + }{ + "testdata/count_mismatch": { + count: 2, + }, + "testdata/invalid_header": { err: true, }, - "tests/valid": { + "testdata/valid": { count: 7, - }} + }, + } for k, v := range cases { t.Run(k, func(t *testing.T) { t.Parallel() - fsys, err := fs.Sub(files, k) - if err != nil { - t.Fatal(err) - } - pt, err := LoadThumbnails(fsys) + pt, err := LoadThumbnails(os.DirFS(k)) if (err != nil) != v.err { t.Error(err) }