github.com/zeebo/goof@v0.0.0-20230907150950-e9457bc94477/helpers_test.go (about)

     1  package goof
     2  
     3  import (
     4  	"os"
     5  	"os/exec"
     6  	"reflect"
     7  	"strings"
     8  	"testing"
     9  )
    10  
    11  const importPath = "github.com/zeebo/goof"
    12  
    13  var troop Troop
    14  
    15  type suite struct{}
    16  
    17  func Test(t *testing.T) {
    18  	// normal test invocations don't come with dwarf info, so we can't actually
    19  	// run the tests :(
    20  	//
    21  	// it's ok. skip it here and hope TestCompile checks it.
    22  	if err := troop.check(); err != nil {
    23  		t.Log("skipping test due to error:", troop.check())
    24  		t.SkipNow()
    25  	}
    26  
    27  	// this way we don't have to remember to add the tests to this function
    28  	// to run.
    29  	s := reflect.TypeOf(suite{})
    30  	for i := 0; i < s.NumMethod(); i++ {
    31  		method := s.Method(i)
    32  		if !strings.HasPrefix(method.Name, "Test") {
    33  			continue
    34  		}
    35  		t.Run(method.Name[4:], func(t *testing.T) {
    36  			method.Func.Call([]reflect.Value{
    37  				reflect.ValueOf(suite{}),
    38  				reflect.ValueOf(t),
    39  			})
    40  		})
    41  	}
    42  }
    43  
    44  // TestCompile tries to actually run the tests on an environment that can
    45  // compile the test. HEH.
    46  func TestCompile(t *testing.T) {
    47  	if os.Getenv("GOOF_SKIP_COMPILE") != "" {
    48  		t.SkipNow()
    49  	}
    50  	if err := exec.Command("go", "test", "-c", importPath).Run(); err != nil {
    51  		t.Logf("skipping compile test due to error: %v", err)
    52  		t.SkipNow()
    53  	}
    54  	cmd := exec.Command("./goof.test", "-test.v")
    55  	cmd.Env = append(cmd.Env, "GOOF_SKIP_COMPILE=true")
    56  	output, err := cmd.CombinedOutput()
    57  	if err != nil {
    58  		t.Logf("%s", output)
    59  		t.Fatal(err)
    60  	}
    61  	t.Log("\n" + string(output))
    62  }