Skip to content

Commit 56512e7

Browse files
committed
add new invalid repo test case for branch package
1 parent 6701f39 commit 56512e7

11 files changed

+84
-25
lines changed

.github/workflows/go-collab.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ jobs:
4646
sudo chown $(whoami) /usr/local/gitconvex/
4747
go generate
4848
$GITHUB_WORKSPACE/build_scripts/clone_test_repo.sh
49-
go test ./... -count=1 -v
49+
go test ./... -count=1 -cover
5050
5151
gitconvex-build:
5252
needs:

.github/workflows/go.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ jobs:
4646
sudo chown $(whoami) /usr/local/gitconvex/
4747
go generate
4848
$GITHUB_WORKSPACE/build_scripts/clone_test_repo.sh
49-
go test ./... -count=1 -v
49+
go test ./... -count=1 -cover
5050
5151
gitconvex-build:
5252
needs:

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ gitconvex-test/
99

1010
env_config.json
1111
gitclient
12+
coverage.out
1213
/*.exe
1314
/*.dll
1415
gitconvex

Makefile

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,20 @@ test:
3434
rm -rf $$GITCONVEX_TEST_REPO && \
3535
go clean --cache && \
3636
./build_scripts/clone_test_repo.sh && \
37-
go test ./... -count=1 && \
37+
go test ./... -count=1 -cover -coverprofile=coverage.out && \
3838
rm -rf $$GITCONVEX_TEST_REPO
3939

40+
test-pretty:
41+
export GITCONVEX_TEST_REPO="$(PWD)/gitconvex-test" && \
42+
export GITCONVEX_DEFAULT_PATH="$(PWD)/gitconvex-test" && \
43+
rm -rf $$GITCONVEX_TEST_REPO && \
44+
go clean --cache && \
45+
./build_scripts/clone_test_repo.sh && \
46+
gotestsum ./... -count=1 -cover -coverprofile=coverage.out && \
47+
rm -rf $$GITCONVEX_TEST_REPO
48+
49+
show-coverage:
50+
go tool cover -html=coverage.out
51+
4052
start:
4153
./dist/gitconvex

build_scripts/clone_test_repo.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
#!/bin/bash
22

3-
git clone https://github.com/neel1996/gitconvex-test.git $GITCONVEX_TEST_REPO
3+
git clone https://github.com/neel1996/gitconvex-test.git $GITCONVEX_TEST_REPO
4+
git init $GITCONVEX_TEST_REPO/no_head
5+
clear

git/branch/branch_validation_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ import (
1010

1111
type BranchFieldValidationTestSuite struct {
1212
suite.Suite
13-
repo *git2go.Repository
14-
branchName string
15-
validation Validation
13+
repo *git2go.Repository
14+
branchNames []string
15+
validation Validation
1616
}
1717

1818
func TestBranchFieldValidationTestSuite(t *testing.T) {
@@ -26,8 +26,8 @@ func (suite *BranchFieldValidationTestSuite) SetupTest() {
2626
}
2727

2828
suite.repo = r
29-
suite.branchName = "test_branch"
30-
suite.validation = NewBranchFieldsValidation(suite.repo, suite.branchName)
29+
suite.branchNames = []string{"test_branch_1", "test_branch_2"}
30+
suite.validation = NewBranchFieldsValidation(suite.repo, suite.branchNames[0], suite.branchNames[1])
3131
}
3232

3333
func (suite *BranchFieldValidationTestSuite) TestValidateBranchFields_WhenAllFieldsAreValid_ShouldReturnNil() {
@@ -45,7 +45,7 @@ func (suite *BranchFieldValidationTestSuite) TestValidateBranchFields_WhenRepoIs
4545
}
4646

4747
func (suite *BranchFieldValidationTestSuite) TestValidateBranchFields_WhenBranchNameIsEmpty_ShouldReturnError() {
48-
suite.validation = NewBranchFieldsValidation(suite.repo, "")
48+
suite.validation = NewBranchFieldsValidation(suite.repo, "", "")
4949
err := suite.validation.ValidateBranchFields()
5050

5151
suite.NotNil(err)

git/branch/git_branch_add_test.go

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,16 @@ import (
55
git2go "github.com/libgit2/git2go/v31"
66
"github.com/stretchr/testify/suite"
77
"os"
8+
"path/filepath"
89
"testing"
910
)
1011

1112
type BranchAddTestSuite struct {
1213
suite.Suite
13-
repo *git2go.Repository
14-
branchAdd Add
14+
repo *git2go.Repository
15+
noHeadRepo *git2go.Repository
16+
branchName string
17+
branchAdd Add
1518
}
1619

1720
func TestBranchAddTestSuite(t *testing.T) {
@@ -23,13 +26,18 @@ func (suite *BranchAddTestSuite) SetupTest() {
2326
if err != nil {
2427
fmt.Println(err)
2528
}
29+
noHeadPath := os.Getenv("GITCONVEX_TEST_REPO") + string(filepath.Separator) + "no_head"
30+
noHeadRepo, _ := git2go.OpenRepository(noHeadPath)
31+
2632
suite.repo = r
27-
suite.branchAdd = NewAddBranch(suite.repo, "test_1", false, nil)
33+
suite.noHeadRepo = noHeadRepo
34+
suite.branchName = "test_1"
35+
suite.branchAdd = NewAddBranch(suite.repo, suite.branchName, false, nil)
2836
}
2937

30-
func (suite *BranchAddTestSuite) TearDownTest() {
38+
func (suite *BranchAddTestSuite) TearDownSuite() {
3139
r, _ := git2go.OpenRepository(os.Getenv("GITCONVEX_TEST_REPO"))
32-
NewDeleteBranch(r, "test_1").DeleteBranch()
40+
_ = NewDeleteBranch(r, suite.branchName).DeleteBranch()
3341
}
3442

3543
func (suite *BranchAddTestSuite) TestAddBranch_WhenBranchAdditionSucceeds_ShouldReturnNil() {
@@ -46,16 +54,22 @@ func (suite *BranchAddTestSuite) TestAddBranch_WhenRepoIsNil_ShouldReturnError()
4654
}
4755

4856
func (suite *BranchAddTestSuite) TestAddBranch_WhenBranchNameIsEmpty_ShouldReturnError() {
49-
r, _ := git2go.OpenRepository(os.Getenv("GITCONVEX_TEST_REPO"))
50-
suite.branchAdd = NewAddBranch(r, "", false, nil)
57+
suite.branchAdd = NewAddBranch(suite.repo, "", false, nil)
58+
branchAddError := suite.branchAdd.AddBranch()
59+
60+
suite.NotNil(branchAddError)
61+
}
62+
63+
func (suite *BranchAddTestSuite) TestAddBranch_WhenHeadIsNil_ShouldReturnError() {
64+
suite.branchAdd = NewAddBranch(suite.noHeadRepo, suite.branchName, false, nil)
5165
branchAddError := suite.branchAdd.AddBranch()
5266

5367
suite.NotNil(branchAddError)
5468
}
5569

5670
func (suite *BranchAddTestSuite) TestAddBranch_WhenBranchAdditionFails_ShouldReturnError() {
5771
r, _ := git2go.OpenRepository(os.Getenv("GITCONVEX_TEST_REPO"))
58-
suite.branchAdd = NewAddBranch(r, "test_1", false, nil)
72+
suite.branchAdd = NewAddBranch(r, "master", false, nil)
5973

6074
//Adding duplicate branches
6175
_ = suite.branchAdd.AddBranch()

git/branch/git_branch_checkout_test.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
git2go "github.com/libgit2/git2go/v31"
66
"github.com/stretchr/testify/suite"
77
"os"
8+
"path/filepath"
89
"testing"
910
)
1011

@@ -13,18 +14,24 @@ type BranchCheckoutTestSuite struct {
1314
branchName string
1415
checkoutBranch Checkout
1516
repo *git2go.Repository
17+
noHeadRepo *git2go.Repository
1618
}
1719

1820
func TestBranchCheckoutTestSuite(t *testing.T) {
1921
suite.Run(t, new(BranchCheckoutTestSuite))
2022
}
2123

22-
func (suite *BranchCheckoutTestSuite) SetupTest() {
24+
func (suite *BranchCheckoutTestSuite) SetupSuite() {
2325
r, err := git2go.OpenRepository(os.Getenv("GITCONVEX_TEST_REPO"))
2426
if err != nil {
2527
fmt.Println(err)
2628
}
29+
30+
noHeadPath := os.Getenv("GITCONVEX_TEST_REPO") + string(filepath.Separator) + "no_head"
31+
noHeadRepo, _ := git2go.OpenRepository(noHeadPath)
32+
2733
suite.repo = r
34+
suite.noHeadRepo = noHeadRepo
2835
suite.branchName = "test_checkout"
2936
addErr := NewAddBranch(suite.repo, suite.branchName, false, nil).AddBranch()
3037
if addErr != nil {
@@ -39,7 +46,7 @@ func (suite *BranchCheckoutTestSuite) TearDownSuite() {
3946
if err != nil {
4047
return
4148
}
42-
NewDeleteBranch(suite.repo, suite.branchName).DeleteBranch()
49+
_ = NewDeleteBranch(suite.repo, suite.branchName).DeleteBranch()
4350
}
4451

4552
func (suite *BranchCheckoutTestSuite) TestCheckoutBranch_WhenBranchIsCheckedOut_ShouldReturnNil() {
@@ -71,6 +78,13 @@ func (suite *BranchCheckoutTestSuite) TestCheckoutBranch_WhenBranchNameIsEmpty_S
7178
suite.Equal("branch name is empty", err.Error())
7279
}
7380

81+
func (suite *BranchCheckoutTestSuite) TestCheckoutBranch_WhenRepoHasNoHead_ShouldReturnError() {
82+
suite.checkoutBranch = NewBranchCheckout(suite.noHeadRepo, suite.branchName)
83+
err := suite.checkoutBranch.CheckoutBranch()
84+
85+
suite.NotNil(err)
86+
}
87+
7488
func (suite *BranchCheckoutTestSuite) TestCheckoutBranch_WhenNonExistingBranchIsSelected_ShouldReturnError() {
7589
suite.checkoutBranch = NewBranchCheckout(suite.repo, "no_exists")
7690
err := suite.checkoutBranch.CheckoutBranch()

git/branch/git_branch_compare_test.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/stretchr/testify/suite"
99
"io/ioutil"
1010
"os"
11+
"path/filepath"
1112
"testing"
1213
"time"
1314
)
@@ -31,7 +32,7 @@ func (suite *BranchCompareTestSuite) SetupSuite() {
3132
fmt.Println(err)
3233
}
3334
suite.repo = r
34-
suite.testFile = os.Getenv("GITCONVEX_TEST_REPO") + "/" + "compare_test.txt"
35+
suite.testFile = os.Getenv("GITCONVEX_TEST_REPO") + string(filepath.Separator) + "compare_test.txt"
3536
suite.baseBranch = "master"
3637
suite.compareBranch = "new_compare"
3738
addErr := NewAddBranch(r, suite.compareBranch, false, nil).AddBranch()
@@ -53,7 +54,7 @@ func (suite *BranchCompareTestSuite) SetupTest() {
5354
}
5455

5556
func (suite *BranchCompareTestSuite) TearDownSuite() {
56-
checkoutErr := NewBranchCheckout(suite.repo, suite.baseBranch).CheckoutBranch()
57+
checkoutErr := NewBranchCheckout(suite.repo, "master").CheckoutBranch()
5758
if checkoutErr != nil {
5859
logger.Log(checkoutErr.Error(), global.StatusError)
5960
return
@@ -80,9 +81,10 @@ func (suite *BranchCompareTestSuite) TestCompareBranch_WhenBranchesHaveDifferent
8081
time.UTC,
8182
).String()
8283

83-
suite.Len(compareResults, 1)
84+
suite.NotNil(compareResults)
85+
suite.NotZero(len(compareResults))
86+
suite.NotZero(len(compareResults[0].Commits))
8487

85-
suite.Len(compareResults[0].Commits, 1)
8688
suite.NotNil(*compareResults[0].Commits[0])
8789
suite.NotEmpty(*compareResults[0].Commits[0].CommitMessage)
8890
suite.Contains(timeString, compareResults[0].Date)

git/branch/git_branch_delete_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
git2go "github.com/libgit2/git2go/v31"
66
"github.com/stretchr/testify/suite"
77
"os"
8+
"path/filepath"
89
"testing"
910
)
1011

@@ -13,6 +14,7 @@ type BranchDeleteTestSuite struct {
1314
branchDelete Delete
1415
branchName string
1516
repo *git2go.Repository
17+
noHeadRepo *git2go.Repository
1618
}
1719

1820
func TestBranchDeleteTestSuite(t *testing.T) {
@@ -24,7 +26,12 @@ func (suite *BranchDeleteTestSuite) SetupTest() {
2426
if err != nil {
2527
fmt.Println(err)
2628
}
29+
30+
noHeadPath := os.Getenv("GITCONVEX_TEST_REPO") + string(filepath.Separator) + "no_head"
31+
noHeadRepo, _ := git2go.OpenRepository(noHeadPath)
32+
2733
suite.repo = r
34+
suite.noHeadRepo = noHeadRepo
2835
suite.branchName = "delete_branch"
2936
suite.branchDelete = NewDeleteBranch(suite.repo, suite.branchName)
3037
}
@@ -70,6 +77,13 @@ func (suite *BranchDeleteTestSuite) TestDeleteBranch_WhenBranchNameIsEmpty_Shoul
7077
suite.NotNil(err)
7178
}
7279

80+
func (suite *BranchDeleteTestSuite) TestDeleteBranch_WhenRepoHasNoHead_ShouldReturnError() {
81+
suite.branchDelete = NewDeleteBranch(suite.noHeadRepo, suite.branchName)
82+
err := suite.branchDelete.DeleteBranch()
83+
84+
suite.NotNil(err)
85+
}
86+
7387
func (suite *BranchDeleteTestSuite) TestDeleteBranch_WhenBranchDoesNotExists_ShouldReturnError() {
7488
suite.branchDelete = NewDeleteBranch(suite.repo, "no_exists")
7589
err := suite.branchDelete.DeleteBranch()

git/branch/git_branch_list_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func (suite *BranchListTestSuite) SetupTest() {
2626
fmt.Println(err)
2727
}
2828
noHeadPath := os.Getenv("GITCONVEX_TEST_REPO") + string(filepath.Separator) + "no_head"
29-
noHeadRepo, _ := git2go.InitRepository(noHeadPath, false)
29+
noHeadRepo, _ := git2go.OpenRepository(noHeadPath)
3030

3131
suite.repo = r
3232
suite.noHeadRepo = noHeadRepo

0 commit comments

Comments
 (0)