Skip to content

Commit

Permalink
Add a test for examples replacement
Browse files Browse the repository at this point in the history
  • Loading branch information
iwahbe committed Aug 15, 2024
1 parent 0f1e694 commit 5805dcf
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 5 deletions.
17 changes: 14 additions & 3 deletions pkg/tfgen/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,10 @@ type tfMarkdownParser struct {
editRules editRules

ret entityDocs

// readFile allows tests to mock out external files. It should not be set outside
// of test cases.
readFileFunc func(string) ([]byte, error)
}

const (
Expand All @@ -502,13 +506,20 @@ const (
sectionImports = 5
)

func (p *tfMarkdownParser) parseSupplementaryExamples() (string, error) {
func (p *tfMarkdownParser) readFile(name string) ([]byte, error) {
if p.readFileFunc != nil {
return p.readFileFunc(name)
}
return os.ReadFile(name)
}

func (p *tfMarkdownParser) readSupplementaryExamples() (string, error) {
examplesFileName := fmt.Sprintf("docs/%s/%s.examples.md", p.kind, p.rawname)
absPath, err := filepath.Abs(examplesFileName)
if err != nil {
return "", err
}
fileBytes, err := os.ReadFile(absPath)
fileBytes, err := p.readFile(absPath)
if err != nil {
p.sink.error("explicitly marked resource documentation for replacement, but found no file at %q", examplesFileName)
return "", err
Expand Down Expand Up @@ -550,7 +561,7 @@ func (p *tfMarkdownParser) parse(tfMarkdown []byte) (entityDocs, error) {
}

// now we are going to inject the new source of examples
newExamples, err := p.parseSupplementaryExamples()
newExamples, err := p.readSupplementaryExamples()
if err != nil {
return entityDocs{}, err
}
Expand Down
26 changes: 24 additions & 2 deletions pkg/tfgen/docs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"os"
"path/filepath"
"runtime"
"strings"
"testing"
"text/template"

Expand Down Expand Up @@ -2137,6 +2138,8 @@ func TestParseTFMarkdown(t *testing.T) {
rawName string

fileName string

readFileFunc func(string) ([]byte, error)
}

// Assert that file contents match the expected description.
Expand Down Expand Up @@ -2182,6 +2185,24 @@ func TestParseTFMarkdown(t *testing.T) {
[]byte(`checking custom replaces`)), nil
})),
test("codeblock-header"),
test("replace-examples",
func(tc *testCase) {
tc.readFileFunc = func(name string) ([]byte, error) {
switch {
// This test works on windows if and only if we use the correct separator.
case strings.HasSuffix(name, filepath.Join("docs", "resource", "pkg_mod1_res1.examples.md")):
return []byte(`## REPLACEMENT TEXT
This should be interpolated in.
`), nil
default:
return nil, fmt.Errorf("invalid path %q", name)
}

}
tc.info = &tfbridge.ResourceInfo{Docs: &tfbridge.DocInfo{
ReplaceExamplesSection: true,
}}
}),
}

for _, tt := range tests {
Expand All @@ -2203,7 +2224,8 @@ func TestParseTFMarkdown(t *testing.T) {
pkg: "pkg",
info: tt.providerInfo,
},
editRules: getEditRules(tt.providerInfo.DocRules),
editRules: getEditRules(tt.providerInfo.DocRules),
readFileFunc: tt.readFileFunc,
}

inputBytes, err := os.ReadFile(input)
Expand Down Expand Up @@ -2346,7 +2368,7 @@ func (r *mockResource) GetFields() map[string]*tfbridge.SchemaInfo {
}

func (r *mockResource) ReplaceExamplesSection() bool {
return false
return r.docs.ReplaceExamplesSection
}

func (r *mockResource) GetDocs() *tfbridge.DocInfo {
Expand Down
6 changes: 6 additions & 0 deletions pkg/tfgen/test_data/replace-examples/expected.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"Description": "main body\n\n## Some other section\n\nother body\n\n## REPLACEMENT TEXT\n\nThis should be interpolated in.",
"Arguments": {},
"Attributes": {},
"Import": ""
}
26 changes: 26 additions & 0 deletions pkg/tfgen/test_data/replace-examples/input.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
page_title: "test_replace_examples Resource - terraform-provider-test"
description: |-
Validate that we can replace examples correctly.
---

# test_replace_examples (Resource)

main body

## Example Usage

Should be skipped

```terraform
## Minimal
resource "snowflake_database" "primary" {
name = "database_name"
}
```

## Some other section

other body

## Example Usage - also replaced

0 comments on commit 5805dcf

Please sign in to comment.