Skip to content

Commit

Permalink
fix: build docs command for files with multiple tests (#1087)
Browse files Browse the repository at this point in the history
Signed-off-by: Charles-Edouard Brétéché <[email protected]>
  • Loading branch information
eddycharly authored Mar 9, 2024
1 parent 7ce628b commit 6c91795
Show file tree
Hide file tree
Showing 26 changed files with 130 additions and 15 deletions.
3 changes: 2 additions & 1 deletion .release-notes/main.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ Release notes for `TODO`.
- Fixed resource templating always enabled in `patch` operation, regardless of the configured `template` field
- Added missing operations to the `build docs` command template
- Added test-level catch statements to the `build docs` command template
- Added binding name validation markers
- Added binding `name` validation markers
- Fixed `build docs` command for files with multiple tests

## ⭐ Examples ⭐

Expand Down
30 changes: 22 additions & 8 deletions pkg/commands/build/docs/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,12 @@ func Command() *cobra.Command {
if err != nil {
return err
}
testsSet := map[string][]discovery.Test{}
for _, test := range tests {
testsSet[test.BasePath] = append(testsSet[test.BasePath], test)
}
out := cmd.OutOrStdout()
if err := generateDocs(out, options.readmeFile, tests...); err != nil {
if err := generateDocs(out, options.readmeFile, testsSet); err != nil {
return err
}
if options.catalog != "" {
Expand All @@ -63,20 +67,30 @@ func Command() *cobra.Command {
return cmd
}

func generateDocs(out io.Writer, fileName string, tests ...discovery.Test) error {
for _, test := range tests {
if test.Err == nil {
file, err := os.Create(filepath.Join(test.BasePath, fileName))
func generateDocs(out io.Writer, fileName string, tests map[string][]discovery.Test) error {
for path, tests := range tests {
err := func() error {
var validTests []discovery.Test
for _, test := range tests {
if test.Err != nil {
fmt.Fprintf(out, "ERROR: failed to load test %s (%s)", test.BasePath, test.Err)
} else {
validTests = append(validTests, test)
}
}
file, err := os.Create(filepath.Join(path, fileName))
if err != nil {
return err
}
defer file.Close()
output := file
if err := docsTmpl.Execute(output, test); err != nil {
if err := docsTmpl.Execute(output, validTests); err != nil {
return err
}
} else {
fmt.Fprintf(out, "ERROR: failed to load test %s (%s)", test.BasePath, test.Err)
return nil
}()
if err != nil {
return err
}
}
return nil
Expand Down
19 changes: 13 additions & 6 deletions pkg/commands/build/docs/docs.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ sleep
{{- else if .Wait -}}
wait
{{- else -}}
{{ fail (print "unknown operation " (toJson .)) }}
{{- fail (print "unknown operation " (toJson .)) -}}
{{- end -}}
{{- end -}}

Expand All @@ -44,7 +44,7 @@ sleep
{{- else if .Wait -}}
wait
{{- else -}}
{{ fail (print "unknown catch " (toJson .)) }}
{{- fail (print "unknown catch " (toJson .)) -}}
{{- end -}}
{{- end -}}

Expand All @@ -68,10 +68,13 @@ sleep
{{- else if .Wait -}}
wait
{{- else -}}
{{ fail (print "unknown finally " (toJson .)) }}
{{- fail (print "unknown finally " (toJson .)) -}}
{{- end -}}
{{- end -}}

{{- range . }}
{{- $test := . -}}

# Test: `{{ .Name }}`

{{ default "*No description*" .Spec.Description }}
Expand All @@ -84,7 +87,7 @@ wait
|:-:|---|:-:|:-:|:-:|
{{- range $i, $step := . }}
{{- $name := default (print "step-" (add $i 1)) $step.Name }}
| {{ add $i 1 }} | [{{ $name }}](#step-{{ $name }}) | {{ len $step.Try }} | {{ add (len $step.Catch) (len $.Spec.Catch) }} | {{ len $step.Finally }} |
| {{ add $i 1 }} | [{{ $name }}](#step-{{ $name }}) | {{ len $step.Try }} | {{ add (len $step.Catch) (len $test.Spec.Catch) }} | {{ len $step.Finally }} |
{{- end }}
{{- end }}

Expand Down Expand Up @@ -112,11 +115,11 @@ wait

| # | Operation | Description |
|:-:|---|---|
{{- range $i, $op := $.Spec.Catch }}
{{- range $i, $op := $test.Spec.Catch }}
| {{ add $i 1 }} | `{{ template "CatchType" $op }}` | {{ default "*No description*" $op.Description }} |
{{- end }}
{{- range $i, $op := . }}
| {{ add (len $.Spec.Catch) $i 1 }} | `{{ template "CatchType" $op }}` | {{ default "*No description*" $op.Description }} |
| {{ add (len $test.Spec.Catch) $i 1 }} | `{{ template "CatchType" $op }}` | {{ default "*No description*" $op.Description }} |
{{- end }}
{{- end }}

Expand All @@ -132,3 +135,7 @@ wait
{{- end }}

{{- end }}

---

{{ end -}}
27 changes: 27 additions & 0 deletions testdata/e2e/examples/apply-outputs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,30 @@

### Steps

| # | Name | Try | Catch | Finally |
|:-:|---|:-:|:-:|:-:|
| 1 | [step-1](#step-step-1) | 3 | 0 | 0 |

## Step: `step-1`

*No description*

### Try

| # | Operation | Description |
|:-:|---|---|
| 1 | `apply` | *No description* |
| 2 | `script` | *No description* |
| 3 | `assert` | *No description* |

---

# Test: `apply-outputs`

*No description*

### Steps

| # | Name | Try | Catch | Finally |
|:-:|---|:-:|:-:|:-:|
| 1 | [step-1](#step-step-1) | 6 | 0 | 0 |
Expand All @@ -22,3 +46,6 @@
| 4 | `script` | *No description* |
| 5 | `assert` | *No description* |
| 6 | `assert` | *No description* |

---

3 changes: 3 additions & 0 deletions testdata/e2e/examples/array-assertion/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,6 @@
|:-:|---|---|
| 1 | `apply` | *No description* |
| 2 | `assert` | *No description* |

---

3 changes: 3 additions & 0 deletions testdata/e2e/examples/assertion-tree/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,6 @@
| # | Operation | Description |
|:-:|---|---|
| 1 | `assert` | *No description* |

---

3 changes: 3 additions & 0 deletions testdata/e2e/examples/basic/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,6 @@ This steps applies the configmap in the cluster and checks the configmap content
| 1 | `apply` | Create the configmap. |
| 2 | `assert` | Check the configmap content. |
| 3 | `script` | *No description* |

---

3 changes: 3 additions & 0 deletions testdata/e2e/examples/bindings/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,6 @@
| # | Operation | Description |
|:-:|---|---|
| 1 | `assert` | *No description* |

---

3 changes: 3 additions & 0 deletions testdata/e2e/examples/catch/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,6 @@
|:-:|---|---|
| 1 | `events` | *No description* |
| 2 | `describe` | *No description* |

---

3 changes: 3 additions & 0 deletions testdata/e2e/examples/command-output/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,6 @@
| # | Operation | Description |
|:-:|---|---|
| 1 | `script` | *No description* |

---

3 changes: 3 additions & 0 deletions testdata/e2e/examples/delete/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,6 @@
|:-:|---|---|
| 1 | `apply` | *No description* |
| 2 | `delete` | *No description* |

---

3 changes: 3 additions & 0 deletions testdata/e2e/examples/deployment/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,6 @@
|:-:|---|---|
| 1 | `apply` | *No description* |
| 2 | `assert` | *No description* |

---

3 changes: 3 additions & 0 deletions testdata/e2e/examples/finally/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,6 @@
|:-:|---|---|
| 1 | `events` | *No description* |
| 2 | `script` | *No description* |

---

3 changes: 3 additions & 0 deletions testdata/e2e/examples/inline/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,6 @@
|:-:|---|---|
| 1 | `apply` | *No description* |
| 2 | `assert` | *No description* |

---

3 changes: 3 additions & 0 deletions testdata/e2e/examples/k8s-server-version/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,6 @@
| # | Operation | Description |
|:-:|---|---|
| 1 | `script` | *No description* |

---

3 changes: 3 additions & 0 deletions testdata/e2e/examples/list/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,6 @@
| 1 | `apply` | *No description* |
| 2 | `assert` | *No description* |
| 3 | `assert` | *No description* |

---

3 changes: 3 additions & 0 deletions testdata/e2e/examples/namespace-template/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,6 @@
| # | Operation | Description |
|:-:|---|---|
| 1 | `assert` | *No description* |

---

3 changes: 3 additions & 0 deletions testdata/e2e/examples/non-resource-assertion/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,6 @@
|:-:|---|---|
| 1 | `assert` | *No description* |
| 2 | `error` | *No description* |

---

3 changes: 3 additions & 0 deletions testdata/e2e/examples/outputs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,6 @@
|:-:|---|---|
| 1 | `command` | *No description* |
| 2 | `assert` | *No description* |

---

3 changes: 3 additions & 0 deletions testdata/e2e/examples/patch/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,6 @@
| 1 | `apply` | *No description* |
| 2 | `patch` | *No description* |
| 3 | `assert` | *No description* |

---

3 changes: 3 additions & 0 deletions testdata/e2e/examples/script-env/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,6 @@
|:-:|---|---|
| 1 | `script` | *No description* |
| 2 | `command` | *No description* |

---

3 changes: 3 additions & 0 deletions testdata/e2e/examples/sleep/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,6 @@
| # | Operation | Description |
|:-:|---|---|
| 1 | `sleep` | *No description* |

---

3 changes: 3 additions & 0 deletions testdata/e2e/examples/template/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,6 @@
| 1 | `apply` | *No description* |
| 2 | `assert` | *No description* |
| 3 | `delete` | *No description* |

---

3 changes: 3 additions & 0 deletions testdata/e2e/examples/timeout/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,6 @@
| # | Operation | Description |
|:-:|---|---|
| 1 | `script` | *No description* |

---

3 changes: 3 additions & 0 deletions testdata/e2e/examples/values/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,6 @@
| # | Operation | Description |
|:-:|---|---|
| 1 | `assert` | *No description* |

---

3 changes: 3 additions & 0 deletions testdata/e2e/examples/wait/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,6 @@
|:-:|---|---|
| 1 | `delete` | *No description* |
| 2 | `wait` | *No description* |

---

0 comments on commit 6c91795

Please sign in to comment.