1package git23import (4 "context"5 "os"6 "path/filepath"7 "testing"89 "github.com/stretchr/testify/assert"10)1112func TestRepository(t *testing.T) {13 path := os.TempDir()14 r := &Repository{15 path: path,16 }1718 assert.Equal(t, path, r.Path())19}2021func TestInit(t *testing.T) {22 ctx := context.Background()23 tests := []struct {24 opt InitOptions25 }{26 {27 opt: InitOptions{},28 },29 {30 opt: InitOptions{31 Bare: true,32 },33 },34 }35 for _, test := range tests {36 t.Run("", func(t *testing.T) {37 // Make sure it does not blow up38 path := tempPath()39 defer func() {40 _ = os.RemoveAll(path)41 }()4243 if err := Init(ctx, path, test.opt); err != nil {44 t.Fatal(err)45 }46 })47 }48}4950func TestOpen(t *testing.T) {51 _, err := Open(testrepo.Path())52 assert.Nil(t, err)5354 _, err = Open(tempPath())55 assert.Equal(t, os.ErrNotExist, err)56}5758func TestClone(t *testing.T) {59 ctx := context.Background()60 tests := []struct {61 opt CloneOptions62 }{63 {64 opt: CloneOptions{},65 },66 {67 opt: CloneOptions{68 Mirror: true,69 Bare: true,70 Quiet: true,71 },72 },73 {74 opt: CloneOptions{75 Branch: "develop",76 },77 },78 {79 opt: CloneOptions{80 Depth: 1,81 },82 },83 {84 opt: CloneOptions{85 Branch: "develop",86 Depth: 1,87 },88 },89 }90 for _, test := range tests {91 t.Run("", func(t *testing.T) {92 // Make sure it does not blow up93 path := tempPath()94 defer func() {95 _ = os.RemoveAll(path)96 }()9798 if err := Clone(ctx, testrepo.Path(), path, test.opt); err != nil {99 t.Fatal(err)100 }101 })102 }103}104105func setupTempRepo() (_ *Repository, cleanup func(), err error) {106 ctx := context.Background()107 path := tempPath()108 cleanup = func() {109 _ = os.RemoveAll(path)110 }111 defer func() {112 if err != nil {113 cleanup()114 }115 }()116117 if err = Clone(ctx, testrepo.Path(), path); err != nil {118 return nil, cleanup, err119 }120121 r, err := Open(path)122 if err != nil {123 return nil, cleanup, err124 }125 return r, cleanup, nil126}127128func TestRepository_Fetch(t *testing.T) {129 ctx := context.Background()130 r, cleanup, err := setupTempRepo()131 if err != nil {132 t.Fatal(err)133 }134 defer cleanup()135136 tests := []struct {137 opt FetchOptions138 }{139 {140 opt: FetchOptions{},141 },142 {143 opt: FetchOptions{144 Prune: true,145 },146 },147 }148 for _, test := range tests {149 t.Run("", func(t *testing.T) {150 // Make sure it does not blow up151 if err := r.Fetch(ctx, test.opt); err != nil {152 t.Fatal(err)153 }154 })155 }156}157158func TestRepository_Pull(t *testing.T) {159 ctx := context.Background()160 r, cleanup, err := setupTempRepo()161 if err != nil {162 t.Fatal(err)163 }164 defer cleanup()165166 tests := []struct {167 opt PullOptions168 }{169 {170 opt: PullOptions{},171 },172 {173 opt: PullOptions{174 Rebase: true,175 },176 },177 {178 opt: PullOptions{179 All: true,180 },181 },182 {183 opt: PullOptions{184 Remote: "origin",185 Branch: "master",186 },187 },188 }189 for _, test := range tests {190 t.Run("", func(t *testing.T) {191 // Make sure it does not blow up192 if err := r.Pull(ctx, test.opt); err != nil {193 t.Fatal(err)194 }195 })196 }197}198199func TestRepository_Push(t *testing.T) {200 ctx := context.Background()201 r, cleanup, err := setupTempRepo()202 if err != nil {203 t.Fatal(err)204 }205 defer cleanup()206207 tests := []struct {208 remote string209 branch string210 opt PushOptions211 }{212 {213 remote: "origin",214 branch: "master",215 opt: PushOptions{},216 },217 {218 remote: "origin",219 branch: "master",220 opt: PushOptions{SetUpstream: true},221 },222 }223 for _, test := range tests {224 t.Run("", func(t *testing.T) {225 // Make sure it does not blow up226 if err := r.Push(ctx, test.remote, test.branch, test.opt); err != nil {227 t.Fatal(err)228 }229 })230 }231}232233func TestRepository_Checkout(t *testing.T) {234 ctx := context.Background()235 r, cleanup, err := setupTempRepo()236 if err != nil {237 t.Fatal(err)238 }239 defer cleanup()240241 tests := []struct {242 branch string243 opt CheckoutOptions244 }{245 {246 branch: "develop",247 opt: CheckoutOptions{},248 },249 {250 branch: "a-new-branch",251 opt: CheckoutOptions{252 BaseBranch: "master",253 },254 },255 }256 for _, test := range tests {257 t.Run("", func(t *testing.T) {258 // Make sure it does not blow up259 if err := r.Checkout(ctx, test.branch, test.opt); err != nil {260 t.Fatal(err)261 }262 })263 }264}265266func TestRepository_Reset(t *testing.T) {267 ctx := context.Background()268 r, cleanup, err := setupTempRepo()269 if err != nil {270 t.Fatal(err)271 }272 defer cleanup()273274 tests := []struct {275 rev string276 opt ResetOptions277 }{278 {279 rev: testrepoMarks[27].String(),280 opt: ResetOptions{281 Hard: true,282 },283 },284 }285 for _, test := range tests {286 t.Run("", func(t *testing.T) {287 // Make sure it does not blow up288 if err := r.Reset(ctx, test.rev, test.opt); err != nil {289 t.Fatal(err)290 }291 })292 }293}294295func TestRepository_Move(t *testing.T) {296 ctx := context.Background()297 r, cleanup, err := setupTempRepo()298 if err != nil {299 t.Fatal(err)300 }301 defer cleanup()302303 tests := []struct {304 src string305 dst string306 opt MoveOptions307 }{308 {309 src: "run.sh",310 dst: "runme.sh",311 opt: MoveOptions{},312 },313 }314 for _, test := range tests {315 t.Run("", func(t *testing.T) {316 // Make sure it does not blow up317 if err := r.Move(ctx, test.src, test.dst, test.opt); err != nil {318 t.Fatal(err)319 }320 })321 }322}323324func TestRepository_Add(t *testing.T) {325 ctx := context.Background()326 r, cleanup, err := setupTempRepo()327 if err != nil {328 t.Fatal(err)329 }330 defer cleanup()331332 // Generate a file333 fpath := filepath.Join(r.Path(), "TESTFILE")334 err = os.WriteFile(fpath, []byte("something"), 0600)335 if err != nil {336 t.Fatal(err)337 }338339 // Make sure it does not blow up340 if err := r.Add(ctx, AddOptions{341 All: true,342 Pathspecs: []string{"TESTFILE"},343 }); err != nil {344 t.Fatal(err)345 }346}347348func TestRepository_Commit(t *testing.T) {349 ctx := context.Background()350 r, cleanup, err := setupTempRepo()351 if err != nil {352 t.Fatal(err)353 }354 defer cleanup()355356 committer := &Signature{357 Name: "alice",358 Email: "alice@example.com",359 }360 author := &Signature{361 Name: "bob",362 Email: "bob@example.com",363 }364 message := "Add a file"365366 t.Run("nothing to commit", func(t *testing.T) {367 if err = r.Commit(ctx, committer, message, CommitOptions{368 Author: author,369 }); err != nil {370 t.Fatal(err)371 }372 })373374 t.Run("committer is also the author", func(t *testing.T) {375 // Generate a file and add to index376 fpath := filepath.Join(r.Path(), "COMMITTER_IS_AUTHOR")377 err = os.WriteFile(fpath, []byte("something"), 0600)378 if err != nil {379 t.Fatal(err)380 }381382 if err := r.Add(ctx, AddOptions{383 All: true,384 }); err != nil {385 t.Fatal(err)386 }387388 // Make sure it does not blow up389 if err = r.Commit(ctx, committer, message); err != nil {390 t.Fatal(err)391 }392393 // Verify the result394 c, err := r.CatFileCommit(ctx, "master")395 if err != nil {396 t.Fatal(err)397 }398399 assert.Equal(t, committer.Name, c.Committer.Name)400 assert.Equal(t, committer.Email, c.Committer.Email)401 assert.Equal(t, committer.Name, c.Author.Name)402 assert.Equal(t, committer.Email, c.Author.Email)403 assert.Equal(t, message+"\n", c.Message)404 })405406 t.Run("committer is not the author", func(t *testing.T) {407 // Generate a file and add to index408 fpath := filepath.Join(r.Path(), "COMMITTER_IS_NOT_AUTHOR")409 err = os.WriteFile(fpath, []byte("something"), 0600)410 if err != nil {411 t.Fatal(err)412 }413414 if err := r.Add(ctx, AddOptions{415 All: true,416 }); err != nil {417 t.Fatal(err)418 }419420 // Make sure it does not blow up421 if err = r.Commit(ctx, committer, message, CommitOptions{Author: author}); err != nil {422 t.Fatal(err)423 }424425 // Verify the result426 c, err := r.CatFileCommit(ctx, "master")427 if err != nil {428 t.Fatal(err)429 }430431 assert.Equal(t, committer.Name, c.Committer.Name)432 assert.Equal(t, committer.Email, c.Committer.Email)433 assert.Equal(t, author.Name, c.Author.Name)434 assert.Equal(t, author.Email, c.Author.Email)435 assert.Equal(t, message+"\n", c.Message)436 })437}438439func TestRepository_RevParse(t *testing.T) {440 ctx := context.Background()441 tests := []struct {442 rev string443 expID string444 expErr error445 }{446 {447 rev: testrepoMarks[29].String()[:7],448 expID: testrepoMarks[29].String(),449 expErr: nil,450 },451 {452 rev: "release-1.0",453 expID: testrepoMarks[30].String(),454 expErr: nil,455 },456 {457 rev: "RELEASE_1.0",458 expID: testrepoMarks[12].String(),459 expErr: nil,460 },461 {462 rev: "refs/heads/release-1.0",463 expID: testrepoMarks[30].String(),464 expErr: nil,465 },466 {467 rev: "refs/tags/RELEASE_1.0",468 expID: testrepoMarks[12].String(),469 expErr: nil,470 },471472 {473 rev: "refs/tags/404",474 expID: "",475 expErr: ErrRevisionNotExist,476 },477 }478 for _, test := range tests {479 t.Run("", func(t *testing.T) {480 id, err := testrepo.RevParse(ctx, test.rev)481 assert.Equal(t, test.expErr, err)482 assert.Equal(t, test.expID, id)483 })484 }485}486487func TestRepository_CountObjects(t *testing.T) {488 ctx := context.Background()489 // Make sure it does not blow up490 _, err := testrepo.CountObjects(ctx, CountObjectsOptions{})491 if err != nil {492 t.Fatal(err)493 }494}495496func TestRepository_Fsck(t *testing.T) {497 ctx := context.Background()498 // Make sure it does not blow up499 err := testrepo.Fsck(ctx, FsckOptions{})500 if err != nil {501 t.Fatal(err)502 }503}504505func TestRepository_ObjectFormat(t *testing.T) {506 ctx := context.Background()507 obf, err := testrepo.ObjectFormat(ctx)508 if err != nil {509 t.Fatal(err)510 }511 assert.Equal(t, obf, testrepoObjectFormat)512}