From 2dd51d144e4a762c4dc3e0b8095c5dfe2e32d4e8 Mon Sep 17 00:00:00 2001 From: Filipe Roque Date: Fri, 6 Dec 2024 11:08:52 +0000 Subject: [PATCH] Fix CleanResourceName for IDs starting with a - characted --- README.md | 2 +- .../postprocessing/preferred_resource_name.go | 3 +++ .../preferred_resource_name_test.go | 26 ++++++++++++++++++- 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index f71cfdf49..f8134a50e 100644 --- a/README.md +++ b/README.md @@ -46,7 +46,7 @@ provider_installation { Acceptance tests require a running instance of Grafana. You can either handle running an instance of Grafana yourself or use `docker-compose`. -If you choose `docker-compose`, run `make testacc-docker`. This is the simplest +If you choose `docker-compose`, run `make testacc-oss-docker`. This is the simplest option, but often not the quickest. Alternatively you can use the `testacc` target which will use your local `go` diff --git a/pkg/generate/postprocessing/preferred_resource_name.go b/pkg/generate/postprocessing/preferred_resource_name.go index 6637db3fc..46e950b19 100644 --- a/pkg/generate/postprocessing/preferred_resource_name.go +++ b/pkg/generate/postprocessing/preferred_resource_name.go @@ -68,5 +68,8 @@ func CleanResourceName(name string) string { if cleaned[0] >= '0' && cleaned[0] <= '9' { cleaned = "_" + cleaned } + if cleaned[0] == '-' { + cleaned = "_" + cleaned + } return cleaned } diff --git a/pkg/generate/postprocessing/preferred_resource_name_test.go b/pkg/generate/postprocessing/preferred_resource_name_test.go index a3b7f1363..f624a970c 100644 --- a/pkg/generate/postprocessing/preferred_resource_name_test.go +++ b/pkg/generate/postprocessing/preferred_resource_name_test.go @@ -1,6 +1,9 @@ package postprocessing -import "testing" +import ( + "fmt" + "testing" +) func TestUsePreferredResourceNames(t *testing.T) { for _, testFile := range []string{ @@ -11,3 +14,24 @@ func TestUsePreferredResourceNames(t *testing.T) { }) } } + +func TestCleanResourceName(t *testing.T) { + var tests = []struct { + before, after string + }{ + {"qwerty", "qwerty"}, + {"123", "_123"}, + {"-foo", "_-foo"}, + {"_bar", "_bar"}, + } + + for _, tt := range tests { + testname := fmt.Sprintf("%s,%s", tt.before, tt.after) + t.Run(testname, func(t *testing.T) { + cleaned := CleanResourceName(tt.before) + if cleaned != tt.after { + t.Errorf(`Resource name %q was clean as %q and not %q as expected`, tt.before, cleaned, tt.after) + } + }) + } +}