Skip to content

Commit

Permalink
refactor into finction and add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
guineveresaenger committed Jul 19, 2024
1 parent 706c567 commit d15f94b
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 28 deletions.
64 changes: 36 additions & 28 deletions pkg/tfgen/installation_docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,34 +26,10 @@ func plainDocsParser(docFile *DocFile, g *Generator) ([]byte, error) {
// - reformat TF names
// - Ability to omit irrelevant sections

// Obtain default edit rules for documentation files
edits := defaultEditRules()
contentBytes := []byte(contentStr)

// Additional edit rules for installation files
installationFileEdits := editRules{
// Replace all "T/terraform" with "P/pulumi"
reReplace(`Terraform`, `Pulumi`),
reReplace(`terraform`, `pulumi`),
// Replace all "H/hashicorp" strings
reReplace(`Hashicorp`, `Pulumi`),
reReplace(`hashicorp`, `pulumi`),
// Reformat certain headers
reReplace(`The following arguments are supported:`,
`The following configuration inputs are supported:`),
reReplace(`Argument Reference`,
`Configuration Reference`),
reReplace(`block contains the following arguments`,
`input has the following nested fields`),
}

edits = append(edits, installationFileEdits...)
var err error
for _, rule := range edits {
contentBytes, err = rule.Edit(docFile.FileName, contentBytes)
if err != nil {
return nil, err
}
// Apply edit rules to transform the doc for Pulumi-ready presentation
contentBytes, err := applyEditRules([]byte(contentStr), docFile)
if err != nil {
return nil, err
}
return contentBytes, nil
}
Expand Down Expand Up @@ -119,3 +95,35 @@ func writeInstallationInstructions(goImportBasePath, providerName string) string
goImportBasePath,
)
}

func applyEditRules(contentBytes []byte, docFile *DocFile) ([]byte, error) {
// Obtain default edit rules for documentation files
edits := defaultEditRules()

// Additional edit rules for installation files
installationFileEdits := editRules{
// Replace all "T/terraform" with "P/pulumi"
reReplace(`Terraform`, `Pulumi`),
reReplace(`terraform`, `pulumi`),
// Replace all "H/hashicorp" strings
reReplace(`Hashicorp`, `Pulumi`),
reReplace(`hashicorp`, `pulumi`),
// Reformat certain headers
reReplace(`The following arguments are supported`,
`The following configuration inputs are supported`),
reReplace(`Argument Reference`,
`Configuration Reference`),
reReplace(`block contains the following arguments`,
`input has the following nested fields`),
}

edits = append(edits, installationFileEdits...)
var err error
for _, rule := range edits {
contentBytes, err = rule.Edit(docFile.FileName, contentBytes)
if err != nil {
return nil, err
}
}
return contentBytes, nil
}
48 changes: 48 additions & 0 deletions pkg/tfgen/installation_docs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,51 @@ func TestWriteIndexFrontMatter(t *testing.T) {
require.Equal(t, tc.expected, actual)
})
}

func TestApplyEditRules(t *testing.T) {
t.Parallel()

type testCase struct {
// The name of the test case.
name string
docFile DocFile
expected []byte
}

tests := []testCase{
{
name: "Replaces h/Hashicorp With p/Pulumi",
docFile: DocFile{
Content: []byte("Any mention of Hashicorp or hashicorp will be Pulumi or pulumi"),
},
expected: []byte("Any mention of Pulumi or pulumi will be Pulumi or pulumi"),
},
{
name: "Replaces t/Terraform With p/Pulumi",
docFile: DocFile{
Content: []byte("Any mention of Terraform or terraform will be Pulumi or pulumi"),
},
expected: []byte("Any mention of Pulumi or pulumi will be Pulumi or pulumi"),
},
{
name: "Replaces argument headers with input headers",
docFile: DocFile{
Content: []byte("# Argument Reference\n" +
"The following arguments are supported:\n* `some_argument`\n" +
"block contains the following arguments"),
},
expected: []byte("#Configuration Reference\n" +
"The following configuration inputs are supported:\n* `some_argument`\n" +
"input has the following nested fields"),
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
actual, err := applyEditRules(tt.docFile.Content, &tt.docFile)
require.NoError(t, err)
require.Equal(t, string(tt.expected), string(actual))
})
}
}

0 comments on commit d15f94b

Please sign in to comment.