Skip to content

Commit

Permalink
Changing Testing behaviour to test non existing branch
Browse files Browse the repository at this point in the history
  • Loading branch information
Mohit Sharma committed Dec 18, 2023
1 parent e6cc84a commit 494bbe8
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
4 changes: 2 additions & 2 deletions cmd/update-rules/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ func UpdateRules(rules *config.RepositoryRules, branch, goVer string, deleteRule
var deletedBranch bool
// To Check and Remove the existing/deprecated branch
if deleteRule {
for i, br := range r.Branches {
if br.Name == branch {
for i := range r.Branches {
if rules.Rules[j].Branches[i].Name == branch {
glog.Infof("remove rule %s for %s", branch, r.DestinationRepository)
rules.Rules[j].Branches = append(rules.Rules[j].Branches[:i], rules.Rules[j].Branches[i+1:]...)
deletedBranch = true
Expand Down
33 changes: 23 additions & 10 deletions cmd/update-rules/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ limitations under the License.

package main

import "testing"
import (
"reflect"
"testing"
)

var (
testdataRules = "testdata/rules.yaml"
Expand Down Expand Up @@ -166,19 +169,22 @@ func TestUpdateRules(t *testing.T) {

func TestDeleteRules(t *testing.T) {
tests := []struct {
name string
branch string
goVersion string
name string
branch string
goVersion string
isBranchExist bool
}{
{
"deleting rule for non existing branch",
"release-1.20",
"1.17.1",
true,
},
{
"deleting rule for non existing branch",
"deleting rule for non existing branch 1.25",
"release-1.25",
"1.17.1",
false,
},
}

Expand All @@ -189,13 +195,20 @@ func TestDeleteRules(t *testing.T) {
t.Errorf("error loading test rules file %v", err)
}
UpdateRules(rules, tt.branch, tt.goVersion, true)

for _, repoRule := range rules.Rules {
for _, branchRule := range repoRule.Branches {
if branchRule.Name == tt.branch {
t.Errorf("failed to delete %s branch rule from for repo %s", tt.name, repoRule.DestinationRepository)
if tt.isBranchExist {
for _, repoRule := range rules.Rules {
for _, branchRule := range repoRule.Branches {
if branchRule.Name == tt.branch {
t.Errorf("failed to delete %s branch rule from for repo %s", tt.name, repoRule.DestinationRepository)
}
}
}
} else {
if loadedRules, err := load(testdataRules); err != nil {
t.Errorf("error loading test rules file for comparison %v", err)
} else if !reflect.DeepEqual(loadedRules, rules) {
t.Errorf("rules changed after deleting a non existent branch %s", tt.branch)
}
}
})
}
Expand Down

0 comments on commit 494bbe8

Please sign in to comment.