Skip to content

Commit

Permalink
Some refactoring around tests to remove embedding.
Browse files Browse the repository at this point in the history
  • Loading branch information
g026r committed Sep 14, 2024
1 parent 541f86d commit c168362
Show file tree
Hide file tree
Showing 14 changed files with 41 additions and 42 deletions.
10 changes: 8 additions & 2 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
27 changes: 12 additions & 15 deletions pkg/model/entry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ package model

import (
"bytes"
"embed"
"io/fs"
"os"
"slices"
"testing"

Expand All @@ -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}
Expand Down Expand Up @@ -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 {
Expand Down
22 changes: 10 additions & 12 deletions pkg/model/playtimes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package model
import (
"bytes"
"encoding/binary"
"io/fs"
"os"
"slices"
"testing"
"time"
Expand Down Expand Up @@ -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 {
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion pkg/model/thumbnail.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
22 changes: 10 additions & 12 deletions pkg/model/thumbnail_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package model

import (
"image"
"io/fs"
"os"
"testing"

"github.com/disintegration/imaging"
Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit c168362

Please sign in to comment.