Skip to content

Commit

Permalink
Expand Repository.MergeMutable to cover more fields
Browse files Browse the repository at this point in the history
There was no test coverage, so I added some.

Closes #683.
  • Loading branch information
isker committed Nov 8, 2023
1 parent bec12a7 commit 6101c5e
Show file tree
Hide file tree
Showing 2 changed files with 149 additions and 2 deletions.
19 changes: 17 additions & 2 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -650,8 +650,6 @@ func (r *Repository) UnmarshalJSON(data []byte) error {
//
// Note: We ignore RawConfig fields which are duplicated into Repository:
// name and id.
//
// Note: URL, *Template fields are ignored. They are not used by Sourcegraph.
func (r *Repository) MergeMutable(x *Repository) (mutated bool, err error) {
if r.ID != x.ID {
// Sourcegraph: strange behaviour may occur if ID changes but names don't.
Expand Down Expand Up @@ -682,6 +680,23 @@ func (r *Repository) MergeMutable(x *Repository) (mutated bool, err error) {
}
}

if r.URL != x.URL {
mutated = true
r.URL = x.URL
}
if r.CommitURLTemplate != x.CommitURLTemplate {
mutated = true
r.CommitURLTemplate = x.CommitURLTemplate
}
if r.FileURLTemplate != x.FileURLTemplate {
mutated = true
r.FileURLTemplate = x.FileURLTemplate
}
if r.LineFragmentTemplate != x.LineFragmentTemplate {
mutated = true
r.LineFragmentTemplate = x.LineFragmentTemplate
}

return mutated, nil
}

Expand Down
132 changes: 132 additions & 0 deletions api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package zoekt // import "github.com/sourcegraph/zoekt"
import (
"bytes"
"encoding/gob"
"reflect"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -136,3 +137,134 @@ func TestSizeBytesChunkMatches(t *testing.T) {
t.Fatalf("want %d, got %d", wantBytes, cm.sizeBytes())
}
}

func TestRepositoryMergeMutable(t *testing.T) {
a := Repository{
ID: 0,
Name: "name",
Branches: []RepositoryBranch{
{
Name: "branchName",
Version: "branchVersion",
},
},
RawConfig: nil,
URL: "url",
CommitURLTemplate: "commitUrlTemplate",
FileURLTemplate: "fileUrlTemplate",
LineFragmentTemplate: "lineFragmentTemplate",
}

t.Run("different ID", func(t *testing.T) {
b := a
b.ID = 1
mutated, err := a.MergeMutable(&b)
if err == nil {
t.Fatalf("want err, got mutated=%t", mutated)
}
})
t.Run("different Name", func(t *testing.T) {
b := a
b.Name = "otherName"
mutated, err := a.MergeMutable(&b)
if err == nil {
t.Fatalf("want err, got mutated=%t", mutated)
}
})
t.Run("different Branches", func(t *testing.T) {
b := a
b.Branches = []RepositoryBranch{
{
Name: "otherBranchName",
Version: "branchVersion",
},
}
mutated, err := a.MergeMutable(&b)
if err == nil {
t.Fatalf("want err, got mutated=%t", mutated)
}
})
t.Run("different RawConfig", func(t *testing.T) {
b := a
b.RawConfig = map[string]string{"foo": "bar"}
mutated, err := a.MergeMutable(&b)
if err != nil {
t.Fatalf("got err %v", err)
}
if !mutated {
t.Fatalf("want mutated=true, got false")
}
if !reflect.DeepEqual(a.RawConfig, b.RawConfig) {
t.Fatalf("got different RawConfig, %v vs %v", a.RawConfig, b.RawConfig)
}
})
t.Run("different URL", func(t *testing.T) {
b := a
b.URL = "otherURL"
mutated, err := a.MergeMutable(&b)
if err != nil {
t.Fatalf("got err %v", err)
}
if !mutated {
t.Fatalf("want mutated=true, got false")
}
if a.URL != b.URL {
t.Fatalf("got different URL, %s vs %s", a.URL, b.URL)
}
})
t.Run("different CommitURLTemplate", func(t *testing.T) {
b := a
b.CommitURLTemplate = "otherCommitUrlTemplate"
mutated, err := a.MergeMutable(&b)
if err != nil {
t.Fatalf("got err %v", err)
}
if !mutated {
t.Fatalf("want mutated=true, got false")
}
if a.CommitURLTemplate != b.CommitURLTemplate {
t.Fatalf("got different CommitURLTemplate, %s vs %s", a.CommitURLTemplate, b.CommitURLTemplate)
}
})
t.Run("different FileURLTemplate", func(t *testing.T) {
b := a
b.FileURLTemplate = "otherFileUrlTemplate"
mutated, err := a.MergeMutable(&b)
if err != nil {
t.Fatalf("got err %v", err)
}
if !mutated {
t.Fatalf("want mutated=true, got false")
}
if a.FileURLTemplate != b.FileURLTemplate {
t.Fatalf("got different FileURLTemplate, %s vs %s", a.FileURLTemplate, b.FileURLTemplate)
}
})
t.Run("different LineFragmentTemplate", func(t *testing.T) {
b := a
b.LineFragmentTemplate = "otherLineFragmentTemplate"
mutated, err := a.MergeMutable(&b)
if err != nil {
t.Fatalf("got err %v", err)
}
if !mutated {
t.Fatalf("want mutated=true, got false")
}
if a.LineFragmentTemplate != b.LineFragmentTemplate {
t.Fatalf("got different LineFragmentTemplate, %s vs %s", a.LineFragmentTemplate, b.LineFragmentTemplate)
}
})
t.Run("all same", func(t *testing.T) {
b := a
mutated, err := a.MergeMutable(&b)
if err != nil {
t.Fatalf("got err %v", err)
}
if mutated {
t.Fatalf("want mutated=false, got true")
}
if !reflect.DeepEqual(a, b) {
t.Fatalf("got different Repository, %v vs %v", a, b)
}
})
}

0 comments on commit 6101c5e

Please sign in to comment.