1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
|
package git
import (
"bufio"
"encoding/hex"
"errors"
"fmt"
"io"
"strings"
"sync/atomic"
"testing"
"github.com/stretchr/testify/assert"
)
type ArgsTestCase struct {
Include []string
Exclude []string
Opt *ScanRefsOptions
ExpectedStdin string
ExpectedArgs []string
ExpectedErr string
}
func (c *ArgsTestCase) Assert(t *testing.T) {
stdin, args, err := revListArgs(c.Include, c.Exclude, c.Opt)
if len(c.ExpectedErr) > 0 {
assert.EqualError(t, err, c.ExpectedErr)
} else {
assert.Nil(t, err)
}
assert.EqualValues(t, c.ExpectedArgs, args)
if stdin != nil {
b, err := io.ReadAll(stdin)
assert.Nil(t, err)
assert.Equal(t, c.ExpectedStdin, string(b))
} else if len(c.ExpectedStdin) > 0 {
t.Errorf("git: expected stdin contents %s, got none", c.ExpectedStdin)
}
}
var (
s1 = "decafdecafdecafdecafdecafdecafdecafdecaf"
s2 = "cafecafecafecafecafecafecafecafecafecafe"
)
func TestRevListArgs(t *testing.T) {
for desc, c := range map[string]*ArgsTestCase{
"scan refs deleted, include and exclude": {
Include: []string{s1}, Exclude: []string{s2}, Opt: &ScanRefsOptions{
Mode: ScanRefsMode,
SkipDeletedBlobs: false,
},
ExpectedStdin: fmt.Sprintf("%s\n^%s", s1, s2),
ExpectedArgs: []string{"rev-list", "--objects", "--do-walk", "--stdin", "--"},
},
"scan refs not deleted, include and exclude": {
Include: []string{s1}, Exclude: []string{s2}, Opt: &ScanRefsOptions{
Mode: ScanRefsMode,
SkipDeletedBlobs: true,
},
ExpectedStdin: fmt.Sprintf("%s\n^%s", s1, s2),
ExpectedArgs: []string{"rev-list", "--objects", "--no-walk", "--stdin", "--"},
},
"scan refs deleted, include only": {
Include: []string{s1}, Opt: &ScanRefsOptions{
Mode: ScanRefsMode,
SkipDeletedBlobs: false,
},
ExpectedStdin: s1,
ExpectedArgs: []string{"rev-list", "--objects", "--do-walk", "--stdin", "--"},
},
"scan refs not deleted, include only": {
Include: []string{s1}, Opt: &ScanRefsOptions{
Mode: ScanRefsMode,
SkipDeletedBlobs: true,
},
ExpectedStdin: s1,
ExpectedArgs: []string{"rev-list", "--objects", "--no-walk", "--stdin", "--"},
},
"scan all": {
Include: []string{s1}, Exclude: []string{s2}, Opt: &ScanRefsOptions{
Mode: ScanAllMode,
},
ExpectedArgs: []string{"rev-list", "--objects", "--all", "--stdin", "--"},
},
"scan include to remote, no skipped refs": {
Include: []string{s1}, Opt: &ScanRefsOptions{
Mode: ScanRangeToRemoteMode,
Remote: "origin",
SkippedRefs: []string{},
},
ExpectedStdin: s1,
ExpectedArgs: []string{"rev-list", "--objects", "--ignore-missing", "--not", "--remotes=origin", "--stdin", "--"},
},
"scan include to remote, skipped refs": {
Include: []string{s1}, Exclude: []string{s2}, Opt: &ScanRefsOptions{
Mode: ScanRangeToRemoteMode,
Remote: "origin",
SkippedRefs: []string{"a", "b", "c"},
},
ExpectedArgs: []string{"rev-list", "--objects", "--ignore-missing", "--stdin", "--"},
ExpectedStdin: s1 + "\n^" + s2 + "\na\nb\nc",
},
"scan unknown type": {
Include: []string{s1}, Exclude: []string{s2}, Opt: &ScanRefsOptions{
Mode: ScanningMode(-1),
},
ExpectedErr: "unknown scan type: -1",
},
"scan date order": {
Include: []string{s1}, Exclude: []string{s2}, Opt: &ScanRefsOptions{
Mode: ScanRefsMode,
Order: DateRevListOrder,
},
ExpectedStdin: fmt.Sprintf("%s\n^%s", s1, s2),
ExpectedArgs: []string{"rev-list", "--objects", "--date-order", "--do-walk", "--stdin", "--"},
},
"scan author date order": {
Include: []string{s1}, Exclude: []string{s2}, Opt: &ScanRefsOptions{
Mode: ScanRefsMode,
Order: AuthorDateRevListOrder,
},
ExpectedStdin: fmt.Sprintf("%s\n^%s", s1, s2),
ExpectedArgs: []string{"rev-list", "--objects", "--author-date-order", "--do-walk", "--stdin", "--"},
},
"scan topo order": {
Include: []string{s1}, Exclude: []string{s2}, Opt: &ScanRefsOptions{
Mode: ScanRefsMode,
Order: TopoRevListOrder,
},
ExpectedStdin: fmt.Sprintf("%s\n^%s", s1, s2),
ExpectedArgs: []string{"rev-list", "--objects", "--topo-order", "--do-walk", "--stdin", "--"},
},
"scan commits only": {
Include: []string{s1}, Exclude: []string{s2}, Opt: &ScanRefsOptions{
Mode: ScanRefsMode,
CommitsOnly: true,
},
ExpectedStdin: fmt.Sprintf("%s\n^%s", s1, s2),
ExpectedArgs: []string{"rev-list", "--do-walk", "--stdin", "--"},
},
"scan reverse": {
Include: []string{s1}, Exclude: []string{s2}, Opt: &ScanRefsOptions{
Mode: ScanRefsMode,
Reverse: true,
},
ExpectedStdin: fmt.Sprintf("%s\n^%s", s1, s2),
ExpectedArgs: []string{"rev-list", "--objects", "--reverse", "--do-walk", "--stdin", "--"},
},
} {
t.Run(desc, c.Assert)
}
}
func TestRevListScannerCallsClose(t *testing.T) {
var called uint32
err := errors.New("this is a marker error")
s := &RevListScanner{
closeFn: func() error {
atomic.AddUint32(&called, 1)
return err
},
}
got := s.Close()
assert.EqualValues(t, 1, atomic.LoadUint32(&called))
assert.Equal(t, err, got)
}
func TestRevListScannerTreatsCloseFnAsOptional(t *testing.T) {
s := &RevListScanner{
closeFn: nil,
}
defer func() { assert.Nil(t, recover()) }()
assert.Nil(t, s.Close())
}
func TestRevListScannerParsesLinesWithNames(t *testing.T) {
given := "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa name.dat"
s := &RevListScanner{
s: bufio.NewScanner(strings.NewReader(given)),
}
assert.True(t, s.Scan())
assert.Equal(t, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", hex.EncodeToString(s.OID()))
assert.Equal(t, "name.dat", s.Name())
assert.Nil(t, s.Err())
assert.False(t, s.Scan())
assert.Equal(t, "", s.Name())
assert.Nil(t, s.OID())
assert.Nil(t, s.Err())
}
func TestRevListScannerParsesLinesWithoutName(t *testing.T) {
given := "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
s := &RevListScanner{
s: bufio.NewScanner(strings.NewReader(given)),
}
assert.True(t, s.Scan())
assert.Equal(t, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", hex.EncodeToString(s.OID()))
assert.Nil(t, s.Err())
assert.False(t, s.Scan())
assert.Equal(t, "", s.Name())
assert.Nil(t, s.OID())
assert.Nil(t, s.Err())
}
|