Skip to content

Commit

Permalink
Merge branch 'main' into custom-regexp-external-issues
Browse files Browse the repository at this point in the history
  • Loading branch information
lafriks authored Nov 12, 2021
2 parents 777dc41 + df64fa4 commit 9a40b16
Show file tree
Hide file tree
Showing 143 changed files with 1,178 additions and 874 deletions.
6 changes: 3 additions & 3 deletions contrib/fixtures/fixture_generation.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"path/filepath"

"code.gitea.io/gitea/models"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/unittest"
)

// To generate derivative fixtures, execute the following from Gitea's repository base dir:
Expand All @@ -31,13 +31,13 @@ var (
func main() {
pathToGiteaRoot := "."
fixturesDir = filepath.Join(pathToGiteaRoot, "models", "fixtures")
if err := db.CreateTestEngine(db.FixturesOptions{
if err := unittest.CreateTestEngine(unittest.FixturesOptions{
Dir: fixturesDir,
}); err != nil {
fmt.Printf("CreateTestEngine: %+v", err)
os.Exit(1)
}
if err := db.PrepareTestDatabase(); err != nil {
if err := unittest.PrepareTestDatabase(); err != nil {
fmt.Printf("PrepareTestDatabase: %+v\n", err)
os.Exit(1)
}
Expand Down
7 changes: 4 additions & 3 deletions contrib/pr/checkout.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (

"code.gitea.io/gitea/models"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/unittest"
gitea_git "code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/markup"
"code.gitea.io/gitea/modules/markup/external"
Expand Down Expand Up @@ -99,16 +100,16 @@ func runPR() {
})
db.HasEngine = true
//x.ShowSQL(true)
err = db.InitFixtures(
db.FixturesOptions{
err = unittest.InitFixtures(
unittest.FixturesOptions{
Dir: path.Join(curDir, "models/fixtures/"),
},
)
if err != nil {
fmt.Printf("Error initializing test database: %v\n", err)
os.Exit(1)
}
db.LoadFixtures()
unittest.LoadFixtures()
util.RemoveAll(setting.RepoRootPath)
util.RemoveAll(models.LocalCopyPath())
util.CopyDir(path.Join(curDir, "integrations/gitea-repositories-meta"), setting.RepoRootPath)
Expand Down
59 changes: 59 additions & 0 deletions docs/content/doc/developers/guidelines-frontend.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,65 @@ We recommend [Google HTML/CSS Style Guide](https://google.github.io/styleguide/h
6. The backend can pass complex data to the frontend by using `ctx.PageData["myModuleData"] = map[]{}`
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).


### `async` Functions

Only mark a function as `async` if and only if there are `await` calls
or `Promise` returns inside the function.

It's not recommended to use `async` event listeners, which may lead to problems.
The reason is that the code after await is executed outside the event dispatch.
Reference: https://github.com/github/eslint-plugin-github/blob/main/docs/rules/async-preventdefault.md

If we want to call an `async` function in a non-async context,
it's recommended to use `const _promise = asyncFoo()` to tell readers
that this is done by purpose, we want to call the async function and ignore the Promise.
Some lint rules and IDEs also have warnings if the returned Promise is not handled.

#### DOM Event Listener

```js
el.addEventListener('click', (e) => {
(async () => {
await asyncFoo(); // recommended
// then we shound't do e.preventDefault() after await, no effect
})();

const _promise = asyncFoo(); // recommended

e.preventDefault(); // correct
});

el.addEventListener('async', async (e) => { // not recommended but acceptable
e.preventDefault(); // acceptable
await asyncFoo(); // skip out event dispath
e.preventDefault(); // WRONG
});
```

#### jQuery Event Listener

```js
$('#el').on('click', (e) => {
(async () => {
await asyncFoo(); // recommended
// then we shound't do e.preventDefault() after await, no effect
})();

const _promise = asyncFoo(); // recommended

e.preventDefault(); // correct
return false; // correct
});

$('#el').on('click', async (e) => { // not recommended but acceptable
e.preventDefault(); // acceptable
return false; // WRONG, jQuery expects the returned value is a boolean, not a Promise
await asyncFoo(); // skip out event dispath
return false; // WRONG
});
```

### Vue2/Vue3 and JSX

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.
10 changes: 6 additions & 4 deletions integrations/api_issue_label_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"strings"
"testing"

"code.gitea.io/gitea/models/unittest"

"code.gitea.io/gitea/models"
"code.gitea.io/gitea/models/db"
api "code.gitea.io/gitea/modules/structs"
Expand All @@ -18,7 +20,7 @@ import (
)

func TestAPIModifyLabels(t *testing.T) {
assert.NoError(t, db.LoadFixtures())
assert.NoError(t, unittest.LoadFixtures())

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

func TestAPIAddIssueLabels(t *testing.T) {
assert.NoError(t, db.LoadFixtures())
assert.NoError(t, unittest.LoadFixtures())

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

func TestAPIReplaceIssueLabels(t *testing.T) {
assert.NoError(t, db.LoadFixtures())
assert.NoError(t, unittest.LoadFixtures())

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

func TestAPIModifyOrgLabels(t *testing.T) {
assert.NoError(t, db.LoadFixtures())
assert.NoError(t, unittest.LoadFixtures())

repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 3}).(*models.Repository)
owner := db.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User)
Expand Down
11 changes: 6 additions & 5 deletions integrations/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
"time"

"code.gitea.io/gitea/models"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/unittest"
"code.gitea.io/gitea/modules/base"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/graceful"
Expand Down Expand Up @@ -84,6 +84,7 @@ func NewNilResponseHashSumRecorder() *NilResponseHashSumRecorder {
func TestMain(m *testing.M) {
defer log.Close()

unittest.InitUnitTestBridge()
managerCtx, cancel := context.WithCancel(context.Background())
graceful.InitManager(managerCtx)
defer cancel()
Expand Down Expand Up @@ -112,8 +113,8 @@ func TestMain(m *testing.M) {
}
}

err := db.InitFixtures(
db.FixturesOptions{
err := unittest.InitFixtures(
unittest.FixturesOptions{
Dir: filepath.Join(filepath.Dir(setting.AppPath), "models/fixtures/"),
},
)
Expand Down Expand Up @@ -250,7 +251,7 @@ func prepareTestEnv(t testing.TB, skip ...int) func() {
ourSkip += skip[0]
}
deferFn := PrintCurrentTest(t, ourSkip)
assert.NoError(t, db.LoadFixtures())
assert.NoError(t, unittest.LoadFixtures())
assert.NoError(t, util.RemoveAll(setting.RepoRootPath))

assert.NoError(t, util.CopyDir(path.Join(filepath.Dir(setting.AppPath), "integrations/gitea-repositories-meta"), setting.RepoRootPath))
Expand Down Expand Up @@ -527,7 +528,7 @@ func GetCSRF(t testing.TB, session *TestSession, urlStr string) string {
// within a single test this is required
func resetFixtures(t *testing.T) {
assert.NoError(t, queue.GetManager().FlushAll(context.Background(), -1))
assert.NoError(t, db.LoadFixtures())
assert.NoError(t, unittest.LoadFixtures())
assert.NoError(t, util.RemoveAll(setting.RepoRootPath))
assert.NoError(t, util.CopyDir(path.Join(filepath.Dir(setting.AppPath), "integrations/gitea-repositories-meta"), setting.RepoRootPath))
}
4 changes: 3 additions & 1 deletion integrations/migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"os"
"testing"

"code.gitea.io/gitea/models/unittest"

"code.gitea.io/gitea/models"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/modules/migrations"
Expand All @@ -17,7 +19,7 @@ import (
)

func TestMigrateLocalPath(t *testing.T) {
assert.NoError(t, db.PrepareTestDatabase())
assert.NoError(t, unittest.PrepareTestDatabase())

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

Expand Down
9 changes: 5 additions & 4 deletions integrations/repofiles_delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import (
"net/url"
"testing"

"code.gitea.io/gitea/models/unittest"

"code.gitea.io/gitea/models"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/modules/repofiles"
api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/test"
Expand Down Expand Up @@ -67,7 +68,7 @@ func TestDeleteRepoFile(t *testing.T) {

func testDeleteRepoFile(t *testing.T, u *url.URL) {
// setup
db.PrepareTestEnv(t)
unittest.PrepareTestEnv(t)
ctx := test.MockContext(t, "user2/repo1")
ctx.SetParams(":id", "1")
test.LoadRepo(t, ctx, 1)
Expand Down Expand Up @@ -106,7 +107,7 @@ func TestDeleteRepoFileWithoutBranchNames(t *testing.T) {

func testDeleteRepoFileWithoutBranchNames(t *testing.T, u *url.URL) {
// setup
db.PrepareTestEnv(t)
unittest.PrepareTestEnv(t)
ctx := test.MockContext(t, "user2/repo1")
ctx.SetParams(":id", "1")
test.LoadRepo(t, ctx, 1)
Expand Down Expand Up @@ -136,7 +137,7 @@ func testDeleteRepoFileWithoutBranchNames(t *testing.T, u *url.URL) {

func TestDeleteRepoFileErrors(t *testing.T) {
// setup
db.PrepareTestEnv(t)
unittest.PrepareTestEnv(t)
ctx := test.MockContext(t, "user2/repo1")
ctx.SetParams(":id", "1")
test.LoadRepo(t, ctx, 1)
Expand Down
15 changes: 8 additions & 7 deletions models/access_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ import (
"testing"

"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/unittest"
"github.com/stretchr/testify/assert"
)

func TestAccessLevel(t *testing.T) {
assert.NoError(t, db.PrepareTestDatabase())
assert.NoError(t, unittest.PrepareTestDatabase())

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

func TestHasAccess(t *testing.T) {
assert.NoError(t, db.PrepareTestDatabase())
assert.NoError(t, unittest.PrepareTestDatabase())

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

func TestUser_GetRepositoryAccesses(t *testing.T) {
assert.NoError(t, db.PrepareTestDatabase())
assert.NoError(t, unittest.PrepareTestDatabase())

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

func TestUser_GetAccessibleRepositories(t *testing.T) {
assert.NoError(t, db.PrepareTestDatabase())
assert.NoError(t, unittest.PrepareTestDatabase())

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

func TestRepository_RecalculateAccesses(t *testing.T) {
// test with organization repo
assert.NoError(t, db.PrepareTestDatabase())
assert.NoError(t, unittest.PrepareTestDatabase())
repo1 := db.AssertExistsAndLoadBean(t, &Repository{ID: 3}).(*Repository)
assert.NoError(t, repo1.GetOwner())

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

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

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

func TestRepository_RecalculateAccesses3(t *testing.T) {
assert.NoError(t, db.PrepareTestDatabase())
assert.NoError(t, unittest.PrepareTestDatabase())
team5 := db.AssertExistsAndLoadBean(t, &Team{ID: 5}).(*Team)
user29 := db.AssertExistsAndLoadBean(t, &User{ID: 29}).(*User)

Expand Down
9 changes: 5 additions & 4 deletions models/action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,22 @@ import (
"testing"

"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/unittest"
"code.gitea.io/gitea/modules/setting"

"github.com/stretchr/testify/assert"
)

func TestAction_GetRepoPath(t *testing.T) {
assert.NoError(t, db.PrepareTestDatabase())
assert.NoError(t, unittest.PrepareTestDatabase())
repo := db.AssertExistsAndLoadBean(t, &Repository{}).(*Repository)
owner := db.AssertExistsAndLoadBean(t, &User{ID: repo.OwnerID}).(*User)
action := &Action{RepoID: repo.ID}
assert.Equal(t, path.Join(owner.Name, repo.Name), action.GetRepoPath())
}

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

func TestGetFeeds(t *testing.T) {
// test with an individual user
assert.NoError(t, db.PrepareTestDatabase())
assert.NoError(t, unittest.PrepareTestDatabase())
user := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)

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

func TestGetFeeds2(t *testing.T) {
// test with an organization user
assert.NoError(t, db.PrepareTestDatabase())
assert.NoError(t, unittest.PrepareTestDatabase())
org := db.AssertExistsAndLoadBean(t, &User{ID: 3}).(*User)
user := db.AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)

Expand Down
Loading

0 comments on commit 9a40b16

Please sign in to comment.