Skip to content

Commit

Permalink
a
Browse files Browse the repository at this point in the history
  • Loading branch information
Markus Westerlind committed May 10, 2022
1 parent 0442094 commit 77141a2
Showing 1 changed file with 125 additions and 1 deletion.
126 changes: 125 additions & 1 deletion stdlib/testing.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
package stdlib

import ast "github.com/influxdata/flux/ast"
import (
"fmt"
"io/ioutil"
"path"
"path/filepath"
"strings"

ast "github.com/influxdata/flux/ast"
"github.com/influxdata/flux/codes"
"github.com/influxdata/flux/internal/errors"
"github.com/influxdata/flux/internal/token"
"github.com/influxdata/flux/parser"
)

// TestingRunCalls constructs an ast.File that calls testing.run for each test case within the package.
func TestingRunCalls(pkg *ast.Package) *ast.File {
Expand Down Expand Up @@ -60,3 +72,115 @@ func (v testStmtVisitor) Visit(node ast.Node) ast.Visitor {
}

func (v testStmtVisitor) Done(node ast.Node) {}

/// Scans `rootDir` for all packages that contain `testcase` statements and returns them
func FindTestPackages(rootDir string) ([]*ast.Package, error) {
var testPackages []*ast.Package
pkgName := "github.com/influxdata/flux/stdlib"
err := walkDirs(rootDir, func(dir string) error {
// Determine the absolute flux package path
fluxPath, err := filepath.Rel(rootDir, dir)
if err != nil {
return err
}

fset := new(token.FileSet)
pkgs, err := parser.ParseDir(fset, dir)
if err != nil {
return err
}

// Annotate the packages with the absolute flux package path.
for _, pkg := range pkgs {
pkg.Path = fluxPath
}

var testPkg *ast.Package
switch len(pkgs) {
case 0:
return nil
case 1:
for k, v := range pkgs {
if strings.HasSuffix(k, "_test") {
testPkg = v
}
}
case 2:
for k, v := range pkgs {
if strings.HasSuffix(k, "_test") {
testPkg = v
continue
}
}
if testPkg == nil {
return fmt.Errorf("cannot have two distinct non-test Flux packages in the same directory")
}
default:
keys := make([]string, 0, len(pkgs))
for k := range pkgs {
keys = append(keys, k)
}
return fmt.Errorf("found more than 2 flux packages in directory %s; packages %v", dir, keys)
}

if testPkg != nil {
// Strip out test files with the testcase statement.
validFiles := []*ast.File{}
for _, file := range testPkg.Files {
valid := true
for _, item := range file.Body {
if _, ok := item.(*ast.TestCaseStatement); ok {
valid = false
}
}
if valid {
validFiles = append(validFiles, file)
}
}
if len(validFiles) < len(testPkg.Files) {
testPkg.Files = validFiles
}

if ast.Check(testPkg) > 0 {
return errors.Wrapf(ast.GetError(testPkg), codes.Inherit, "failed to parse test package %q", testPkg.Package)
}
// Validate test package file use _test.flux suffix for the file name
for _, f := range testPkg.Files {
if !strings.HasSuffix(f.Name, "_test.flux") {
return fmt.Errorf("flux test files must use the _test.flux suffix in their file name, found %q", path.Join(dir, f.Name))
}
}
// Track go import path
importPath := path.Join(pkgName, dir)
if importPath != pkgName {
testPackages = append(testPackages, testPkg)
}

}
return nil
})
if err != nil {
return nil, err
}

return testPackages, nil
}

func walkDirs(path string, f func(dir string) error) error {
files, err := ioutil.ReadDir(path)
if err != nil {
return err
}
if err := f(path); err != nil {
return err
}

for _, file := range files {
if file.IsDir() {
if err := walkDirs(filepath.Join(path, file.Name()), f); err != nil {
return err
}
}
}
return nil
}

0 comments on commit 77141a2

Please sign in to comment.