github.com/artpar/rclone@v1.67.3/bin/test_independence.go (about)

     1  //go:build ignore
     2  
     3  // Test that the tests in the suite passed in are independent
     4  
     5  package main
     6  
     7  import (
     8  	"flag"
     9  	"log"
    10  	"os"
    11  	"os/exec"
    12  	"regexp"
    13  )
    14  
    15  var matchLine = regexp.MustCompile(`(?m)^=== RUN\s*(TestIntegration/\S*)\s*$`)
    16  
    17  // run the test pass in and grep out the test names
    18  func findTests(packageToTest string) (tests []string) {
    19  	cmd := exec.Command("go", "test", "-v", packageToTest)
    20  	out, err := cmd.CombinedOutput()
    21  	if err != nil {
    22  		_, _ = os.Stderr.Write(out)
    23  		log.Fatal(err)
    24  	}
    25  	results := matchLine.FindAllSubmatch(out, -1)
    26  	if results == nil {
    27  		log.Fatal("No tests found")
    28  	}
    29  	for _, line := range results {
    30  		tests = append(tests, string(line[1]))
    31  	}
    32  	return tests
    33  }
    34  
    35  // run the test passed in with the -run passed in
    36  func runTest(packageToTest string, testName string) {
    37  	cmd := exec.Command("go", "test", "-v", packageToTest, "-run", "^"+testName+"$")
    38  	out, err := cmd.CombinedOutput()
    39  	if err != nil {
    40  		log.Printf("%s FAILED ------------------", testName)
    41  		_, _ = os.Stderr.Write(out)
    42  		log.Printf("%s FAILED ------------------", testName)
    43  	} else {
    44  		log.Printf("%s OK", testName)
    45  	}
    46  }
    47  func main() {
    48  	flag.Parse()
    49  	args := flag.Args()
    50  	if len(args) != 1 {
    51  		log.Fatalf("Syntax: %s <test_to_run>", os.Args[0])
    52  	}
    53  	packageToTest := args[0]
    54  	testNames := findTests(packageToTest)
    55  	// fmt.Printf("%s\n", testNames)
    56  	for _, testName := range testNames {
    57  		runTest(packageToTest, testName)
    58  	}
    59  }