Skip to content

Commit 9f9e1f7

Browse files
author
Gusted
authored
Merge branch 'main' into fix-badCalls
2 parents 1965009 + 4744808 commit 9f9e1f7

File tree

191 files changed

+3564
-4953
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

191 files changed

+3564
-4953
lines changed

.eslintrc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,7 @@ rules:
368368
unicorn/no-array-push-push: [2]
369369
unicorn/no-console-spaces: [0]
370370
unicorn/no-document-cookie: [2]
371+
unicorn/no-empty-file: [2]
371372
unicorn/no-fn-reference-in-iterator: [0]
372373
unicorn/no-for-loop: [0]
373374
unicorn/no-hex-escape: [0]
@@ -404,6 +405,7 @@ rules:
404405
unicorn/prefer-date-now: [2]
405406
unicorn/prefer-default-parameters: [0]
406407
unicorn/prefer-event-key: [2]
408+
unicorn/prefer-export-from: [2]
407409
unicorn/prefer-includes: [2]
408410
unicorn/prefer-math-trunc: [2]
409411
unicorn/prefer-modern-dom-apis: [0]

.golangci.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ linters:
99
- unused
1010
- structcheck
1111
- varcheck
12-
- golint
1312
- dupl
1413
#- gocyclo # The cyclomatic complexety of a lot of functions is too high, we should refactor those another time.
1514
- gofmt

.stylelintrc

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,31 @@
11
extends: stylelint-config-standard
22

3+
overrides:
4+
- files: ["**/*.less"]
5+
customSyntax: postcss-less
6+
37
rules:
8+
alpha-value-notation: null
49
at-rule-empty-line-before: null
510
block-closing-brace-empty-line-before: null
11+
color-function-notation: null
612
color-hex-length: null
713
comment-empty-line-before: null
14+
declaration-block-no-redundant-longhand-properties: null
815
declaration-block-single-line-max-declarations: null
916
declaration-empty-line-before: null
17+
hue-degree-notation: null
1018
indentation: 2
19+
max-line-length: null
1120
no-descending-specificity: null
21+
no-invalid-position-at-import-rule: null
1222
number-leading-zero: never
23+
number-max-precision: null
24+
property-no-vendor-prefix: null
1325
rule-empty-line-before: null
26+
selector-class-pattern: null
27+
selector-id-pattern: null
1428
selector-pseudo-element-colon-notation: double
1529
shorthand-property-no-redundant-values: true
16-
no-invalid-position-at-import-rule: null
30+
string-quotes: null
31+
value-no-vendor-prefix: null

contrib/fixtures/fixture_generation.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"path/filepath"
1111

1212
"code.gitea.io/gitea/models"
13-
"code.gitea.io/gitea/models/db"
13+
"code.gitea.io/gitea/models/unittest"
1414
)
1515

