2024-06-16 13:28:07 +03:00
|
|
|
// Copyright 2024 The Forgejo Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
|
|
|
package integration
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2024-08-06 02:27:38 +03:00
|
|
|
"net/http"
|
2024-06-16 13:28:07 +03:00
|
|
|
"net/url"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
unit_model "code.gitea.io/gitea/models/unit"
|
|
|
|
"code.gitea.io/gitea/models/unittest"
|
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
|
|
|
"code.gitea.io/gitea/routers/web/repo"
|
|
|
|
"code.gitea.io/gitea/services/context"
|
|
|
|
"code.gitea.io/gitea/services/contexttest"
|
|
|
|
files_service "code.gitea.io/gitea/services/repository/files"
|
2024-08-06 02:27:38 +03:00
|
|
|
"code.gitea.io/gitea/tests"
|
2024-06-16 13:28:07 +03:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func createRepoAndGetContext(t *testing.T, files []string, deleteMdReadme bool) (*context.Context, func()) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{Name: "user1"})
|
|
|
|
|
|
|
|
size := len(files)
|
|
|
|
if deleteMdReadme {
|
|
|
|
size++
|
|
|
|
}
|
|
|
|
changeFiles := make([]*files_service.ChangeRepoFile, size)
|
|
|
|
for i, e := range files {
|
|
|
|
changeFiles[i] = &files_service.ChangeRepoFile{
|
|
|
|
Operation: "create",
|
|
|
|
TreePath: e,
|
|
|
|
ContentReader: strings.NewReader("test"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if deleteMdReadme {
|
|
|
|
changeFiles[len(files)] = &files_service.ChangeRepoFile{
|
|
|
|
Operation: "delete",
|
|
|
|
TreePath: "README.md",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// README.md is already added by auto init
|
2024-08-25 03:47:35 +03:00
|
|
|
repo, _, f := tests.CreateDeclarativeRepo(t, user, "readmetest", []unit_model.Type{unit_model.TypeCode}, nil, changeFiles)
|
2024-06-16 13:28:07 +03:00
|
|
|
|
|
|
|
ctx, _ := contexttest.MockContext(t, "user1/readmetest")
|
|
|
|
ctx.SetParams(":id", fmt.Sprint(repo.ID))
|
|
|
|
contexttest.LoadRepo(t, ctx, repo.ID)
|
2024-08-26 09:03:48 +03:00
|
|
|
contexttest.LoadGitRepo(t, ctx)
|
2024-06-16 13:28:07 +03:00
|
|
|
contexttest.LoadRepoCommit(t, ctx)
|
2024-08-26 09:03:48 +03:00
|
|
|
|
|
|
|
return ctx, func() {
|
|
|
|
f()
|
|
|
|
ctx.Repo.GitRepo.Close()
|
|
|
|
}
|
2024-06-16 13:28:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRepoView_FindReadme(t *testing.T) {
|
2024-11-10 20:25:41 +03:00
|
|
|
onGiteaRun(t, func(t *testing.T, u *url.URL) {
|
|
|
|
t.Run("PrioOneLocalizedMdReadme", func(t *testing.T) {
|
|
|
|
defer tests.PrintCurrentTest(t)()
|
2024-06-16 13:28:07 +03:00
|
|
|
ctx, f := createRepoAndGetContext(t, []string{"README.en.md", "README.en.org", "README.org", "README.txt", "README.tex"}, false)
|
|
|
|
defer f()
|
|
|
|
|
|
|
|
tree, _ := ctx.Repo.Commit.SubTree(ctx.Repo.TreePath)
|
|
|
|
entries, _ := tree.ListEntries()
|
|
|
|
_, file, _ := repo.FindReadmeFileInEntries(ctx, entries, false)
|
|
|
|
|
|
|
|
assert.Equal(t, "README.en.md", file.Name())
|
|
|
|
})
|
2024-11-10 20:25:41 +03:00
|
|
|
t.Run("PrioTwoMdReadme", func(t *testing.T) {
|
|
|
|
defer tests.PrintCurrentTest(t)()
|
2024-06-16 13:28:07 +03:00
|
|
|
ctx, f := createRepoAndGetContext(t, []string{"README.en.org", "README.org", "README.txt", "README.tex"}, false)
|
|
|
|
defer f()
|
|
|
|
|
|
|
|
tree, _ := ctx.Repo.Commit.SubTree(ctx.Repo.TreePath)
|
|
|
|
entries, _ := tree.ListEntries()
|
|
|
|
_, file, _ := repo.FindReadmeFileInEntries(ctx, entries, false)
|
|
|
|
|
|
|
|
assert.Equal(t, "README.md", file.Name())
|
|
|
|
})
|
2024-11-10 20:25:41 +03:00
|
|
|
t.Run("PrioThreeLocalizedOrgReadme", func(t *testing.T) {
|
|
|
|
defer tests.PrintCurrentTest(t)()
|
2024-06-16 13:28:07 +03:00
|
|
|
ctx, f := createRepoAndGetContext(t, []string{"README.en.org", "README.org", "README.txt", "README.tex"}, true)
|
|
|
|
defer f()
|
|
|
|
|
|
|
|
tree, _ := ctx.Repo.Commit.SubTree(ctx.Repo.TreePath)
|
|
|
|
entries, _ := tree.ListEntries()
|
|
|
|
_, file, _ := repo.FindReadmeFileInEntries(ctx, entries, false)
|
|
|
|
|
|
|
|
assert.Equal(t, "README.en.org", file.Name())
|
|
|
|
})
|
2024-11-10 20:25:41 +03:00
|
|
|
t.Run("PrioFourOrgReadme", func(t *testing.T) {
|
|
|
|
defer tests.PrintCurrentTest(t)()
|
2024-06-16 13:28:07 +03:00
|
|
|
ctx, f := createRepoAndGetContext(t, []string{"README.org", "README.txt", "README.tex"}, true)
|
|
|
|
defer f()
|
|
|
|
|
|
|
|
tree, _ := ctx.Repo.Commit.SubTree(ctx.Repo.TreePath)
|
|
|
|
entries, _ := tree.ListEntries()
|
|
|
|
_, file, _ := repo.FindReadmeFileInEntries(ctx, entries, false)
|
|
|
|
|
|
|
|
assert.Equal(t, "README.org", file.Name())
|
|
|
|
})
|
2024-11-10 20:25:41 +03:00
|
|
|
t.Run("PrioFiveTxtReadme", func(t *testing.T) {
|
|
|
|
defer tests.PrintCurrentTest(t)()
|
2024-06-16 13:28:07 +03:00
|
|
|
ctx, f := createRepoAndGetContext(t, []string{"README.txt", "README", "README.tex"}, true)
|
|
|
|
defer f()
|
|
|
|
|
|
|
|
tree, _ := ctx.Repo.Commit.SubTree(ctx.Repo.TreePath)
|
|
|
|
entries, _ := tree.ListEntries()
|
|
|
|
_, file, _ := repo.FindReadmeFileInEntries(ctx, entries, false)
|
|
|
|
|
|
|
|
assert.Equal(t, "README.txt", file.Name())
|
|
|
|
})
|
2024-11-10 20:25:41 +03:00
|
|
|
t.Run("PrioSixWithoutExtensionReadme", func(t *testing.T) {
|
|
|
|
defer tests.PrintCurrentTest(t)()
|
2024-06-16 13:28:07 +03:00
|
|
|
ctx, f := createRepoAndGetContext(t, []string{"README", "README.tex"}, true)
|
|
|
|
defer f()
|
|
|
|
|
|
|
|
tree, _ := ctx.Repo.Commit.SubTree(ctx.Repo.TreePath)
|
|
|
|
entries, _ := tree.ListEntries()
|
|
|
|
_, file, _ := repo.FindReadmeFileInEntries(ctx, entries, false)
|
|
|
|
|
|
|
|
assert.Equal(t, "README", file.Name())
|
|
|
|
})
|
2024-11-10 20:25:41 +03:00
|
|
|
t.Run("PrioSevenAnyReadme", func(t *testing.T) {
|
|
|
|
defer tests.PrintCurrentTest(t)()
|
2024-06-16 13:28:07 +03:00
|
|
|
ctx, f := createRepoAndGetContext(t, []string{"README.tex"}, true)
|
|
|
|
defer f()
|
|
|
|
|
|
|
|
tree, _ := ctx.Repo.Commit.SubTree(ctx.Repo.TreePath)
|
|
|
|
entries, _ := tree.ListEntries()
|
|
|
|
_, file, _ := repo.FindReadmeFileInEntries(ctx, entries, false)
|
|
|
|
|
|
|
|
assert.Equal(t, "README.tex", file.Name())
|
|
|
|
})
|
2024-11-10 20:25:41 +03:00
|
|
|
t.Run("DoNotPickReadmeIfNonPresent", func(t *testing.T) {
|
|
|
|
defer tests.PrintCurrentTest(t)()
|
2024-06-16 13:28:07 +03:00
|
|
|
ctx, f := createRepoAndGetContext(t, []string{}, true)
|
|
|
|
defer f()
|
|
|
|
|
|
|
|
tree, _ := ctx.Repo.Commit.SubTree(ctx.Repo.TreePath)
|
|
|
|
entries, _ := tree.ListEntries()
|
|
|
|
_, file, _ := repo.FindReadmeFileInEntries(ctx, entries, false)
|
|
|
|
|
|
|
|
assert.Nil(t, file)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
2024-08-06 02:27:38 +03:00
|
|
|
|
|
|
|
func TestRepoViewFileLines(t *testing.T) {
|
|
|
|
onGiteaRun(t, func(t *testing.T, _ *url.URL) {
|
|
|
|
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
|
2024-08-25 03:47:35 +03:00
|
|
|
repo, _, f := tests.CreateDeclarativeRepo(t, user, "file-lines", []unit_model.Type{unit_model.TypeCode}, nil, []*files_service.ChangeRepoFile{
|
2024-08-06 02:27:38 +03:00
|
|
|
{
|
|
|
|
Operation: "create",
|
|
|
|
TreePath: "test-1",
|
|
|
|
ContentReader: strings.NewReader("No newline"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Operation: "create",
|
|
|
|
TreePath: "test-2",
|
|
|
|
ContentReader: strings.NewReader("No newline\n"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Operation: "create",
|
|
|
|
TreePath: "test-3",
|
|
|
|
ContentReader: strings.NewReader("Two\nlines"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Operation: "create",
|
|
|
|
TreePath: "test-4",
|
|
|
|
ContentReader: strings.NewReader("Really two\nlines\n"),
|
|
|
|
},
|
2024-08-19 18:47:59 +03:00
|
|
|
{
|
|
|
|
Operation: "create",
|
|
|
|
TreePath: "empty",
|
|
|
|
ContentReader: strings.NewReader(""),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Operation: "create",
|
|
|
|
TreePath: "seemingly-empty",
|
|
|
|
ContentReader: strings.NewReader("\n"),
|
|
|
|
},
|
2024-08-06 02:27:38 +03:00
|
|
|
})
|
|
|
|
defer f()
|
|
|
|
|
2024-08-19 18:47:59 +03:00
|
|
|
testEOL := func(t *testing.T, filename string, hasEOL bool) {
|
|
|
|
t.Helper()
|
|
|
|
req := NewRequestf(t, "GET", "%s/src/branch/main/%s", repo.Link(), filename)
|
2024-08-06 02:27:38 +03:00
|
|
|
resp := MakeRequest(t, req, http.StatusOK)
|
|
|
|
htmlDoc := NewHTMLParser(t, resp.Body)
|
|
|
|
|
|
|
|
fileInfo := htmlDoc.Find(".file-info").Text()
|
2024-08-19 18:47:59 +03:00
|
|
|
if hasEOL {
|
|
|
|
assert.NotContains(t, fileInfo, "No EOL")
|
|
|
|
} else {
|
|
|
|
assert.Contains(t, fileInfo, "No EOL")
|
|
|
|
}
|
|
|
|
}
|
2024-08-06 02:27:38 +03:00
|
|
|
|
2024-08-19 18:47:59 +03:00
|
|
|
t.Run("No EOL", func(t *testing.T) {
|
|
|
|
defer tests.PrintCurrentTest(t)()
|
2024-08-06 02:27:38 +03:00
|
|
|
|
2024-08-19 18:47:59 +03:00
|
|
|
testEOL(t, "test-1", false)
|
|
|
|
testEOL(t, "test-3", false)
|
2024-08-06 02:27:38 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("With EOL", func(t *testing.T) {
|
|
|
|
defer tests.PrintCurrentTest(t)()
|
|
|
|
|
2024-08-19 18:47:59 +03:00
|
|
|
testEOL(t, "test-2", true)
|
|
|
|
testEOL(t, "test-4", true)
|
|
|
|
testEOL(t, "empty", true)
|
|
|
|
testEOL(t, "seemingly-empty", true)
|
2024-08-06 02:27:38 +03:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|