From e4dd8b1898792ee26d8bb6ed95d5624594da66b7 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Fri, 5 Jul 2024 15:01:58 +0200 Subject: [PATCH] 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 --- cli/context/store/store.go | 3 +++ cli/context/store/store_test.go | 6 ++++++ 2 files changed, 9 insertions(+) diff --git a/cli/context/store/store.go b/cli/context/store/store.go index 44e9477fb0..066b5769d7 100644 --- a/cli/context/store/store.go +++ b/cli/context/store/store.go @@ -124,6 +124,9 @@ func (s *ContextStore) List() ([]Metadata, error) { // Names return Metadata names for a Lister func Names(s Lister) ([]string, error) { + if s == nil { + return nil, errors.New("nil lister") + } list, err := s.List() if err != nil { return nil, err diff --git a/cli/context/store/store_test.go b/cli/context/store/store_test.go index ad61036dbe..c785b2e015 100644 --- a/cli/context/store/store_test.go +++ b/cli/context/store/store_test.go @@ -260,3 +260,9 @@ func TestCorruptMetadata(t *testing.T) { _, err = s.GetMetadata("source") 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)) +}