From 113444a24dec3bc97f85df20a856c0650dabc0cd Mon Sep 17 00:00:00 2001 From: logica0419 <84759675+logica0419@users.noreply.github.com> Date: Mon, 11 Dec 2023 07:42:48 +0900 Subject: [PATCH] moved MustOpenGif to testutils --- service/imaging/processor_default_test.go | 43 +++++++++-------------- testutils/gif.go | 15 ++++++++ 2 files changed, 32 insertions(+), 26 deletions(-) create mode 100644 testutils/gif.go diff --git a/service/imaging/processor_default_test.go b/service/imaging/processor_default_test.go index 032a35610..02a9c4e57 100644 --- a/service/imaging/processor_default_test.go +++ b/service/imaging/processor_default_test.go @@ -6,12 +6,11 @@ import ( "image" "image/png" "io" - "io/fs" "os" "testing" "github.com/stretchr/testify/assert" - "github.com/traPtitech/traQ/testdata/gif" + "github.com/traPtitech/traQ/testutils" "github.com/traPtitech/traQ/utils" ) @@ -34,6 +33,15 @@ func setup() (Processor, *os.File) { return processor, mustOpen("test.png") } +func setupCustomConc(conc int) Processor { + processor := NewProcessor(Config{ + MaxPixels: 500 * 500, + Concurrency: conc, + ThumbnailMaxSize: image.Point{X: 50, Y: 50}, + }) + return processor +} + func assertImg(t *testing.T, actualImg image.Image, expectedFilePath string) { actualImgBytesBuffer := &bytes.Buffer{} err := png.Encode(actualImgBytesBuffer, actualImg) @@ -71,23 +79,6 @@ func TestProcessorDefault_Fit(t *testing.T) { assertImg(t, actualImg, "test_fit.png") } -func setupCustomConc(conc int) Processor { - processor := NewProcessor(Config{ - MaxPixels: 500 * 500, - Concurrency: conc, - ThumbnailMaxSize: image.Point{X: 50, Y: 50}, - }) - return processor -} - -func mustOpenGif(name string) fs.File { - f, err := gif.FS.Open(name) - if err != nil { - panic(err) - } - return f -} - func TestProcessorDefault_FitAnimationGIF(t *testing.T) { t.Parallel() @@ -108,38 +99,38 @@ func TestProcessorDefault_FitAnimationGIF(t *testing.T) { }, { name: "invalid (invalid gif)", - reader: io.LimitReader(mustOpenGif("cube.gif"), 10), + reader: io.LimitReader(testutils.MustOpenGif("cube.gif"), 10), want: nil, err: ErrInvalidImageSrc, }, { name: "success (cube 正方形、透明ピクセルあり)", file: "cube.gif", - want: utils.MustIoReaderToBytes(mustOpenGif("cube_resized.gif")), + want: utils.MustIoReaderToBytes(testutils.MustOpenGif("cube_resized.gif")), err: nil, }, { name: "success (miku 横長、差分最適化)", file: "miku.gif", - want: utils.MustIoReaderToBytes(mustOpenGif("miku_resized.gif")), + want: utils.MustIoReaderToBytes(testutils.MustOpenGif("miku_resized.gif")), err: nil, }, { name: "success (parapara 正方形、差分最適化)", file: "parapara.gif", - want: utils.MustIoReaderToBytes(mustOpenGif("parapara_resized.gif")), + want: utils.MustIoReaderToBytes(testutils.MustOpenGif("parapara_resized.gif")), err: nil, }, { name: "success (miku2 縦長、差分最適化)", file: "miku2.gif", - want: utils.MustIoReaderToBytes(mustOpenGif("miku2_resized.gif")), + want: utils.MustIoReaderToBytes(testutils.MustOpenGif("miku2_resized.gif")), err: nil, }, { name: "success (rabbit 小サイズ)", file: "rabbit.gif", - want: utils.MustIoReaderToBytes(mustOpenGif("rabbit_resized.gif")), + want: utils.MustIoReaderToBytes(testutils.MustOpenGif("rabbit_resized.gif")), err: nil, }, } @@ -159,7 +150,7 @@ func TestProcessorDefault_FitAnimationGIF(t *testing.T) { processor := setupCustomConc(conc) if tt.file != "" { // ファイルはこのタイミングで開かないと正常なデータにならない - tt.reader = mustOpenGif(tt.file) + tt.reader = testutils.MustOpenGif(tt.file) } actual, err := processor.FitAnimationGIF(tt.reader, 256, 256) diff --git a/testutils/gif.go b/testutils/gif.go new file mode 100644 index 000000000..8d133bc90 --- /dev/null +++ b/testutils/gif.go @@ -0,0 +1,15 @@ +package testutils + +import ( + "io/fs" + + "github.com/traPtitech/traQ/testdata/gif" +) + +func MustOpenGif(name string) fs.File { + f, err := gif.FS.Open(name) + if err != nil { + panic(err) + } + return f +}