Files
team/server/channels/app/imaging/preview_test.go
Jesse HallamandGitHub edf2075381 MM-68149: Upgrade to Go 1.26.2 (#36418)
* MM-68149: upgrade to Go 1.26.2

Update go directive in go.mod and .go-version.

* MM-68149: replace pointer helpers with Go 1.26 new()

Go 1.26 extends the built-in new() to accept an initial value expression,
making typed-pointer helpers like model.NewPointer(x), bToP(x), and boolPtr(x)
redundant. Replace every call site with new(x) and remove the now-unused
helper functions and their //go:fix inline directives.

* MM-68149: apply go fix for reflect API and format-string changes

- reflect.Ptr → reflect.Pointer (renamed in Go 1.18, deprecated alias removed in 1.26)
- reflect range-over-struct: for i := 0; i < t.NumField(); i++ → for field := range t.Fields()
  and the equivalent for Methods() and interface types
- Fix format-string concatenation and variadic-arg mismatches flagged by go vet

* MM-68149: update JPEG fixtures and test infrastructure for Go 1.26 encoder

Go 1.26 ships a new image/jpeg encoder that produces slightly different output.
Regenerate all JPEG fixture files and switch the comparison helpers from
byte-equality to pixel-level comparison with a small per-channel tolerance,
so minor encoder drift across patch versions is handled automatically.

Add -update-fixtures flag to make it easy to regenerate fixtures after future
major Go upgrades. Document the update procedure in tests/README.md.

* MM-68149: CI check that go fix ./... produces no changes

* Fix real bugs flagged by CodeRabbit review

- group.go: set newGroup.MemberCount not group.MemberCount (member count
  was populated on the wrong variable and lost before publish/return)
- file_test.go: guard compareImage(GetFilePreview) on the preview slice
  length, not the thumbnail slice length (copy-paste error)
- config_test.go: remove duplicate MinimumLength assignment

* fixup! Fix real bugs flagged by CodeRabbit review
2026-05-12 15:59:12 +00:00

155 lines
3.9 KiB
Go

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package imaging
import (
"bytes"
"flag"
"image"
"os"
"path/filepath"
"testing"
"github.com/mattermost/mattermost/server/v8/channels/utils/fileutils"
"github.com/stretchr/testify/require"
)
var updateImagingFixtures = flag.Bool("update-fixtures", false, "overwrite imaging fixture files with actual output")
func TestGenerateThumbnail(t *testing.T) {
tcs := []struct {
name string
inputImg image.Image
targetWidth int
targetHeight int
expectedWidth int
expectedHeight int
}{
{
name: "empty image",
inputImg: image.NewRGBA(image.Rect(0, 0, 0, 0)),
targetWidth: 120,
targetHeight: 100,
},
{
name: "both dimensions lower than targets",
inputImg: image.NewRGBA(image.Rect(0, 0, 100, 50)),
targetWidth: 120,
targetHeight: 100,
expectedWidth: 120,
expectedHeight: 60,
},
{
name: "both dimensions equal to targets",
inputImg: image.NewRGBA(image.Rect(0, 0, 120, 100)),
targetWidth: 120,
targetHeight: 100,
expectedWidth: 120,
expectedHeight: 100,
},
{
name: "both dimensions higher than targets",
inputImg: image.NewRGBA(image.Rect(0, 0, 1000, 500)),
targetWidth: 120,
targetHeight: 100,
expectedWidth: 120,
expectedHeight: 60,
},
{
name: "width higher than target",
inputImg: image.NewRGBA(image.Rect(0, 0, 200, 100)),
targetWidth: 120,
targetHeight: 100,
expectedWidth: 120,
expectedHeight: 60,
},
{
name: "height higher than target",
inputImg: image.NewRGBA(image.Rect(0, 0, 100, 200)),
targetWidth: 120,
targetHeight: 100,
expectedWidth: 50,
expectedHeight: 100,
},
}
for _, tc := range tcs {
t.Run(tc.name, func(t *testing.T) {
thumb := GenerateThumbnail(tc.inputImg, tc.targetWidth, tc.targetHeight)
require.Equal(t, tc.expectedWidth, thumb.Bounds().Dx(), "expectedWidth")
require.Equal(t, tc.expectedHeight, thumb.Bounds().Dy(), "expectedHeight")
})
}
}
func TestGeneratePreview(t *testing.T) {
imgDir, ok := fileutils.FindDir("tests")
require.True(t, ok)
d, err := NewDecoder(DecoderOptions{})
require.NoError(t, err)
require.NotNil(t, d)
inputFile, err := os.Open(filepath.Join(imgDir, "qa-data-graph.png"))
require.NoError(t, err)
defer inputFile.Close()
inputImg, format, err := d.Decode(inputFile)
require.NoError(t, err)
require.Equal(t, "png", format)
expectedFile, err := os.Open(filepath.Join(imgDir, "preview_test_qa_data_graph_1024.png"))
require.NoError(t, err)
defer expectedFile.Close()
expectedImg, format, err := d.Decode(expectedFile)
require.NoError(t, err)
require.Equal(t, "png", format)
preview := GeneratePreview(inputImg, 1024)
requireSameImage(t, expectedImg, preview)
}
func TestGenerateMiniPreviewImage(t *testing.T) {
imgDir, ok := fileutils.FindDir("tests")
require.True(t, ok)
d, err := NewDecoder(DecoderOptions{})
require.NoError(t, err)
require.NotNil(t, d)
inputFile, err := os.Open(filepath.Join(imgDir, "qa-data-graph.png"))
require.NoError(t, err)
defer inputFile.Close()
inputImg, format, err := d.Decode(inputFile)
require.NoError(t, err)
require.Equal(t, "png", format)
fixturePath := filepath.Join(imgDir, "mini_preview_test_qa_data_graph_16x16_q90.jpg")
out, err := GenerateMiniPreviewImage(inputImg, 16, 16, 90)
require.NoError(t, err)
if *updateImagingFixtures {
require.NoError(t, os.WriteFile(fixturePath, out, 0600))
return
}
expectedFile, err := os.Open(fixturePath)
require.NoError(t, err)
defer expectedFile.Close()
expectedImg, format, err := d.Decode(expectedFile)
require.NoError(t, err)
require.Equal(t, "jpeg", format)
actualImg, format, err := d.Decode(bytes.NewReader(out))
require.NoError(t, err)
require.Equal(t, "jpeg", format)
requireSameImage(t, expectedImg, actualImg)
}