From 0517bf74adec99e455a5502093c1e059d8c92195 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ra=C3=BAl=20Barroso?= Date: Fri, 10 May 2024 10:52:44 +0100 Subject: [PATCH] fix: sanitize with nil config (#48) --- config/config.go | 6 +++++- config/config_test.go | 28 +++++++++++++++++++++++----- 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/config/config.go b/config/config.go index 2aa4c4d..afc93e4 100644 --- a/config/config.go +++ b/config/config.go @@ -34,8 +34,12 @@ import ( type Config map[string]string // Sanitize removes leading and trailing spaces from all keys and values in the -// configuration. +// configuration, and makes sure it's not nil. func (c Config) Sanitize() Config { + if c == nil { + return map[string]string{} + } + for key, val := range c { delete(c, key) key = strings.TrimSpace(key) diff --git a/config/config_test.go b/config/config_test.go index c6130f7..454154e 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -26,12 +26,30 @@ import ( ) func TestConfig_Sanitize(t *testing.T) { - is := is.New(t) - have := Config{" key ": " value "} - want := Config{"key": "value"} + tests := []struct { + name string + have Config + want Config + }{ + { + name: "nil config", + have: nil, + want: map[string]string{}, + }, + { + name: "with spaces", + have: Config{" key ": " value "}, + want: Config{"key": "value"}, + }, + } - have.Sanitize() - is.Equal(have, want) + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + is := is.New(t) + got := tt.have.Sanitize() + is.Equal(got, tt.want) + }) + } } func TestConfig_Validate_ParameterType(t *testing.T) {