Test for ScanPreviousVersions

This commit is contained in:
Steve Streeting 2015-08-24 15:15:16 +01:00
parent 9a318224cf
commit 21596a0e17
2 changed files with 89 additions and 0 deletions

@ -5,7 +5,9 @@ package lfs_test // to avoid import cycles
// which avoids import cycles with testutils
import (
"sort"
"testing"
"time"
. "github.com/github/git-lfs/lfs"
"github.com/github/git-lfs/test"
@ -74,3 +76,83 @@ func TestScanUnpushed(t *testing.T) {
assert.Equal(t, 0, len(pointers), "Should be 0 pointers unpushed")
}
func TestScanPreviousVersions(t *testing.T) {
repo := test.NewRepo(t)
repo.Pushd()
defer func() {
repo.Popd()
repo.Cleanup()
}()
now := time.Now()
inputs := []*test.CommitInput{
{ // 0
CommitDate: now.AddDate(0, 0, -20),
Files: []*test.FileInput{
{"file1.txt", 20, nil},
{"file2.txt", 30, nil},
{"folder/nested.txt", 40, nil},
{"folder/nested2.txt", 31, nil},
},
},
{ // 1
CommitDate: now.AddDate(0, 0, -10),
Files: []*test.FileInput{
{"file2.txt", 22, nil},
},
},
{ // 2
NewBranch: "excluded",
CommitDate: now.AddDate(0, 0, -6),
Files: []*test.FileInput{
{"file2.txt", 12, nil},
{"folder/nested2.txt", 16, nil},
},
},
{ // 3
ParentBranches: []string{"master"},
CommitDate: now.AddDate(0, 0, -4),
Files: []*test.FileInput{
{"folder/nested.txt", 42, nil},
{"folder/nested2.txt", 6, nil},
},
},
{ // 4
Files: []*test.FileInput{
{"folder/nested.txt", 22, nil},
},
},
}
outputs := repo.AddCommits(inputs)
// Previous commits excludes final state of each file, which is:
// file1.txt [0] (unchanged since first commit so excluded)
// file2.txt [1] (because [2] is on another branch so excluded)
// folder/nested.txt [4] (updated at last commit)
// folder/nested2.txt [3]
// The only changes which will be included are changes prior to final state
// where the '-' side of the diff is inside the date range
// 7 day limit excludes [0] commit, but includes state from that if there
// was a subsequent change
pointers, err := ScanPreviousVersions("master", now.AddDate(0, 0, -7))
assert.Equal(t, nil, err)
// Includes the following 'before' state at commits:
// folder/nested.txt [-diff at 4, ie 3, -diff at 3 ie 0]
// folder/nested2.txt [-diff at 3 ie 0]
// others are either on diff branches, before this window, or unchanged
expected := []*WrappedPointer{
{Name: "folder/nested.txt", Size: outputs[3].Files[0].Size, Pointer: outputs[3].Files[0]},
{Name: "folder/nested.txt", Size: outputs[0].Files[2].Size, Pointer: outputs[0].Files[2]},
{Name: "folder/nested2.txt", Size: outputs[0].Files[3].Size, Pointer: outputs[0].Files[3]},
}
// Need to sort to compare equality
sort.Sort(test.WrappedPointersByOid(expected))
sort.Sort(test.WrappedPointersByOid(pointers))
assert.Equal(t, expected, pointers)
}

@ -377,3 +377,10 @@ type RefsByName []*git.Ref
func (a RefsByName) Len() int { return len(a) }
func (a RefsByName) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a RefsByName) Less(i, j int) bool { return a[i].Name < a[j].Name }
// WrappedPointersByOid implements sort.Interface for []*lfs.WrappedPointer based on oid
type WrappedPointersByOid []*lfs.WrappedPointer
func (a WrappedPointersByOid) Len() int { return len(a) }
func (a WrappedPointersByOid) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a WrappedPointersByOid) Less(i, j int) bool { return a[i].Pointer.Oid < a[j].Pointer.Oid }