Skip to content

Commit

Permalink
lint corrections.
Browse files Browse the repository at this point in the history
  • Loading branch information
bengarrett committed Sep 10, 2024
1 parent 99398b9 commit c313102
Show file tree
Hide file tree
Showing 23 changed files with 187 additions and 182 deletions.
2 changes: 1 addition & 1 deletion handler/app/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
)

//go:embed*
var emptyFS embed.FS
var emptyFS embed.FS //nolint:gochecknoglobals

const (
exampleURL = "https://example.com"
Expand Down
3 changes: 1 addition & 2 deletions handler/app/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,7 @@ func artifacts(c echo.Context, db *sql.DB, uri string, page int) error {
BaseURL: "/files/" + uri,
RangeStep: steps(lastPage),
}
err = c.Render(http.StatusOK, name, data)
if err != nil {
if err = c.Render(http.StatusOK, name, data); err != nil {
return InternalErr(c, errs, err)
}
return nil
Expand Down
2 changes: 1 addition & 1 deletion handler/app/internal/filerecord/filerecord.go
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ func (e *entry) parseMusicID3(path string) bool {
}

// ListContent returns a list of the files contained in the archive file.
func ListContent(art *models.File, src string) template.HTML {
func ListContent(art *models.File, src string) template.HTML { //nolint:funlen
if art == nil {
return "error, no artifact"
}
Expand Down
2 changes: 1 addition & 1 deletion handler/app/internal/fileslice/fileslice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func TestRecords(t *testing.T) {
require.Error(t, err)
assert.Nil(t, x)

var proof = fileslice.URI(45).String()
proof := fileslice.URI(45).String()
x, err = fileslice.Records(context.TODO(), nil, proof, 1, 1)
require.Error(t, err)
assert.Nil(t, x)
Expand Down
44 changes: 23 additions & 21 deletions handler/app/internal/simple/simple.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import (
"fmt"
"html/template"
"image"
_ "image/gif" // gif format decoder
_ "image/jpeg" // jpeg format decoder
_ "image/png" // png format decoder
"net/http"
"net/url"
"os"
Expand All @@ -15,12 +18,6 @@ import (
"strings"
"time"

_ "image/gif" // gif format decoder
_ "image/jpeg" // jpeg format decoder
_ "image/png" // png format decoder

_ "golang.org/x/image/webp" // webp format decoder

"github.com/Defacto2/helper"
"github.com/Defacto2/magicnumber"
"github.com/Defacto2/releaser"
Expand All @@ -30,6 +27,7 @@ import (
"github.com/dustin/go-humanize"
"github.com/h2non/filetype"
"github.com/volatiletech/null/v8"
_ "golang.org/x/image/webp" // webp format decoder
)

var (
Expand Down Expand Up @@ -205,8 +203,10 @@ func ImageXY(name string) [2]string {
if err != nil {
return [2]string{err.Error(), ""}
}
return [2]string{humanize.Comma(st.Size()),
fmt.Sprintf("%dx%d", config.Width, config.Height)}
return [2]string{
humanize.Comma(st.Size()),
fmt.Sprintf("%dx%d", config.Width, config.Height),
}
}

// LinkID creates a URL to link to the record.
Expand Down Expand Up @@ -462,7 +462,6 @@ func ReleaserPair(a, b any) [2]string {
// Supported formats are webp, png, jpg and avif.
func Screenshot(unid, desc, previewDir string) template.HTML {
const separator = "/"
class := "rounded mx-auto d-block img-fluid"
alt := strings.ToLower(desc) + " screenshot"

srcW := strings.Join([]string{config.StaticOriginal(), unid + webp}, separator)
Expand All @@ -485,40 +484,43 @@ func Screenshot(unid, desc, previewDir string) template.HTML {
elm := template.HTML("<picture>")
switch {
case sizeA > 0:
elm += template.HTML(fmt.Sprintf("<source srcset=\"%s?%s\" type=\"image/avif\" integrity=\"%s\" />", srcA, hashA, hashA))
elm += template.HTML(fmt.Sprintf("<source srcset=\"%s?%s\""+
" type=\"image/avif\" integrity=\"%s\" />", srcA, hashA, hashA))
case sizeW > 0:
elm += template.HTML(fmt.Sprintf("<source srcset=\"%s?%s\" type=\"image/webp\" integrity=\"%s\" />", srcW, hashW, hashW))
elm += template.HTML(fmt.Sprintf("<source srcset=\"%s?%s\""+
" type=\"image/webp\" integrity=\"%s\" />", srcW, hashW, hashW))
}
// the <picture> element is used to provide multiple sources for an image.
// if no <img> element is provided, the <picture> element won't be rendered by the browser.
useSmallerJpg := sizeJ > 0 && sizeJ < sizeP
switch {
case useSmallerJpg:
elm += img(srcJ, alt, class, hashJ)
elm += img(srcJ, alt, hashJ)
case sizeP > 0:
elm += img(srcP, alt, class, hashP)
elm += img(srcP, alt, hashP)
default:
elm += img(srcJ, alt, class, hashJ)
elm += img(srcJ, alt, hashJ)
}
return elm + "</picture>"
}
switch {
case sizeA > 0:
return img(srcA, alt, class, hashA)
return img(srcA, alt, hashA)
case sizeW > 0:
return img(srcW, alt, class, hashW)
return img(srcW, alt, hashW)
case sizeJ > 0:
return img(srcJ, alt, class, hashJ)
return img(srcJ, alt, hashJ)
case sizeP > 0:
return img(srcP, alt, class, hashP)
return img(srcP, alt, hashP)
}
return ""
}

// img returns a HTML image tag.
func img(src, alt, class, integrity string) template.HTML {
return template.HTML(fmt.Sprintf("<img src=\"%s?%s\" loading=\"lazy\" alt=\"%s\" class=\"%s\" integrity=\"%s\" />",
src, integrity, alt, class, integrity))
func img(src, alt, integrity string) template.HTML {
return template.HTML(fmt.Sprintf("<img src=\"%s?%s\" loading=\"lazy\" alt=\"%s\""+
" class=\"rounded mx-auto d-block img-fluid\" integrity=\"%s\" />",
src, integrity, alt, integrity))
}

// StatHumanize returns the last modified date, size in bytes and size formatted
Expand Down
33 changes: 16 additions & 17 deletions handler/app/internal/simple/simple_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package simple_test

import (
"fmt"
"os"
"path/filepath"
"runtime"
Expand All @@ -15,7 +14,8 @@ import (
"github.com/volatiletech/null/v8"
)

func testImage(t *testing.T) string {
func imagefiler(t *testing.T) string {
t.Helper()
_, file, _, ok := runtime.Caller(0)
require.True(t, ok)
return filepath.Join(filepath.Dir(file), "testdata", "TEST.png")
Expand Down Expand Up @@ -158,9 +158,9 @@ func TestImageSampleStat(t *testing.T) {
t.Parallel()
x := simple.ImageSampleStat("", "")
assert.False(t, x)
name := filepath.Base(testImage(t))
name := filepath.Base(imagefiler(t))
name = strings.TrimSuffix(name, filepath.Ext(name))
dir := filepath.Dir(testImage(t))
dir := filepath.Dir(imagefiler(t))
x = simple.ImageSampleStat(name, dir)
assert.True(t, x)
}
Expand All @@ -169,11 +169,10 @@ func TestImageXY(t *testing.T) {
t.Parallel()
s := simple.ImageXY("")
assert.Contains(t, s, "stat : no such file or directory")
img := testImage(t)
img := imagefiler(t)
s = simple.ImageXY(img)
fmt.Println(img)
assert.Equal(t, s[0], "4,163")
assert.Equal(t, s[1], "500x500")
assert.Equal(t, "4,163", s[0])
assert.Equal(t, "500x500", s[1])
}

func TestLinkID(t *testing.T) {
Expand All @@ -185,7 +184,7 @@ func TestLinkID(t *testing.T) {
require.Error(t, err)
assert.Empty(t, s)
s, err = simple.LinkID(1, "")
assert.NoError(t, err)
require.NoError(t, err)
assert.Equal(t, "/9b1c6", s)
}

Expand Down Expand Up @@ -216,15 +215,15 @@ func TestMagicAsTitle(t *testing.T) {
t.Parallel()
s := simple.MagicAsTitle("")
assert.Equal(t, "file not found", s)
s = simple.MagicAsTitle(testImage(t))
s = simple.MagicAsTitle(imagefiler(t))
assert.Contains(t, s, "Portable Network Graphics")
}

func TestMIME(t *testing.T) {
t.Parallel()
s := simple.MIME("")
assert.Equal(t, "file not found", s)
s = simple.MIME(testImage(t))
s = simple.MIME(imagefiler(t))
assert.Equal(t, "image/png", s)
}

Expand Down Expand Up @@ -254,7 +253,7 @@ func TestScreenshot(t *testing.T) {
t.Parallel()
s := simple.Screenshot("", "", "")
assert.Empty(t, s)
dir := filepath.Dir(testImage(t))
dir := filepath.Dir(imagefiler(t))
s = simple.Screenshot("TEST", "test", dir)
assert.Contains(t, s, `alt="test screenshot"`)
assert.Contains(t, s, `<img src="/public/image`)
Expand All @@ -267,7 +266,7 @@ func TestStatHumanize(t *testing.T) {
assert.Equal(t, none, x)
assert.Equal(t, none, y)
assert.Equal(t, none, z)
x, y, z = simple.StatHumanize(testImage(t))
x, y, z = simple.StatHumanize(imagefiler(t))
assert.Equal(t, "2024-Sep-03", x)
assert.Equal(t, "4,163", y)
assert.Contains(t, z, "4.2 kB")
Expand All @@ -277,9 +276,9 @@ func TestThumb(t *testing.T) {
t.Parallel()
s := simple.Thumb("", "", "", false)
assert.Contains(t, "<!-- no thumbnail found -->", s)
name := filepath.Base(testImage(t))
name := filepath.Base(imagefiler(t))
name = strings.TrimSuffix(name, filepath.Ext(name))
dir := filepath.Dir(testImage(t))
dir := filepath.Dir(imagefiler(t))
s = simple.Thumb(name, "a description", dir, false)
assert.Contains(t, s, `alt="a description thumbnail"`)
}
Expand All @@ -289,9 +288,9 @@ func TestThumbSample(t *testing.T) {
const missing = "No thumbnail"
x := simple.ThumbSample("", "")
assert.Contains(t, x, missing)
name := filepath.Base(testImage(t))
name := filepath.Base(imagefiler(t))
name = strings.TrimSuffix(name, filepath.Ext(name))
dir := filepath.Dir(testImage(t))
dir := filepath.Dir(imagefiler(t))
x = simple.ThumbSample(name, dir)
assert.Contains(t, x, "sha384-SK3qCpS11QMhNxUUnyeUeWWXBMPORDgLTI")
}
2 changes: 1 addition & 1 deletion handler/app/milestone.go
Original file line number Diff line number Diff line change
Expand Up @@ -746,7 +746,7 @@ func Collection() Milestones {
"It is more believable that the kids formed these Berlin-based cracking groups at the earliest, in Christmas/New Years 1983-84, " +
"after the Commodore 64 dropped massively in price and became readily available.</p>" +
// citations
"</p><strong>The controversal citations</strong> <sup><a href=\"#berlin-bear-controversy-fn8\">[8]</a></sup>" +
"</p><strong>The controversial citations</strong> <sup><a href=\"#berlin-bear-controversy-fn8\">[8]</a></sup>" +
ul0 +
"<li><q>The first intro was a picture of the Berlin Bear from the city flag and was released by <abbr title=\"Berlin Cracking Service\">BCS</abbr> in <strong>1982</strong>. " +
"It was a kind of co-production by several people...</q> <sup><a href=\"#berlin-bear-controversy-fn9\">[9]</a></sup></li>" +
Expand Down
3 changes: 2 additions & 1 deletion handler/app/remote/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,8 @@ func (got *PouetLink) Download(c echo.Context, db *sql.DB, downloadDir string) e
if downloadURL == "" {
return nil
}
df, err := GetFile(downloadURL, 10*time.Second)
const timeout = 10 * time.Second
df, err := GetFile(downloadURL, timeout)
if err != nil {
return fmt.Errorf("could not get file, %s: %w", downloadURL, err)
}
Expand Down
3 changes: 2 additions & 1 deletion handler/app/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ import (

"github.com/Defacto2/server/handler/app"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestTemplTemplates(t *testing.T) {
t.Parallel()
tpl := app.Templ{}
x, err := tpl.Templates(nil)
assert.Error(t, err)
require.Error(t, err)
assert.Nil(t, x)
}

Expand Down
6 changes: 3 additions & 3 deletions handler/form/form_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@ import (

"github.com/Defacto2/server/handler/form"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestHumanizeCount(t *testing.T) {
t.Parallel()
html, err := form.HumanizeCount(nil, "", "")
assert.NoError(t, err)
require.NoError(t, err)
assert.Contains(t, html, `0 existing artifacts`)
htm := form.HumanizeCountStr(nil, "", "")
assert.NoError(t, err)
require.NoError(t, err)
assert.Contains(t, htm, `0 existing artifacts`)

}

func TestSanitizeFilename(t *testing.T) {
Expand Down
16 changes: 8 additions & 8 deletions handler/html3/html3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,25 +122,25 @@ func TestQuery(t *testing.T) {
assert.Empty(t, b)
assert.Empty(t, c)
assert.Empty(t, fs)
assert.Error(t, err)
require.Error(t, err)
a, b, c, fs, err = html3.Query(newContext(), nil, html3.Everything, -1)
assert.Empty(t, a)
assert.Empty(t, b)
assert.Empty(t, c)
assert.Empty(t, fs)
assert.Error(t, err)
require.Error(t, err)
_, _, _, _, err = html3.Query(newContext(), nil, html3.BySection, -1)
assert.Error(t, err)
require.Error(t, err)
_, _, _, _, err = html3.Query(newContext(), nil, html3.ByPlatform, -1)
assert.Error(t, err)
require.Error(t, err)
_, _, _, _, err = html3.Query(newContext(), nil, html3.ByGroup, -1)
assert.Error(t, err)
require.Error(t, err)
_, _, _, _, err = html3.Query(newContext(), nil, html3.AsArt, -1)
assert.Error(t, err)
require.Error(t, err)
_, _, _, _, err = html3.Query(newContext(), nil, html3.AsDocument, -1)
assert.Error(t, err)
require.Error(t, err)
_, _, _, _, err = html3.Query(newContext(), nil, html3.AsSoftware, -1)
assert.Error(t, err)
require.Error(t, err)
}

func TestListInfo(t *testing.T) {
Expand Down
8 changes: 4 additions & 4 deletions handler/htmx/htmx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,21 +111,21 @@ func TestDataList(t *testing.T) {
func TestTemplates(t *testing.T) {
t.Parallel()
x := htmx.Templates(embed.FS{})
assert.Equal(t, 3, len(x))
assert.Len(t, x, 3)
}

func TestTemplateFuncMap(t *testing.T) {
t.Parallel()
x := htmx.TemplateFuncMap()
assert.Greater(t, len(x), 0)
assert.Empty(t, len(x))
}

func TestSuggestion(t *testing.T) {
t.Parallel()
s := htmx.Suggestion("", "", "")
assert.Equal(t, s, "suggestion type error: string")
assert.Equal(t, "suggestion type error: string", s)
s = htmx.Suggestion("a group", "grp", 10)
assert.Equal(t, s, "a group, grp (10 items)")
assert.Equal(t, "a group, grp (10 items)", s)
}

func TestHumanizeCount(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion handler/pouet/pouet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,5 +124,5 @@ func TestPlatformType(t *testing.T) {
pp := pouet.Production{}
a, b := pp.PlatformType()
assert.Equal(t, tags.Tag(-1), a)
assert.Equal(t, tags.Tag(tags.Intro), b)
assert.Equal(t, tags.Intro, b)
}
Loading

0 comments on commit c313102

Please sign in to comment.