github.com/git-lfs/git-lfs@v2.5.2+incompatible/lfs/lfs_test.go (about)

     1  package lfs_test // avoid import cycle
     2  
     3  import (
     4  	"fmt"
     5  	"sort"
     6  	"testing"
     7  
     8  	"github.com/git-lfs/git-lfs/fs"
     9  	"github.com/git-lfs/git-lfs/lfs"
    10  	test "github.com/git-lfs/git-lfs/t/cmd/util"
    11  	"github.com/stretchr/testify/assert"
    12  )
    13  
    14  func TestAllCurrentObjectsNone(t *testing.T) {
    15  	repo := test.NewRepo(t)
    16  	repo.Pushd()
    17  	defer func() {
    18  		repo.Popd()
    19  		repo.Cleanup()
    20  	}()
    21  
    22  	empty := true
    23  	repo.Filesystem().EachObject(func(obj fs.Object) error {
    24  		empty = false
    25  		t.Logf("Found: %+v", obj)
    26  		return nil
    27  	})
    28  	if !empty {
    29  		t.Error("Should be no objects")
    30  	}
    31  }
    32  
    33  func TestAllCurrentObjectsSome(t *testing.T) {
    34  	repo := test.NewRepo(t)
    35  	repo.Pushd()
    36  	defer func() {
    37  		repo.Popd()
    38  		repo.Cleanup()
    39  	}()
    40  
    41  	// We're not testing commits here, just storage, so just create a single
    42  	// commit input with lots of files to generate many oids
    43  	numFiles := 20
    44  	files := make([]*test.FileInput, 0, numFiles)
    45  	for i := 0; i < numFiles; i++ {
    46  		// Must be >=16 bytes for each file to be unique
    47  		files = append(files, &test.FileInput{Filename: fmt.Sprintf("file%d.txt", i), Size: 30})
    48  	}
    49  
    50  	inputs := []*test.CommitInput{
    51  		{Files: files},
    52  	}
    53  
    54  	outputs := repo.AddCommits(inputs)
    55  
    56  	expected := make([]*lfs.Pointer, 0, numFiles)
    57  	for _, f := range outputs[0].Files {
    58  		expected = append(expected, f)
    59  	}
    60  
    61  	actual := make([]*lfs.Pointer, 0)
    62  	repo.Filesystem().EachObject(func(obj fs.Object) error {
    63  		actual = append(actual, lfs.NewPointer(obj.Oid, obj.Size, nil))
    64  		return nil
    65  	})
    66  
    67  	// sort to ensure comparison is equal
    68  	sort.Sort(test.PointersByOid(expected))
    69  	sort.Sort(test.PointersByOid(actual))
    70  	assert.Equal(t, expected, actual, "Oids from disk should be the same as in commits")
    71  }