Skip to content

Commit

Permalink
check: Fix file check handling after CDKTF introduction, begin partia…
Browse files Browse the repository at this point in the history
…lly checking CDKTF files (#69)
  • Loading branch information
bflad authored May 31, 2023
1 parent 8470ba0 commit bd76dcb
Show file tree
Hide file tree
Showing 9 changed files with 148 additions and 70 deletions.
62 changes: 54 additions & 8 deletions check/check.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package check

import (
"fmt"
"sort"

"github.com/bflad/tfproviderdocs/markdown"
"github.com/hashicorp/go-multierror"
)

Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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 {
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand Down
5 changes: 4 additions & 1 deletion check/contents.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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)
Expand Down
1 change: 1 addition & 0 deletions check/contents/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package contents
type CheckOptions struct {
ArgumentsSection *CheckArgumentsSectionOptions
AttributesSection *CheckAttributesSectionOptions
ExamplesSection *CheckExamplesSectionOptions
}

func (d *Document) Check(opts *CheckOptions) error {
Expand Down
21 changes: 19 additions & 2 deletions check/contents/check_example_section.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand Down
1 change: 0 additions & 1 deletion check/file_extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
)

const (
FileExtensionErb = `.erb`
FileExtensionHtmlMarkdown = `.html.markdown`
FileExtensionHtmlMd = `.html.md`
FileExtensionMarkdown = `.markdown`
Expand Down
8 changes: 4 additions & 4 deletions check/legacy_resource_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
}
}
Expand Down
56 changes: 31 additions & 25 deletions check/legacy_resource_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
}

Expand All @@ -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")
Expand Down
8 changes: 4 additions & 4 deletions check/registry_resource_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
}
}
Expand Down
Loading

0 comments on commit bd76dcb

Please sign in to comment.