Skip to content

Commit 9a40b16

Browse files
authored
Merge branch 'main' into custom-regexp-external-issues
2 parents 777dc41 + df64fa4 commit 9a40b16

File tree

143 files changed

+1178
-874
lines changed

Some content is hidden

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

143 files changed

+1178
-874
lines changed

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

models/action_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,22 @@ import (
99
"testing"
1010

1111
"code.gitea.io/gitea/models/db"
12+
"code.gitea.io/gitea/models/unittest"
1213
"code.gitea.io/gitea/modules/setting"
1314

1415
"github.com/stretchr/testify/assert"
1516
)
1617

1718
func TestAction_GetRepoPath(t *testing.T) {
18-
assert.NoError(t, db.PrepareTestDatabase())
19+
assert.NoError(t, unittest.PrepareTestDatabase())
1920
repo := db.AssertExistsAndLoadBean(t, &Repository{}).(*Repository)
2021
owner := db.AssertExistsAndLoadBean(t, &User{ID: repo.OwnerID}).(*User)
2122
action := &Action{RepoID: repo.ID}
2223
assert.Equal(t, path.Join(owner.Name, repo.Name), action.GetRepoPath())
2324
}
2425

2526
func TestAction_GetRepoLink(t *testing.T) {
26-
assert.NoError(t, db.PrepareTestDatabase())
27+
assert.NoError(t, unittest.PrepareTestDatabase())
2728
repo := db.AssertExistsAndLoadBean(t, &Repository{}).(*Repository)
2829
owner := db.AssertExistsAndLoadBean(t, &User{ID: repo.OwnerID}).(*User)
2930
action := &Action{RepoID: repo.ID}
@@ -34,7 +35,7 @@ func TestAction_GetRepoLink(t *testing.T) {
3435

3536
func TestGetFeeds(t *testing.T) {
3637
// test with an individual user
37-
assert.NoError(t, db.PrepareTestDatabase())
38+
assert.NoError(t, unittest.PrepareTestDatabase())
3839
user := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
3940

4041
actions, err := GetFeeds(GetFeedsOptions{
@@ -62,7 +63,7 @@ func TestGetFeeds(t *testing.T) {
6263

6364
func TestGetFeeds2(t *testing.T) {
6465
// test with an organization user
65-
assert.NoError(t, db.PrepareTestDatabase())
66+
assert.NoError(t, unittest.PrepareTestDatabase())
6667
org := db.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User)
6768
user := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
6869

0 commit comments

Comments
 (0)