Skip to content

Commit

Permalink
fix test case to separate datasource and resource schema diff
Browse files Browse the repository at this point in the history
  • Loading branch information
iyabchen committed Oct 18, 2024
1 parent 99f6585 commit 3df2265
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 40 deletions.
50 changes: 20 additions & 30 deletions tools/diff-processor/cmd/detect_missing_docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,44 +59,16 @@ func (o *detectMissingDocsOptions) run(args []string) error {
if err != nil {
return err
}
resources := maps.Keys(detectedResources)
slices.Sort(resources)
resourceInfo := []detector.MissingDocDetails{}
for _, r := range resources {
details := detectedResources[r]
sort.Slice(details.Fields, func(i, j int) bool {
return details.Fields[i].Field < details.Fields[j].Field
})
resourceInfo = append(resourceInfo, detector.MissingDocDetails{
Name: r,
FilePath: details.FilePath,
Fields: details.Fields,
})
}

datasourceSchemaDiff := o.computeDatasourceSchemaDiff()
detectedDataSources, err := detector.DetectMissingDocsForDatasource(datasourceSchemaDiff, args[0])
if err != nil {
return err
}
dataSources := maps.Keys(detectedDataSources)
slices.Sort(dataSources)
dataSourceInfo := []detector.MissingDocDetails{}
for _, r := range resources {
details := detectedDataSources[r]
sort.Slice(details.Fields, func(i, j int) bool {
return details.Fields[i].Field < details.Fields[j].Field
})
dataSourceInfo = append(dataSourceInfo, detector.MissingDocDetails{
Name: r,
FilePath: details.FilePath,
Fields: details.Fields,
})
}

sum := MissingDocsSummary{
Resource: resourceInfo,
DataSource: dataSourceInfo,
Resource: sortMissingDocDetails(detectedResources),
DataSource: sortMissingDocDetails(detectedDataSources),
}

if err := json.NewEncoder(o.stdout).Encode(sum); err != nil {
Expand All @@ -105,3 +77,21 @@ func (o *detectMissingDocsOptions) run(args []string) error {

return nil
}

func sortMissingDocDetails(m map[string]detector.MissingDocDetails) []detector.MissingDocDetails {
itemNames := maps.Keys(m)
slices.Sort(itemNames)
arr := []detector.MissingDocDetails{}
for _, itemName := range itemNames {
details := m[itemName]
sort.Slice(details.Fields, func(i, j int) bool {
return details.Fields[i].Field < details.Fields[j].Field
})
arr = append(arr, detector.MissingDocDetails{
Name: itemName,
FilePath: details.FilePath,
Fields: details.Fields,
})
}
return arr
}
27 changes: 17 additions & 10 deletions tools/diff-processor/cmd/detect_missing_docs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ import (

func TestDetectMissingDocs(t *testing.T) {
cases := []struct {
name string
oldResourceMap map[string]*schema.Resource
newResourceMap map[string]*schema.Resource
want MissingDocsSummary
name string
oldResourceMap map[string]*schema.Resource
newResourceMap map[string]*schema.Resource
oldDataSourceMap map[string]*schema.Resource
newDataSourceMap map[string]*schema.Resource
want MissingDocsSummary
}{
{
name: "no new fields",
Expand Down Expand Up @@ -53,6 +55,14 @@ func TestDetectMissingDocs(t *testing.T) {
},
},
oldResourceMap: map[string]*schema.Resource{},
newDataSourceMap: map[string]*schema.Resource{
"google_data_y": {
Schema: map[string]*schema.Schema{
"field-a": {Description: "beep"},
},
},
},
oldDataSourceMap: map[string]*schema.Resource{},
want: MissingDocsSummary{
Resource: []detector.MissingDocDetails{
{
Expand All @@ -72,15 +82,12 @@ func TestDetectMissingDocs(t *testing.T) {
},
DataSource: []detector.MissingDocDetails{
{
Name: "google_x",
FilePath: "/website/docs/d/x.html.markdown",
Name: "google_data_y",
FilePath: "/website/docs/d/data_y.html.markdown",
Fields: []detector.MissingDocField{
{
Field: "field-a",
},
{
Field: "field-b",
},
},
},
},
Expand All @@ -96,7 +103,7 @@ func TestDetectMissingDocs(t *testing.T) {
return diff.ComputeSchemaDiff(tc.oldResourceMap, tc.newResourceMap)
},
computeDatasourceSchemaDiff: func() diff.SchemaDiff {
return diff.ComputeSchemaDiff(tc.oldResourceMap, tc.newResourceMap)
return diff.ComputeSchemaDiff(tc.oldDataSourceMap, tc.newDataSourceMap)
},
stdout: &buf,
}
Expand Down
1 change: 1 addition & 0 deletions tools/diff-processor/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func newRootCmd() (*cobra.Command, *rootOptions, error) {
cmd.AddCommand(newBreakingChangesCmd(o))
cmd.AddCommand(newChangedSchemaResourcesCmd(o))
cmd.AddCommand(newDetectMissingTestsCmd(o))
cmd.AddCommand(newDetectMissingDocsCmd(o))
return cmd, o, nil
}

Expand Down

0 comments on commit 3df2265

Please sign in to comment.