git-lfs/git/ls_tree_scanner_test.go
brian m. carlson cca4977b23
commands/fsck: exit 1 on failed fsck
We currently exit successfully on a failed fsck, but we should instead
exit 1 so that users can tell that there's been a problem.  This also
means that users can easily use git lfs fsck in CI to check for invalid
pointers in their commits and have CI fail if there are.

Add some tests for our new functionality as well.

Reorder the existing code to reduce the need for an additional if
statement.
2021-07-14 18:15:31 +00:00

52 lines
1.5 KiB
Go

package git
import (
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
type genericScanner interface {
Err() error
Scan() bool
}
func assertNextScan(t *testing.T, scanner genericScanner) {
assert.True(t, scanner.Scan())
assert.Nil(t, scanner.Err())
}
func assertScannerDone(t *testing.T, scanner genericScanner) {
assert.False(t, scanner.Scan())
assert.Nil(t, scanner.Err())
}
func TestLsTreeParser(t *testing.T) {
stdout := "100644 blob d899f6551a51cf19763c5955c7a06a2726f018e9 42 .gitattributes\000100644 blob 4d343e022e11a8618db494dc3c501e80c7e18197 126 PB SCN 16 Odhrán.wav"
scanner := NewLsTreeScanner(strings.NewReader(stdout))
assertNextTreeBlob(t, scanner, "d899f6551a51cf19763c5955c7a06a2726f018e9", ".gitattributes")
assertNextTreeBlob(t, scanner, "4d343e022e11a8618db494dc3c501e80c7e18197", "PB SCN 16 Odhrán.wav")
assertScannerDone(t, scanner)
}
func assertNextTreeBlob(t *testing.T, scanner *LsTreeScanner, oid, filename string) {
assertNextScan(t, scanner)
b := scanner.TreeBlob()
assert.NotNil(t, b)
assert.Equal(t, oid, b.Oid)
assert.Equal(t, filename, b.Filename)
}
func BenchmarkLsTreeParser(b *testing.B) {
stdout := "100644 blob d899f6551a51cf19763c5955c7a06a2726f018e9 42 .gitattributes\000100644 blob 4d343e022e11a8618db494dc3c501e80c7e18197 126 PB SCN 16 Odhrán.wav"
// run the Fib function b.N times
for n := 0; n < b.N; n++ {
scanner := NewLsTreeScanner(strings.NewReader(stdout))
for scanner.Scan() {
}
}
}