From 86b46085c79126cc60816ac490f48e6baae08a67 Mon Sep 17 00:00:00 2001 From: Gusted Date: Thu, 29 Feb 2024 00:09:20 +0100 Subject: [PATCH] [BUG] Sort file list case insensitively - Make the sorting done on the entries list case insensitive. - Adds integration test. - Resolves #317 --- modules/base/natural_sort.go | 2 + modules/base/natural_sort_test.go | 6 +++ tests/integration/repo_test.go | 62 +++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+) diff --git a/modules/base/natural_sort.go b/modules/base/natural_sort.go index e920177f89..5b5febb906 100644 --- a/modules/base/natural_sort.go +++ b/modules/base/natural_sort.go @@ -5,11 +5,13 @@ package base import ( "math/big" + "strings" "unicode/utf8" ) // NaturalSortLess compares two strings so that they could be sorted in natural order func NaturalSortLess(s1, s2 string) bool { + s1, s2 = strings.ToLower(s1), strings.ToLower(s2) var i1, i2 int for { rune1, j1, end1 := getNextRune(s1, i1) diff --git a/modules/base/natural_sort_test.go b/modules/base/natural_sort_test.go index 91e864ad2a..7378d9a643 100644 --- a/modules/base/natural_sort_test.go +++ b/modules/base/natural_sort_test.go @@ -20,4 +20,10 @@ func TestNaturalSortLess(t *testing.T) { test("a-1-a", "a-1-b", true) test("2", "12", true) test("a", "ab", true) + + // Test for case insensitive. + test("A", "ab", true) + test("B", "ab", false) + test("a", "AB", true) + test("b", "AB", false) } diff --git a/tests/integration/repo_test.go b/tests/integration/repo_test.go index b62297e0e9..03124ecaf8 100644 --- a/tests/integration/repo_test.go +++ b/tests/integration/repo_test.go @@ -6,6 +6,7 @@ package integration import ( "fmt" "net/http" + "net/url" "path" "strings" "testing" @@ -15,11 +16,13 @@ import ( repo_model "code.gitea.io/gitea/models/repo" 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/modules/git" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/test" "code.gitea.io/gitea/modules/translation" repo_service "code.gitea.io/gitea/services/repository" + files_service "code.gitea.io/gitea/services/repository/files" "code.gitea.io/gitea/tests" "github.com/PuerkitoBio/goquery" @@ -899,3 +902,62 @@ func TestRepoHomeViewRedirect(t *testing.T) { assert.Equal(t, "Wiki", txt) }) } + +func TestRepoFilesList(t *testing.T) { + onGiteaRun(t, func(t *testing.T, u *url.URL) { + user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) + + // create the repo + repo, _, f := CreateDeclarativeRepo(t, user2, "", + []unit_model.Type{unit_model.TypeCode}, nil, + []*files_service.ChangeRepoFile{ + { + Operation: "create", + TreePath: "zEta", + ContentReader: strings.NewReader("zeta"), + }, + { + Operation: "create", + TreePath: "licensa", + ContentReader: strings.NewReader("licensa"), + }, + { + Operation: "create", + TreePath: "licensz", + ContentReader: strings.NewReader("licensz"), + }, + { + Operation: "create", + TreePath: "delta", + ContentReader: strings.NewReader("delta"), + }, + { + Operation: "create", + TreePath: "Charlie/aa.txt", + ContentReader: strings.NewReader("charlie"), + }, + { + Operation: "create", + TreePath: "Beta", + ContentReader: strings.NewReader("beta"), + }, + { + Operation: "create", + TreePath: "alpha", + ContentReader: strings.NewReader("alpha"), + }, + }, + ) + defer f() + + req := NewRequest(t, "GET", "/"+repo.FullName()) + resp := MakeRequest(t, req, http.StatusOK) + + htmlDoc := NewHTMLParser(t, resp.Body) + filesList := htmlDoc.Find("#repo-files-table tbody tr").Map(func(_ int, s *goquery.Selection) string { + return s.AttrOr("data-entryname", "") + }) + + assert.EqualValues(t, []string{"Charlie", "alpha", "Beta", "delta", "licensa", "LICENSE", "licensz", "README.md", "zEta"}, filesList) + }) +}