Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make missing test detector reader into a separate module #10115

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading