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

Rewrite Validate Command #341

Merged
merged 31 commits into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
713f451
Initial validate implementation
SBGoods Mar 1, 2024
fd6e513
Add function tests to TestFileMismatchCheck
SBGoods Mar 4, 2024
f6810df
Add MixedDirectoryCheck and refactor relevant tests
SBGoods Mar 4, 2024
7e3458e
Add functions and guides to glob pattern
SBGoods Mar 5, 2024
71b2b72
Update README.md
SBGoods Mar 5, 2024
7a851fd
Update documentationDirGlob to pass tests
SBGoods Mar 5, 2024
bbaa513
Add copyright headers
SBGoods Mar 5, 2024
31cea02
Resolve linting errors
SBGoods Mar 5, 2024
ffb5959
Update README.md with a link to provider documentation guidelines
SBGoods Mar 5, 2024
6b5d908
Merge branch 'main' into SBGoods/rewrite-validate-command
SBGoods Mar 5, 2024
53a168f
Resolve linting errors
SBGoods Mar 5, 2024
57bb756
Add changelog entries
SBGoods Mar 5, 2024
86eb271
Add `NumberOfFilesCheck` to validate
SBGoods Mar 5, 2024
6fefacb
Make file paths OS-agnostic
SBGoods Mar 5, 2024
7e9d98a
Fix file path in test error assertion
SBGoods Mar 5, 2024
0fd871a
Update Go version to `1.21`
SBGoods Mar 5, 2024
b2eef92
Change wording to past tense on changelog entries
SBGoods Mar 13, 2024
e4fff52
Remove unused file
SBGoods Mar 13, 2024
cc6300a
Remove `filecheck` interface
SBGoods Mar 13, 2024
a1b156e
Refactor `schemamd` and `functionmd` packages to `internal/`
SBGoods Mar 13, 2024
96e07d4
Remove unused method
SBGoods Mar 13, 2024
64373fb
Revert "Refactor `schemamd` and `functionmd` packages to `internal/`"
SBGoods Mar 22, 2024
3f27ed2
Remove go compatability section
SBGoods Mar 25, 2024
1bbf77f
Switch to `yuin/goldmark` for frontmatter parsing
SBGoods Mar 25, 2024
3f57c3a
Remove unused test
SBGoods Mar 25, 2024
498a524
Move global variables to top of file
SBGoods Mar 25, 2024
ed37a07
Refactor `testcases` to use `map[string]struct`
SBGoods Mar 26, 2024
7411fa7
Refactor to use `t.tempDir()`
SBGoods Mar 26, 2024
5702e4f
Fix documentationGlobPattern and add more debug and info logs
SBGoods Mar 26, 2024
0ed7c0a
Add negative scenario acceptance tests
SBGoods Mar 26, 2024
07b9f75
Merge remote-tracking branch 'origin/main' into SBGoods/rewrite-valid…
SBGoods Mar 26, 2024
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
5 changes: 5 additions & 0 deletions .changes/unreleased/FEATURES-20240305-135426.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
kind: FEATURES
body: 'validate: Add support for Provider-defined Function documentation'
SBGoods marked this conversation as resolved.
Show resolved Hide resolved
time: 2024-03-05T13:54:26.307742-05:00
custom:
Issue: "341"
6 changes: 6 additions & 0 deletions .changes/unreleased/FEATURES-20240305-135726.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: FEATURES
body: 'validate: Add `InvalidDirectoriesCheck` which checks for valid provider documentation
structure'
SBGoods marked this conversation as resolved.
Show resolved Hide resolved
time: 2024-03-05T13:57:26.273538-05:00
custom:
Issue: "341"
6 changes: 6 additions & 0 deletions .changes/unreleased/FEATURES-20240305-135933.yaml
SBGoods marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: FEATURES
body: 'validate: Add `MixedDirectoriesCheck` which throws an error if both legacy
documentation and registry documentation are found'
time: 2024-03-05T13:59:33.741601-05:00
custom:
Issue: "341"
6 changes: 6 additions & 0 deletions .changes/unreleased/FEATURES-20240305-140106.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: FEATURES
body: 'validate: Add `NumberOfFilesCheck` which checks the number of provider
documentation files against the registry limit'
time: 2024-03-05T14:01:06.742843-05:00
custom:
Issue: "341"
6 changes: 6 additions & 0 deletions .changes/unreleased/FEATURES-20240305-140234.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: FEATURES
body: 'validate: Add `FileSizeCheck` which checks the provider documentation file
size against the registry limit'
time: 2024-03-05T14:02:34.112782-05:00
custom:
Issue: "341"
6 changes: 6 additions & 0 deletions .changes/unreleased/FEATURES-20240305-140346.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: FEATURES
body: 'validate: Add `FileExtensionCheck` which checks for valid provider documentation
file extensions'
time: 2024-03-05T14:03:46.816256-05:00
custom:
Issue: "341"
6 changes: 6 additions & 0 deletions .changes/unreleased/FEATURES-20240305-140451.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: FEATURES
body: 'validate: Add `FrontMatterCheck` which checks the YAML frontmatter of provider
documentation for missing required fields or invalid fields'
time: 2024-03-05T14:04:51.781688-05:00
custom:
Issue: "341"
6 changes: 6 additions & 0 deletions .changes/unreleased/FEATURES-20240305-140622.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: FEATURES
body: 'validate: Add `FileMismatchCheck` which checks the names/number of provider
documentation files against the provider schema'
time: 2024-03-05T14:06:22.168518-05:00
custom:
Issue: "341"
29 changes: 28 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ Available commands are:
the generate command is run by default
generate generates a plugin website from code, templates, and examples
migrate migrates website files from either the legacy rendered website directory (`website/docs/r`) or the docs rendered website directory (`docs/resources`) to the tfplugindocs supported structure (`templates/`).
validate validates a plugin website for the current directory
validate validates a plugin website

