Skip to content

Commit a2779de

Browse files
authored
Test views of LFS files (#22196)
1 parent ea5a752 commit a2779de

25 files changed

+174
-2
lines changed

CONTRIBUTING.md

+2
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,8 @@ To maintain understandable code and avoid circular dependencies it is important
190190
- **templates:** Golang templates for generating the html output.
191191
- **tests/e2e:** End to end tests
192192
- **tests/integration:** Integration tests
193+
- **tests/gitea-repositories-meta:** Sample repos used in integration tests. Adding a new repo requires editing `models/fixtures/repositories.yml` and `models/fixtures/repo_unit.yml` to match.
194+
- **tests/gitea-lfs-meta:** Sample LFS objects used in integration tests. Adding a new object requires editing `models/fixtures/lfs_meta_object.yml` to match.
193195
- **vendor:** External code that Gitea depends on.
194196

195197
## Documentation

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, 80, repoCnt)
28+
assert.EqualValues(t, 81, 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}

models/fixtures/lfs_meta_object.yml

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# These are the LFS objects in user2/lfs.git
2+
-
3+
4+
id: 1
5+
oid: 0b8d8b5f15046343fd32f451df93acc2bdd9e6373be478b968e4cad6b6647351
6+
size: 107
7+
repository_id: 54
8+
created_unix: 1671607299
9+
10+
-
11+
12+
id: 2
13+
oid: 2eccdb43825d2a49d99d542daa20075cff1d97d9d2349a8977efe9c03661737c
14+
size: 107
15+
repository_id: 54
16+
created_unix: 1671607299
17+
18+
-
19+
20+
id: 3
21+
oid: 7b6b2c88dba9f760a1a58469b67fee2b698ef7e9399c4ca4f34a14ccbe39f623
22+
size: 27
23+
repository_id: 54
24+
created_unix: 1671607299
25+
26+
-
27+
28+
id: 4
29+
oid: 9d172e5c64b4f0024b9901ec6afe9ea052f3c9b6ff9f4b07956d8c48c86fca82
30+
size: 25
31+
repository_id: 54
32+
created_unix: 1671607299

models/fixtures/repo_unit.yml

+6
Original file line numberDiff line numberDiff line change
@@ -550,3 +550,9 @@
550550
repo_id: 53
551551
type: 1
552552
created_unix: 946684810
553+
554+
-
555+
id: 81
556+
repo_id: 54
557+
type: 1
558+
created_unix: 946684810

models/fixtures/repository.yml

+11
Original file line numberDiff line numberDiff line change
@@ -1585,3 +1585,14 @@
15851585
size: 0
15861586
is_fsck_enabled: true
15871587
close_issues_via_commit_in_any_branch: false
1588+
1589+
-
1590+
id: 54
1591+
owner_id: 2
1592+
owner_name: user2
1593+
lower_name: lfs
1594+
name: lfs
1595+
is_empty: false
1596+
is_archived: false
1597+
is_private: true
1598+
status: 0

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: 9
69+
num_repos: 10
7070
num_teams: 0
7171
num_members: 0
7272
visibility: 0
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Testing documents in LFS
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Testing READMEs in LFS
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,2 @@
1+
x��Q
2+
B!E�v��bƗ�AD����7���0���V���¹'�Rr���M��Y0Y";2��8:/A�Ĝ&�Z�_��6�����]��g���\dk9�%�i��h�XG��?O]�g���@}97�
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
73cf03db6ece34e12bf91e8853dc58f678f2f82d

