1package git23import (4 "context"5 "errors"6 "fmt"7 "testing"8 "time"910 "github.com/stretchr/testify/assert"11)1213func Test_escapePath(t *testing.T) {14 tests := []struct {15 path string16 expPath string17 }{18 {19 path: "",20 expPath: "",21 },22 {23 path: "normal",24 expPath: "normal",25 },26 {27 path: ":normal",28 expPath: "\\:normal",29 },30 }31 for _, test := range tests {32 t.Run("", func(t *testing.T) {33 assert.Equal(t, test.expPath, escapePath(test.path))34 })35 }36}3738func TestRepository_CatFileCommit(t *testing.T) {39 ctx := context.Background()4041 t.Run("invalid revision", func(t *testing.T) {42 c, err := testrepo.CatFileCommit(ctx, "bad_revision")43 assert.Equal(t, ErrRevisionNotExist, err)44 assert.Nil(t, c)45 })4647 c, err := testrepo.CatFileCommit(ctx, testrepoMarks[31].String())48 if err != nil {49 t.Fatal(err)50 }5152 assert.Equal(t, testrepoMarks[31].String(), c.ID.String())53 assert.Equal(t, "Add a symlink\n", c.Message)54}5556func TestRepository_BranchCommit(t *testing.T) {57 ctx := context.Background()5859 t.Run("invalid branch", func(t *testing.T) {60 c, err := testrepo.BranchCommit(ctx, "refs/heads/release-1.0")61 assert.Equal(t, ErrRevisionNotExist, err)62 assert.Nil(t, c)63 })6465 c, err := testrepo.BranchCommit(ctx, "release-1.0")66 if err != nil {67 t.Fatal(err)68 }6970 assert.Equal(t, testrepoMarks[30].String(), c.ID.String())71 assert.Equal(t, "Rename shell script\n", c.Message)72}7374func TestRepository_TagCommit(t *testing.T) {75 ctx := context.Background()7677 t.Run("invalid branch", func(t *testing.T) {78 c, err := testrepo.BranchCommit(ctx, "refs/tags/v1.0.0")79 assert.Equal(t, ErrRevisionNotExist, err)80 assert.Nil(t, c)81 })8283 c, err := testrepo.BranchCommit(ctx, "release-1.0")84 if err != nil {85 t.Fatal(err)86 }8788 assert.Equal(t, testrepoMarks[30].String(), c.ID.String())89 assert.Equal(t, "Rename shell script\n", c.Message)90}9192func TestRepository_Log(t *testing.T) {93 ctx := context.Background()94 tests := []struct {95 rev string96 opt LogOptions97 expCommitIDs []string98 }{99 {100 rev: testrepoMarks[30].String(),101 opt: LogOptions{102 Since: time.Unix(1581250680, 0),103 },104 expCommitIDs: []string{105 testrepoMarks[30].String(),106 testrepoMarks[29].String(),107 },108 },109 {110 rev: testrepoMarks[30].String(),111 opt: LogOptions{112 Since: time.Now().AddDate(100, 0, 0),113 },114 expCommitIDs: []string{},115 },116 }117 for _, test := range tests {118 t.Run("", func(t *testing.T) {119 commits, err := testrepo.Log(ctx, test.rev, test.opt)120 if err != nil {121 t.Fatal(err)122 }123124 assert.Equal(t, test.expCommitIDs, commitsToIDs(commits))125 })126 }127}128129func TestRepository_CommitByRevision(t *testing.T) {130 ctx := context.Background()131132 t.Run("invalid revision", func(t *testing.T) {133 c, err := testrepo.CommitByRevision(ctx, "bad_revision")134 assert.Equal(t, ErrRevisionNotExist, err)135 assert.Nil(t, c)136 })137138 tests := []struct {139 rev string140 opt CommitByRevisionOptions141 expID string142 }{143 {144 rev: testrepoMarks[29].String()[:7],145 expID: testrepoMarks[29].String(),146 },147 }148 for _, test := range tests {149 t.Run("", func(t *testing.T) {150 c, err := testrepo.CommitByRevision(ctx, test.rev, test.opt)151 if err != nil {152 t.Fatal(err)153 }154155 assert.Equal(t, test.expID, c.ID.String())156 })157 }158}159160func TestRepository_CommitsSince(t *testing.T) {161 ctx := context.Background()162 tests := []struct {163 rev string164 since time.Time165 opt CommitsSinceOptions166 expCommitIDs []string167 }{168 {169 rev: testrepoMarks[30].String(),170 since: time.Unix(1581250680, 0),171 expCommitIDs: []string{172 testrepoMarks[30].String(),173 testrepoMarks[29].String(),174 },175 },176 {177 rev: testrepoMarks[30].String(),178 since: time.Now().AddDate(100, 0, 0),179 expCommitIDs: []string{},180 },181 }182 for _, test := range tests {183 t.Run("", func(t *testing.T) {184 commits, err := testrepo.CommitsSince(ctx, test.rev, test.since, test.opt)185 if err != nil {186 t.Fatal(err)187 }188189 assert.Equal(t, test.expCommitIDs, commitsToIDs(commits))190 })191 }192}193194func TestRepository_DiffNameOnly(t *testing.T) {195 ctx := context.Background()196 tests := []struct {197 base string198 head string199 opt DiffNameOnlyOptions200 expFiles []string201 }{202 {203 base: testrepoMarks[26].String(),204 head: testrepoMarks[27].String(),205 expFiles: []string{"fix.txt"},206 },207 {208 base: testrepoMarks[24].String(),209 head: testrepoMarks[27].String(),210 opt: DiffNameOnlyOptions{211 NeedsMergeBase: true,212 },213 expFiles: []string{"fix.txt", "pom.xml", "src/test/java/com/github/AppTest.java"},214 },215216 {217 base: testrepoMarks[24].String(),218 head: testrepoMarks[27].String(),219 opt: DiffNameOnlyOptions{220 Path: "src",221 },222 expFiles: []string{"src/test/java/com/github/AppTest.java"},223 },224 {225 base: testrepoMarks[24].String(),226 head: testrepoMarks[27].String(),227 opt: DiffNameOnlyOptions{228 Path: "resources",229 },230 expFiles: []string{},231 },232 }233 for _, test := range tests {234 t.Run("", func(t *testing.T) {235 files, err := testrepo.DiffNameOnly(ctx, test.base, test.head, test.opt)236 if err != nil {237 t.Fatal(err)238 }239240 assert.Equal(t, test.expFiles, files)241 })242 }243}244245func TestRepository_RevListCount(t *testing.T) {246 ctx := context.Background()247248 t.Run("no refspecs", func(t *testing.T) {249 count, err := testrepo.RevListCount(ctx, []string{})250 assert.Equal(t, errors.New("must have at least one refspec"), err)251 assert.Zero(t, count)252 })253254 tests := []struct {255 refspecs []string256 opt RevListCountOptions257 expCount int64258 }{259 {260 refspecs: []string{testrepoMarks[1].String()},261 expCount: 1,262 },263 {264 refspecs: []string{testrepoMarks[5].String()},265 expCount: 5,266 },267 {268 refspecs: []string{testrepoMarks[27].String()},269 expCount: 27,270 },271272 {273 refspecs: []string{testrepoMarks[21].String()},274 opt: RevListCountOptions{275 Path: "README.txt",276 },277 expCount: 3,278 },279 {280 refspecs: []string{testrepoMarks[21].String()},281 opt: RevListCountOptions{282 Path: "resources",283 },284 expCount: 1,285 },286 }287 for _, test := range tests {288 t.Run("", func(t *testing.T) {289 count, err := testrepo.RevListCount(ctx, test.refspecs, test.opt)290 if err != nil {291 t.Fatal(err)292 }293294 assert.Equal(t, test.expCount, count)295 })296 }297}298299func TestRepository_RevList(t *testing.T) {300 ctx := context.Background()301302 t.Run("no refspecs", func(t *testing.T) {303 commits, err := testrepo.RevList(ctx, []string{})304 assert.Equal(t, errors.New("must have at least one refspec"), err)305 assert.Nil(t, commits)306 })307308 tests := []struct {309 refspecs []string310 opt RevListOptions311 expCommitIDs []string312 }{313 {314 refspecs: []string{fmt.Sprintf("%s...%s", testrepoMarks[24].String(), testrepoMarks[27].String())},315 expCommitIDs: []string{316 testrepoMarks[27].String(),317 testrepoMarks[26].String(),318 testrepoMarks[25].String(),319 },320 },321 {322 refspecs: []string{fmt.Sprintf("%s...%s", testrepoMarks[24].String(), testrepoMarks[27].String())},323 opt: RevListOptions{324 Path: "src",325 },326 expCommitIDs: []string{327 testrepoMarks[25].String(),328 },329 },330 }331 for _, test := range tests {332 t.Run("", func(t *testing.T) {333 commits, err := testrepo.RevList(ctx, test.refspecs, test.opt)334 if err != nil {335 t.Fatal(err)336 }337338 assert.Equal(t, test.expCommitIDs, commitsToIDs(commits))339 })340 }341}342343func TestRepository_LatestCommitTime(t *testing.T) {344 ctx := context.Background()345 tests := []struct {346 opt LatestCommitTimeOptions347 expTime time.Time348 }{349 {350 opt: LatestCommitTimeOptions{351 Branch: "release-1.0",352 },353 expTime: time.Unix(1581256638, 0),354 },355 }356 for _, test := range tests {357 t.Run("", func(t *testing.T) {358 got, err := testrepo.LatestCommitTime(ctx, test.opt)359 if err != nil {360 t.Fatal(err)361 }362363 assert.Equal(t, test.expTime.Unix(), got.Unix())364 })365 }366}