Skip to content

Commit 6431879

Browse files
committed
Test README rendering
1 parent 067b0c2 commit 6431879

File tree

48 files changed

+192
-4
lines changed

Some content is hidden

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

48 files changed

+192
-4
lines changed

Diff for: models/db/iterate_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func TestIterate(t *testing.T) {
2525
return nil
2626
})
2727
assert.NoError(t, err)
28-
assert.EqualValues(t, 83, repoCnt)
28+
assert.EqualValues(t, 84, repoCnt)
2929

3030
err = db.Iterate(db.DefaultContext, nil, func(ctx context.Context, repoUnit *repo_model.RepoUnit) error {
3131
reopUnit2 := repo_model.RepoUnit{ID: repoUnit.ID}

Diff for: models/db/list_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ func TestFind(t *testing.T) {
3535
var repoUnits []repo_model.RepoUnit
3636
err := db.Find(db.DefaultContext, &opts, &repoUnits)
3737
assert.NoError(t, err)
38-
assert.EqualValues(t, 83, len(repoUnits))
38+
assert.EqualValues(t, 84, len(repoUnits))
3939

4040
cnt, err := db.Count(db.DefaultContext, &opts, new(repo_model.RepoUnit))
4141
assert.NoError(t, err)
42-
assert.EqualValues(t, 83, cnt)
42+
assert.EqualValues(t, 84, cnt)
4343

4444
repoUnits = make([]repo_model.RepoUnit, 0, 10)
4545
newCnt, err := db.FindAndCount(db.DefaultContext, &opts, &repoUnits)

Diff for: models/fixtures/repo_unit.yml

+6
Original file line numberDiff line numberDiff line change
@@ -569,3 +569,9 @@
569569
type: 3
570570
config: "{\"IgnoreWhitespaceConflicts\":false,\"AllowMerge\":true,\"AllowRebase\":true,\"AllowRebaseMerge\":true,\"AllowSquash\":true}"
571571
created_unix: 946684810
572+
573+
-
574+
id: 84
575+
repo_id: 56
576+
type: 1
577+
created_unix: 946684810

Diff for: models/fixtures/repository.yml

+13
Original file line numberDiff line numberDiff line change
@@ -1634,3 +1634,16 @@
16341634
is_private: true
16351635
num_issues: 1
16361636
status: 0
1637+
1638+
-
1639+
id: 56
1640+
owner_id: 2
1641+
owner_name: user2
1642+
lower_name: readme-test
1643+
name: readme-test
1644+
default_branch: master
1645+
is_empty: false
1646+
is_archived: false
1647+
is_private: true
1648+
status: 0
1649+
num_issues: 0

Diff for: models/fixtures/user.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
num_followers: 2
6767
num_following: 1
6868
num_stars: 2
69-
num_repos: 11
69+
num_repos: 12
7070
num_teams: 0
7171
num_members: 0
7272
visibility: 0
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ref: refs/heads/master
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[core]
2+
repositoryformatversion = 0
3+
filemode = true
4+
bare = true
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# git ls-files --others --exclude-from=.git/info/exclude
2+
# Lines that start with '#' are comments.
3+
# For a project mostly in C, the following would be a good set of
4+
# exclude patterns (uncomment them if you want to use them):
5+
# *.[oa]
6+
# *~
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
ea9ef877d1d88af76682d8798418081264f10cfc refs/heads/fallbacks
2+
0d4c14db927c9ffba01fa7e126cc748b5c02c01e refs/heads/fallbacks2
3+
c66d5b07c2063d3268707f22226c708b589574ef refs/heads/fallbacks3
4+
89f8426e9eb5eff35c09b3565836c8f8e15d0ce9 refs/heads/fallbacks4
5+
b0e902496eae435ad03c92a5d479f916ef2d4893 refs/heads/fallbacks5
6+
84a5500b5cc040b11daf53fc42c542a99589dc76 refs/heads/fallbacks6
7+
cf406a96e416d7de5c4c1bbfffdd672300c822bf refs/heads/fallbacks7
8+
0d6ac644b969e9199915a492da9dba08c179fd23 refs/heads/fallbacks8
9+
5038febc0c57215beb3748d7ae4091a25a4acc93 refs/heads/fallbacks9
10+
9134e1f178ca4cccf1a197142646f2d7627e8cd5 refs/heads/i18n
11+
744d2441e55bc0010d6b340d303f0106a627ad29 refs/heads/master
12+
3c492566170b057e962c025515ab38bbd7444077 refs/heads/plain
13+
3882d6373a0882a6739b3cd9b24d21c630621234 refs/heads/sp-ace
14+
bf5ed898252eaa50dcc01108ed4417c3ea98a294 refs/heads/special-subdir-.gitea
15+
c03543573ab088ce1cf7090a387d2be621426234 refs/heads/special-subdir-.github
16+
e75957ad9b7e6ed16dda183529ec283db0bbc5fe refs/heads/special-subdir-docs
17+
46f5d5ab33d701642e08c713fab42af89fdd4fea refs/heads/special-subdir-nested
18+
9c0f872256b839c2b97ec22fd348d87b14045513 refs/heads/subdir
19+
d7a854fff61e45b98234d7aa79ecbcb1619cd3dd refs/heads/symlink
20+
30b9c0ed4b1039dbd99f3fb537b84ca507e0549d refs/heads/symlink-loop
21+
41489b7be5c2244d2b7b524dcb31caf3bd1f9ccc refs/heads/txt
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
x��;�0 @�s
2+
_�*N��H��1q�u(�?�t��T�0��==���Q��+�*4��d��h�S�η.z���͙Z3�ct��0'�As�5h�zL�=D��B�\cx-ݴ��!O������q�����<����� 0�T�装���5�=-�����U s�7,O�#�M�

Diff for: tests/gitea-repositories-meta/user2/readme-test.git/objects/3b/23d7f1a9cb904cb46f5f2272bfa5ed5f871fb9

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
x��M� �aל� � 0&Ƹs�� ئ�'�.���x������4 ]���.)�D��Q�|@b6Xbd��}�2+b�%�T� �I>g� 2�7Q���. (c�����"o��n�M�<�[6�_^橼�Z��T�U�� ���n��qO�*3��{���N\
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
x��;�0 @�s
2+
_�*N뤕bcc���Њ�Tҁ�S!N�����d�>���!���քLu�Ul�#q����l�Q�,�ꔡ��lCBn$6��X�Dɹ�bbҖR0���y�[/O�n���
3+
�i�����2�1��Ї@�e�p�d���iޭ�殯 ��!�<���N�

Diff for: tests/gitea-repositories-meta/user2/readme-test.git/objects/e3/a6fd8fe49e323ee10017f72b777a53fbd8076f

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
x��M
2+
1 �a�=E.��m�� �Νwh����#�"��A<��g�~|eǾ�#�jU:����$%�9o��{�9F �գQdsOU��H��rA�(�=�x��E���$nkҳ]�
3+
�����\ҫV�M 7��yx�mؔ��1�-�1 ��}ږ��3�/�%=�f>&L�
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
P pack-8933bd634b76f8154310cccb52537a0195e43166.pack
2+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# pack-refs with: peeled fully-peeled sorted
2+
ea9ef877d1d88af76682d8798418081264f10cfc refs/heads/fallbacks
3+
0d4c14db927c9ffba01fa7e126cc748b5c02c01e refs/heads/fallbacks2
4+
c66d5b07c2063d3268707f22226c708b589574ef refs/heads/fallbacks3
5+
89f8426e9eb5eff35c09b3565836c8f8e15d0ce9 refs/heads/fallbacks4
6+
b0e902496eae435ad03c92a5d479f916ef2d4893 refs/heads/fallbacks5
7+
84a5500b5cc040b11daf53fc42c542a99589dc76 refs/heads/fallbacks6
8+
cf406a96e416d7de5c4c1bbfffdd672300c822bf refs/heads/fallbacks7
9+
0d6ac644b969e9199915a492da9dba08c179fd23 refs/heads/fallbacks8
10+
5038febc0c57215beb3748d7ae4091a25a4acc93 refs/heads/fallbacks9
11+
9134e1f178ca4cccf1a197142646f2d7627e8cd5 refs/heads/i18n
12+
744d2441e55bc0010d6b340d303f0106a627ad29 refs/heads/master
13+
3c492566170b057e962c025515ab38bbd7444077 refs/heads/plain
14+
3882d6373a0882a6739b3cd9b24d21c630621234 refs/heads/sp-ace
15+
bf5ed898252eaa50dcc01108ed4417c3ea98a294 refs/heads/special-subdir-.gitea
16+
c03543573ab088ce1cf7090a387d2be621426234 refs/heads/special-subdir-.github
17+
e75957ad9b7e6ed16dda183529ec283db0bbc5fe refs/heads/special-subdir-docs
18+
46f5d5ab33d701642e08c713fab42af89fdd4fea refs/heads/special-subdir-nested
19+
9c0f872256b839c2b97ec22fd348d87b14045513 refs/heads/subdir
20+
d7a854fff61e45b98234d7aa79ecbcb1619cd3dd refs/heads/symlink
21+
30b9c0ed4b1039dbd99f3fb537b84ca507e0549d refs/heads/symlink-loop
22+
41489b7be5c2244d2b7b524dcb31caf3bd1f9ccc refs/heads/txt
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fe495ea336f079ef2bed68648d0ba9a37cdbd4aa

Diff for: tests/integration/repo_test.go

+103
Original file line numberDiff line numberDiff line change
@@ -256,3 +256,106 @@ func TestViewRepoDirectory(t *testing.T) {
256256
assert.Zero(t, repoTopics.Length())
257257
assert.Zero(t, repoSummary.Length())
258258
}
259+
260+
// ensure that the all the different ways to find and render a README work
261+
func TestViewRepoDirectoryReadme(t *testing.T) {
262+
defer tests.PrepareTestEnv(t)()
263+
264+
// there are many combinations:
265+
// - READMEs can be .md, .txt, or have no extension
266+
// - READMEs can be tagged with a language and even a country code
267+
// - READMEs can be stored in docs/, .gitea/, or .github/
268+
// - READMEs can be symlinks to other files
269+
// - READMEs can be broken symlinks which should not render
270+
271+
session := loginUser(t, "user2")
272+
273+
check := func(name, url, filename, content string) {
274+
t.Run(name, func(t *testing.T) {
275+
defer tests.PrintCurrentTest(t)()
276+
277+
req := NewRequest(t, "GET", url)
278+
resp := session.MakeRequest(t, req, http.StatusOK)
279+
280+
htmlDoc := NewHTMLParser(t, resp.Body)
281+
readmeName := htmlDoc.doc.Find("h4.file-header")
282+
readmeContent := htmlDoc.doc.Find(".file-view") // TODO: add a id="readme" to the output to make this test more precise
283+
284+
assert.Equal(t, filename, strings.TrimSpace(readmeName.Text()))
285+
assert.Contains(t, readmeContent.Text(), content)
286+
})
287+
}
288+
289+
// this doesn't cover all possible cases, just the major branches of the code
290+
291+
// viewing the top level
292+
check("Home", "/user2/readme-test/", "README.md", "The cake is a lie.")
293+
294+
// viewing different file extensions
295+
check("md", "/user2/readme-test/src/branch/master/", "README.md", "The cake is a lie.")
296+
check("txt", "/user2/readme-test/src/branch/txt/", "README.txt", "My spoon is too big.")
297+
check("plain", "/user2/readme-test/src/branch/plain/", "README", "Birken my stocks gee howdy")
298+
check("i18n", "/user2/readme-test/src/branch/i18n/", "README.zh.md", "蛋糕是一个谎言")
299+
300+
// viewing different subdirectories
301+
check("subdir", "/user2/readme-test/src/branch/subdir/libcake", "README.md", "Four pints of sugar.")
302+
check("docs-direct", "/user2/readme-test/src/branch/special-subdir-docs/docs/", "README.md", "This is in docs/")
303+
check("docs", "/user2/readme-test/src/branch/special-subdir-docs/", "docs/README.md", "This is in docs/")
304+
check(".gitea", "/user2/readme-test/src/branch/special-subdir-.gitea/", ".gitea/README.md", "This is in .gitea/")
305+
check(".github", "/user2/readme-test/src/branch/special-subdir-.github/", ".github/README.md", "This is in .github/")
306+
307+
// symlinks
308+
// symlinks are subtle:
309+
// - they should be able to handle going a reasonable number of times up and down in the tree
310+
// - they shouldn't get stuck on link cycles
311+
// - they should determine the filetype based on the name of the link, not the target
312+
check("symlink", "/user2/readme-test/src/branch/symlink/", "README.md", "This is in some/other/path")
313+
check("symlink-multiple", "/user2/readme-test/src/branch/symlink/some/", "README.txt", "This is in some/other/path")
314+
check("symlink-up-and-down", "/user2/readme-test/src/branch/symlink/up/back/down/down", "README.md", "It's a me, mario")
315+
316+
// testing fallback rules
317+
// READMEs are searched in this order:
318+
// - [README.zh-cn.md, README.zh_cn.md, README.zh.md, README_zh.md, README.md, README.txt, README,
319+
// docs/README.zh-cn.md, docs/README.zh_cn.md, docs/README.zh.md, docs/README_zh.md, docs/README.md, docs/README.txt, docs/README,
320+
// .gitea/README.zh-cn.md, .gitea/README.zh_cn.md, .gitea/README.zh.md, .gitea/README_zh.md, .gitea/README.md, .gitea/README.txt, .gitea/README,
321+
322+
// .github/README.zh-cn.md, .github/README.zh_cn.md, .github/README.zh.md, .github/README_zh.md, .github/README.md, .github/README.txt, .github/README]
323+
// and a broken/looped symlink counts as not existing at all and should be skipped.
324+
// again, this doesn't cover all cases, but it covers a few
325+
check("fallback/top", "/user2/readme-test/src/branch/fallbacks/", "README.en.md", "This is README.en.md")
326+
check("fallback/2", "/user2/readme-test/src/branch/fallbacks2/", "README.md", "This is README.md")
327+
check("fallback/3", "/user2/readme-test/src/branch/fallbacks3/", "README", "This is README")
328+
check("fallback/4", "/user2/readme-test/src/branch/fallbacks4/", "docs/README.en.md", "This is docs/README.en.md")
329+
check("fallback/5", "/user2/readme-test/src/branch/fallbacks5/", "docs/README.md", "This is docs/README.md")
330+
check("fallback/6", "/user2/readme-test/src/branch/fallbacks6/", "docs/README", "This is docs/README")
331+
check("fallback/7", "/user2/readme-test/src/branch/fallbacks7/", ".gitea/README.en.md", "This is .gitea/README.en.md")
332+
check("fallback/8", "/user2/readme-test/src/branch/fallbacks8/", ".gitea/README.md", "This is .gitea/README.md")
333+
check("fallback/9", "/user2/readme-test/src/branch/fallbacks9/", ".gitea/README", "This is .gitea/README")
334+
335+
// this case tests that broken symlinks count as missing files, instead of rendering their contents
336+
check("fallbacks-broken-symlinks", "/user2/readme-test/src/branch/fallbacks-broken-symlinks/", "docs/README", "This is docs/README")
337+
338+
// some cases that should NOT render a README
339+
// - /readme
340+
// - /.github/docs/README.md
341+
// - a symlink loop
342+
343+
missing := func(name, url string) {
344+
t.Run("missing/"+name, func(t *testing.T) {
345+
defer tests.PrintCurrentTest(t)()
346+
347+
req := NewRequest(t, "GET", url)
348+
resp := session.MakeRequest(t, req, http.StatusOK)
349+
350+
htmlDoc := NewHTMLParser(t, resp.Body)
351+
_, exists := htmlDoc.doc.Find(".file-view").Attr("class")
352+
fmt.Printf("%s", resp.Body)
353+
354+
assert.False(t, exists, "README should not have rendered")
355+
})
356+
}
357+
missing("sp-ace", "/user2/readme-test/src/branch/sp-ace/")
358+
missing("nested-special", "/user2/readme-test/src/branch/special-subdir-nested/subproject") // the special subdirs should only trigger on the repo root
359+
// missing("special-subdir-nested", "/user2/readme-test/src/branch/special-subdir-nested/") // This is currently FAILING, due to a bug introduced in https://github.com/go-gitea/gitea/pull/22177
360+
missing("symlink-loop", "/user2/readme-test/src/branch/symlink-loop/")
361+
}

0 commit comments

Comments
 (0)