Repository: gogs/gogs
File: internal/lfsutil/storage_test.go (Go)

Code before:
		})
	}
}

func TestLocalStorage_Upload(t *testing.T) {
	s := &LocalStorage{
		Root: t.TempDir(),
	}

	// SHA-256 of "Hello world!" is c0535e...
	const helloWorldOID = OID("c0535e4be2b79ffd93291305436bf889314e4a3faec05ecffcbb7df31ad9e51a")

	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)
	})

	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)
	})

	t.Run("oid mismatch", func(t *testing.T) {
		// Use a valid OID format that doesn't match the content.
		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) {
	oid := OID("ef797c8118f02dfb649607dd5d3f8c7623048c9c063d532cc95c5ed7a898a64f")
	s := &LocalStorage{

Review comment (bug):
```suggestion
	t.Run("valid OIC but wrong content", func(t *testing.T) {
```

Code after:
		})
	}
}

func TestLocalStorage_Upload(t *testing.T) {
	base := t.TempDir()
	s := &LocalStorage{
		Root:    filepath.Join(base, "lfs-objects"),
		TempDir: filepath.Join(base, "tmp", "lfs"),
	}

	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)
	})

	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)
	})

	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) {
	oid := OID("ef797c8118f02dfb649607dd5d3f8c7623048c9c063d532cc95c5ed7a898a64f")
	s := &LocalStorage{