MCPcopy Create free account
hub / github.com/gogs/gogs / TestLocalStorage_Upload

Function TestLocalStorage_Upload

internal/lfsutil/storage_test.go:51–105  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

49}
50
51func TestLocalStorage_Upload(t *testing.T) {
52 base := t.TempDir()
53 s := &LocalStorage{
54 Root: filepath.Join(base, "lfs-objects"),
55 TempDir: filepath.Join(base, "tmp", "lfs"),
56 }
57
58 const helloWorldOID = OID("c0535e4be2b79ffd93291305436bf889314e4a3faec05ecffcbb7df31ad9e51a") // "Hello world!"
59
60 t.Run("invalid OID", func(t *testing.T) {
61 written, err := s.Upload("bad_oid", io.NopCloser(strings.NewReader("")))
62 assert.Equal(t, int64(0), written)
63 assert.Equal(t, ErrInvalidOID, err)
64 })
65
66 t.Run("valid OID", func(t *testing.T) {
67 written, err := s.Upload(helloWorldOID, io.NopCloser(strings.NewReader("Hello world!")))
68 require.NoError(t, err)
69 assert.Equal(t, int64(12), written)
70 })
71
72 t.Run("valid OID but wrong content", func(t *testing.T) {
73 oid := OID("ef797c8118f02dfb649607dd5d3f8c7623048c9c063d532cc95c5ed7a898a64f")
74 written, err := s.Upload(oid, io.NopCloser(strings.NewReader("Hello world!")))
75 assert.Equal(t, int64(0), written)
76 assert.Equal(t, ErrOIDMismatch, err)
77
78 // File should have been cleaned up.
79 assert.False(t, osutil.IsFile(s.storagePath(oid)))
80 })
81
82 t.Run("duplicate upload with matching content returns existing size", func(t *testing.T) {
83 written, err := s.Upload(helloWorldOID, io.NopCloser(strings.NewReader("Hello world!")))
84 require.NoError(t, err)
85 assert.Equal(t, int64(12), written)
86
87 // Verify original content is preserved.
88 var buf bytes.Buffer
89 err = s.Download(helloWorldOID, &buf)
90 require.NoError(t, err)
91 assert.Equal(t, "Hello world!", buf.String())
92 })
93
94 t.Run("duplicate upload with mismatched content is rejected", func(t *testing.T) {
95 written, err := s.Upload(helloWorldOID, io.NopCloser(strings.NewReader("different bytes")))
96 assert.Equal(t, int64(0), written)
97 assert.Equal(t, ErrOIDMismatch, err)
98
99 // Verify original content is preserved.
100 var buf bytes.Buffer
101 err = s.Download(helloWorldOID, &buf)
102 require.NoError(t, err)
103 assert.Equal(t, "Hello world!", buf.String())
104 })
105}
106
107func TestLocalStorage_Download(t *testing.T) {
108 oid := OID("ef797c8118f02dfb649607dd5d3f8c7623048c9c063d532cc95c5ed7a898a64f")

Callers

nothing calls this directly

Calls 6

UploadMethod · 0.95
storagePathMethod · 0.95
DownloadMethod · 0.95
IsFileFunction · 0.92
OIDTypeAlias · 0.85
StringMethod · 0.45

Tested by

no test coverage detected