Skip to content

Commit

Permalink
Make missing test detector reader into a separate module
Browse files Browse the repository at this point in the history
  • Loading branch information
trodge committed Mar 4, 2024
1 parent ae819d0 commit b7fa6b0
Show file tree
Hide file tree
Showing 13 changed files with 19 additions and 16 deletions.
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 b7fa6b0

Please sign in to comment.