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

[Sketch] Support other testing frameworks #11

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
17 changes: 17 additions & 0 deletions internal/runner/go.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package runner

import "github.com/buildkite/test-splitter/internal/api"

type GoTests struct{}

func (GoTests) FindFiles() ([]string, error) {
return nil, nil
}

func (GoTests) Run(testCases []string) error {
return nil
}

func (GoTests) Report([]api.TestCase) {

}
31 changes: 23 additions & 8 deletions internal/runner/rspec.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
package runner

import (
"errors"
"fmt"
"log"
"math"
"os"
"os/exec"
"path/filepath"
"strings"
"syscall"
"time"

"github.com/buildkite/test-splitter/internal/api"
)

type Rspec struct {
}
type Rspec struct{}

func (Rspec) GetFiles() []string {
func (Rspec) FindFiles() ([]string, error) {
var files []string

// Use filepath.Walk to traverse the directory recursively
Expand All @@ -34,24 +34,39 @@ func (Rspec) GetFiles() []string {

// Handle potential error from filepath.Walk
if err != nil {
log.Fatal("Error when getting files: ", err)
return nil, fmt.Errorf("walking to find files: %w", err)
}

return files
return files, nil
}

func (Rspec) Run(testCases []string) error {
func (r Rspec) Run(testCases []string) error {
args := []string{"--options", ".rspec.ci"}

args = append(args, testCases...)

// TODO: Figure out in advance whether we'll hit ARG_MAX and make args
// an appropriate size

fmt.Println("+++ :test-analytics: Executing tests 🏃")
fmt.Println("bin/rspec", strings.Join(args, " "))

cmd := exec.Command("bin/rspec", args...)
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
return cmd.Run()

err := cmd.Run()
if errors.Is(err, syscall.E2BIG) { // Will this work on e.g. Windows?
n := len(testCases) / 2
if err := r.Run(testCases[:n]); err != nil {
return err
}
if err := r.Run(testCases[n:]); err != nil {
return err
}
return nil
}
return err
}

type RspecExample struct {
Expand Down
27 changes: 24 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package main

import (
"encoding/json"
"flag"
"fmt"
"log"

Expand All @@ -22,12 +23,32 @@ type RspecData struct {
}

func main() {
// TODO: detect test runner and use appropriate runner
testRunner := runner.Rspec{}
testType := flag.String("type", "rspec", "Test `framework` to be run [rspec, go]")
flag.Parse()

var testRunner interface {
FindFiles() ([]string, error)
Run([]string) error
Report([]api.TestCase)
}

switch *testType {
case "rspec":
testRunner = runner.Rspec{}

case "go":
testRunner = runner.GoTests{}

default:
log.Fatalf("Unsupported test type %q", *testType)
}

// get files
fmt.Println("--- :test-analytics: Gathering test plan context and creating test plan request 🐿️")
files := testRunner.GetFiles()
files, err := testRunner.FindFiles()
if err != nil {
log.Fatalf("Couldn't find test files: %v", err)
}
fmt.Printf("Found %d files\n", len(files))

// fetch env vars
Expand Down