Skip to content

Commit

Permalink
Fix CleanResourceName for IDs starting with a - characted
Browse files Browse the repository at this point in the history
  • Loading branch information
froque committed Dec 6, 2024
1 parent 0cb6577 commit 2dd51d1
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
3 changes: 3 additions & 0 deletions pkg/generate/postprocessing/preferred_resource_name.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
26 changes: 25 additions & 1 deletion pkg/generate/postprocessing/preferred_resource_name_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package postprocessing

import "testing"
import (
"fmt"
"testing"
)

func TestUsePreferredResourceNames(t *testing.T) {
for _, testFile := range []string{
Expand All @@ -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)
}
})
}
}

0 comments on commit 2dd51d1

Please sign in to comment.