-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add a subcommand into diff processor to detect missing docs (#11156)
- Loading branch information
Showing
8 changed files
with
1,094 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package cmd | ||
|
||
import ( | ||
newProvider "google/provider/new/google/provider" | ||
oldProvider "google/provider/old/google/provider" | ||
"slices" | ||
"sort" | ||
|
||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"os" | ||
|
||
"github.com/GoogleCloudPlatform/magic-modules/tools/diff-processor/detector" | ||
"github.com/GoogleCloudPlatform/magic-modules/tools/diff-processor/diff" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
"github.com/spf13/cobra" | ||
"golang.org/x/exp/maps" | ||
) | ||
|
||
const detectMissingDocDesc = `Compute list of fields missing documents` | ||
|
||
type MissingDocsInfo struct { | ||
Name string | ||
FilePath string | ||
Fields []string | ||
} | ||
|
||
type detectMissingDocsOptions struct { | ||
rootOptions *rootOptions | ||
computeSchemaDiff func() diff.SchemaDiff | ||
newResourceSchema map[string]*schema.Resource | ||
stdout io.Writer | ||
} | ||
|
||
func newDetectMissingDocsCmd(rootOptions *rootOptions) *cobra.Command { | ||
o := &detectMissingDocsOptions{ | ||
rootOptions: rootOptions, | ||
computeSchemaDiff: func() diff.SchemaDiff { | ||
return diff.ComputeSchemaDiff(oldProvider.ResourceMap(), newProvider.ResourceMap()) | ||
}, | ||
stdout: os.Stdout, | ||
} | ||
cmd := &cobra.Command{ | ||
Use: "detect-missing-docs", | ||
Short: detectMissingDocDesc, | ||
Long: detectMissingDocDesc, | ||
Args: cobra.ExactArgs(1), | ||
RunE: func(c *cobra.Command, args []string) error { | ||
return o.run(args) | ||
}, | ||
} | ||
return cmd | ||
} | ||
func (o *detectMissingDocsOptions) run(args []string) error { | ||
schemaDiff := o.computeSchemaDiff() | ||
detectedResources, err := detector.DetectMissingDocs(schemaDiff, args[0], o.newResourceSchema) | ||
if err != nil { | ||
return err | ||
} | ||
resources := maps.Keys(detectedResources) | ||
slices.Sort(resources) | ||
info := []MissingDocsInfo{} | ||
for _, r := range resources { | ||
details := detectedResources[r] | ||
sort.Strings(details.Fields) | ||
info = append(info, MissingDocsInfo{ | ||
Name: r, | ||
FilePath: details.FilePath, | ||
Fields: details.Fields, | ||
}) | ||
} | ||
|
||
if err := json.NewEncoder(o.stdout).Encode(info); err != nil { | ||
return fmt.Errorf("error encoding json: %w", err) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package cmd | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"testing" | ||
|
||
"github.com/GoogleCloudPlatform/magic-modules/tools/diff-processor/diff" | ||
"github.com/google/go-cmp/cmp" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
) | ||
|
||
func TestDetectMissingDocs(t *testing.T) { | ||
cases := []struct { | ||
name string | ||
oldResourceMap map[string]*schema.Resource | ||
newResourceMap map[string]*schema.Resource | ||
want []MissingDocsInfo | ||
}{ | ||
{ | ||
name: "no new fields", | ||
oldResourceMap: map[string]*schema.Resource{ | ||
"google_x": { | ||
Schema: map[string]*schema.Schema{ | ||
"field-a": {Description: "beep", Computed: true, Optional: true}, | ||
"field-b": {Description: "beep", Computed: true}, | ||
}, | ||
}, | ||
}, | ||
newResourceMap: map[string]*schema.Resource{ | ||
"google_x": { | ||
Schema: map[string]*schema.Schema{ | ||
"field-a": {Description: "beep", Computed: true, Optional: true}, | ||
"field-b": {Description: "beep", Computed: true}, | ||
}, | ||
}, | ||
}, | ||
want: []MissingDocsInfo{}, | ||
}, | ||
{ | ||
name: "multiple new fields missing doc", | ||
oldResourceMap: map[string]*schema.Resource{}, | ||
newResourceMap: map[string]*schema.Resource{ | ||
"google_x": { | ||
Schema: map[string]*schema.Schema{ | ||
"field-a": {Description: "beep", Computed: true, Optional: true}, | ||
"field-b": {Description: "beep", Computed: true}, | ||
}, | ||
}, | ||
}, | ||
want: []MissingDocsInfo{ | ||
{ | ||
Name: "google_x", | ||
FilePath: "/website/docs/r/x.html.markdown", | ||
Fields: []string{"field-a", "field-b"}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
for _, tc := range cases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
var buf bytes.Buffer | ||
o := detectMissingDocsOptions{ | ||
computeSchemaDiff: func() diff.SchemaDiff { | ||
return diff.ComputeSchemaDiff(tc.oldResourceMap, tc.newResourceMap) | ||
}, | ||
newResourceSchema: tc.newResourceMap, | ||
stdout: &buf, | ||
} | ||
|
||
err := o.run([]string{t.TempDir()}) | ||
if err != nil { | ||
t.Fatalf("Error running command: %s", err) | ||
} | ||
|
||
out := make([]byte, buf.Len()) | ||
buf.Read(out) | ||
|
||
var got []MissingDocsInfo | ||
if err = json.Unmarshal(out, &got); err != nil { | ||
t.Fatalf("Failed to unmarshall output: %s", err) | ||
} | ||
|
||
if diff := cmp.Diff(tc.want, got); diff != "" { | ||
t.Errorf("Unexpected result. Want %+v, got %+v. ", tc.want, got) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.