Skip to content

Commit

Permalink
cirrus internal test: allow filtering the tests using a glob expressi…
Browse files Browse the repository at this point in the history
…on (#735)

* cirrus internal test: allow filtering the tests using a glob expression

* cirrus internal test: return an error when no tests were matched
  • Loading branch information
edigaryev authored May 21, 2024
1 parent 36aee9f commit e459a2c
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 9 deletions.
18 changes: 15 additions & 3 deletions cmd/cirrus/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package main
import (
"context"
"crypto/x509"
"errors"
"fmt"
"github.com/breml/rootcerts/embedded"
"github.com/cirruslabs/cirrus-cli/internal/agent"
"github.com/cirruslabs/cirrus-cli/internal/commands"
"github.com/cirruslabs/cirrus-cli/internal/commands/helpers"
"github.com/cirruslabs/cirrus-cli/internal/opentelemetry"
"github.com/cirruslabs/cirrus-cli/internal/version"
"github.com/getsentry/sentry-go"
Expand Down Expand Up @@ -80,10 +82,20 @@ func main() {
sentry.CaptureException(err)
sentry.Flush(2 * time.Second)

// Capture the error into stderr and terminate
//nolint:gocritic // "log.Fatal will exit, and `defer sentry.Recover()` will not run" — it's OK,
// Capture the error into stderr
_, _ = fmt.Fprintln(os.Stderr, err)

// Terminate
exitCode := 1

var exitCodeError helpers.ExitCodeError
if errors.As(err, &exitCodeError) {
exitCode = exitCodeError.ExitCode()
}

//nolint:gocritic // "os.Exit will exit, and `defer sentry.Recover()` will not run" — it's OK,
// since we're already capturing the error above
log.Fatal(err)
os.Exit(exitCode)
}
}

Expand Down
21 changes: 21 additions & 0 deletions internal/commands/helpers/exitcodeerror.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package helpers

type ExitCodeError struct {
exitCode int
err error
}

func NewExitCodeError(exitCode int, err error) ExitCodeError {
return ExitCodeError{
exitCode: exitCode,
err: err,
}
}

func (exitCodeError ExitCodeError) ExitCode() int {
return exitCodeError.exitCode
}

func (exitCodeError ExitCodeError) Error() string {
return exitCodeError.err.Error()
}
52 changes: 49 additions & 3 deletions internal/commands/internal/test/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/bmatcuk/doublestar"
"github.com/cirruslabs/cirrus-cli/internal/commands/helpers"
"github.com/cirruslabs/cirrus-cli/internal/commands/logs"
"github.com/cirruslabs/cirrus-cli/internal/executor/environment"
"github.com/cirruslabs/cirrus-cli/pkg/larker"
Expand Down Expand Up @@ -143,17 +145,35 @@ func test(cmd *cobra.Command, args []string) error {
// Discover tests
var testDirs []string
err := filepath.Walk(".", func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}

// Does it look like a Starlark test?
if info.Name() == ".cirrus.expected.yml" {
testDirs = append(testDirs, filepath.Dir(path))
if info.Name() != ".cirrus.expected.yml" {
return nil
}

ok, err := match(args, path)
if err != nil {
return err
}
if !ok {
return nil
}

testDirs = append(testDirs, filepath.Dir(path))

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

if len(args) != 0 && len(testDirs) == 0 {
return helpers.NewExitCodeError(2, fmt.Errorf("no tests matched"))
}

// Configure hierarchical progress renderer
logger, cancel := logs.GetLogger(output, false, cmd.OutOrStdout(), os.Stdout)
defer cancel()
Expand Down Expand Up @@ -221,6 +241,32 @@ func test(cmd *cobra.Command, args []string) error {
return nil
}

func match(globs []string, path string) (bool, error) {
if len(globs) == 0 {
return true, nil
}

for _, glob := range globs {
fullMatch, err := doublestar.PathMatch(glob, filepath.Dir(path))
if err != nil {
return false, err
}
if fullMatch {
return true, nil
}

directoryMatch, err := doublestar.PathMatch(glob, filepath.Base(filepath.Dir(path)))
if err != nil {
return false, err
}
if directoryMatch {
return true, nil
}
}

return false, nil
}

func logDifferenceIfAny(logger *echelon.Logger, where string, a, b string) *Comparison {
if a == b {
return &Comparison{FoundDifference: false}
Expand All @@ -245,7 +291,7 @@ func logDifferenceIfAny(logger *echelon.Logger, where string, a, b string) *Comp

func NewTestCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "test",
Use: "test [GLOB ...]",
Short: "Discover and run Starlark tests",
RunE: test,
}
Expand Down
8 changes: 5 additions & 3 deletions internal/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ import (

func NewRootCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "cirrus",
Short: "Cirrus CLI",
Version: version.FullVersion,
Use: "cirrus",
Short: "Cirrus CLI",
Version: version.FullVersion,
SilenceUsage: true,
SilenceErrors: true,
}

commands := []*cobra.Command{
Expand Down

0 comments on commit e459a2c

Please sign in to comment.