Skip to content

Commit

Permalink
Make missing test detector reader into a separate module (GoogleCloud…
Browse files Browse the repository at this point in the history
  • Loading branch information
trodge authored Mar 5, 2024
1 parent 10450a2 commit 5ad9866
Show file tree
Hide file tree
Showing 14 changed files with 23 additions and 19 deletions.
7 changes: 4 additions & 3 deletions tools/missing-test-detector/detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"sort"
"strings"

"github.com/GoogleCloudPlatform/magic-modules/tools/missing-test-detector/reader"
"github.com/hashicorp/hcl/v2/hclwrite"
"github.com/zclconf/go-cty/cty"
)
Expand All @@ -19,7 +20,7 @@ type FieldSet map[string]struct{}

// Detect missing tests for the given resource changes map in the given slice of tests.
// Return a map of resource names to missing test info about that resource.
func detectMissingTests(changedFields map[string]ResourceChanges, allTests []*Test) (map[string]*MissingTestInfo, error) {
func detectMissingTests(changedFields map[string]ResourceChanges, allTests []*reader.Test) (map[string]*MissingTestInfo, error) {
resourceNamesToTests := make(map[string][]string)
for _, test := range allTests {
for _, step := range test.Steps {
Expand Down Expand Up @@ -51,13 +52,13 @@ func detectMissingTests(changedFields map[string]ResourceChanges, allTests []*Te
return missingTests, nil
}

func markCoverage(fieldCoverage ResourceChanges, config Resource) error {
func markCoverage(fieldCoverage ResourceChanges, config reader.Resource) error {
for fieldName, fieldValue := range config {
if coverage, ok := fieldCoverage[fieldName]; ok {
if field, ok := coverage.(*Field); ok {
field.Tested = true
} else if objectCoverage, ok := coverage.(ResourceChanges); ok {
if fieldValueConfig, ok := fieldValue.(Resource); ok {
if fieldValueConfig, ok := fieldValue.(reader.Resource); ok {
if err := markCoverage(objectCoverage, fieldValueConfig); err != nil {
return fmt.Errorf("error parsing %q: %s", fieldName, err)
}
Expand Down
4 changes: 3 additions & 1 deletion tools/missing-test-detector/detector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package main
import (
"reflect"
"testing"

"github.com/GoogleCloudPlatform/magic-modules/tools/missing-test-detector/reader"
)

func TestDetectMissingTests(t *testing.T) {
allTests, errs := readAllTests("testdata")
allTests, errs := reader.ReadAllTests("reader/testdata")
if len(errs) > 0 {
t.Errorf("errors reading tests before testing detect missing tests: %v", errs)
}
Expand Down
2 changes: 1 addition & 1 deletion tools/missing-test-detector/go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/trodge/magic-modules/tools/missing-test-detector
module github.com/GoogleCloudPlatform/magic-modules/tools/missing-test-detector

go 1.20

Expand Down
3 changes: 2 additions & 1 deletion tools/missing-test-detector/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strings"
"text/template"

"github.com/GoogleCloudPlatform/magic-modules/tools/missing-test-detector/reader"
"github.com/golang/glog"
)

Expand All @@ -15,7 +16,7 @@ var flagServicesDir = flag.String("services-dir", "", "directory where service d
func main() {
flag.Parse()

allTests, errs := readAllTests(*flagServicesDir)
allTests, errs := reader.ReadAllTests(*flagServicesDir)
for path, err := range errs {
glog.Infof("error reading path: %s, err: %v", path, err)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package reader

import (
"fmt"
Expand Down Expand Up @@ -33,7 +33,7 @@ func (t *Test) String() string {
}

// Return a slice of tests as well as a map of file or test names to errors encountered.
func readAllTests(servicesDir string) ([]*Test, map[string]error) {
func ReadAllTests(servicesDir string) ([]*Test, map[string]error) {
dirs, err := os.ReadDir(servicesDir)
if err != nil {
return nil, map[string]error{servicesDir: err}
Expand All @@ -52,7 +52,7 @@ func readAllTests(servicesDir string) ([]*Test, map[string]error) {
testFileNames = append(testFileNames, filepath.Join(servicePath, file.Name()))
}
}
serviceTests, serviceErrs := readTestFiles(testFileNames)
serviceTests, serviceErrs := ReadTestFiles(testFileNames)
for fileName, err := range serviceErrs {
allErrs[fileName] = err
}
Expand All @@ -65,7 +65,7 @@ func readAllTests(servicesDir string) ([]*Test, map[string]error) {
}

// Read all the test files in a service directory together to capture cross-file function usage.
func readTestFiles(filenames []string) ([]*Test, map[string]error) {
func ReadTestFiles(filenames []string) ([]*Test, map[string]error) {
funcDecls := make(map[string]*ast.FuncDecl) // map of function names to function declarations
varDecls := make(map[string]*ast.BasicLit) // map of variable names to value expressions
errs := make(map[string]error) // map of file or test names to errors encountered parsing
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package reader

import (
"os"
Expand All @@ -9,7 +9,7 @@ import (
// This test only ensures there isn't a panic reading tests in the provider.
func TestReadAllTests(t *testing.T) {
if servicesDir := os.Getenv("SERVICES_DIR"); servicesDir != "" {
_, errs := readAllTests(servicesDir)
_, errs := ReadAllTests(servicesDir)
for path, err := range errs {
t.Logf("path: %s, err: %v", path, err)
}
Expand All @@ -19,7 +19,7 @@ func TestReadAllTests(t *testing.T) {
}

func TestReadCoveredResourceTestFile(t *testing.T) {
tests, err := readTestFiles([]string{"testdata/service/covered_resource_test.go"})
tests, err := ReadTestFiles([]string{"testdata/service/covered_resource_test.go"})
if err != nil {
t.Fatalf("error reading covered resource test file: %v", err)
}
Expand Down Expand Up @@ -47,7 +47,7 @@ func TestReadCoveredResourceTestFile(t *testing.T) {
}

func TestReadConfigVariableTestFile(t *testing.T) {
tests, err := readTestFiles([]string{"testdata/service/config_variable_test.go"})
tests, err := ReadTestFiles([]string{"testdata/service/config_variable_test.go"})
if err != nil {
t.Fatalf("error reading config variable test file: %v", err)
}
Expand All @@ -67,7 +67,7 @@ func TestReadConfigVariableTestFile(t *testing.T) {
}

func TestReadMultipleResourcesTestFile(t *testing.T) {
tests, err := readTestFiles([]string{"testdata/service/multiple_resource_test.go"})
tests, err := ReadTestFiles([]string{"testdata/service/multiple_resource_test.go"})
if err != nil {
t.Fatalf("error reading multiple resources test file: %v", err)
}
Expand Down Expand Up @@ -101,7 +101,7 @@ func TestReadMultipleResourcesTestFile(t *testing.T) {
}

func TestReadSerialResourceTestFile(t *testing.T) {
tests, err := readTestFiles([]string{"testdata/service/serial_resource_test.go"})
tests, err := ReadTestFiles([]string{"testdata/service/serial_resource_test.go"})
if err != nil {
t.Fatalf("error reading serial resource test file: %v", err)
}
Expand Down Expand Up @@ -140,7 +140,7 @@ func TestReadSerialResourceTestFile(t *testing.T) {
}

func TestReadCrossFileTests(t *testing.T) {
tests, err := readTestFiles([]string{"testdata/service/cross_file_1_test.go", "testdata/service/cross_file_2_test.go"})
tests, err := ReadTestFiles([]string{"testdata/service/cross_file_1_test.go", "testdata/service/cross_file_2_test.go"})
if err != nil {
t.Fatalf("error reading cross file tests: %v", err)
}
Expand Down Expand Up @@ -183,7 +183,7 @@ func TestReadCrossFileTests(t *testing.T) {
}

func TestReadHelperFunctionCall(t *testing.T) {
tests, err := readTestFiles([]string{"testdata/service/function_call_test.go"})
tests, err := ReadTestFiles([]string{"testdata/service/function_call_test.go"})
if err != nil {
t.Fatalf("error reading function call test: %v", err)
}
Expand All @@ -193,7 +193,7 @@ func TestReadHelperFunctionCall(t *testing.T) {
expectedTest := &Test{
Name: "TestAccFunctionCallResource",
Steps: []Step{
Step{
{
"helped_resource": Resources{
"primary": Resource{
"field_one": "\"value-one\"",
Expand Down

0 comments on commit 5ad9866

Please sign in to comment.