[codegen/docs] Sort the package tree items (#8262)
* Sort the modules, resources and functions individually as the package tree is created * Update package tree test
This commit is contained in:
parent
cb51313512
commit
d73c380bef
|
@ -19,6 +19,7 @@
|
|||
package docs
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/pulumi/pulumi/pkg/v3/codegen/internal/test"
|
||||
"github.com/pulumi/pulumi/pkg/v3/codegen/schema"
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
@ -127,7 +128,95 @@ func initTestPackageSpec(t *testing.T) {
|
|||
},
|
||||
},
|
||||
},
|
||||
Provider: schema.ResourceSpec{
|
||||
ObjectTypeSpec: schema.ObjectTypeSpec{
|
||||
Description: fmt.Sprintf("The provider type for the %s package.", providerPackage),
|
||||
Type: "object",
|
||||
},
|
||||
InputProperties: map[string]schema.PropertySpec{
|
||||
"stringProp": {
|
||||
Description: "A stringProp for the provider resource.",
|
||||
TypeSpec: schema.TypeSpec{
|
||||
Type: "string",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
Resources: map[string]schema.ResourceSpec{
|
||||
"prov:module2/resource2:Resource2": {
|
||||
ObjectTypeSpec: schema.ObjectTypeSpec{
|
||||
Description: `This is a module-level resource called Resource.
|
||||
{{% examples %}}
|
||||
## Example Usage
|
||||
|
||||
{{% example %}}
|
||||
### Basic Example
|
||||
|
||||
` + codeFence + `typescript
|
||||
// Some TypeScript code.
|
||||
` + codeFence + `
|
||||
` + codeFence + `python
|
||||
# Some Python code.
|
||||
` + codeFence + `
|
||||
{{% /example %}}
|
||||
{{% example %}}
|
||||
### Custom Sub-Domain Example
|
||||
|
||||
` + codeFence + `typescript
|
||||
// Some typescript code
|
||||
` + codeFence + `
|
||||
` + codeFence + `python
|
||||
# Some Python code.
|
||||
` + codeFence + `
|
||||
{{% /example %}}
|
||||
{{% /examples %}}
|
||||
|
||||
## Import
|
||||
|
||||
The import docs would be here
|
||||
|
||||
` + codeFence + `sh
|
||||
$ pulumi import prov:module/resource:Resource test test
|
||||
` + codeFence + `
|
||||
`,
|
||||
},
|
||||
InputProperties: map[string]schema.PropertySpec{
|
||||
"integerProp": {
|
||||
Description: "This is integerProp's description.",
|
||||
TypeSpec: schema.TypeSpec{
|
||||
Type: "integer",
|
||||
},
|
||||
},
|
||||
"stringProp": {
|
||||
Description: "This is stringProp's description.",
|
||||
TypeSpec: schema.TypeSpec{
|
||||
Type: "string",
|
||||
},
|
||||
},
|
||||
"boolProp": {
|
||||
Description: "A bool prop.",
|
||||
TypeSpec: schema.TypeSpec{
|
||||
Type: "boolean",
|
||||
},
|
||||
},
|
||||
"optionsProp": {
|
||||
TypeSpec: schema.TypeSpec{
|
||||
Ref: "#/types/prov:module/ResourceOptions:ResourceOptions",
|
||||
},
|
||||
},
|
||||
"options2Prop": {
|
||||
TypeSpec: schema.TypeSpec{
|
||||
Ref: "#/types/prov:module/ResourceOptions2:ResourceOptions2",
|
||||
},
|
||||
},
|
||||
"recursiveType": {
|
||||
Description: "I am a recursive type.",
|
||||
TypeSpec: schema.TypeSpec{
|
||||
Ref: "#/types/prov:module/ResourceOptions:ResourceOptions",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
"prov:module/resource:Resource": {
|
||||
ObjectTypeSpec: schema.ObjectTypeSpec{
|
||||
Description: `This is a module-level resource called Resource.
|
||||
|
|
|
@ -2,6 +2,7 @@ package docs
|
|||
|
||||
import (
|
||||
"github.com/pkg/errors"
|
||||
"sort"
|
||||
)
|
||||
|
||||
type entryType string
|
||||
|
@ -47,6 +48,9 @@ func generatePackageTree(rootMod modContext) ([]PackageTreeItem, error) {
|
|||
|
||||
packageTree = append(packageTree, ti)
|
||||
}
|
||||
sort.Slice(packageTree, func(i, j int) bool {
|
||||
return packageTree[i].Name < packageTree[j].Name
|
||||
})
|
||||
|
||||
for _, r := range rootMod.resources {
|
||||
name := resourceName(r)
|
||||
|
@ -59,6 +63,15 @@ func generatePackageTree(rootMod modContext) ([]PackageTreeItem, error) {
|
|||
|
||||
packageTree = append(packageTree, ti)
|
||||
}
|
||||
sort.SliceStable(packageTree, func(i, j int) bool {
|
||||
pti, ptj := packageTree[i], packageTree[j]
|
||||
switch {
|
||||
case pti.Type != ptj.Type:
|
||||
return pti.Type == entryTypeModule && ptj.Type != entryTypeModule
|
||||
default:
|
||||
return pti.Name < ptj.Name
|
||||
}
|
||||
})
|
||||
|
||||
for _, f := range rootMod.functions {
|
||||
name := tokenToName(f.Token)
|
||||
|
@ -71,5 +84,15 @@ func generatePackageTree(rootMod modContext) ([]PackageTreeItem, error) {
|
|||
|
||||
packageTree = append(packageTree, ti)
|
||||
}
|
||||
sort.SliceStable(packageTree, func(i, j int) bool {
|
||||
pti, ptj := packageTree[i], packageTree[j]
|
||||
switch {
|
||||
case pti.Type != ptj.Type:
|
||||
return (pti.Type == entryTypeModule || pti.Type == entryTypeResource) &&
|
||||
(ptj.Type != entryTypeModule && ptj.Type != entryTypeResource)
|
||||
default:
|
||||
return pti.Name < ptj.Name
|
||||
}
|
||||
})
|
||||
return packageTree, nil
|
||||
}
|
||||
|
|
|
@ -39,9 +39,18 @@ func TestGeneratePackageTree(t *testing.T) {
|
|||
|
||||
t.Run("ValidatePackageTreeTopLevelItems", func(t *testing.T) {
|
||||
assert.Equal(t, entryTypeModule, pkgTree[0].Type)
|
||||
assert.Equal(t, entryTypeResource, pkgTree[1].Type)
|
||||
assert.Equal(t, entryTypeModule, pkgTree[1].Type)
|
||||
assert.Equal(t, entryTypeResource, pkgTree[2].Type)
|
||||
assert.Equal(t, entryTypeFunction, pkgTree[3].Type)
|
||||
assert.Equal(t, entryTypeResource, pkgTree[3].Type)
|
||||
assert.Equal(t, entryTypeFunction, pkgTree[4].Type)
|
||||
})
|
||||
|
||||
t.Run("ValidateSortOrder", func(t *testing.T) {
|
||||
assert.Equal(t, "module", pkgTree[0].Name)
|
||||
assert.Equal(t, "module2", pkgTree[1].Name)
|
||||
assert.Equal(t, "PackageLevelResource", pkgTree[2].Name)
|
||||
assert.Equal(t, "Provider", pkgTree[3].Name)
|
||||
assert.Equal(t, "getPackageResource", pkgTree[4].Name)
|
||||
})
|
||||
|
||||
t.Run("ValidatePackageTreeModuleChildren", func(t *testing.T) {
|
||||
|
|
Loading…
Reference in a new issue