Skip to content

Commit

Permalink
Test MusicAnalyzer and handle unexpected EOF
Browse files Browse the repository at this point in the history
  • Loading branch information
hryx committed Oct 18, 2022
1 parent ffdae1e commit fe8cc15
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 4 deletions.
12 changes: 8 additions & 4 deletions zipserver/music_analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"io"

"github.com/dhowden/tag"
"github.com/go-errors/errors"
)

// MusicAnalyzer uses rules according to music albums.
Expand All @@ -15,7 +14,7 @@ import (
type MusicAnalyzer struct{}

func (m MusicAnalyzer) Analyze(r io.Reader, key string) (AnalyzeResult, error) {
res := AnalyzeResult{Key: key}
res := AnalyzeResult{}

// TODO: The music tag library requires a ReadSeeker (understandably),
// which a Reader does not satisfy. Here is a naive implementation that
Expand All @@ -25,13 +24,17 @@ func (m MusicAnalyzer) Analyze(r io.Reader, key string) (AnalyzeResult, error) {

b, err := io.ReadAll(r)
if err != nil {
return res, errors.Wrap(err, 0)
return res, fmt.Errorf("read bytes: %w", err)
}

rs := bytes.NewReader(b)
md, err := tag.ReadFrom(rs)
if err != nil {
return res, errors.Wrap(err, 0)
// Tag parser expects at least 11 bytes.
if err == io.ErrUnexpectedEOF {
return res, fmt.Errorf("%w: file too short", ErrSkipped)
}
return res, fmt.Errorf("new tag reader: %w", err)
}

// Package tag provides these already, but let's set them explicitly
Expand Down Expand Up @@ -71,6 +74,7 @@ func (m MusicAnalyzer) Analyze(r io.Reader, key string) (AnalyzeResult, error) {
Lyrics: md.Lyrics(),
Comment: md.Comment(),
}
res.Key = key

return res, nil
}
Expand Down
97 changes: 97 additions & 0 deletions zipserver/music_analyzer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package zipserver

import (
"bytes"
"embed"
"errors"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

//go:embed testdata/music/*
var testAudioFiles embed.FS

func TestMusicAnalyzer(t *testing.T) {
testCases := []struct {
name string
err error
res AnalyzeResult
}{
{
name: "tone.thisisflac",
res: AnalyzeResult{
Key: "key",
ContentType: "audio/flac",
Metadata: TrackInfo{
FileType: "FLAC",
Title: "Long drone",
Album: "Fall asleep",
Artist: "Smith",
AlbumArtist: "Smith",
Composer: "Smith",
Year: 2005,
Track: 4,
TrackTotal: 6,
Disc: 2,
DiscTotal: 2,
},
},
},
{
name: "tone.thisisopus",
res: AnalyzeResult{
Key: "key",
ContentType: "audio/ogg",
Metadata: TrackInfo{
FileType: "Ogg",
Title: "Halloween song 2",
Album: "Don't listen to any of these songs",
Artist: "Creepy Weirdo",
Genre: "horror",
Year: 2022,
Track: 12,
TrackTotal: 666,
Lyrics: "pumpkins yeah\\ndecorate your house",
Comment: "spooky tune",
},
},
},
{
name: "tone.thisismp3",
res: AnalyzeResult{
Key: "key",
ContentType: "audio/mpeg",
Metadata: TrackInfo{
FileType: "MP3",
Title: "Boring",
Album: "Wat?",
Artist: "Small Dude",
Genre: "Acoustic",
Year: 1994,
Track: 2,
TrackTotal: 10,
Comment: "wrote this when I was bored",
},
},
},
{
name: "garbage.dat",
err: ErrSkipped,
},
}

var analyzer MusicAnalyzer

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
b, err := testAudioFiles.ReadFile("testdata/music/" + tc.name)
require.NoError(t, err)
buf := bytes.NewBuffer(b)
res, err := analyzer.Analyze(buf, "key")
require.Truef(t, errors.Is(err, tc.err), "error %q does not wrap %q", err, tc.err)
assert.Equal(t, tc.res, res)
})
}
}
1 change: 1 addition & 0 deletions zipserver/testdata/music/README.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Data files are renamed to test content detection.
1 change: 1 addition & 0 deletions zipserver/testdata/music/garbage.dat
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
��D��`
Binary file added zipserver/testdata/music/tone.thisisflac
Binary file not shown.
Binary file added zipserver/testdata/music/tone.thisismp3
Binary file not shown.
Binary file added zipserver/testdata/music/tone.thisisopus
Binary file not shown.

0 comments on commit fe8cc15

Please sign in to comment.