Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a test for examples replacement #2317

Merged
merged 1 commit into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 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,22 @@ const (
sectionImports = 5
)

func (p *tfMarkdownParser) parseSupplementaryExamples() (string, error) {
func (p *tfMarkdownParser) readFile(name string) ([]byte, error) {
if p.readFileFunc != nil {
iwahbe marked this conversation as resolved.
Show resolved Hide resolved
// p.readFileFunc is a testing hard-point. Outside of tests,
// p.readFileFunc should be 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 +563,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