Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not render empty code choosers on generated index page #2822

Merged
merged 7 commits into from
Jan 14, 2025

Conversation

guineveresaenger
Copy link
Contributor

@guineveresaenger guineveresaenger commented Jan 10, 2025

This pull request does the following:

  1. Prevents the generation of code choosers with empty content
  2. Writes a YAML config file with no code choosers if the example is config-only
  3. Checks the pclExample object for empty pulumi YAML or PCL explicitly and before attempting to convert, removing the empty string check from the converter functions
  4. Adds tests to illustrate all four situations:
    • valid provider config + example
    • invalid code
    • valid provider config but no example
    • valid example but no provider config
  5. Adds exampleUnavailable placeholder constant for use when conversion fails on an individual language
  6. Adds a successfulConversion flag that flips to True as soon as any one language registers as having content other than the exampleUnavailable message. The idea is that if even only one pulumi language converted, we want to display the code chooser.

Fixes #2819

Copy link

codecov bot commented Jan 10, 2025

Codecov Report

Attention: Patch coverage is 85.45455% with 8 lines in your changes missing coverage. Please review.

Project coverage is 68.69%. Comparing base (c158e94) to head (b41002c).
Report is 9 commits behind head on master.

Files with missing lines Patch % Lines
pkg/tfgen/convert_cli.go 0.00% 3 Missing and 1 partial ⚠️
pkg/tfgen/installation_docs.go 92.15% 3 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #2822      +/-   ##
==========================================
+ Coverage   68.65%   68.69%   +0.04%     
==========================================
  Files         325      325              
  Lines       41537    41621      +84     
==========================================
+ Hits        28516    28593      +77     
- Misses      11418    11422       +4     
- Partials     1603     1606       +3     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

pkg/tfgen/installation_docs.go Outdated Show resolved Hide resolved

err := ast.Walk(astNode, func(n ast.Node, entering bool) (ast.WalkStatus, error) {
if section, ok := n.(*section.Section); ok && entering {
sectionText := section.Text([]byte(contentStr))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of converting to text and then checking if nothing came out, can we do this with a more structured check:

A section is empty if it has one child (the title). We can then get the content of the title and validate that.

pkg/tfgen/installation_docs.go Outdated Show resolved Hide resolved
Comment on lines +3 to +6
## Example Usage



Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought a goal of this PR was to remove empty ## Example Usage sections like this.

Copy link
Contributor Author

@guineveresaenger guineveresaenger Jan 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes! This test is only for the code conversion. We would still have an empty Examples section after code conversion.

@guineveresaenger guineveresaenger force-pushed the guin/fix-empty-code-choosers branch from 05fe998 to eb4af28 Compare January 10, 2025 18:18
err := ast.Walk(astNode, func(n ast.Node, entering bool) (ast.WalkStatus, error) {
if section, ok := n.(*section.Section); ok && entering {
sectionText := section.Text(contentBytes)
// A little confusingly, we check if the section text _only_ contains the title, "Example Usage".
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// A little confusingly, we check if the section text _only_ contains the title, "Example Usage".
// A little confusingly, we check if the section text _only_ contains the title.

Comment on lines 654 to 660
if diags.HasErrors() {
source = "Example currently unavailable in this language\n"
err = fmt.Errorf("failed to convert an example: %s", diags.Error())
err = fmt.Errorf("conversion errors: %s", diags.Error())
}

if source == "" {
source = exampleUnavailable
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand why this change makes sense:

If there are no errors (!diag.HasErrors()), but source == "", should we really set source = exampleUnavailable.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two reasons:

  1. It is possible for diag to have no errors (for example, when the input is empty), which results in source being empty. We can return an empty string, or declare that we currently have no example.

  2. It is also possible for diag to have errors but the conversion being successful regardless.

Therefore it makes more sense to check for "what are we looking to display to the user" and if it comes up empty, say that.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you put that in a comment on if source == "" {?

@iwahbe iwahbe self-requested a review January 13, 2025 17:48
@guineveresaenger guineveresaenger enabled auto-merge (squash) January 14, 2025 01:14
@guineveresaenger guineveresaenger merged commit 3ca48ac into master Jan 14, 2025
17 checks passed
@guineveresaenger guineveresaenger deleted the guin/fix-empty-code-choosers branch January 14, 2025 02:37
@pulumi-bot
Copy link
Contributor

This PR has been shipped in release v3.102.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Ensure that an empty block doesn't have a chooser, ideally rolling up empty heading blocks
3 participants