-
Notifications
You must be signed in to change notification settings - Fork 875
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
Generate re-usable functional test library [WiP] #6533
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
*.png filter=lfs diff=lfs merge=lfs -text | ||
*.jpg filter=lfs diff=lfs merge=lfs -text | ||
tests/testlib/* linguist-generated=true | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,220 @@ | ||
// The MIT License | ||
// | ||
// Copyright (c) 2020 Temporal Technologies Inc. All rights reserved. | ||
// | ||
// Copyright (c) 2020 Uber Technologies, Inc. | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in | ||
// all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
// THE SOFTWARE. | ||
|
||
package main | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"log" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
"regexp" | ||
"sort" | ||
"strings" | ||
"text/template" | ||
) | ||
|
||
var structRegex = regexp.MustCompile(`([A-Za-z0-9_]+Suite)\s+struct`) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Intentionally includes lower-case suite names: the generated |
||
|
||
const ( | ||
testFileSuffix = "_test.go" | ||
genHeader = "// Code generated by `make gentestsuites`. DO NOT EDIT.\n\n" | ||
outputDirName = "testlib" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Happy to consider a different name here. |
||
outputFile = "suites_gen.go" | ||
) | ||
|
||
func main() { | ||
if len(os.Args) != 2 { | ||
log.Fatalf("Usage: gentestsuites <directory>") | ||
} | ||
path := os.Args[1] | ||
|
||
srcDir, err := filepath.Abs(path) | ||
if err != nil { | ||
log.Fatalf("Error resolving file path: %v", err) | ||
} | ||
fmt.Printf("Scanning: %s\n", srcDir) | ||
|
||
outDir := filepath.Join(srcDir, outputDirName) | ||
if err = os.Mkdir(outDir, 0777); err != nil { | ||
log.Fatalf("Error creating output dir: %v", err) | ||
} | ||
|
||
if err = copyFiles(srcDir, outDir); err != nil { | ||
log.Fatalf("Error copying files: %v", err) | ||
} | ||
|
||
suites, err := findSuites(srcDir) | ||
if err != nil { | ||
log.Fatalf("Error finding suites: %v", err) | ||
} | ||
if len(suites) == 0 { | ||
log.Fatalf("No suites found") | ||
} | ||
|
||
pkg, err := findPackage(srcDir) | ||
if err != nil { | ||
log.Fatalf("Error finding package: %v", err) | ||
} | ||
|
||
if err = generateSuitesFile(srcDir, pkg, suites); err != nil { | ||
log.Fatalf("Error generating suite file: %v", err) | ||
} | ||
} | ||
|
||
func copyFiles(srcDir, outDir string) error { | ||
files, err := os.ReadDir(srcDir) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for _, file := range files { | ||
if file.IsDir() { | ||
continue | ||
} | ||
|
||
srcFilePath := filepath.Join(srcDir, file.Name()) | ||
destFileName := strings.TrimSuffix(file.Name(), testFileSuffix) + ".go" | ||
destFilePath := filepath.Join(outDir, destFileName) | ||
|
||
err = copyFile(srcFilePath, destFilePath) | ||
if err != nil { | ||
return err | ||
} | ||
fmt.Printf("Generated: %s\n", destFilePath) | ||
} | ||
return err | ||
} | ||
|
||
func copyFile(src, dst string) error { | ||
srcFile, err := os.Open(src) | ||
if err != nil { | ||
return err | ||
} | ||
defer srcFile.Close() | ||
|
||
destFile, err := os.Create(dst) | ||
if err != nil { | ||
return err | ||
} | ||
defer destFile.Close() | ||
|
||
_, err = destFile.WriteString(genHeader) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
_, err = io.Copy(destFile, srcFile) | ||
return err | ||
} | ||
|
||
func findSuites(dir string) ([]string, error) { | ||
var suites []string | ||
|
||
files, err := os.ReadDir(dir) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
for _, file := range files { | ||
if file.IsDir() || !strings.HasSuffix(file.Name(), testFileSuffix) { | ||
continue | ||
} | ||
|
||
content, err := os.ReadFile(filepath.Join(dir, file.Name())) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
matches := structRegex.FindAllStringSubmatch(string(content), -1) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Simple approach that can miss suites that don't match the regex. If that risk is unacceptable, we could also parse the Go files and search for suites by type. |
||
if matches != nil { | ||
for _, match := range matches { | ||
suite := match[1] | ||
fmt.Printf("Found suite: %s\n", suite) | ||
suites = append(suites, suite) | ||
} | ||
} | ||
} | ||
|
||
sort.Strings(suites) | ||
return suites, nil | ||
} | ||
|
||
func findPackage(path string) (string, error) { | ||
cmd := exec.Command("go", "list", path) | ||
listOutput, err := cmd.Output() | ||
if err != nil { | ||
return "", err | ||
} | ||
return strings.TrimSpace(string(listOutput)), nil | ||
} | ||
|
||
func generateSuitesFile(dir, pkg string, suites []string) error { | ||
outputPath := filepath.Join(dir, outputFile) | ||
f, err := os.Create(outputPath) | ||
if err != nil { | ||
return err | ||
} | ||
defer f.Close() | ||
|
||
packagePaths := strings.Split(strings.TrimSpace(string(pkg)), "/") | ||
packageName := packagePaths[len(packagePaths)-1] | ||
importPackagePath := fmt.Sprintf("%s/%s", pkg, outputDirName) | ||
|
||
tmpl := ` | ||
{{- printf "%s" .Header }}package {{ .PackageName }} | ||
|
||
import ( | ||
"flag" | ||
"testing" | ||
"github.com/stretchr/testify/suite" | ||
lib "{{ .ImportPackagePath }}" | ||
) | ||
|
||
var Suites = []suite.TestingSuite{ | ||
{{- range .Suites }} | ||
new(lib.{{ . }}), | ||
{{- end }} | ||
} | ||
|
||
func TestAll(t *testing.T) { | ||
flag.Parse() | ||
for _, s := range Suites { | ||
suite.Run(t, s) | ||
} | ||
}` | ||
if err = template.Must(template.New("code").Parse(tmpl)).Execute(f, map[string]any{ | ||
"Header": genHeader, | ||
"ImportPackagePath": importPackagePath, | ||
"PackageName": packageName, | ||
"Suites": suites, | ||
}); err != nil { | ||
return err | ||
} | ||
|
||
fmt.Printf("Generated: %s\n", outputPath) | ||
return nil | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Prevent polluting PRs with generated files.