1616
// To generate derivative fixtures, execute the following from Gitea's repository base dir:
@@ -31,13 +31,13 @@ var (
3131
func main() {
3232
pathToGiteaRoot := "."
3333
fixturesDir = filepath.Join(pathToGiteaRoot, "models", "fixtures")
34-
if err := db.CreateTestEngine(db.FixturesOptions{
34+
if err := unittest.CreateTestEngine(unittest.FixturesOptions{
3535
Dir: fixturesDir,
3636
}); err != nil {
3737
fmt.Printf("CreateTestEngine: %+v", err)
3838
os.Exit(1)
3939
}
40-
if err := db.PrepareTestDatabase(); err != nil {
40+
if err := unittest.PrepareTestDatabase(); err != nil {
4141
fmt.Printf("PrepareTestDatabase: %+v\n", err)
4242
os.Exit(1)
4343
}

contrib/pr/checkout.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626

2727
"code.gitea.io/gitea/models"
2828
"code.gitea.io/gitea/models/db"
29+
"code.gitea.io/gitea/models/unittest"
2930
gitea_git "code.gitea.io/gitea/modules/git"
3031
"code.gitea.io/gitea/modules/markup"
3132
"code.gitea.io/gitea/modules/markup/external"
@@ -99,16 +100,16 @@ func runPR() {
99100
})
100101
db.HasEngine = true
101102
//x.ShowSQL(true)
102-
err = db.InitFixtures(
103-
db.FixturesOptions{
103+
err = unittest.InitFixtures(
104+
unittest.FixturesOptions{
104105
Dir: path.Join(curDir, "models/fixtures/"),
105106
},
106107
)
107108
if err != nil {
108109
fmt.Printf("Error initializing test database: %v\n", err)
109110
os.Exit(1)
110111
}
111-
db.LoadFixtures()
112+
unittest.LoadFixtures()
112113
util.RemoveAll(setting.RepoRootPath)
113114
util.RemoveAll(models.LocalCopyPath())
114115
util.CopyDir(path.Join(curDir, "integrations/gitea-repositories-meta"), setting.RepoRootPath)

docs/content/doc/developers/guidelines-frontend.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,65 @@ We recommend [Google HTML/CSS Style Guide](https://google.github.io/styleguide/h
3939
6. The backend can pass complex data to the frontend by using `ctx.PageData["myModuleData"] = map[]{}`
4040
7. Simple pages and SEO-related pages use Go HTML Template render to generate static Fomantic-UI HTML output. Complex pages can use Vue2 (or Vue3 in future).
4141

42+
43+
### `async` Functions
44+
45+
Only mark a function as `async` if and only if there are `await` calls
46+
or `Promise` returns inside the function.
47+
48+
It's not recommended to use `async` event listeners, which may lead to problems.
49+
The reason is that the code after await is executed outside the event dispatch.
50+
Reference: https://github.com/github/eslint-plugin-github/blob/main/docs/rules/async-preventdefault.md
51+
52+
If we want to call an `async` function in a non-async context,
53+
it's recommended to use `const _promise = asyncFoo()` to tell readers
54+
that this is done by purpose, we want to call the async function and ignore the Promise.
55+
Some lint rules and IDEs also have warnings if the returned Promise is not handled.
56+
57+
#### DOM Event Listener
58+
59+
```js
60+
el.addEventListener('click', (e) => {
61+
(async () => {
62+
await asyncFoo(); // recommended
63+
// then we shound't do e.preventDefault() after await, no effect
64+
})();
65+
66+
const _promise = asyncFoo(); // recommended
67+
68+
e.preventDefault(); // correct
69+
});
70+
71+
el.addEventListener('async', async (e) => { // not recommended but acceptable
72+
e.preventDefault(); // acceptable
73+
await asyncFoo(); // skip out event dispath
74+
e.preventDefault(); // WRONG
75+
});
76+
```
77+
78+
#### jQuery Event Listener
79+
80+
```js
81+
$('#el').on('click', (e) => {
82+
(async () => {
83+
await asyncFoo(); // recommended
84+
// then we shound't do e.preventDefault() after await, no effect
85+
})();
86+
87+
const _promise = asyncFoo(); // recommended
88+
89+
e.preventDefault(); // correct
90+
return false; // correct
91+
});
92+
93+
$('#el').on('click', async (e) => { // not recommended but acceptable
94+
e.preventDefault(); // acceptable
95+
return false; // WRONG, jQuery expects the returned value is a boolean, not a Promise
96+
await asyncFoo(); // skip out event dispath
97+
return false; // WRONG
98+
});
99+
```
100+
42101
### Vue2/Vue3 and JSX
43102

44103
Gitea is using Vue2 now, we plan to upgrade to Vue3. We decided not to introduce JSX to keep the HTML and the JavaScript code separated.

integrations/api_issue_label_test.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import (
1010
"strings"
1111
"testing"
1212

13+
"code.gitea.io/gitea/models/unittest"
14+
1315
"code.gitea.io/gitea/models"
1416
"code.gitea.io/gitea/models/db"
1517
api "code.gitea.io/gitea/modules/structs"
@@ -18,7 +20,7 @@ import (
1820
)
1921

2022
func TestAPIModifyLabels(t *testing.T) {
21-
assert.NoError(t, db.LoadFixtures())
23+
assert.NoError(t, unittest.LoadFixtures())
2224

2325
repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 2}).(*models.Repository)
2426
owner := db.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User)
@@ -88,7 +90,7 @@ func TestAPIModifyLabels(t *testing.T) {
8890
}
8991

9092
func TestAPIAddIssueLabels(t *testing.T) {
91-
assert.NoError(t, db.LoadFixtures())
93+
assert.NoError(t, unittest.LoadFixtures())
9294

9395
repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository)
9496
issue := db.AssertExistsAndLoadBean(t, &models.Issue{RepoID: repo.ID}).(*models.Issue)
@@ -111,7 +113,7 @@ func TestAPIAddIssueLabels(t *testing.T) {
111113
}
112114

113115
func TestAPIReplaceIssueLabels(t *testing.T) {
114-
assert.NoError(t, db.LoadFixtures())
116+
assert.NoError(t, unittest.LoadFixtures())
115117

116118
repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository)
117119
issue := db.AssertExistsAndLoadBean(t, &models.Issue{RepoID: repo.ID}).(*models.Issue)
@@ -137,7 +139,7 @@ func TestAPIReplaceIssueLabels(t *testing.T) {
137139
}
138140

139141
func TestAPIModifyOrgLabels(t *testing.T) {
140-
assert.NoError(t, db.LoadFixtures())
142+
assert.NoError(t, unittest.LoadFixtures())
141143

142144
repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 3}).(*models.Repository)
143145
owner := db.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User)

integrations/integration_test.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import (
2525
"time"
2626

2727
"code.gitea.io/gitea/models"
28-
"code.gitea.io/gitea/models/db"
28+
"code.gitea.io/gitea/models/unittest"
2929
"code.gitea.io/gitea/modules/base"
3030
"code.gitea.io/gitea/modules/git"
3131
"code.gitea.io/gitea/modules/graceful"
@@ -84,6 +84,7 @@ func NewNilResponseHashSumRecorder() *NilResponseHashSumRecorder {
8484
func TestMain(m *testing.M) {
8585
defer log.Close()
8686

87+
unittest.InitUnitTestBridge()
8788
managerCtx, cancel := context.WithCancel(context.Background())
8889
graceful.InitManager(managerCtx)
8990
defer cancel()
@@ -112,8 +113,8 @@ func TestMain(m *testing.M) {
112113
}
113114
}
114115

115-
err := db.InitFixtures(
116-
db.FixturesOptions{
116+
err := unittest.InitFixtures(
117+
unittest.FixturesOptions{
117118
Dir: filepath.Join(filepath.Dir(setting.AppPath), "models/fixtures/"),
118119
},
119120
)
@@ -250,7 +251,7 @@ func prepareTestEnv(t testing.TB, skip ...int) func() {
250251
ourSkip += skip[0]
251252
}
252253
deferFn := PrintCurrentTest(t, ourSkip)
253-
assert.NoError(t, db.LoadFixtures())
254+
assert.NoError(t, unittest.LoadFixtures())
254255
assert.NoError(t, util.RemoveAll(setting.RepoRootPath))
255256

256257
assert.NoError(t, util.CopyDir(path.Join(filepath.Dir(setting.AppPath), "integrations/gitea-repositories-meta"), setting.RepoRootPath))
@@ -527,7 +528,7 @@ func GetCSRF(t testing.TB, session *TestSession, urlStr string) string {
527528
// within a single test this is required
528529
func resetFixtures(t *testing.T) {
529530
assert.NoError(t, queue.GetManager().FlushAll(context.Background(), -1))
530-
assert.NoError(t, db.LoadFixtures())
531+
assert.NoError(t, unittest.LoadFixtures())
531532
assert.NoError(t, util.RemoveAll(setting.RepoRootPath))
532533
assert.NoError(t, util.CopyDir(path.Join(filepath.Dir(setting.AppPath), "integrations/gitea-repositories-meta"), setting.RepoRootPath))
533534
}

integrations/migrate_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
"os"
99
"testing"
1010

11+
"code.gitea.io/gitea/models/unittest"
12+
1113
"code.gitea.io/gitea/models"
1214
"code.gitea.io/gitea/models/db"
1315
"code.gitea.io/gitea/modules/migrations"
@@ -17,7 +19,7 @@ import (
1719
)
1820

1921
func TestMigrateLocalPath(t *testing.T) {
20-
assert.NoError(t, db.PrepareTestDatabase())
22+
assert.NoError(t, unittest.PrepareTestDatabase())
2123

2224
adminUser := db.AssertExistsAndLoadBean(t, &models.User{Name: "user1"}).(*models.User)
2325

integrations/repofiles_delete_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ import (
88
"net/url"
99
"testing"
1010

11+
"code.gitea.io/gitea/models/unittest"
12+
1113
"code.gitea.io/gitea/models"
12-
"code.gitea.io/gitea/models/db"
1314
"code.gitea.io/gitea/modules/repofiles"
1415
api "code.gitea.io/gitea/modules/structs"
1516
"code.gitea.io/gitea/modules/test"
@@ -67,7 +68,7 @@ func TestDeleteRepoFile(t *testing.T) {
6768

6869
func testDeleteRepoFile(t *testing.T, u *url.URL) {
6970
// setup
70-
db.PrepareTestEnv(t)
71+
unittest.PrepareTestEnv(t)
7172
ctx := test.MockContext(t, "user2/repo1")
7273
ctx.SetParams(":id", "1")
7374
test.LoadRepo(t, ctx, 1)
@@ -106,7 +107,7 @@ func TestDeleteRepoFileWithoutBranchNames(t *testing.T) {
106107

107108
func testDeleteRepoFileWithoutBranchNames(t *testing.T, u *url.URL) {
108109
// setup
109-
db.PrepareTestEnv(t)
110+
unittest.PrepareTestEnv(t)
110111
ctx := test.MockContext(t, "user2/repo1")
111112
ctx.SetParams(":id", "1")
112113
test.LoadRepo(t, ctx, 1)
@@ -136,7 +137,7 @@ func testDeleteRepoFileWithoutBranchNames(t *testing.T, u *url.URL) {
136137

137138
func TestDeleteRepoFileErrors(t *testing.T) {
138139
// setup
139-
db.PrepareTestEnv(t)
140+
unittest.PrepareTestEnv(t)
140141
ctx := test.MockContext(t, "user2/repo1")
141142
ctx.SetParams(":id", "1")
142143
test.LoadRepo(t, ctx, 1)

models/access_test.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@ import (
88
"testing"
99

1010
"code.gitea.io/gitea/models/db"
11+
"code.gitea.io/gitea/models/unittest"
1112
"github.com/stretchr/testify/assert"
1213
)
1314

1415
func TestAccessLevel(t *testing.T) {
15-
assert.NoError(t, db.PrepareTestDatabase())
16+
assert.NoError(t, unittest.PrepareTestDatabase())
1617

1718
user2 := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
1819
user5 := db.AssertExistsAndLoadBean(t, &User{ID: 5}).(*User)
@@ -63,7 +64,7 @@ func TestAccessLevel(t *testing.T) {
6364
}
6465

6566
func TestHasAccess(t *testing.T) {
66-
assert.NoError(t, db.PrepareTestDatabase())
67+
assert.NoError(t, unittest.PrepareTestDatabase())
6768

6869
user1 := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
6970
user2 := db.AssertExistsAndLoadBean(t, &User{ID: 5}).(*User)
@@ -89,7 +90,7 @@ func TestHasAccess(t *testing.T) {
8990
}
9091

9192
func TestUser_GetRepositoryAccesses(t *testing.T) {
92-
assert.NoError(t, db.PrepareTestDatabase())
93+
assert.NoError(t, unittest.PrepareTestDatabase())
9394

9495
user1 := db.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User)
9596
accesses, err := user1.GetRepositoryAccesses()
@@ -103,7 +104,7 @@ func TestUser_GetRepositoryAccesses(t *testing.T) {
103104
}
104105

105106
func TestUser_GetAccessibleRepositories(t *testing.T) {
106-
assert.NoError(t, db.PrepareTestDatabase())
107+
assert.NoError(t, unittest.PrepareTestDatabase())
107108

108109
user1 := db.AssertExistsAndLoadBean(t, &User{ID: 1}).(*User)
109110
repos, err := user1.GetAccessibleRepositories(0)
@@ -123,7 +124,7 @@ func TestUser_GetAccessibleRepositories(t *testing.T) {
123124

124125
func TestRepository_RecalculateAccesses(t *testing.T) {
125126
// test with organization repo
126-
assert.NoError(t, db.PrepareTestDatabase())
127+
assert.NoError(t, unittest.PrepareTestDatabase())
127128
repo1 := db.AssertExistsAndLoadBean(t, &Repository{ID: 3}).(*Repository)
128129
assert.NoError(t, repo1.GetOwner())
129130

@@ -140,7 +141,7 @@ func TestRepository_RecalculateAccesses(t *testing.T) {
140141

141142
func TestRepository_RecalculateAccesses2(t *testing.T) {
142143
// test with non-organization repo
143-
assert.NoError(t, db.PrepareTestDatabase())
144+
assert.NoError(t, unittest.PrepareTestDatabase())
144145
repo1 := db.AssertExistsAndLoadBean(t, &Repository{ID: 4}).(*Repository)
145146
assert.NoError(t, repo1.GetOwner())
146147

@@ -154,7 +155,7 @@ func TestRepository_RecalculateAccesses2(t *testing.T) {
154155
}
155156

156157
func TestRepository_RecalculateAccesses3(t *testing.T) {
157-
assert.NoError(t, db.PrepareTestDatabase())
158+
assert.NoError(t, unittest.PrepareTestDatabase())
158159
team5 := db.AssertExistsAndLoadBean(t, &Team{ID: 5}).(*Team)
159160
user29 := db.AssertExistsAndLoadBean(t, &User{ID: 29}).(*User)
160161

0 commit comments

Comments
 (0)