tests/integration/lfs_view_test.go

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
// Copyright 2022 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package integration
5+
6+
import (
7+
"net/http"
8+
"testing"
9+
10+
"code.gitea.io/gitea/tests"
11+
12+
"github.com/stretchr/testify/assert"
13+
)
14+
15+
// check that files stored in LFS render properly in the web UI
16+
func TestLFSRender(t *testing.T) {
17+
defer tests.PrepareTestEnv(t)()
18+
19+
session := loginUser(t, "user2")
20+
21+
// check that a markup file is flagged with "Stored in Git LFS" and shows its text
22+
t.Run("Markup", func(t *testing.T) {
23+
defer tests.PrintCurrentTest(t)()
24+
25+
req := NewRequest(t, "GET", "/user2/lfs/src/branch/master/CONTRIBUTING.md")
26+
resp := session.MakeRequest(t, req, http.StatusOK)
27+
28+
doc := NewHTMLParser(t, resp.Body).doc
29+
30+
fileInfo := doc.Find("div.file-info-entry").First().Text()
31+
assert.Contains(t, fileInfo, "Stored with Git LFS")
32+
33+
content := doc.Find("div.file-view").Text()
34+
assert.Contains(t, content, "Testing documents in LFS")
35+
})
36+
37+
// check that an image is flagged with "Stored in Git LFS" and renders inline
38+
t.Run("Image", func(t *testing.T) {
39+
defer tests.PrintCurrentTest(t)()
40+
41+
req := NewRequest(t, "GET", "/user2/lfs/src/branch/master/jpeg.jpg")
42+
resp := session.MakeRequest(t, req, http.StatusOK)
43+
44+
doc := NewHTMLParser(t, resp.Body).doc
45+
46+
fileInfo := doc.Find("div.file-info-entry").First().Text()
47+
assert.Contains(t, fileInfo, "Stored with Git LFS")
48+
49+
src, exists := doc.Find(".file-view img").Attr("src")
50+
assert.True(t, exists, "The image should be in an <img> tag")
51+
assert.Equal(t, "/user2/lfs/media/branch/master/jpeg.jpg", src, "The image should use the /media link because it's in LFS")
52+
})
53+
54+
// check that a binary file is flagged with "Stored in Git LFS" and renders a /media/ link instead of a /raw/ link
55+
t.Run("Binary", func(t *testing.T) {
56+
defer tests.PrintCurrentTest(t)()
57+
58+
req := NewRequest(t, "GET", "/user2/lfs/src/branch/master/crypt.bin")
59+
resp := session.MakeRequest(t, req, http.StatusOK)
60+
61+
doc := NewHTMLParser(t, resp.Body).doc
62+
63+
fileInfo := doc.Find("div.file-info-entry").First().Text()
64+
assert.Contains(t, fileInfo, "Stored with Git LFS")
65+
66+
rawLink, exists := doc.Find("div.file-view > div.view-raw > a").Attr("href")
67+
assert.True(t, exists, "Download link should render instead of content because this is a binary file")
68+
assert.Equal(t, "/user2/lfs/media/branch/master/crypt.bin", rawLink, "The download link should use the proper /media link because it's in LFS")
69+
})
70+
71+
// check that a directory with a README file shows its text
72+
t.Run("Readme", func(t *testing.T) {
73+
defer tests.PrintCurrentTest(t)()
74+
75+
req := NewRequest(t, "GET", "/user2/lfs/src/branch/master/subdir")
76+
resp := session.MakeRequest(t, req, http.StatusOK)
77+
78+
doc := NewHTMLParser(t, resp.Body).doc
79+
80+
content := doc.Find("div.file-view").Text()
81+
assert.Contains(t, content, "Testing READMEs in LFS")
82+
})
83+
}

tests/test_utils.go

+28
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,11 @@ func PrepareTestEnv(t testing.TB, skip ...int) func() {
167167
ourSkip += skip[0]
168168
}
169169
deferFn := PrintCurrentTest(t, ourSkip)
170+
171+
// load database fixtures
170172
assert.NoError(t, unittest.LoadFixtures())
173+
174+
// load git repo fixtures
171175
assert.NoError(t, util.RemoveAll(setting.RepoRootPath))
172176
assert.NoError(t, unittest.CopyDir(path.Join(filepath.Dir(setting.AppPath), "tests/gitea-repositories-meta"), setting.RepoRootPath))
173177
ownerDirs, err := os.ReadDir(setting.RepoRootPath)
@@ -190,6 +194,16 @@ func PrepareTestEnv(t testing.TB, skip ...int) func() {
190194
}
191195
}
192196

197+
// load LFS object fixtures
198+
// (LFS storage can be on any of several backends, including remote servers, so we init it with the storage API)
199+
lfsFixtures, err := storage.NewStorage("", storage.LocalStorageConfig{Path: path.Join(filepath.Dir(setting.AppPath), "tests/gitea-lfs-meta")})
200+
assert.NoError(t, err)
201+
assert.NoError(t, storage.Clean(storage.LFS))
202+
assert.NoError(t, lfsFixtures.IterateObjects(func(path string, _ storage.Object) error {
203+
_, err := storage.Copy(storage.LFS, path, lfsFixtures, path)
204+
return err
205+
}))
206+
193207
return deferFn
194208
}
195209

@@ -198,7 +212,11 @@ func PrepareTestEnv(t testing.TB, skip ...int) func() {
198212
// within a single test this is required
199213
func ResetFixtures(t *testing.T) {
200214
assert.NoError(t, queue.GetManager().FlushAll(context.Background(), -1))
215+
216+
// load database fixtures
201217
assert.NoError(t, unittest.LoadFixtures())
218+
219+
// load git repo fixtures
202220
assert.NoError(t, util.RemoveAll(setting.RepoRootPath))
203221
assert.NoError(t, unittest.CopyDir(path.Join(filepath.Dir(setting.AppPath), "tests/gitea-repositories-meta"), setting.RepoRootPath))
204222
ownerDirs, err := os.ReadDir(setting.RepoRootPath)
@@ -220,4 +238,14 @@ func ResetFixtures(t *testing.T) {
220238
_ = os.MkdirAll(filepath.Join(setting.RepoRootPath, ownerDir.Name(), repoDir.Name(), "refs", "tag"), 0o755)
221239
}
222240
}
241+
242+
// load LFS object fixtures
243+
// (LFS storage can be on any of several backends, including remote servers, so we init it with the storage API)
244+
lfsFixtures, err := storage.NewStorage("", storage.LocalStorageConfig{Path: path.Join(filepath.Dir(setting.AppPath), "tests/gitea-lfs-meta")})
245+
assert.NoError(t, err)
246+
assert.NoError(t, storage.Clean(storage.LFS))
247+
assert.NoError(t, lfsFixtures.IterateObjects(func(path string, _ storage.Object) error {
248+
_, err := storage.Copy(storage.LFS, path, lfsFixtures, path)
249+
return err
250+
}))
223251
}

0 commit comments

Comments
 (0)