```

Expand Down Expand Up @@ -81,6 +81,11 @@ Usage: tfplugindocs generate [<args>]
$ tfplugindocs validate --help

Usage: tfplugindocs validate [<args>]

--provider-dir <ARG> relative or absolute path to the root provider code directory; this will default to the current working directory if not set
--provider-name <ARG> provider name, as used in Terraform configurations
--providers-schema <ARG> path to the providers schema JSON file, which contains the output of the terraform providers schema -json command. Setting this flag will skip building the provider and calling Terraform CLI
--tf-version <ARG> terraform binary version to download. If not provided, will look for a terraform binary in the local environment. If not found in the environment, will download the latest version of Terraform
```

`migrate` command:
Expand Down Expand Up @@ -145,6 +150,22 @@ Otherwise, the provider developer can set an arbitrary description like this:
// ...
```

#### Validate subcommand

The `validate` subcommand can be used to validate the provider website documentation against the [Terraform Registry's provider documentation guidelines](https://developer.hashicorp.com/terraform/registry/providers/docs) and provider documentation best practices. The current checks in the `validate` command are:

| Check | Description |
|---------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `InvalidDirectoriesCheck` | Checks for valid subdirectory structure and throws an error if an invalid Terraform Provider documentation subdirectory is found. |
| `MixedDirectoriesCheck` | Throws an error if both legacy documentation (`/website/docs`) and registry documentation (`/docs`) are found. |
| `NumberOfFilesCheck` | Throws an error if the number of files in a directory is larger than the registry limit. |
| `FileSizeCheck` | Throws an error if the documentation file is above the registry storage limit. |
SBGoods marked this conversation as resolved.
Show resolved Hide resolved
| `FileExtensionCheck` | Throws an error if the extension of the given file is not a valid registry documentation extension. |
| `FrontMatterCheck` | Checks the YAML frontmatter of documentation for missing required fields or invalid fields. |
| `FileMismatchCheck` | Throws an error if the names/number of resources/datasources/functions in the provider schema does not match the names/number of files in the corresponding documentation directory |

All check errors are wrapped and returned as a single error message to stderr.

#### Migrate subcommand

The `migrate` subcommand can be used to migrate website files from either the legacy rendered website directory (`website/docs/r`) or the docs
Expand Down Expand Up @@ -306,6 +327,12 @@ Your help and patience is truly appreciated.

## Contributing

## Go Compatibility

This project follows the [support policy](https://golang.org/doc/devel/release.html#policy) of Go as its support policy. The two latest major releases of Go are supported by the project.

Currently, that means Go **1.21** or later must be used when including this project as a dependency.

SBGoods marked this conversation as resolved.
Show resolved Hide resolved
### License Headers
All source code files in this repository (excluding autogenerated files like `go.mod`, prose, and files excluded in [.copywrite.hcl](.copywrite.hcl)) must have a license header at the top.

Expand Down
8 changes: 8 additions & 0 deletions cmd/tfplugindocs/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,11 @@ func Test_SchemaJson_MigrateAcceptanceTests(t *testing.T) {
Dir: "testdata/scripts/schema-json/migrate",
})
}

func Test_SchemaJson_ValidateAcceptanceTests(t *testing.T) {
t.Parallel()

testscript.Run(t, testscript.Params{
Dir: "testdata/scripts/schema-json/validate",
})
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
# Copyright (c) HashiCorp, Inc.
# SPDX-License-Identifier: MPL-2.0

# Successful run of tfplugindocs validate command on a Framework provider with docs in the legacy directory structure (i.e. r/<resource name>.md.tmpl)
[!unix] skip
exec tfplugindocs validate --provider-name=terraform-provider-scaffolding --providers-schema=schema.json
cmp stdout expected-output.txt

-- expected-output.txt --
exporting schema from JSON file
getting provider schema
detected legacy website directory, running checks
running invalid directories check on website/docs/d
running file checks on website/docs/d/example.html.md
running invalid directories check on website/docs/functions
running file checks on website/docs/functions/example.html.md
running invalid directories check on website/docs/guides
running file checks on website/docs/guides/example.html.md
running file checks on website/docs/index.html.md
running invalid directories check on website/docs/r
running file checks on website/docs/r/example.html.md
running file mismatch check
-- website/docs/guides/example.html.md --
---
subcategory: "Example"
layout: "example"
page_title: "Example Guide"
description: |-
Example description.
---

# Example Guide

Example contents.

-- website/docs/r/example.html.md --
---
subcategory: "Example"
layout: "example"
page_title: "Example: example_thing"
description: |-
Example description.
---
# Data Fields

Name: {{.Name}}
Type: {{.Type}}
-- website/docs/d/example.html.md --
---
subcategory: "Example"
layout: "example"
page_title: "Example: example_thing"
description: |-
Example description.
---
# Data Fields

Name: {{.Name}}
Type: {{.Type}}
-- website/docs/functions/example.html.md --
---
subcategory: "Example"
layout: "example"
page_title: "Example: example_thing"
description: |-
Example description.
---
# Data Fields

Name: {{.Name}}
Type: {{.Type}}
-- website/docs/index.html.md --
---
layout: "example"
page_title: "Example Provider"
description: |-
Example description.
---
# Data Fields

Name: {{.Name}}
Type: {{.Type}}
-- schema.json --
{
"format_version": "1.0",
"provider_schemas": {
"registry.terraform.io/hashicorp/scaffolding": {
"provider": {
"version": 0,
"block": {
"attributes": {
"endpoint": {
"type": "string",
"description": "Example provider attribute",
"description_kind": "markdown",
"optional": true
}
},
"description": "Example provider",
"description_kind": "markdown"
}
},
"resource_schemas": {
"scaffolding_example": {
"version": 0,
"block": {
"attributes": {
"configurable_attribute": {
"type": "string",
"description": "Example configurable attribute",
"description_kind": "markdown",
"optional": true
},
"defaulted": {
"type": "string",
"description": "Example configurable attribute with default value",
"description_kind": "markdown",
"optional": true,
"computed": true
},
"id": {
"type": "string",
"description": "Example identifier",
"description_kind": "markdown",
"computed": true
}
},
"description": "Example resource",
"description_kind": "markdown"
}
}
},
"data_source_schemas": {
"scaffolding_example": {
"version": 0,
"block": {
"attributes": {
"configurable_attribute": {
"type": "string",
"description": "Example configurable attribute",
"description_kind": "markdown",
"optional": true
},
"id": {
"type": "string",
"description": "Example identifier",
"description_kind": "markdown",
"computed": true
}
},
"description": "Example data source",
"description_kind": "markdown"
}
}
},
"functions": {
"example": {
"description": "Given a string value, returns the same value.",
"summary": "Echo a string",
"return_type": "string",
"parameters": [
{
"name": "input",
"description": "Value to echo.",
"type": "string"
}
],
"variadic_parameter": {
"name": "variadicInput",
"description": "Variadic input to echo.",
"type": "string"
}
}
}
}
}
}
Loading
Loading