From a4c3698cb20089e4c8b095376bb9866cfa4f1945 Mon Sep 17 00:00:00 2001 From: Ian Wahbe Date: Fri, 16 Aug 2024 08:03:15 -0700 Subject: [PATCH] Add a test for examples replacement (#2317) I'm adding test coverage in preparation for https://github.com/pulumi/pulumi-terraform-bridge/pull/2312. --- pkg/tfgen/docs.go | 19 +++++++++++--- pkg/tfgen/docs_test.go | 26 +++++++++++++++++-- .../test_data/replace-examples/expected.json | 6 +++++ pkg/tfgen/test_data/replace-examples/input.md | 26 +++++++++++++++++++ 4 files changed, 72 insertions(+), 5 deletions(-) create mode 100644 pkg/tfgen/test_data/replace-examples/expected.json create mode 100644 pkg/tfgen/test_data/replace-examples/input.md diff --git a/pkg/tfgen/docs.go b/pkg/tfgen/docs.go index c6dc8572f..dfd3bec6b 100644 --- a/pkg/tfgen/docs.go +++ b/pkg/tfgen/docs.go @@ -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 ( @@ -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 { + // 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 @@ -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 } diff --git a/pkg/tfgen/docs_test.go b/pkg/tfgen/docs_test.go index 6c7c7952a..720e33e40 100644 --- a/pkg/tfgen/docs_test.go +++ b/pkg/tfgen/docs_test.go @@ -24,6 +24,7 @@ import ( "os" "path/filepath" "runtime" + "strings" "testing" "text/template" @@ -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. @@ -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 { @@ -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) @@ -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 { diff --git a/pkg/tfgen/test_data/replace-examples/expected.json b/pkg/tfgen/test_data/replace-examples/expected.json new file mode 100644 index 000000000..74332fb4e --- /dev/null +++ b/pkg/tfgen/test_data/replace-examples/expected.json @@ -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": "" +} \ No newline at end of file diff --git a/pkg/tfgen/test_data/replace-examples/input.md b/pkg/tfgen/test_data/replace-examples/input.md new file mode 100644 index 000000000..705b0a53c --- /dev/null +++ b/pkg/tfgen/test_data/replace-examples/input.md @@ -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