Skip to content

Commit

Permalink
Add support for running specific test suite(s) (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
kkajla12 authored Oct 12, 2023
1 parent 61670f7 commit 9208da8
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
19 changes: 15 additions & 4 deletions cmd/apirunner/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,33 @@ import (
"fmt"
"os"
"path/filepath"
"regexp"

"github.com/warrant-dev/apirunner"
)

func main() {
if len(os.Args) < 2 || len(os.Args) > 3 {
if len(os.Args) < 2 || len(os.Args) > 4 {
fmt.Printf("Invalid args")
os.Exit(1)
}
testDir := os.Args[1]
testFilenameMatchRegex := regexp.MustCompile(".*")
if len(os.Args) > 2 {
var err error
testFilenameMatchRegex, err = regexp.Compile(os.Args[2])
if err != nil {
fmt.Printf("Invalid test name match regex: %v\n", err)
os.Exit(1)
}
}

configFile := filepath.Join(testDir, "apirunner.conf")
if len(os.Args) == 3 {
if len(os.Args) == 4 {
// If configFile passed in, use that
configFile = os.Args[2]
configFile = os.Args[3]
}
passed, err := apirunner.Run(configFile, testDir)
passed, err := apirunner.Run(configFile, testDir, testFilenameMatchRegex)
if err != nil {
fmt.Printf("Error executing tests: %v\n", err)
os.Exit(1)
Expand Down
7 changes: 4 additions & 3 deletions run.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"net/http"
"os"
"path/filepath"
"regexp"
"strings"
"time"

Expand All @@ -33,8 +34,8 @@ type RunConfig struct {
HttpClient HttpClient
}

// Execute all test files in 'testDir'. Returns true if all tests pass, false otherwise (including on err)
func Run(runConfigFilename string, testDir string) (bool, error) {
// Run executes all test files in 'testDir'. Returns true if all tests pass, false otherwise (including on err)
func Run(runConfigFilename string, testDir string, testFilenameMatchRegex *regexp.Regexp) (bool, error) {
// Load and validate RunConfig
configFile, err := os.Open(runConfigFilename)
if err != nil {
Expand All @@ -60,7 +61,7 @@ func Run(runConfigFilename string, testDir string) (bool, error) {
return err
}

if !info.IsDir() && strings.HasSuffix(info.Name(), ".json") {
if !info.IsDir() && strings.HasSuffix(info.Name(), ".json") && testFilenameMatchRegex.MatchString(info.Name()) {
fmt.Printf("Found '%s'\n", path)
testFiles = append(testFiles, path)
}
Expand Down
2 changes: 1 addition & 1 deletion suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ func (result TestResult) Result() string {
return resultString
}

// Execute a test suite and print + return results
// ExecuteSuite executes a test suite and prints + returns the results
func ExecuteSuite(runConfig RunConfig, testFilename string, logFailureDetails bool) (TestSuiteResult, error) {
// Read test suite spec
jsonFile, err := os.Open(testFilename)
Expand Down

0 comments on commit 9208da8

Please sign in to comment.