pulumi/pkg/codegen/docs/description_test.go

80 lines
2.0 KiB
Go
Raw Normal View History

// Copyright 2024, Pulumi Corporation.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
Add ability to process docs without dedicated Examples logic (#15475) This pull request depends on corresponding changes in the pulumi-terraform bridge. The bridge will mark up each code section it finds (not just ones found under an "Example Usage" header) with an HTML comment - currently this will be `<!--Begin Code Chooser -->` and `<!--End Code Chooser -->` but the name is subject to change. In this PR, we are adding logic to detect if an incoming Description block has an `{{% examples }}` shortcode markup: if yes, we will process it as before. If no, we will switch to new functionality that relies on documentation with code sections having no short codes, no "Examples Section" logic, and will generate every code section it finds in an incoming schema that is in the Description/Content section of a registry page with a chooser and language choosables. This work is part of https://github.com/pulumi/pulumi-aws/issues/2624. Edit: unit tests are added This PR is a prerequisite for https://github.com/pulumi/pulumi-terraform-bridge/pull/1689. It aims to support both legacy and new behavior. <!--- Thanks so much for your contribution! If this is your first time contributing, please ensure that you have read the [CONTRIBUTING](https://github.com/pulumi/pulumi/blob/master/CONTRIBUTING.md) documentation. --> # Description <!--- Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. --> Fixes # (issue) ## Checklist - [x] I have run `make tidy` to update any new dependencies - [x] I have run `make lint` to verify my code passes the lint check - [x] I have formatted my code using `gofumpt` <!--- Please provide details if the checkbox below is to be left unchecked. --> - [x] I have added tests that prove my fix is effective or that my feature works <!--- User-facing changes require a CHANGELOG entry. --> - [ ] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change <!-- If the change(s) in this PR is a modification of an existing call to the Pulumi Cloud, then the service should honor older versions of the CLI where this change would not exist. You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add it to the service. --> - [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Cloud API version <!-- @Pulumi employees: If yes, you must submit corresponding changes in the service repo. -->
2024-02-23 18:06:44 +00:00
package docs
import (
"os"
"path/filepath"
"testing"
"github.com/hexops/autogold/v2"
Add ability to process docs without dedicated Examples logic (#15475) This pull request depends on corresponding changes in the pulumi-terraform bridge. The bridge will mark up each code section it finds (not just ones found under an "Example Usage" header) with an HTML comment - currently this will be `<!--Begin Code Chooser -->` and `<!--End Code Chooser -->` but the name is subject to change. In this PR, we are adding logic to detect if an incoming Description block has an `{{% examples }}` shortcode markup: if yes, we will process it as before. If no, we will switch to new functionality that relies on documentation with code sections having no short codes, no "Examples Section" logic, and will generate every code section it finds in an incoming schema that is in the Description/Content section of a registry page with a chooser and language choosables. This work is part of https://github.com/pulumi/pulumi-aws/issues/2624. Edit: unit tests are added This PR is a prerequisite for https://github.com/pulumi/pulumi-terraform-bridge/pull/1689. It aims to support both legacy and new behavior. <!--- Thanks so much for your contribution! If this is your first time contributing, please ensure that you have read the [CONTRIBUTING](https://github.com/pulumi/pulumi/blob/master/CONTRIBUTING.md) documentation. --> # Description <!--- Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. --> Fixes # (issue) ## Checklist - [x] I have run `make tidy` to update any new dependencies - [x] I have run `make lint` to verify my code passes the lint check - [x] I have formatted my code using `gofumpt` <!--- Please provide details if the checkbox below is to be left unchecked. --> - [x] I have added tests that prove my fix is effective or that my feature works <!--- User-facing changes require a CHANGELOG entry. --> - [ ] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change <!-- If the change(s) in this PR is a modification of an existing call to the Pulumi Cloud, then the service should honor older versions of the CLI where this change would not exist. You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add it to the service. --> - [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Cloud API version <!-- @Pulumi employees: If yes, you must submit corresponding changes in the service repo. -->
2024-02-23 18:06:44 +00:00
"github.com/stretchr/testify/require"
)
func TestProcessDescription(t *testing.T) {
t.Parallel()
tests := []struct {
prefix string
}{
{"lambda-description"},
{"scaleway-k8s-cluster-description"}, // Repro: https://github.com/pulumi/registry/issues/4202
}
Add ability to process docs without dedicated Examples logic (#15475) This pull request depends on corresponding changes in the pulumi-terraform bridge. The bridge will mark up each code section it finds (not just ones found under an "Example Usage" header) with an HTML comment - currently this will be `<!--Begin Code Chooser -->` and `<!--End Code Chooser -->` but the name is subject to change. In this PR, we are adding logic to detect if an incoming Description block has an `{{% examples }}` shortcode markup: if yes, we will process it as before. If no, we will switch to new functionality that relies on documentation with code sections having no short codes, no "Examples Section" logic, and will generate every code section it finds in an incoming schema that is in the Description/Content section of a registry page with a chooser and language choosables. This work is part of https://github.com/pulumi/pulumi-aws/issues/2624. Edit: unit tests are added This PR is a prerequisite for https://github.com/pulumi/pulumi-terraform-bridge/pull/1689. It aims to support both legacy and new behavior. <!--- Thanks so much for your contribution! If this is your first time contributing, please ensure that you have read the [CONTRIBUTING](https://github.com/pulumi/pulumi/blob/master/CONTRIBUTING.md) documentation. --> # Description <!--- Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. --> Fixes # (issue) ## Checklist - [x] I have run `make tidy` to update any new dependencies - [x] I have run `make lint` to verify my code passes the lint check - [x] I have formatted my code using `gofumpt` <!--- Please provide details if the checkbox below is to be left unchecked. --> - [x] I have added tests that prove my fix is effective or that my feature works <!--- User-facing changes require a CHANGELOG entry. --> - [ ] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change <!-- If the change(s) in this PR is a modification of an existing call to the Pulumi Cloud, then the service should honor older versions of the CLI where this change would not exist. You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add it to the service. --> - [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Cloud API version <!-- @Pulumi employees: If yes, you must submit corresponding changes in the service repo. -->
2024-02-23 18:06:44 +00:00
for _, tt := range tests {
tt := tt
t.Run(tt.prefix, func(t *testing.T) {
t.Parallel()
Add ability to process docs without dedicated Examples logic (#15475) This pull request depends on corresponding changes in the pulumi-terraform bridge. The bridge will mark up each code section it finds (not just ones found under an "Example Usage" header) with an HTML comment - currently this will be `<!--Begin Code Chooser -->` and `<!--End Code Chooser -->` but the name is subject to change. In this PR, we are adding logic to detect if an incoming Description block has an `{{% examples }}` shortcode markup: if yes, we will process it as before. If no, we will switch to new functionality that relies on documentation with code sections having no short codes, no "Examples Section" logic, and will generate every code section it finds in an incoming schema that is in the Description/Content section of a registry page with a chooser and language choosables. This work is part of https://github.com/pulumi/pulumi-aws/issues/2624. Edit: unit tests are added This PR is a prerequisite for https://github.com/pulumi/pulumi-terraform-bridge/pull/1689. It aims to support both legacy and new behavior. <!--- Thanks so much for your contribution! If this is your first time contributing, please ensure that you have read the [CONTRIBUTING](https://github.com/pulumi/pulumi/blob/master/CONTRIBUTING.md) documentation. --> # Description <!--- Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. --> Fixes # (issue) ## Checklist - [x] I have run `make tidy` to update any new dependencies - [x] I have run `make lint` to verify my code passes the lint check - [x] I have formatted my code using `gofumpt` <!--- Please provide details if the checkbox below is to be left unchecked. --> - [x] I have added tests that prove my fix is effective or that my feature works <!--- User-facing changes require a CHANGELOG entry. --> - [ ] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change <!-- If the change(s) in this PR is a modification of an existing call to the Pulumi Cloud, then the service should honor older versions of the CLI where this change would not exist. You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add it to the service. --> - [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Cloud API version <!-- @Pulumi employees: If yes, you must submit corresponding changes in the service repo. -->
2024-02-23 18:06:44 +00:00
input := readFile(t, filepath.Join("testdata", tt.prefix+".md"))
Add ability to constrain supported languages of resource and function overlays (#16579) The existing overlays (e.g. Chart v3 in Kubernetes, or CallbackFunction in AWS) are not available in every language Pulumi supports. This often confuses users because the generated docs include all languages Pulumi supports (e.g. see https://github.com/pulumi/pulumi-kubernetes/issues/2181). To solve that problem, this change adds a new optional parameter to the schema that allows configuring the languages an overlay (resource or function) supports. To support this in docsgen the existing Language Chooser (`LangChooserLanguages`) of resources is made configurable and extended to functions. Note: This doesn't support resource methods right now. They'll need extra handling because and overlay resource method might not support all of the languages its resource supports. I'll tackle this in a follow up PR. Here's a screenshot of how this will look like for the Helm v3 chart for example: <img width="1046" alt="Screenshot 2024-07-01 at 16 11 23" src="https://github.com/pulumi/pulumi/assets/2453580/b1a1365a-6dee-4099-829a-2859639a4c8c"> The PR contains the following commits. I'd recommend to look at the first three ones and then check the regenerated golden files in the last one: - **Add schema parameter to constrain supported languages for overlays** - **Update developer docs and changelog** - **Refactor LanguageChooser and always pass supported languages** - **Regenerate testdata** relates to #13231
2024-07-09 14:54:50 +00:00
dctx := newDocGenContext()
actual := dctx.processDescription(input, dctx.getSupportedSnippetLanguages(false, nil)).description
autogold.ExpectFile(t, autogold.Raw(actual))
})
}
Add ability to process docs without dedicated Examples logic (#15475) This pull request depends on corresponding changes in the pulumi-terraform bridge. The bridge will mark up each code section it finds (not just ones found under an "Example Usage" header) with an HTML comment - currently this will be `<!--Begin Code Chooser -->` and `<!--End Code Chooser -->` but the name is subject to change. In this PR, we are adding logic to detect if an incoming Description block has an `{{% examples }}` shortcode markup: if yes, we will process it as before. If no, we will switch to new functionality that relies on documentation with code sections having no short codes, no "Examples Section" logic, and will generate every code section it finds in an incoming schema that is in the Description/Content section of a registry page with a chooser and language choosables. This work is part of https://github.com/pulumi/pulumi-aws/issues/2624. Edit: unit tests are added This PR is a prerequisite for https://github.com/pulumi/pulumi-terraform-bridge/pull/1689. It aims to support both legacy and new behavior. <!--- Thanks so much for your contribution! If this is your first time contributing, please ensure that you have read the [CONTRIBUTING](https://github.com/pulumi/pulumi/blob/master/CONTRIBUTING.md) documentation. --> # Description <!--- Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. --> Fixes # (issue) ## Checklist - [x] I have run `make tidy` to update any new dependencies - [x] I have run `make lint` to verify my code passes the lint check - [x] I have formatted my code using `gofumpt` <!--- Please provide details if the checkbox below is to be left unchecked. --> - [x] I have added tests that prove my fix is effective or that my feature works <!--- User-facing changes require a CHANGELOG entry. --> - [ ] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change <!-- If the change(s) in this PR is a modification of an existing call to the Pulumi Cloud, then the service should honor older versions of the CLI where this change would not exist. You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add it to the service. --> - [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Cloud API version <!-- @Pulumi employees: If yes, you must submit corresponding changes in the service repo. -->
2024-02-23 18:06:44 +00:00
}
func TestDecomposeDocstringDescription(t *testing.T) {
Add ability to process docs without dedicated Examples logic (#15475) This pull request depends on corresponding changes in the pulumi-terraform bridge. The bridge will mark up each code section it finds (not just ones found under an "Example Usage" header) with an HTML comment - currently this will be `<!--Begin Code Chooser -->` and `<!--End Code Chooser -->` but the name is subject to change. In this PR, we are adding logic to detect if an incoming Description block has an `{{% examples }}` shortcode markup: if yes, we will process it as before. If no, we will switch to new functionality that relies on documentation with code sections having no short codes, no "Examples Section" logic, and will generate every code section it finds in an incoming schema that is in the Description/Content section of a registry page with a chooser and language choosables. This work is part of https://github.com/pulumi/pulumi-aws/issues/2624. Edit: unit tests are added This PR is a prerequisite for https://github.com/pulumi/pulumi-terraform-bridge/pull/1689. It aims to support both legacy and new behavior. <!--- Thanks so much for your contribution! If this is your first time contributing, please ensure that you have read the [CONTRIBUTING](https://github.com/pulumi/pulumi/blob/master/CONTRIBUTING.md) documentation. --> # Description <!--- Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. --> Fixes # (issue) ## Checklist - [x] I have run `make tidy` to update any new dependencies - [x] I have run `make lint` to verify my code passes the lint check - [x] I have formatted my code using `gofumpt` <!--- Please provide details if the checkbox below is to be left unchecked. --> - [x] I have added tests that prove my fix is effective or that my feature works <!--- User-facing changes require a CHANGELOG entry. --> - [ ] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change <!-- If the change(s) in this PR is a modification of an existing call to the Pulumi Cloud, then the service should honor older versions of the CLI where this change would not exist. You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add it to the service. --> - [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Cloud API version <!-- @Pulumi employees: If yes, you must submit corresponding changes in the service repo. -->
2024-02-23 18:06:44 +00:00
t.Parallel()
tests := []struct {
prefix string
}{
{"lambda-description"}, // renders code choosers
{"certificate-validation-description"}, // renders legacy shortcode examples
}
for _, tt := range tests {
tt := tt
t.Run(tt.prefix, func(t *testing.T) {
t.Parallel()
input := readFile(t, filepath.Join("testdata", tt.prefix+".md"))
Add ability to constrain supported languages of resource and function overlays (#16579) The existing overlays (e.g. Chart v3 in Kubernetes, or CallbackFunction in AWS) are not available in every language Pulumi supports. This often confuses users because the generated docs include all languages Pulumi supports (e.g. see https://github.com/pulumi/pulumi-kubernetes/issues/2181). To solve that problem, this change adds a new optional parameter to the schema that allows configuring the languages an overlay (resource or function) supports. To support this in docsgen the existing Language Chooser (`LangChooserLanguages`) of resources is made configurable and extended to functions. Note: This doesn't support resource methods right now. They'll need extra handling because and overlay resource method might not support all of the languages its resource supports. I'll tackle this in a follow up PR. Here's a screenshot of how this will look like for the Helm v3 chart for example: <img width="1046" alt="Screenshot 2024-07-01 at 16 11 23" src="https://github.com/pulumi/pulumi/assets/2453580/b1a1365a-6dee-4099-829a-2859639a4c8c"> The PR contains the following commits. I'd recommend to look at the first three ones and then check the regenerated golden files in the last one: - **Add schema parameter to constrain supported languages for overlays** - **Update developer docs and changelog** - **Refactor LanguageChooser and always pass supported languages** - **Regenerate testdata** relates to #13231
2024-07-09 14:54:50 +00:00
dctx := newDocGenContext()
actual := dctx.decomposeDocstring(input, dctx.getSupportedSnippetLanguages(false, nil)).description
autogold.ExpectFile(t, autogold.Raw(actual))
})
}
}
func readFile(t *testing.T, filepath string) string {
inputBytes, err := os.ReadFile(filepath)
Add ability to process docs without dedicated Examples logic (#15475) This pull request depends on corresponding changes in the pulumi-terraform bridge. The bridge will mark up each code section it finds (not just ones found under an "Example Usage" header) with an HTML comment - currently this will be `<!--Begin Code Chooser -->` and `<!--End Code Chooser -->` but the name is subject to change. In this PR, we are adding logic to detect if an incoming Description block has an `{{% examples }}` shortcode markup: if yes, we will process it as before. If no, we will switch to new functionality that relies on documentation with code sections having no short codes, no "Examples Section" logic, and will generate every code section it finds in an incoming schema that is in the Description/Content section of a registry page with a chooser and language choosables. This work is part of https://github.com/pulumi/pulumi-aws/issues/2624. Edit: unit tests are added This PR is a prerequisite for https://github.com/pulumi/pulumi-terraform-bridge/pull/1689. It aims to support both legacy and new behavior. <!--- Thanks so much for your contribution! If this is your first time contributing, please ensure that you have read the [CONTRIBUTING](https://github.com/pulumi/pulumi/blob/master/CONTRIBUTING.md) documentation. --> # Description <!--- Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. --> Fixes # (issue) ## Checklist - [x] I have run `make tidy` to update any new dependencies - [x] I have run `make lint` to verify my code passes the lint check - [x] I have formatted my code using `gofumpt` <!--- Please provide details if the checkbox below is to be left unchecked. --> - [x] I have added tests that prove my fix is effective or that my feature works <!--- User-facing changes require a CHANGELOG entry. --> - [ ] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change <!-- If the change(s) in this PR is a modification of an existing call to the Pulumi Cloud, then the service should honor older versions of the CLI where this change would not exist. You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add it to the service. --> - [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Cloud API version <!-- @Pulumi employees: If yes, you must submit corresponding changes in the service repo. -->
2024-02-23 18:06:44 +00:00
require.NoError(t, err)
return string(inputBytes)
Add ability to process docs without dedicated Examples logic (#15475) This pull request depends on corresponding changes in the pulumi-terraform bridge. The bridge will mark up each code section it finds (not just ones found under an "Example Usage" header) with an HTML comment - currently this will be `<!--Begin Code Chooser -->` and `<!--End Code Chooser -->` but the name is subject to change. In this PR, we are adding logic to detect if an incoming Description block has an `{{% examples }}` shortcode markup: if yes, we will process it as before. If no, we will switch to new functionality that relies on documentation with code sections having no short codes, no "Examples Section" logic, and will generate every code section it finds in an incoming schema that is in the Description/Content section of a registry page with a chooser and language choosables. This work is part of https://github.com/pulumi/pulumi-aws/issues/2624. Edit: unit tests are added This PR is a prerequisite for https://github.com/pulumi/pulumi-terraform-bridge/pull/1689. It aims to support both legacy and new behavior. <!--- Thanks so much for your contribution! If this is your first time contributing, please ensure that you have read the [CONTRIBUTING](https://github.com/pulumi/pulumi/blob/master/CONTRIBUTING.md) documentation. --> # Description <!--- Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. --> Fixes # (issue) ## Checklist - [x] I have run `make tidy` to update any new dependencies - [x] I have run `make lint` to verify my code passes the lint check - [x] I have formatted my code using `gofumpt` <!--- Please provide details if the checkbox below is to be left unchecked. --> - [x] I have added tests that prove my fix is effective or that my feature works <!--- User-facing changes require a CHANGELOG entry. --> - [ ] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change <!-- If the change(s) in this PR is a modification of an existing call to the Pulumi Cloud, then the service should honor older versions of the CLI where this change would not exist. You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add it to the service. --> - [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Cloud API version <!-- @Pulumi employees: If yes, you must submit corresponding changes in the service repo. -->
2024-02-23 18:06:44 +00:00
}