Skip to content

Commit

Permalink
Address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
guineveresaenger committed Jan 10, 2025
1 parent 5fb65af commit eb4af28
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 39 deletions.
28 changes: 12 additions & 16 deletions pkg/tfgen/installation_docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
42 changes: 21 additions & 21 deletions pkg/tfgen/installation_docs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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())
}
Original file line number Diff line number Diff line change
@@ -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

Expand Down
2 changes: 1 addition & 1 deletion pkg/tfgen/test_data/installation-docs/example-only.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down

0 comments on commit eb4af28

Please sign in to comment.