loading up the forgejo repo on tangled to test page performance
1// Copyright 2021 The Gitea Authors. All rights reserved.
2// SPDX-License-Identifier: MIT
3
4package codeformat
5
6import (
7 "testing"
8
9 "github.com/stretchr/testify/assert"
10 "github.com/stretchr/testify/require"
11)
12
13func TestFormatImportsSimple(t *testing.T) {
14 formatted, err := formatGoImports([]byte(`
15package codeformat
16
17import (
18 "github.com/stretchr/testify/assert"
19 "testing"
20)
21`))
22
23 expected := `
24package codeformat
25
26import (
27 "testing"
28
29 "github.com/stretchr/testify/assert"
30)
31`
32
33 require.NoError(t, err)
34 assert.Equal(t, expected, string(formatted))
35}
36
37func TestFormatImportsGroup(t *testing.T) {
38 // gofmt/goimports won't group the packages, for example, they produce such code:
39 // "bytes"
40 // "image"
41 // (a blank line)
42 // "fmt"
43 // "image/color/palette"
44 // our formatter does better, and these packages are grouped into one.
45
46 formatted, err := formatGoImports([]byte(`
47package test
48
49import (
50 "bytes"
51 "fmt"
52 "image"
53 "image/color"
54
55 _ "image/gif" // for processing gif images
56 _ "image/jpeg" // for processing jpeg images
57 _ "image/png" // for processing png images
58
59 "code.gitea.io/other/package"
60
61 "forgejo.org/modules/setting"
62 "forgejo.org/modules/util"
63
64 "xorm.io/the/package"
65
66 "github.com/issue9/identicon"
67 "github.com/nfnt/resize"
68 "github.com/oliamb/cutter"
69)
70`))
71
72 expected := `
73package test
74
75import (
76 "bytes"
77 "fmt"
78 "image"
79 "image/color"
80
81 _ "image/gif" // for processing gif images
82 _ "image/jpeg" // for processing jpeg images
83 _ "image/png" // for processing png images
84
85 "forgejo.org/modules/setting"
86 "forgejo.org/modules/util"
87
88 "code.gitea.io/other/package"
89 "github.com/issue9/identicon"
90 "github.com/nfnt/resize"
91 "github.com/oliamb/cutter"
92 "xorm.io/the/package"
93)
94`
95
96 require.NoError(t, err)
97 assert.Equal(t, expected, string(formatted))
98}
99
100func TestFormatImportsInvalidComment(t *testing.T) {
101 // why we shouldn't write comments between imports: it breaks the grouping of imports
102 // for example:
103 // "pkg1"
104 // "pkg2"
105 // // a comment
106 // "pkgA"
107 // "pkgB"
108 // the comment splits the packages into two groups, pkg1/2 are sorted separately, pkgA/B are sorted separately
109 // we don't want such code, so the code should be:
110 // "pkg1"
111 // "pkg2"
112 // "pkgA" // a comment
113 // "pkgB"
114
115 _, err := formatGoImports([]byte(`
116package test
117
118import (
119 "image/jpeg"
120 // for processing gif images
121 "image/gif"
122)
123`))
124 require.ErrorIs(t, err, errInvalidCommentBetweenImports)
125}