Merge pull request #6596 from thomastaylor312/feat/get_repo_file

feat(repo): Ports repo file `Get` method from v2
release-v3.0.0-beta.4
Taylor Thomas 5 years ago committed by GitHub
commit b9d504dd67
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -81,12 +81,18 @@ func (r *File) update(e *Entry) {
// Has returns true if the given name is already a repository name.
func (r *File) Has(name string) bool {
for _, rf := range r.Repositories {
if rf.Name == name {
return true
entry := r.Get(name)
return entry != nil
}
// Get returns an entry with the given name if it exists, otherwise returns nil
func (r *File) Get(name string) *Entry {
for _, entry := range r.Repositories {
if entry.Name == name {
return entry
}
}
return false
return nil
}
// Remove removes the entry from the list of repositories.

@ -91,6 +91,44 @@ func TestNewFile(t *testing.T) {
}
}
func TestRepoFile_Get(t *testing.T) {
repo := NewFile()
repo.Add(
&Entry{
Name: "first",
URL: "https://example.com/first",
},
&Entry{
Name: "second",
URL: "https://example.com/second",
},
&Entry{
Name: "third",
URL: "https://example.com/third",
},
&Entry{
Name: "fourth",
URL: "https://example.com/fourth",
},
)
name := "second"
entry := repo.Get(name)
if entry == nil {
t.Fatalf("Expected repo entry %q to be found", name)
}
if entry.URL != "https://example.com/second" {
t.Errorf("Expected repo URL to be %q but got %q", "https://example.com/second", entry.URL)
}
entry = repo.Get("nonexistent")
if entry != nil {
t.Errorf("Got unexpected entry %+v", entry)
}
}
func TestRemoveRepository(t *testing.T) {
sampleRepository := NewFile()
sampleRepository.Add(

Loading…
Cancel
Save