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

     1  package lfs_test // to avoid import cycles
     2  
     3  // This is for doing complete git-level tests using test utils
     4  // Needs to be a separate file from scanner_test so that we can use a diff package
     5  // which avoids import cycles with testutils
     6  
     7  import (
     8  	"fmt"
     9  	"sort"
    10  	"testing"
    11  	"time"
    12  
    13  	. "github.com/git-lfs/git-lfs/lfs"
    14  	test "github.com/git-lfs/git-lfs/t/cmd/util"
    15  	"github.com/stretchr/testify/assert"
    16  )
    17  
    18  func TestScanUnpushed(t *testing.T) {
    19  	repo := test.NewRepo(t)
    20  	repo.Pushd()
    21  	defer func() {
    22  		repo.Popd()
    23  		repo.Cleanup()
    24  	}()
    25  
    26  	inputs := []*test.CommitInput{
    27  		{ // 0
    28  			Files: []*test.FileInput{
    29  				{Filename: "file1.txt", Size: 20},
    30  			},
    31  		},
    32  		{ // 1
    33  			NewBranch: "branch2",
    34  			Files: []*test.FileInput{
    35  				{Filename: "file1.txt", Size: 25},
    36  			},
    37  		},
    38  		{ // 2
    39  			ParentBranches: []string{"master"}, // back on master
    40  			Files: []*test.FileInput{
    41  				{Filename: "file1.txt", Size: 30},
    42  			},
    43  		},
    44  		{ // 3
    45  			NewBranch: "branch3",
    46  			Files: []*test.FileInput{
    47  				{Filename: "file1.txt", Size: 32},
    48  			},
    49  		},
    50  	}
    51  	repo.AddCommits(inputs)
    52  
    53  	// Add a couple of remotes and test state depending on what's pushed
    54  	repo.AddRemote("origin")
    55  	repo.AddRemote("upstream")
    56  
    57  	pointers, err := scanUnpushed("")
    58  	assert.Nil(t, err, "Should be no error calling ScanUnpushed")
    59  	assert.Len(t, pointers, 4, "Should be 4 pointers because none pushed")
    60  
    61  	test.RunGitCommand(t, true, "push", "origin", "branch2")
    62  	// Branch2 will have pushed 2 commits
    63  	pointers, err = scanUnpushed("")
    64  	assert.Nil(t, err, "Should be no error calling ScanUnpushed")
    65  	assert.Len(t, pointers, 2, "Should be 2 pointers")
    66  
    67  	test.RunGitCommand(t, true, "push", "upstream", "master")
    68  	// Master pushes 1 more commit
    69  	pointers, err = scanUnpushed("")
    70  	assert.Nil(t, err, "Should be no error calling ScanUnpushed")
    71  	assert.Len(t, pointers, 1, "Should be 1 pointer")
    72  
    73  	test.RunGitCommand(t, true, "push", "origin", "branch3")
    74  	// All pushed (somewhere)
    75  	pointers, err = scanUnpushed("")
    76  	assert.Nil(t, err, "Should be no error calling ScanUnpushed")
    77  	assert.Empty(t, pointers, "Should be 0 pointers unpushed")
    78  
    79  	// Check origin
    80  	pointers, err = scanUnpushed("origin")
    81  	assert.Nil(t, err, "Should be no error calling ScanUnpushed")
    82  	assert.Empty(t, pointers, "Should be 0 pointers unpushed to origin")
    83  
    84  	// Check upstream
    85  	pointers, err = scanUnpushed("upstream")
    86  	assert.Nil(t, err, "Should be no error calling ScanUnpushed")
    87  	assert.Len(t, pointers, 2, "Should be 2 pointers unpushed to upstream")
    88  }
    89  
    90  func scanUnpushed(remoteName string) ([]*WrappedPointer, error) {
    91  	pointers := make([]*WrappedPointer, 0, 10)
    92  	var multiErr error
    93  
    94  	gitscanner := NewGitScanner(func(p *WrappedPointer, err error) {
    95  		if err != nil {
    96  			if multiErr != nil {
    97  				multiErr = fmt.Errorf("%v\n%v", multiErr, err)
    98  			} else {
    99  				multiErr = err
   100  			}
   101  			return
   102  		}
   103  
   104  		pointers = append(pointers, p)
   105  	})
   106  
   107  	if err := gitscanner.ScanUnpushed(remoteName, nil); err != nil {
   108  		return nil, err
   109  	}
   110  
   111  	gitscanner.Close()
   112  	return pointers, multiErr
   113  }
   114  
   115  func TestScanPreviousVersions(t *testing.T) {
   116  	repo := test.NewRepo(t)
   117  	repo.Pushd()
   118  	defer func() {
   119  		repo.Popd()
   120  		repo.Cleanup()
   121  	}()
   122  
   123  	now := time.Now()
   124  
   125  	inputs := []*test.CommitInput{
   126  		{ // 0
   127  			CommitDate: now.AddDate(0, 0, -20),
   128  			Files: []*test.FileInput{
   129  				{Filename: "file1.txt", Size: 20},
   130  				{Filename: "file2.txt", Size: 30},
   131  				{Filename: "folder/nested.txt", Size: 40},
   132  				{Filename: "folder/nested2.txt", Size: 31},
   133  			},
   134  		},
   135  		{ // 1
   136  			CommitDate: now.AddDate(0, 0, -10),
   137  			Files: []*test.FileInput{
   138  				{Filename: "file2.txt", Size: 22},
   139  			},
   140  		},
   141  		{ // 2
   142  			NewBranch:  "excluded",
   143  			CommitDate: now.AddDate(0, 0, -6),
   144  			Files: []*test.FileInput{
   145  				{Filename: "file2.txt", Size: 12},
   146  				{Filename: "folder/nested2.txt", Size: 16},
   147  			},
   148  		},
   149  		{ // 3
   150  			ParentBranches: []string{"master"},
   151  			CommitDate:     now.AddDate(0, 0, -4),
   152  			Files: []*test.FileInput{
   153  				{Filename: "folder/nested.txt", Size: 42},
   154  				{Filename: "folder/nested2.txt", Size: 6},
   155  			},
   156  		},
   157  		{ // 4
   158  			Files: []*test.FileInput{
   159  				{Filename: "folder/nested.txt", Size: 22},
   160  			},
   161  		},
   162  	}
   163  	outputs := repo.AddCommits(inputs)
   164  
   165  	// Previous commits excludes final state of each file, which is:
   166  	// file1.txt            [0] (unchanged since first commit so excluded)
   167  	// file2.txt            [1] (because [2] is on another branch so excluded)
   168  	// folder/nested.txt    [4] (updated at last commit)
   169  	// folder/nested2.txt   [3]
   170  
   171  	// The only changes which will be included are changes prior to final state
   172  	// where the '-' side of the diff is inside the date range
   173  
   174  	// 7 day limit excludes [0] commit, but includes state from that if there
   175  	// was a subsequent chang
   176  	pointers, err := scanPreviousVersions(t, "master", now.AddDate(0, 0, -7))
   177  	assert.Equal(t, nil, err)
   178  
   179  	// Includes the following 'before' state at commits:
   180  	// folder/nested.txt [-diff at 4, ie 3, -diff at 3 ie 0]
   181  	// folder/nested2.txt [-diff at 3 ie 0]
   182  	// others are either on diff branches, before this window, or unchanged
   183  	expected := []*WrappedPointer{
   184  		{Name: "folder/nested.txt", Pointer: outputs[3].Files[0]},
   185  		{Name: "folder/nested.txt", Pointer: outputs[0].Files[2]},
   186  		{Name: "folder/nested2.txt", Pointer: outputs[0].Files[3]},
   187  	}
   188  	// Need to sort to compare equality
   189  	sort.Sort(test.WrappedPointersByOid(expected))
   190  	sort.Sort(test.WrappedPointersByOid(pointers))
   191  	assert.Equal(t, expected, pointers)
   192  }
   193  
   194  func scanPreviousVersions(t *testing.T, ref string, since time.Time) ([]*WrappedPointer, error) {
   195  	pointers := make([]*WrappedPointer, 0, 10)
   196  	gitscanner := NewGitScanner(func(p *WrappedPointer, err error) {
   197  		if err != nil {
   198  			t.Error(err)
   199  			return
   200  		}
   201  		pointers = append(pointers, p)
   202  	})
   203  
   204  	err := gitscanner.ScanPreviousVersions(ref, since, nil)
   205  	return pointers, err
   206  }