|
@@ -10,6 +10,9 @@ import (
|
|
|
"testing"
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
+ "github.com/stretchr/testify/require"
|
|
|
|
|
+
|
|
|
|
|
+ "gogs.io/gogs/internal/osutil"
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
func TestLocalStorage_storagePath(t *testing.T) {
|
|
func TestLocalStorage_storagePath(t *testing.T) {
|
|
@@ -46,50 +49,54 @@ func TestLocalStorage_storagePath(t *testing.T) {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
func TestLocalStorage_Upload(t *testing.T) {
|
|
func TestLocalStorage_Upload(t *testing.T) {
|
|
|
|
|
+ base := t.TempDir()
|
|
|
s := &LocalStorage{
|
|
s := &LocalStorage{
|
|
|
- Root: filepath.Join(os.TempDir(), "lfs-objects"),
|
|
|
|
|
|
|
+ Root: filepath.Join(base, "lfs-objects"),
|
|
|
|
|
+ TempDir: filepath.Join(base, "tmp", "lfs"),
|
|
|
}
|
|
}
|
|
|
- t.Cleanup(func() {
|
|
|
|
|
- _ = os.RemoveAll(s.Root)
|
|
|
|
|
|
|
+
|
|
|
|
|
+ const helloWorldOID = OID("c0535e4be2b79ffd93291305436bf889314e4a3faec05ecffcbb7df31ad9e51a") // "Hello world!"
|
|
|
|
|
+
|
|
|
|
|
+ t.Run("invalid OID", func(t *testing.T) {
|
|
|
|
|
+ written, err := s.Upload("bad_oid", io.NopCloser(strings.NewReader("")))
|
|
|
|
|
+ assert.Equal(t, int64(0), written)
|
|
|
|
|
+ assert.Equal(t, ErrInvalidOID, err)
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
- tests := []struct {
|
|
|
|
|
- name string
|
|
|
|
|
- oid OID
|
|
|
|
|
- content string
|
|
|
|
|
- expWritten int64
|
|
|
|
|
- expErr error
|
|
|
|
|
- }{
|
|
|
|
|
- {
|
|
|
|
|
- name: "invalid oid",
|
|
|
|
|
- oid: "bad_oid",
|
|
|
|
|
- expErr: ErrInvalidOID,
|
|
|
|
|
- },
|
|
|
|
|
|
|
+ t.Run("valid OID", func(t *testing.T) {
|
|
|
|
|
+ written, err := s.Upload(helloWorldOID, io.NopCloser(strings.NewReader("Hello world!")))
|
|
|
|
|
+ require.NoError(t, err)
|
|
|
|
|
+ assert.Equal(t, int64(12), written)
|
|
|
|
|
+ })
|
|
|
|
|
|
|
|
- {
|
|
|
|
|
- name: "valid oid",
|
|
|
|
|
- oid: "ef797c8118f02dfb649607dd5d3f8c7623048c9c063d532cc95c5ed7a898a64f",
|
|
|
|
|
- content: "Hello world!",
|
|
|
|
|
- expWritten: 12,
|
|
|
|
|
- },
|
|
|
|
|
- }
|
|
|
|
|
- for _, test := range tests {
|
|
|
|
|
- t.Run(test.name, func(t *testing.T) {
|
|
|
|
|
- written, err := s.Upload(test.oid, io.NopCloser(strings.NewReader(test.content)))
|
|
|
|
|
- assert.Equal(t, test.expWritten, written)
|
|
|
|
|
- assert.Equal(t, test.expErr, err)
|
|
|
|
|
- })
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ t.Run("valid OID but wrong content", func(t *testing.T) {
|
|
|
|
|
+ oid := OID("ef797c8118f02dfb649607dd5d3f8c7623048c9c063d532cc95c5ed7a898a64f")
|
|
|
|
|
+ written, err := s.Upload(oid, io.NopCloser(strings.NewReader("Hello world!")))
|
|
|
|
|
+ assert.Equal(t, int64(0), written)
|
|
|
|
|
+ assert.Equal(t, ErrOIDMismatch, err)
|
|
|
|
|
+
|
|
|
|
|
+ // File should have been cleaned up.
|
|
|
|
|
+ assert.False(t, osutil.IsFile(s.storagePath(oid)))
|
|
|
|
|
+ })
|
|
|
|
|
+
|
|
|
|
|
+ t.Run("duplicate upload returns existing size", func(t *testing.T) {
|
|
|
|
|
+ written, err := s.Upload(helloWorldOID, io.NopCloser(strings.NewReader("should be ignored")))
|
|
|
|
|
+ require.NoError(t, err)
|
|
|
|
|
+ assert.Equal(t, int64(12), written)
|
|
|
|
|
+
|
|
|
|
|
+ // Verify original content is preserved.
|
|
|
|
|
+ var buf bytes.Buffer
|
|
|
|
|
+ err = s.Download(helloWorldOID, &buf)
|
|
|
|
|
+ require.NoError(t, err)
|
|
|
|
|
+ assert.Equal(t, "Hello world!", buf.String())
|
|
|
|
|
+ })
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
func TestLocalStorage_Download(t *testing.T) {
|
|
func TestLocalStorage_Download(t *testing.T) {
|
|
|
oid := OID("ef797c8118f02dfb649607dd5d3f8c7623048c9c063d532cc95c5ed7a898a64f")
|
|
oid := OID("ef797c8118f02dfb649607dd5d3f8c7623048c9c063d532cc95c5ed7a898a64f")
|
|
|
s := &LocalStorage{
|
|
s := &LocalStorage{
|
|
|
- Root: filepath.Join(os.TempDir(), "lfs-objects"),
|
|
|
|
|
|
|
+ Root: filepath.Join(t.TempDir(), "lfs-objects"),
|
|
|
}
|
|
}
|
|
|
- t.Cleanup(func() {
|
|
|
|
|
- _ = os.RemoveAll(s.Root)
|
|
|
|
|
- })
|
|
|
|
|
|
|
|
|
|
fpath := s.storagePath(oid)
|
|
fpath := s.storagePath(oid)
|
|
|
err := os.MkdirAll(filepath.Dir(fpath), os.ModePerm)
|
|
err := os.MkdirAll(filepath.Dir(fpath), os.ModePerm)
|