diff --git a/check/check.go b/check/check.go index c39ee94..3eef485 100644 --- a/check/check.go +++ b/check/check.go @@ -1,8 +1,10 @@ package check import ( + "fmt" "sort" + "github.com/bflad/tfproviderdocs/markdown" "github.com/hashicorp/go-multierror" ) @@ -66,7 +68,7 @@ func (check *Check) Run(directories map[string][]string) error { var result *multierror.Error - if files, ok := directories[RegistryDataSourcesDirectory]; ok { + if files, ok := directories[fmt.Sprintf("%s/%s", RegistryIndexDirectory, RegistryDataSourcesDirectory)]; ok { if err := NewFileMismatchCheck(check.Options.DataSourceFileMismatch).Run(files); err != nil { result = multierror.Append(result, err) } @@ -76,7 +78,7 @@ func (check *Check) Run(directories map[string][]string) error { } } - if files, ok := directories[RegistryGuidesDirectory]; ok { + if files, ok := directories[fmt.Sprintf("%s/%s", RegistryIndexDirectory, RegistryGuidesDirectory)]; ok { if err := NewRegistryGuideFileCheck(check.Options.RegistryGuideFile).RunAll(files); err != nil { result = multierror.Append(result, err) } @@ -88,18 +90,40 @@ func (check *Check) Run(directories map[string][]string) error { } } - if files, ok := directories[RegistryResourcesDirectory]; ok { + if files, ok := directories[fmt.Sprintf("%s/%s", RegistryIndexDirectory, RegistryResourcesDirectory)]; ok { if err := NewFileMismatchCheck(check.Options.ResourceFileMismatch).Run(files); err != nil { result = multierror.Append(result, err) } - if err := NewRegistryResourceFileCheck(check.Options.RegistryResourceFile).RunAll(files); err != nil { + if err := NewRegistryResourceFileCheck(check.Options.RegistryResourceFile).RunAll(files, markdown.FencedCodeBlockLanguageTerraform); err != nil { result = multierror.Append(result, err) } } - legacyDataSourcesFiles, legacyDataSourcesOk := directories[LegacyDataSourcesDirectory] - legacyResourcesFiles, legacyResourcesOk := directories[LegacyResourcesDirectory] + for _, cdktfLanguage := range ValidCdktfLanguages { + if files, ok := directories[fmt.Sprintf("%s/%s/%s/%s", RegistryIndexDirectory, CdktfIndexDirectory, cdktfLanguage, RegistryDataSourcesDirectory)]; ok { + if err := NewFileMismatchCheck(check.Options.DataSourceFileMismatch).Run(files); err != nil { + result = multierror.Append(result, err) + } + + if err := NewRegistryDataSourceFileCheck(check.Options.RegistryDataSourceFile).RunAll(files); err != nil { + result = multierror.Append(result, err) + } + } + + if files, ok := directories[fmt.Sprintf("%s/%s/%s/%s", RegistryIndexDirectory, CdktfIndexDirectory, cdktfLanguage, RegistryResourcesDirectory)]; ok { + if err := NewFileMismatchCheck(check.Options.ResourceFileMismatch).Run(files); err != nil { + result = multierror.Append(result, err) + } + + if err := NewRegistryResourceFileCheck(check.Options.RegistryResourceFile).RunAll(files, cdktfLanguage); err != nil { + result = multierror.Append(result, err) + } + } + } + + legacyDataSourcesFiles, legacyDataSourcesOk := directories[fmt.Sprintf("%s/%s", LegacyIndexDirectory, LegacyDataSourcesDirectory)] + legacyResourcesFiles, legacyResourcesOk := directories[fmt.Sprintf("%s/%s", LegacyIndexDirectory, LegacyResourcesDirectory)] if legacyDataSourcesOk { if err := NewFileMismatchCheck(check.Options.DataSourceFileMismatch).Run(legacyDataSourcesFiles); err != nil { @@ -111,7 +135,7 @@ func (check *Check) Run(directories map[string][]string) error { } } - if files, ok := directories[LegacyGuidesDirectory]; ok { + if files, ok := directories[fmt.Sprintf("%s/%s", LegacyIndexDirectory, LegacyGuidesDirectory)]; ok { if err := NewLegacyGuideFileCheck(check.Options.LegacyGuideFile).RunAll(files); err != nil { result = multierror.Append(result, err) } @@ -128,11 +152,33 @@ func (check *Check) Run(directories map[string][]string) error { result = multierror.Append(result, err) } - if err := NewLegacyResourceFileCheck(check.Options.LegacyResourceFile).RunAll(legacyResourcesFiles); err != nil { + if err := NewLegacyResourceFileCheck(check.Options.LegacyResourceFile).RunAll(legacyResourcesFiles, markdown.FencedCodeBlockLanguageTerraform); err != nil { result = multierror.Append(result, err) } } + for _, cdktfLanguage := range ValidCdktfLanguages { + if files, ok := directories[fmt.Sprintf("%s/%s/%s/%s", LegacyIndexDirectory, CdktfIndexDirectory, cdktfLanguage, LegacyDataSourcesDirectory)]; ok { + if err := NewFileMismatchCheck(check.Options.DataSourceFileMismatch).Run(files); err != nil { + result = multierror.Append(result, err) + } + + if err := NewLegacyDataSourceFileCheck(check.Options.LegacyDataSourceFile).RunAll(files); err != nil { + result = multierror.Append(result, err) + } + } + + if files, ok := directories[fmt.Sprintf("%s/%s/%s/%s", LegacyIndexDirectory, CdktfIndexDirectory, cdktfLanguage, LegacyResourcesDirectory)]; ok { + if err := NewFileMismatchCheck(check.Options.ResourceFileMismatch).Run(files); err != nil { + result = multierror.Append(result, err) + } + + if err := NewLegacyResourceFileCheck(check.Options.LegacyResourceFile).RunAll(files, cdktfLanguage); err != nil { + result = multierror.Append(result, err) + } + } + } + if result != nil { sort.Sort(result) } diff --git a/check/contents.go b/check/contents.go index c067fa4..3b89774 100644 --- a/check/contents.go +++ b/check/contents.go @@ -35,7 +35,7 @@ func NewContentsCheck(opts *ContentsOptions) *ContentsCheck { return check } -func (check *ContentsCheck) Run(path string) error { +func (check *ContentsCheck) Run(path string, exampleLanguage string) error { if !check.Options.Enable { return nil } @@ -47,6 +47,9 @@ func (check *ContentsCheck) Run(path string) error { AttributesSection: &contents.CheckAttributesSectionOptions{ RequireSchemaOrdering: check.Options.RequireSchemaOrdering, }, + ExamplesSection: &contents.CheckExamplesSectionOptions{ + ExpectedCodeBlockLanguage: exampleLanguage, + }, } doc := contents.NewDocument(path, check.Options.ProviderName) diff --git a/check/contents/check.go b/check/contents/check.go index 0077b64..c4238ad 100644 --- a/check/contents/check.go +++ b/check/contents/check.go @@ -3,6 +3,7 @@ package contents type CheckOptions struct { ArgumentsSection *CheckArgumentsSectionOptions AttributesSection *CheckAttributesSectionOptions + ExamplesSection *CheckExamplesSectionOptions } func (d *Document) Check(opts *CheckOptions) error { diff --git a/check/contents/check_example_section.go b/check/contents/check_example_section.go index 6410abc..4e418f9 100644 --- a/check/contents/check_example_section.go +++ b/check/contents/check_example_section.go @@ -7,7 +7,19 @@ import ( "github.com/bflad/tfproviderdocs/markdown" ) +type CheckExamplesSectionOptions struct { + ExpectedCodeBlockLanguage string +} + func (d *Document) checkExampleSection() error { + checkOpts := &CheckExamplesSectionOptions{ + ExpectedCodeBlockLanguage: markdown.FencedCodeBlockLanguageTerraform, + } + + if d.CheckOptions != nil && d.CheckOptions.ExamplesSection != nil { + checkOpts = d.CheckOptions.ExamplesSection + } + section := d.Sections.Example if section == nil { @@ -27,11 +39,16 @@ func (d *Document) checkExampleSection() error { return fmt.Errorf("example section heading (%s) should be: %s", headingText, expectedHeadingText) } + // CDKTF conversion will leave the original terraform code blocks if unsuccessful + if checkOpts.ExpectedCodeBlockLanguage != markdown.FencedCodeBlockLanguageTerraform { + return nil + } + for _, fencedCodeBlock := range section.FencedCodeBlocks { language := markdown.FencedCodeBlockLanguage(fencedCodeBlock, d.source) - if language != markdown.FencedCodeBlockLanguageTerraform { - return fmt.Errorf("example section code block language (%s) should be: ```%s", language, markdown.FencedCodeBlockLanguageTerraform) + if language != checkOpts.ExpectedCodeBlockLanguage { + return fmt.Errorf("example section code block language (%s) should be: ```%s", language, checkOpts.ExpectedCodeBlockLanguage) } text := markdown.FencedCodeBlockText(fencedCodeBlock, d.source) diff --git a/check/file_extension.go b/check/file_extension.go index fb0702e..cb52c95 100644 --- a/check/file_extension.go +++ b/check/file_extension.go @@ -7,7 +7,6 @@ import ( ) const ( - FileExtensionErb = `.erb` FileExtensionHtmlMarkdown = `.html.markdown` FileExtensionHtmlMd = `.html.md` FileExtensionMarkdown = `.markdown` diff --git a/check/legacy_resource_file.go b/check/legacy_resource_file.go index 25b9143..cd55ead 100644 --- a/check/legacy_resource_file.go +++ b/check/legacy_resource_file.go @@ -55,7 +55,7 @@ func NewLegacyResourceFileCheck(opts *LegacyResourceFileOptions) *LegacyResource return check } -func (check *LegacyResourceFileCheck) Run(path string) error { +func (check *LegacyResourceFileCheck) Run(path string, exampleLanguage string) error { fullpath := check.Options.FullPath(path) log.Printf("[DEBUG] Checking file: %s", fullpath) @@ -78,18 +78,18 @@ func (check *LegacyResourceFileCheck) Run(path string) error { return fmt.Errorf("%s: error checking file frontmatter: %w", path, err) } - if err := NewContentsCheck(check.Options.Contents).Run(fullpath); err != nil { + if err := NewContentsCheck(check.Options.Contents).Run(fullpath, exampleLanguage); err != nil { return fmt.Errorf("%s: error checking file contents: %w", path, err) } return nil } -func (check *LegacyResourceFileCheck) RunAll(files []string) error { +func (check *LegacyResourceFileCheck) RunAll(files []string, exampleLanguage string) error { var result *multierror.Error for _, file := range files { - if err := check.Run(file); err != nil { + if err := check.Run(file, exampleLanguage); err != nil { result = multierror.Append(result, err) } } diff --git a/check/legacy_resource_file_test.go b/check/legacy_resource_file_test.go index a932f4e..bc7c2ab 100644 --- a/check/legacy_resource_file_test.go +++ b/check/legacy_resource_file_test.go @@ -6,40 +6,46 @@ import ( func TestLegacyResourceFileCheck(t *testing.T) { testCases := []struct { - Name string - BasePath string - Path string - Options *LegacyResourceFileOptions - ExpectError bool + Name string + BasePath string + Path string + ExampleLanguage string + Options *LegacyResourceFileOptions + ExpectError bool }{ { - Name: "valid", - BasePath: "testdata/valid-legacy-files", - Path: "resource.html.markdown", + Name: "valid", + BasePath: "testdata/valid-legacy-files", + Path: "resource.html.markdown", + ExampleLanguage: "terraform", }, { - Name: "invalid extension", - BasePath: "testdata/invalid-legacy-files", - Path: "resource_invalid_extension.txt", - ExpectError: true, + Name: "invalid extension", + BasePath: "testdata/invalid-legacy-files", + Path: "resource_invalid_extension.txt", + ExampleLanguage: "terraform", + ExpectError: true, }, { - Name: "invalid frontmatter", - BasePath: "testdata/invalid-legacy-files", - Path: "resource_invalid_frontmatter.html.markdown", - ExpectError: true, + Name: "invalid frontmatter", + BasePath: "testdata/invalid-legacy-files", + Path: "resource_invalid_frontmatter.html.markdown", + ExampleLanguage: "terraform", + ExpectError: true, }, { - Name: "invalid frontmatter with sidebar_current", - BasePath: "testdata/invalid-legacy-files", - Path: "resource_with_sidebar_current.html.markdown", - ExpectError: true, + Name: "invalid frontmatter with sidebar_current", + BasePath: "testdata/invalid-legacy-files", + Path: "resource_with_sidebar_current.html.markdown", + ExampleLanguage: "terraform", + ExpectError: true, }, { - Name: "invalid frontmatter without layout", - BasePath: "testdata/invalid-legacy-files", - Path: "resource_without_layout.html.markdown", - ExpectError: true, + Name: "invalid frontmatter without layout", + BasePath: "testdata/invalid-legacy-files", + Path: "resource_without_layout.html.markdown", + ExampleLanguage: "terraform", + ExpectError: true, }, } @@ -55,7 +61,7 @@ func TestLegacyResourceFileCheck(t *testing.T) { } } - got := NewLegacyResourceFileCheck(testCase.Options).Run(testCase.Path) + got := NewLegacyResourceFileCheck(testCase.Options).Run(testCase.Path, testCase.ExampleLanguage) if got == nil && testCase.ExpectError { t.Errorf("expected error, got no error") diff --git a/check/registry_resource_file.go b/check/registry_resource_file.go index 3d86d15..15e31a9 100644 --- a/check/registry_resource_file.go +++ b/check/registry_resource_file.go @@ -53,7 +53,7 @@ func NewRegistryResourceFileCheck(opts *RegistryResourceFileOptions) *RegistryRe return check } -func (check *RegistryResourceFileCheck) Run(path string) error { +func (check *RegistryResourceFileCheck) Run(path string, exampleLanguage string) error { fullpath := check.Options.FullPath(path) log.Printf("[DEBUG] Checking file: %s", fullpath) @@ -76,18 +76,18 @@ func (check *RegistryResourceFileCheck) Run(path string) error { return fmt.Errorf("%s: error checking file frontmatter: %w", path, err) } - if err := NewContentsCheck(check.Options.Contents).Run(fullpath); err != nil { + if err := NewContentsCheck(check.Options.Contents).Run(fullpath, exampleLanguage); err != nil { return fmt.Errorf("%s: error checking file contents: %w", path, err) } return nil } -func (check *RegistryResourceFileCheck) RunAll(files []string) error { +func (check *RegistryResourceFileCheck) RunAll(files []string, exampleLanguage string) error { var result *multierror.Error for _, file := range files { - if err := check.Run(file); err != nil { + if err := check.Run(file, exampleLanguage); err != nil { result = multierror.Append(result, err) } } diff --git a/check/registry_resource_file_test.go b/check/registry_resource_file_test.go index 77bb190..02df886 100644 --- a/check/registry_resource_file_test.go +++ b/check/registry_resource_file_test.go @@ -6,40 +6,46 @@ import ( func TestRegistryResourceFileCheck(t *testing.T) { testCases := []struct { - Name string - BasePath string - Path string - Options *RegistryResourceFileOptions - ExpectError bool + Name string + BasePath string + Path string + ExampleLanguage string + Options *RegistryResourceFileOptions + ExpectError bool }{ { - Name: "valid", - BasePath: "testdata/valid-registry-files", - Path: "resource.md", + Name: "valid", + BasePath: "testdata/valid-registry-files", + Path: "resource.md", + ExampleLanguage: "terraform", }, { - Name: "invalid extension", - BasePath: "testdata/invalid-registry-files", - Path: "resource_invalid_extension.markdown", - ExpectError: true, + Name: "invalid extension", + BasePath: "testdata/invalid-registry-files", + Path: "resource_invalid_extension.markdown", + ExampleLanguage: "terraform", + ExpectError: true, }, { - Name: "invalid frontmatter", - BasePath: "testdata/invalid-registry-files", - Path: "resource_invalid_frontmatter.md", - ExpectError: true, + Name: "invalid frontmatter", + BasePath: "testdata/invalid-registry-files", + Path: "resource_invalid_frontmatter.md", + ExampleLanguage: "terraform", + ExpectError: true, }, { - Name: "invalid frontmatter with layout", - BasePath: "testdata/invalid-registry-files", - Path: "resource_with_layout.md", - ExpectError: true, + Name: "invalid frontmatter with layout", + BasePath: "testdata/invalid-registry-files", + Path: "resource_with_layout.md", + ExampleLanguage: "terraform", + ExpectError: true, }, { - Name: "invalid frontmatter with sidebar_current", - BasePath: "testdata/invalid-registry-files", - Path: "resource_with_sidebar_current.md", - ExpectError: true, + Name: "invalid frontmatter with sidebar_current", + BasePath: "testdata/invalid-registry-files", + Path: "resource_with_sidebar_current.md", + ExampleLanguage: "terraform", + ExpectError: true, }, } @@ -55,7 +61,7 @@ func TestRegistryResourceFileCheck(t *testing.T) { } } - got := NewRegistryResourceFileCheck(testCase.Options).Run(testCase.Path) + got := NewRegistryResourceFileCheck(testCase.Options).Run(testCase.Path, testCase.ExampleLanguage) if got == nil && testCase.ExpectError { t.Errorf("expected error, got no error")