cli/context/store: Names(): fix panic when called with nil-interface

Before this, it would panic when a nil-interface was passed.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2024-07-05 15:01:58 +02:00
parent 42b68a3ed7
commit e4dd8b1898
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C
2 changed files with 9 additions and 0 deletions

View File

@ -124,6 +124,9 @@ func (s *ContextStore) List() ([]Metadata, error) {
// Names return Metadata names for a Lister // Names return Metadata names for a Lister
func Names(s Lister) ([]string, error) { func Names(s Lister) ([]string, error) {
if s == nil {
return nil, errors.New("nil lister")
}
list, err := s.List() list, err := s.List()
if err != nil { if err != nil {
return nil, err return nil, err

View File

@ -260,3 +260,9 @@ func TestCorruptMetadata(t *testing.T) {
_, err = s.GetMetadata("source") _, err = s.GetMetadata("source")
assert.ErrorContains(t, err, fmt.Sprintf("parsing %s: unexpected end of JSON input", contextFile)) assert.ErrorContains(t, err, fmt.Sprintf("parsing %s: unexpected end of JSON input", contextFile))
} }
func TestNames(t *testing.T) {
names, err := Names(nil)
assert.Check(t, is.Error(err, "nil lister"))
assert.Check(t, is.Len(names, 0))
}