Skip to content

Commit 72e4de5

Browse files
SBGoodsbflad
andauthored
Add support for --provider-schema flag and context to generate sub-command errors (#299)
* Add context to errors * Refactor to use `g.providerName` instead of passing the provider name as a parameter. * Add support for `--providers-schema` flag to pass in a providers schema JSON file. * Add schema json acceptance tests and move provider build tests into a separate folder. * Update README.md * Add copyright headers * Add more schema json tests * Add changelog entry * Apply documentation suggestions Co-authored-by: Brian Flad <[email protected]> --------- Co-authored-by: Brian Flad <[email protected]>
1 parent 177a0a2 commit 72e4de5

17 files changed

+2367
-87
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
kind: ENHANCEMENTS
2+
body: 'generate: Add `provider-schema` flag to pass in a file path to a provider schema JSON
3+
file, allowing the command to skip building the provider and calling Terraform CLI'
4+
time: 2023-11-17T15:10:29.850914-05:00
5+
custom:
6+
Issue: "299"

README.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ Usage: tfplugindocs generate [<args>]
4343
--ignore-deprecated <ARG> don't generate documentation for deprecated resources and data-sources (default: "false")
4444
--provider-dir <ARG> relative or absolute path to the root provider code directory when running the command outside the root provider code directory
4545
--provider-name <ARG> provider name, as used in Terraform configurations
46+
--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
4647
--rendered-provider-name <ARG> provider name, as generated in documentation (ex. page titles, ...)
4748
--rendered-website-dir <ARG> output directory based on provider-dir (default: "docs")
4849
--tf-version <ARG> terraform binary version to download
@@ -198,9 +199,15 @@ This can be autogenerated by running `make generate` or running `go generate ./.
198199
199200
This repo uses the `testscript` [package](https://pkg.go.dev/github.com/rogpeppe/go-internal/testscript) for acceptance testing.
200201
201-
You can run `make testacc` to run the acceptance tests. By default, the acceptance tests will create a temporary directory in `/tmp/tftmp` for testing but you can change this location in `cmd/tfplugindocs/main_test.go`
202+
There are two types of acceptance tests: full provider build tests in `tfplugindocs/testdata/scripts/provider-build` and provider schema json tests in `tfplugindocs/testdata/scripts/schema-json`.
202203
203-
The test scripts are defined in the `tfplugindocs/testdata/scripts` directory. Each script includes the test, golden files, and the provider source code needed to run the test.
204+
Provider build tests run the default `tfplugindocs` command which builds the provider source code and runs Terraform to retrieve the schema. These tests require the full provider source code to build a valid provider binary.
205+
206+
Schema json tests run the `tfplugindocs` command with the `--providers-schema=<arg>` flag to specify a provider schemas json file. This allows the test to skip the provider build and Terraform CLI call, instead using the specified file to generate docs.
207+
208+
You can run `make testacc` to run the full suite of acceptance tests. By default, the provider build acceptance tests will create a temporary directory in `/tmp/tftmp` for testing, but you can change this location in `cmd/tfplugindocs/main_test.go`. The schema json tests uses the `testscript` package's [default work directory](https://pkg.go.dev/github.com/rogpeppe/go-internal/testscript#Params.WorkdirRoot).
209+
210+
The test scripts are defined in the `tfplugindocs/testdata/scripts` directory. Each script includes the test, golden files, and the provider source code or schema JSON file needed to run the test.
204211

205212
Each script is a [text archive](https://pkg.go.dev/golang.org/x/tools/txtar). You can install the `txtar` CLI locally by running `go install golang.org/x/exp/cmd/txtar@latest` to extract the files in the test script for debugging.
206213
You can also use `txtar` CLI archive files into the `.txtar` format to create new tests or modify existing ones.

cmd/tfplugindocs/main_test.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ func TestMain(m *testing.M) {
1818
}))
1919
}
2020

21-
func Test_GenerateAcceptanceTests(t *testing.T) {
21+
func Test_ProviderBuild_GenerateAcceptanceTests(t *testing.T) {
2222
t.Parallel()
2323
if os.Getenv("ACCTEST") == "" {
24-
t.Skip("ACCTEST env var not set; skipping acceptance tests.")
24+
t.Skip("ACCTEST env var not set; skipping provider build acceptance tests.")
2525
}
2626
// Setting a custom temp dir instead of relying on os.TempDir()
2727
// because Terraform providers fail to start up when $TMPDIR
@@ -34,7 +34,15 @@ func Test_GenerateAcceptanceTests(t *testing.T) {
3434
defer os.RemoveAll(tmpDir)
3535

3636
testscript.Run(t, testscript.Params{
37-
Dir: "testdata/scripts/generate",
37+
Dir: "testdata/scripts/provider-build/generate",
3838
WorkdirRoot: tmpDir,
3939
})
4040
}
41+
42+
func Test_SchemaJson_GenerateAcceptanceTests(t *testing.T) {
43+
t.Parallel()
44+
45+
testscript.Run(t, testscript.Params{
46+
Dir: "testdata/scripts/schema-json/generate",
47+
})
48+
}

0 commit comments

Comments
 (0)