Skip to content

Commit 177a0a2

Browse files
authored
Add acceptance tests and ignore no such file or directory errors in cleaning rendered website dir (#296)
* Add acceptance tests for `generate` command * Ignore file does not exist error when cleaning up rendered website dir. * Add more template rendering unit tests * Run `go mod tidy` * Run tests from makefile * Add changelog entry * Add copyright headers * Add flag to skip tests for non-unix systems * Add unit test target * Add Acceptance Testing section to Readme * Move `build` package and update goreleaser `idflags` * Add `.SchemaMarkdown` field and `printf` functions to Readme * Add copyright headers
1 parent 905df26 commit 177a0a2

20 files changed

+5152
-35
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
kind: BUG FIXES
2+
body: 'generate: fix `no such file or directory` error when running `generate` with
3+
no existing rendered website directory.'
4+
time: 2023-11-08T17:25:58.561956-05:00
5+
custom:
6+
Issue: "296"

.copywrite.hcl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ project {
99
".changes/unreleased/*.yaml",
1010
".changie.yaml",
1111

12+
# examples used within documentation (prose)
13+
"internal/provider/testdata/**",
14+
1215
# GitHub issue template configuration
1316
".github/ISSUE_TEMPLATE/*.yml",
1417

.github/workflows/test.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,5 @@ jobs:
3333
args: --timeout=3m
3434
- name: go vet
3535
run: go vet ./...
36-
- name: Run tests
37-
run: go test -v -cover -race ./...
36+
- name: Run acceptance tests
37+
run: make testacc

.goreleaser.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ builds:
77
flags:
88
- -trimpath
99
ldflags:
10-
- '-s -w -X main.version={{.Version}} -X main.commit={{.Commit}}'
10+
- '-s -w -X github.com/hashicorp/terraform-plugin-docs/cmd/tfplugindocs/build.version={{.Version}} -X github.com/hashicorp/terraform-plugin-docs/cmd/tfplugindocs/build.commit={{.Commit}}'
1111
goos:
1212
- windows
1313
- linux

GNUmakefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@ TEST?=./...
22

33
default: build
44

5-
.PHONY: build test
5+
.PHONY: build test testacc
66

77
build:
88
go install ./cmd/tfplugindocs
99

1010
test:
1111
go test $(TEST) $(TESTARGS) -timeout=5m
1212

13+
testacc:
14+
ACCTEST=1 go test -v -cover -race -timeout 120m ./...
15+
1316
# Generate copywrite headers
1417
generate:
1518
cd tools; go generate ./...

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ using the following data fields and functions:
146146
| `.ProviderName` | string | Canonical provider name (ex. `terraform-provider-random`) |
147147
| `.ProviderShortName` | string | Short version of the provider name (ex. `random`) |
148148
| `.RenderedProviderName` | string | Value provided via argument `--rendered-provider-name`, otherwise same as `.ProviderName` |
149+
| `.SchemaMarkdown` | string | a Markdown formatted Provider Schema definition |
149150

150151
##### Resources / Data Source
151152

@@ -161,6 +162,7 @@ using the following data fields and functions:
161162
| `.ProviderName` | string | Canonical provider name (ex. `terraform-provider-random`) |
162163
| `.ProviderShortName` | string | Short version of the provider name (ex. `random`) |
163164
| `.RenderedProviderName` | string | Value provided via argument `--rendered-provider-name`, otherwise same as `.ProviderName` |
165+
| `.SchemaMarkdown` | string | a Markdown formatted Resource / Data Source Schema definition |
164166

165167
#### Functions
166168

@@ -170,6 +172,7 @@ using the following data fields and functions:
170172
| `lower` | Equivalent to [`strings.ToLower`](https://pkg.go.dev/strings#ToLower). |
171173
| `plainmarkdown` | Render Markdown content as plaintext. |
172174
| `prefixlines` | Add a prefix to all (newline-separated) lines in a string. |
175+
| `printf` | Equivalent to [`fmt.Printf`](https://pkg.go.dev/fmt#Printf). |
173176
| `split` | Split string into sub-strings, by a given separator (ex. `split .Name "_"`). |
174177
| `title` | Equivalent to [`cases.Title`](https://pkg.go.dev/golang.org/x/text/cases#Title). |
175178
| `tffile` | A special case of the `codefile` function, designed for Terraform files (i.e. `.tf`). |
@@ -190,3 +193,14 @@ Your help and patience is truly appreciated.
190193
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.
191194
192195
This can be autogenerated by running `make generate` or running `go generate ./...` in the [/tools](/tools) directory.
196+
197+
### Acceptance Tests
198+
199+
This repo uses the `testscript` [package](https://pkg.go.dev/github.com/rogpeppe/go-internal/testscript) for acceptance testing.
200+
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+
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+
205+
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.
206+
You can also use `txtar` CLI archive files into the `.txtar` format to create new tests or modify existing ones.

cmd/tfplugindocs/build/version.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package build
5+
6+
var (
7+
// These vars will be set by goreleaser.
8+
version string = `dev`
9+
commit string = ``
10+
)
11+
12+
func GetVersion() string {
13+
version := "tfplugindocs" + " Version " + version
14+
if commit != "" {
15+
version += " from commit " + commit
16+
}
17+
return version
18+
}

cmd/tfplugindocs/main.go

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,9 @@ package main
66
import (
77
"os"
88

9-
"github.com/mattn/go-colorable"
10-
119
"github.com/hashicorp/terraform-plugin-docs/internal/cmd"
1210
)
1311

1412
func main() {
15-
name := "tfplugindocs"
16-
version := name + " Version " + version
17-
if commit != "" {
18-
version += " from commit " + commit
19-
}
20-
21-
os.Exit(cmd.Run(
22-
name,
23-
version,
24-
os.Args[1:],
25-
os.Stdin,
26-
colorable.NewColorableStdout(),
27-
colorable.NewColorableStderr(),
28-
))
13+
os.Exit(cmd.Main())
2914
}

cmd/tfplugindocs/main_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package main
5+
6+
import (
7+
"os"
8+
"testing"
9+
10+
"github.com/rogpeppe/go-internal/testscript"
11+
12+
"github.com/hashicorp/terraform-plugin-docs/internal/cmd"
13+
)
14+
15+
func TestMain(m *testing.M) {
16+
os.Exit(testscript.RunMain(m, map[string]func() int{
17+
"tfplugindocs": cmd.Main,
18+
}))
19+
}
20+
21+
func Test_GenerateAcceptanceTests(t *testing.T) {
22+
t.Parallel()
23+
if os.Getenv("ACCTEST") == "" {
24+
t.Skip("ACCTEST env var not set; skipping acceptance tests.")
25+
}
26+
// Setting a custom temp dir instead of relying on os.TempDir()
27+
// because Terraform providers fail to start up when $TMPDIR
28+
// length is too long: https://github.com/hashicorp/terraform/issues/32787
29+
tmpDir := "/tmp/tftmp"
30+
err := os.MkdirAll(tmpDir, 0755)
31+
if err != nil {
32+
t.Errorf("Error creating temp dir for testing: %s", err.Error())
33+
}
34+
defer os.RemoveAll(tmpDir)
35+
36+
testscript.Run(t, testscript.Params{
37+
Dir: "testdata/scripts/generate",
38+
WorkdirRoot: tmpDir,
39+
})
40+
}

0 commit comments

Comments
 (0)