github.com/wawandco/ox@v0.13.6-0.20230809142027-913b3d837f2a/plugins/base/test/command.go (about)

     1  // test package contains the tooling for the test
     2  // command on the cli. The goal of this package is to provide the
     3  // structure for test commands to run and be organized.
     4  package test
     5  
     6  import (
     7  	"context"
     8  
     9  	"github.com/wawandco/ox/internal/log"
    10  	"github.com/wawandco/ox/plugins/core"
    11  )
    12  
    13  var _ core.Plugin = (*Command)(nil)
    14  var _ core.PluginReceiver = (*Command)(nil)
    15  var _ core.Command = (*Command)(nil)
    16  
    17  type Command struct {
    18  	beforeTesters []BeforeTester
    19  	testers       []Tester
    20  	afterTesters  []AfterTester
    21  }
    22  
    23  func (c Command) Name() string {
    24  	return "test"
    25  }
    26  
    27  func (c Command) Alias() string {
    28  	return "t"
    29  }
    30  
    31  func (c Command) ParentName() string {
    32  	return ""
    33  }
    34  
    35  func (c Command) HelpText() string {
    36  	return "provides the structure for test commands to run and be organized"
    37  }
    38  
    39  func (c *Command) Run(ctx context.Context, root string, args []string) error {
    40  	var err error
    41  	for _, bt := range c.beforeTesters {
    42  		err = bt.RunBeforeTest(ctx, root, args[1:])
    43  		if err != nil {
    44  			log.Warnf("error running %v before tester: %v\n", bt.Name(), err)
    45  			break
    46  		}
    47  	}
    48  
    49  	if err == nil {
    50  		for _, tt := range c.testers {
    51  			err = tt.Test(ctx, root, args[1:])
    52  			if err != nil {
    53  				break
    54  			}
    55  		}
    56  	}
    57  
    58  	for _, at := range c.afterTesters {
    59  		err := at.RunAfterTest(ctx, root, args[1:])
    60  		if err != nil {
    61  			log.Errorf("error running %v after tester: %v\n", at.Name(), err)
    62  		}
    63  	}
    64  
    65  	return err
    66  }