diff --git a/pkg/tfgen/installation_docs.go b/pkg/tfgen/installation_docs.go index 18d547bb0..0fe40067a 100644 --- a/pkg/tfgen/installation_docs.go +++ b/pkg/tfgen/installation_docs.go @@ -67,14 +67,14 @@ func plainDocsParser(docFile *DocFile, g *Generator) ([]byte, error) { } // If the code translation resulted in an empty examples section, remove it - contentStr, err = removeEmptyExamples(contentStr) + content, err = removeEmptySection("Example Usage", []byte(contentStr)) if err != nil { return nil, err } // Apply post-code translation edit rules. This applies all default edit rules and provider-supplied edit rules in // the post-code translation phase. - contentBytes, err = g.editRules.apply(docFile.FileName, []byte(contentStr), info.PostCodeTranslation) + contentBytes, err = g.editRules.apply(docFile.FileName, content, info.PostCodeTranslation) if err != nil { return nil, err } @@ -504,31 +504,27 @@ func getProviderDisplayName(g *Generator) string { return capitalize.String(providerName) } -func removeEmptyExamples(contentStr string) (string, error) { - if checkExamplesEmpty(contentStr) { - mybytes, err := SkipSectionByHeaderContent([]byte(contentStr), func(headerText string) bool { - return headerText == "Example Usage" - }) - contentStr = string(mybytes) - return contentStr, err +func removeEmptySection(title string, contentBytes []byte) ([]byte, error) { + if !isMarkdownSectionEmpty(title, contentBytes) { + return contentBytes, nil } - return contentStr, nil - + return SkipSectionByHeaderContent(contentBytes, func(headerText string) bool { + return headerText == title + }) } -func checkExamplesEmpty(contentStr string) bool { - +func isMarkdownSectionEmpty(title string, contentBytes []byte) bool { gm := goldmark.New(goldmark.WithExtensions(parse.TFRegistryExtension)) - astNode := gm.Parser().Parse(text.NewReader([]byte(contentStr))) + astNode := gm.Parser().Parse(text.NewReader(contentBytes)) isEmpty := false err := ast.Walk(astNode, func(n ast.Node, entering bool) (ast.WalkStatus, error) { if section, ok := n.(*section.Section); ok && entering { - sectionText := section.Text([]byte(contentStr)) + sectionText := section.Text(contentBytes) // A little confusingly, we check if the section text _only_ contains the title, "Example Usage". // Non-empty sections contain the title + content, so if we see only the title, the section is empty. - if string(sectionText) == "Example Usage" { + if string(sectionText) == title { isEmpty = true return ast.WalkStop, nil } diff --git a/pkg/tfgen/installation_docs_test.go b/pkg/tfgen/installation_docs_test.go index d39a0eeaf..da658897f 100644 --- a/pkg/tfgen/installation_docs_test.go +++ b/pkg/tfgen/installation_docs_test.go @@ -645,25 +645,6 @@ func TestSkipDefaultSectionHeaders(t *testing.T) { } } -// Helper func to determine if the HTML rendering is equal. -// This helps in cases where the processed Markdown is slightly different from the expected Markdown -// due to goldmark making some (insignificant to the final HTML) changes when parsing and rendering. -// We convert the expected Markdown and the actual test Markdown output to HTML and verify if they are equal. -func assertEqualHTML(t *testing.T, expected, actual string) bool { - mdRenderer := goldmark.New() - var expectedBuf bytes.Buffer - err := mdRenderer.Convert([]byte(expected), &expectedBuf) - if err != nil { - panic(err) - } - var outputBuf bytes.Buffer - err = mdRenderer.Convert([]byte(actual), &outputBuf) - if err != nil { - panic(err) - } - return assert.Equal(t, expectedBuf.String(), outputBuf.String()) -} - func TestRemoveEmptyExamples(t *testing.T) { t.Parallel() type testCase struct { @@ -680,8 +661,27 @@ func TestRemoveEmptyExamples(t *testing.T) { t.Run(tc.name, func(t *testing.T) { t.Parallel() - actual, err := removeEmptyExamples(tc.input) + actual, err := removeEmptySection("Example Usage", []byte(tc.input)) require.NoError(t, err) - assertEqualHTML(t, tc.expected, actual) + assertEqualHTML(t, tc.expected, string(actual)) }) } + +// Helper func to determine if the HTML rendering is equal. +// This helps in cases where the processed Markdown is slightly different from the expected Markdown +// due to goldmark making some (insignificant to the final HTML) changes when parsing and rendering. +// We convert the expected Markdown and the actual test Markdown output to HTML and verify if they are equal. +func assertEqualHTML(t *testing.T, expected, actual string) bool { + mdRenderer := goldmark.New() + var expectedBuf bytes.Buffer + err := mdRenderer.Convert([]byte(expected), &expectedBuf) + if err != nil { + panic(err) + } + var outputBuf bytes.Buffer + err = mdRenderer.Convert([]byte(actual), &outputBuf) + if err != nil { + panic(err) + } + return assert.Equal(t, expectedBuf.String(), outputBuf.String()) +} diff --git a/pkg/tfgen/test_data/installation-docs/example-only-expected.md b/pkg/tfgen/test_data/installation-docs/example-only-expected.md index 0247fc3b3..d31c827a2 100644 --- a/pkg/tfgen/test_data/installation-docs/example-only-expected.md +++ b/pkg/tfgen/test_data/installation-docs/example-only-expected.md @@ -1,4 +1,4 @@ -This example is invalid and should not be translated for this test to pass +This example will only translate the resource code. It has no configuration file. ## Example Usage diff --git a/pkg/tfgen/test_data/installation-docs/example-only.md b/pkg/tfgen/test_data/installation-docs/example-only.md index 9b4cfd62d..e645676aa 100644 --- a/pkg/tfgen/test_data/installation-docs/example-only.md +++ b/pkg/tfgen/test_data/installation-docs/example-only.md @@ -1,4 +1,4 @@ -This example is invalid and should not be translated for this test to pass +This example will only translate the resource code. It has no configuration file. ## Example Usage