Skip to content

Commit

Permalink
Update validate subcommand to support ephemeral resources
Browse files Browse the repository at this point in the history
  • Loading branch information
SBGoods committed Oct 16, 2024
1 parent 426f4c8 commit cd4a886
Show file tree
Hide file tree
Showing 6 changed files with 332 additions and 27 deletions.
28 changes: 17 additions & 11 deletions internal/check/directory.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,19 @@ import (
const (
CdktfIndexDirectory = `cdktf`

LegacyIndexDirectory = `website/docs`
LegacyDataSourcesDirectory = `d`
LegacyGuidesDirectory = `guides`
LegacyResourcesDirectory = `r`
LegacyFunctionsDirectory = `functions`

RegistryIndexDirectory = `docs`
RegistryDataSourcesDirectory = `data-sources`
RegistryGuidesDirectory = `guides`
RegistryResourcesDirectory = `resources`
RegistryFunctionsDirectory = `functions`
LegacyIndexDirectory = `website/docs`
LegacyDataSourcesDirectory = `d`
LegacyGuidesDirectory = `guides`
LegacyResourcesDirectory = `r`
LegacyFunctionsDirectory = `functions`
LegacyEphemeralResourcesDirectory = `ephemeral-resources`

RegistryIndexDirectory = `docs`
RegistryDataSourcesDirectory = `data-sources`
RegistryGuidesDirectory = `guides`
RegistryResourcesDirectory = `resources`
RegistryFunctionsDirectory = `functions`
RegistryEphemeralResourcesDirectory = `ephemeral-resources`

// Terraform Registry Storage Limits
// https://www.terraform.io/docs/registry/providers/docs.html#storage-limits
Expand All @@ -37,6 +39,7 @@ var ValidLegacyDirectories = []string{
LegacyIndexDirectory + "/" + LegacyGuidesDirectory,
LegacyIndexDirectory + "/" + LegacyResourcesDirectory,
LegacyIndexDirectory + "/" + LegacyFunctionsDirectory,
LegacyIndexDirectory + "/" + LegacyEphemeralResourcesDirectory,
}

var ValidRegistryDirectories = []string{
Expand All @@ -45,6 +48,7 @@ var ValidRegistryDirectories = []string{
RegistryIndexDirectory + "/" + RegistryGuidesDirectory,
RegistryIndexDirectory + "/" + RegistryResourcesDirectory,
RegistryIndexDirectory + "/" + RegistryFunctionsDirectory,
RegistryIndexDirectory + "/" + RegistryEphemeralResourcesDirectory,
}

var ValidCdktfLanguages = []string{
Expand All @@ -60,13 +64,15 @@ var ValidLegacySubdirectories = []string{
LegacyDataSourcesDirectory,
LegacyGuidesDirectory,
LegacyResourcesDirectory,
LegacyEphemeralResourcesDirectory,
}

var ValidRegistrySubdirectories = []string{
RegistryIndexDirectory,
RegistryDataSourcesDirectory,
RegistryGuidesDirectory,
RegistryResourcesDirectory,
RegistryEphemeralResourcesDirectory,
}

func InvalidDirectoriesCheck(dirPath string) error {
Expand Down
2 changes: 1 addition & 1 deletion internal/check/directory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/bmatcuk/doublestar/v4"
)

var DocumentationGlobPattern = `{docs/index.md,docs/{,cdktf/}{data-sources,guides,resources,functions}/**/*,website/docs/**/*}`
var DocumentationGlobPattern = `{docs/index.md,docs/{,cdktf/}{data-sources,guides,resources,functions, ephemeral-resources}/**/*,website/docs/**/*}`

func TestMixedDirectoriesCheck(t *testing.T) {
t.Parallel()
Expand Down
19 changes: 18 additions & 1 deletion internal/check/file_mismatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ type FileMismatchOptions struct {

FunctionEntries []os.DirEntry

EphemeralResourceEntries []os.DirEntry

Schema *tfjson.ProviderSchema
}

Expand Down Expand Up @@ -74,6 +76,11 @@ func (check *FileMismatchCheck) Run() error {
result = errors.Join(result, err)
}

if check.Options.EphemeralResourceEntries != nil {
err := check.ResourceFileMismatchCheck(check.Options.EphemeralResourceEntries, "ephemeral_resource", check.Options.Schema.EphemeralResourceSchemas)
result = errors.Join(result, err)
}

return result
}

Expand Down Expand Up @@ -156,7 +163,7 @@ func (check *FileMismatchCheck) FunctionFileMismatchCheck(files []os.DirEntry, f
continue
}

if check.IgnoreFileMismatch(file.Name()) {
if check.IgnoreFunctionFileMismatch(file.Name()) {
continue
}

Expand Down Expand Up @@ -201,6 +208,16 @@ func (check *FileMismatchCheck) IgnoreFileMismatch(file string) bool {
return false
}

func (check *FileMismatchCheck) IgnoreFunctionFileMismatch(file string) bool {
for _, ignoreFunctionName := range check.Options.IgnoreFileMismatch {
if ignoreFunctionName == TrimFileExtension(file) {
return true
}
}

return false
}

func (check *FileMismatchCheck) IgnoreFileMissing(resourceName string) bool {
for _, ignoreResourceName := range check.Options.IgnoreFileMissing {
if ignoreResourceName == resourceName {
Expand Down
99 changes: 94 additions & 5 deletions internal/check/file_mismatch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,11 @@ func TestFileResourceName(t *testing.T) {
func TestFileMismatchCheck(t *testing.T) {
t.Parallel()
testCases := map[string]struct {
ResourceFiles fstest.MapFS
FunctionFiles fstest.MapFS
Options *FileMismatchOptions
ExpectError bool
ResourceFiles fstest.MapFS
FunctionFiles fstest.MapFS
EphemeralResourceFiles fstest.MapFS
Options *FileMismatchOptions
ExpectError bool
}{
"all found - resource": {
ResourceFiles: fstest.MapFS{
Expand Down Expand Up @@ -130,6 +131,21 @@ func TestFileMismatchCheck(t *testing.T) {
},
},
},
"all found - ephemeral resource": {
EphemeralResourceFiles: fstest.MapFS{
"ephemeral_resource1.md": {},
"ephemeral_resource2.md": {},
},
Options: &FileMismatchOptions{
ProviderShortName: "test",
Schema: &tfjson.ProviderSchema{
EphemeralResourceSchemas: map[string]*tfjson.Schema{
"test_ephemeral_resource1": {},
"test_ephemeral_resource2": {},
},
},
},
},
"extra file - resource": {
ResourceFiles: fstest.MapFS{
"resource1.md": {},
Expand Down Expand Up @@ -164,6 +180,23 @@ func TestFileMismatchCheck(t *testing.T) {
},
ExpectError: true,
},
"extra file - ephemeral resource": {
EphemeralResourceFiles: fstest.MapFS{
"ephemeral_resource1.md": {},
"ephemeral_resource2.md": {},
"ephemeral_resource3.md": {},
},
Options: &FileMismatchOptions{
ProviderShortName: "test",
Schema: &tfjson.ProviderSchema{
EphemeralResourceSchemas: map[string]*tfjson.Schema{
"test_ephemeral_resource1": {},
"test_ephemeral_resource2": {},
},
},
},
ExpectError: true,
},
"ignore extra file - resource": {
ResourceFiles: fstest.MapFS{
"resource1.md": {},
Expand Down Expand Up @@ -194,7 +227,23 @@ func TestFileMismatchCheck(t *testing.T) {
Functions: map[string]*tfjson.FunctionSignature{
"function1": {},
"function2": {},
"function3": {},
},
},
},
},
"ignore extra file - ephemeral resource": {
EphemeralResourceFiles: fstest.MapFS{
"ephemeral_resource1.md": {},
"ephemeral_resource2.md": {},
"ephemeral_resource3.md": {},
},
Options: &FileMismatchOptions{
IgnoreFileMismatch: []string{"test_ephemeral_resource3"},
ProviderShortName: "test",
Schema: &tfjson.ProviderSchema{
EphemeralResourceSchemas: map[string]*tfjson.Schema{
"test_ephemeral_resource1": {},
"test_ephemeral_resource2": {},
},
},
},
Expand Down Expand Up @@ -229,6 +278,22 @@ func TestFileMismatchCheck(t *testing.T) {
},
ExpectError: true,
},
"missing file - ephemeral resource": {
EphemeralResourceFiles: fstest.MapFS{
"ephemeral_resource1.md": {},
},
Options: &FileMismatchOptions{
IgnoreFileMismatch: []string{"test_ephemeral_resource3"},
ProviderShortName: "test",
Schema: &tfjson.ProviderSchema{
EphemeralResourceSchemas: map[string]*tfjson.Schema{
"test_ephemeral_resource1": {},
"test_ephemeral_resource2": {},
},
},
},
ExpectError: true,
},
"ignore missing file - resource": {
ResourceFiles: fstest.MapFS{
"resource1.md": {},
Expand Down Expand Up @@ -259,6 +324,21 @@ func TestFileMismatchCheck(t *testing.T) {
},
},
},
"ignore missing file - ephemeral resource": {
EphemeralResourceFiles: fstest.MapFS{
"ephemeral_resource1.md": {},
},
Options: &FileMismatchOptions{
IgnoreFileMissing: []string{"test_ephemeral_resource2"},
ProviderShortName: "test",
Schema: &tfjson.ProviderSchema{
EphemeralResourceSchemas: map[string]*tfjson.Schema{
"test_ephemeral_resource1": {},
"test_ephemeral_resource2": {},
},
},
},
},
"no files": {
Options: &FileMismatchOptions{
ProviderShortName: "test",
Expand All @@ -271,6 +351,10 @@ func TestFileMismatchCheck(t *testing.T) {
"function1": {},
"function2": {},
},
EphemeralResourceSchemas: map[string]*tfjson.Schema{
"test_ephemeral_resource1": {},
"test_ephemeral_resource2": {},
},
},
},
},
Expand All @@ -281,6 +365,9 @@ func TestFileMismatchCheck(t *testing.T) {
FunctionFiles: fstest.MapFS{
"function1.md": {},
},
EphemeralResourceFiles: fstest.MapFS{
"ephemeral_resource1.md": {},
},
Options: &FileMismatchOptions{
ProviderShortName: "test",
},
Expand All @@ -295,8 +382,10 @@ func TestFileMismatchCheck(t *testing.T) {

resourceFiles, _ := testCase.ResourceFiles.ReadDir(".")
functionFiles, _ := testCase.FunctionFiles.ReadDir(".")
ephemeralResourceFiles, _ := testCase.EphemeralResourceFiles.ReadDir(".")
testCase.Options.ResourceEntries = resourceFiles
testCase.Options.FunctionEntries = functionFiles
testCase.Options.EphemeralResourceEntries = ephemeralResourceFiles
got := NewFileMismatchCheck(testCase.Options).Run()

if got == nil && testCase.ExpectError {
Expand Down
4 changes: 2 additions & 2 deletions internal/provider/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ const (
FileExtensionMarkdown = `.markdown`
FileExtensionMd = `.md`

DocumentationGlobPattern = `{docs/index.md,docs/{,cdktf/}{data-sources,guides,resources,functions}/**/*,website/docs/**/*}`
DocumentationDirGlobPattern = `{docs/{,cdktf/}{data-sources,guides,resources,functions}{,/*},website/docs/**/*}`
DocumentationGlobPattern = `{docs/index.md,docs/{,cdktf/}{data-sources,guides,resources,functions,ephemeral-resources}/**/*,website/docs/**/*}`
DocumentationDirGlobPattern = `{docs/{,cdktf/}{data-sources,guides,resources,functions,ephemeral-resources}{,/*},website/docs/**/*}`
)

var ValidLegacyFileExtensions = []string{
Expand Down
Loading

0 comments on commit cd4a886

Please sign in to comment.