-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #39 from hashicorp/fix-markdown-rendering
Fix Markdown rendering
- Loading branch information
Showing
18 changed files
with
408 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package tfconfig | ||
|
||
import ( | ||
"encoding/json" | ||
"io" | ||
"strings" | ||
"text/template" | ||
) | ||
|
||
func RenderMarkdown(w io.Writer, module *Module) error { | ||
tmpl := template.New("md") | ||
tmpl.Funcs(template.FuncMap{ | ||
"tt": func(s string) string { | ||
return "`" + s + "`" | ||
}, | ||
"commas": func(s []string) string { | ||
return strings.Join(s, ", ") | ||
}, | ||
"json": func(v interface{}) (string, error) { | ||
j, err := json.Marshal(v) | ||
return string(j), err | ||
}, | ||
"severity": func(s DiagSeverity) string { | ||
switch s { | ||
case DiagError: | ||
return "Error: " | ||
case DiagWarning: | ||
return "Warning: " | ||
default: | ||
return "" | ||
} | ||
}, | ||
}) | ||
template.Must(tmpl.Parse(markdownTemplate)) | ||
return tmpl.Execute(w, module) | ||
} | ||
|
||
const markdownTemplate = ` | ||
# Module {{ tt .Path }} | ||
{{- if .RequiredCore}} | ||
Core Version Constraints: | ||
{{- range .RequiredCore }} | ||
* {{ tt . }} | ||
{{- end}}{{end}} | ||
{{- if .RequiredProviders}} | ||
Provider Requirements: | ||
{{- range $name, $req := .RequiredProviders }} | ||
* **{{ $name }}{{ if $req.Source }} ({{ $req.Source | tt }}){{ end }}:** {{ if $req.VersionConstraints }}{{ commas $req.VersionConstraints | tt }}{{ else }}(any version){{ end }} | ||
{{- end}}{{end}} | ||
{{- if .Variables}} | ||
## Input Variables | ||
{{- range .Variables }} | ||
* {{ tt .Name }}{{ if .Default }} (default {{ json .Default | tt }}){{else}} (required){{end}} | ||
{{- if .Description}}: {{ .Description }}{{ end }} | ||
{{- end}}{{end}} | ||
{{- if .Outputs}} | ||
## Output Values | ||
{{- range .Outputs }} | ||
* {{ tt .Name }}{{ if .Description}}: {{ .Description }}{{ end }} | ||
{{- end}}{{end}} | ||
{{- if .ManagedResources}} | ||
## Managed Resources | ||
{{- range .ManagedResources }} | ||
* {{ printf "%s.%s" .Type .Name | tt }} from {{ tt .Provider.Name }} | ||
{{- end}}{{end}} | ||
{{- if .DataResources}} | ||
## Data Resources | ||
{{- range .DataResources }} | ||
* {{ printf "data.%s.%s" .Type .Name | tt }} from {{ tt .Provider.Name }} | ||
{{- end}}{{end}} | ||
{{- if .ModuleCalls}} | ||
## Child Modules | ||
{{- range .ModuleCalls }} | ||
* {{ tt .Name }} from {{ tt .Source }}{{ if .Version }} ({{ tt .Version }}){{ end }} | ||
{{- end}}{{end}} | ||
{{- if .Diagnostics}} | ||
## Problems | ||
{{- range .Diagnostics }} | ||
## {{ severity .Severity }}{{ .Summary }}{{ if .Pos }} | ||
(at {{ tt .Pos.Filename }} line {{ .Pos.Line }}{{ end }}) | ||
{{ if .Detail }} | ||
{{ .Detail }} | ||
{{- end }} | ||
{{- end}}{{end}} | ||
` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package tfconfig | ||
|
||
import ( | ||
"bytes" | ||
"io/ioutil" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/go-test/deep" | ||
) | ||
|
||
func TestRenderMarkdown(t *testing.T) { | ||
fixturesDir := "test-fixtures" | ||
testDirs, err := ioutil.ReadDir(fixturesDir) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
for _, info := range testDirs { | ||
if !info.IsDir() { | ||
continue | ||
} | ||
|
||
t.Run(info.Name(), func(t *testing.T) { | ||
name := info.Name() | ||
path := filepath.Join(fixturesDir, name) | ||
|
||
fullPath := filepath.Join(path, name+".out.md") | ||
expected, err := ioutil.ReadFile(fullPath) | ||
if err != nil { | ||
t.Skipf("%q not found, skipping test", fullPath) | ||
} | ||
|
||
module, _ := LoadModule(path) | ||
if module == nil { | ||
t.Fatalf("result object is nil; want a real object") | ||
} | ||
|
||
var b bytes.Buffer | ||
buf := &b | ||
err = RenderMarkdown(buf, module) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if diff := deep.Equal(buf.String(), string(expected)); diff != nil { | ||
for _, problem := range diff { | ||
t.Errorf("%s", problem) | ||
} | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
|
||
# Module `test-fixtures/basics` | ||
|
||
Provider Requirements: | ||
* **null:** (any version) | ||
|
||
## Input Variables | ||
* `A` (default `"A default"`) | ||
* `B` (required): The B variable | ||
|
||
## Output Values | ||
* `A` | ||
* `B`: I am B | ||
|
||
## Managed Resources | ||
* `null_resource.A` from `null` | ||
* `null_resource.B` from `null` | ||
|
11 changes: 11 additions & 0 deletions
11
tfconfig/test-fixtures/data-resources/data-resources.out.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
|
||
# Module `test-fixtures/data-resources` | ||
|
||
Provider Requirements: | ||
* **external:** (any version) | ||
* **notexternal:** (any version) | ||
|
||
## Data Resources | ||
* `data.external.bar` from `notexternal` | ||
* `data.external.foo` from `external` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
|
||
# Module `test-fixtures/empty` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
|
||
# Module `test-fixtures/for-expression` | ||
|
||
## Input Variables | ||
* `enabled` (default `true`) | ||
* `log_categories` (default `["one","two","three"]`) | ||
* `retention_days` (default `7`) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
|
||
# Module `test-fixtures/invalid-braces` | ||
|
||
## Input Variables | ||
* `foo` (default `"123"`) | ||
|
27 changes: 27 additions & 0 deletions
27
tfconfig/test-fixtures/legacy-block-labels/legacy-block-labels.out.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
|
||
# Module `test-fixtures/legacy-block-labels` | ||
|
||
Core Version Constraints: | ||
* `>= 0.11.0` | ||
|
||
Provider Requirements: | ||
* **aws:** `1.0.0` | ||
* **external:** (any version) | ||
* **notnull:** (any version) | ||
* **noversion:** (any version) | ||
|
||
## Input Variables | ||
* `foo` (default `"foo default"`): foo description | ||
|
||
## Output Values | ||
* `foo`: foo description | ||
|
||
## Managed Resources | ||
* `null_resource.foo` from `notnull` | ||
|
||
## Data Resources | ||
* `data.external.foo` from `external` | ||
|
||
## Child Modules | ||
* `foo` from `foo/bar/baz` (`1.2.3`) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
|
||
# Module `test-fixtures/module-calls` | ||
|
||
## Child Modules | ||
* `bar` from `./child` | ||
* `baz` from `../elsewhere` | ||
* `foo` from `foo/bar/baz` (`1.0.2`) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
|
||
# Module `test-fixtures/overrides` | ||
|
||
Provider Requirements: | ||
* **null:** (any version) | ||
|
||
## Input Variables | ||
* `A` (required): The A variable OVERRIDDEN | ||
* `B` (required): The B variable | ||
* `C` (required): An entirely new variable C | ||
|
||
## Output Values | ||
* `A`: I am an overridden output! | ||
* `B`: I am B | ||
|
||
## Managed Resources | ||
* `null_resource.A` from `null` | ||
* `null_resource.B` from `null` | ||
|
||
## Child Modules | ||
* `foo` from `foo/bar/baz` (`1.0.2_override`) | ||
|
11 changes: 11 additions & 0 deletions
11
tfconfig/test-fixtures/provider-configs/provider-configs.out.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
|
||
# Module `test-fixtures/provider-configs` | ||
|
||
Provider Requirements: | ||
* **bar:** `1.0.0, 1.1.0` | ||
* **baz:** `2.0.0` | ||
* **foo:** (any version) | ||
|
||
## Managed Resources | ||
* `bar_bar.bar` from `bar` | ||
|
Oops, something went wrong.