gittuf/pkg/gitinterface/blob_test.go
dawgdevv 8374150f3f test: improve pkg/gitinterface coverage from 69.4% to 76.2%
This PR adds focused tests for error paths and edge cases across
pkg/gitinterface, raising coverage from 69.4% to 76.2%.
Files changed:
- references_test.go: TestAbsoluteReference with branch/tag/custom/ref lookups
- tag_test.go: TestEnsureIsTag, error paths for GetTagTarget and verifyTagSignature
- commit_test.go: TestEnsureIsCommit, error paths for commit inspection helpers
- object_test.go: Error paths for GetObjectType and GetObjectSize
- blob_test.go: Error path for ReadBlob with non-blob object
- status_test.go: Tests for Status errors and StatusCode helpers
- sync_test.go: Tests for WithFetchDepth, CreateRemote, push/fetch errors
- config_test.go: Error path for SetGitConfig
- utils_test.go: TestResetDueToError
Closes #1244

Signed-off-by: dawgdevv <nraj02415@gmail.com>
2026-04-25 02:44:19 +05:30

59 lines
1.5 KiB
Go

// Copyright The gittuf Authors
// SPDX-License-Identifier: Apache-2.0
package gitinterface
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestRepositoryReadBlob(t *testing.T) {
tempDir := t.TempDir()
repo := CreateTestGitRepository(t, tempDir, false)
contents := []byte("test file read")
expectedBlobID, err := NewHash("2ecdd330475d93568ed27f717a84a7fe207d1c58")
require.Nil(t, err)
blobID, err := repo.WriteBlob(contents)
if err != nil {
t.Fatal(err)
}
assert.Equal(t, expectedBlobID, blobID)
t.Run("read existing blob", func(t *testing.T) {
readContents, err := repo.ReadBlob(blobID)
assert.Nil(t, err)
assert.Equal(t, contents, readContents)
})
t.Run("read non-existing blob", func(t *testing.T) {
_, err := repo.ReadBlob(ZeroHash)
assert.ErrorContains(t, err, "unable to inspect if object is blob")
})
t.Run("read non-blob object", func(t *testing.T) {
treeBuilder := NewTreeBuilder(repo)
treeID, err := treeBuilder.WriteTreeFromEntries(nil)
require.Nil(t, err)
_, err = repo.ReadBlob(treeID)
assert.ErrorContains(t, err, "is not a blob object")
})
}
func TestRepositoryWriteBlob(t *testing.T) {
tempDir := t.TempDir()
repo := CreateTestGitRepository(t, tempDir, false)
contents := []byte("test file write")
expectedBlobID, err := NewHash("999c05e9578e5d244920306842f516789a2498f7")
require.Nil(t, err)
blobID, err := repo.WriteBlob(contents)
assert.Nil(t, err)
assert.Equal(t, expectedBlobID, blobID)
}