package gtest import ( "context" "fmt" "io/ioutil" "log" "os" "os/exec" "time" "path" "errors" "golang.flu0r1ne.net/planr" ) const GTEST_CMAKE = "CMakeLists.txt" func mkUnit(tc *planr.TestCase) cmakeUnit { cfg := tc.AdapterConfig("gtest").(*GtestConfig) return cmakeUnit { tc.Cname, cfg.joinTests(*cfg.Testfile), cfg.srcList(), }; } func chdir(dir string) { if err := os.Chdir(dir); err != nil { log.Fatal(err) } } type GtestAdapter struct {} func (a *GtestAdapter) Config() planr.AdapterConfig { return planr.AdapterConfig { Name: "gtest", ParseConfig: ParseConfig, ParseDefaultConfig: ParseDefaultConfig, } } func (adapter *GtestAdapter) Build(tcs []*planr.TestCase) { buildDir := adapter.Config().Dir() cmakeFile := path.Join(buildDir, GTEST_CMAKE) units := make([]cmakeUnit, 0) for _, tc := range tcs { fmt.Printf("[R] Building %s (%s)\n", tc.Cname, tc.Path) cfg := tc.AdapterConfig("gtest").(*GtestConfig) cfg.ensureSatisfied(tc.Path) units = append(units, mkUnit(tc)) } genCmake(cmakeFile, units) chdir(buildDir) planr.RunCmd("cmake", "-S", ".", "-B", ".") } func (adapter *GtestAdapter) Evaluate(tcs []*planr.TestCase) { planr.RunCmd("make", "-k") buildDir := adapter.Config().Dir() results := make([]planr.TestResult, 0) for _, tc := range tcs { fmt.Printf("[R] Evaluating %s (%s)\n", tc.Cname, tc.Path) exe := path.Join(buildDir, tc.Cname) f, err := ioutil.TempFile(buildDir, "gtest_adapter_*.json") if err != nil { log.Fatal(err) } ctx, cancel := context.WithTimeout(context.Background(), 9999*time.Millisecond) cmd := exec.CommandContext(ctx, exe, "--gtest_output=json:" + f.Name()) defer cancel() defer os.Remove(f.Name()) exitFail := false if err := cmd.Run(); err != nil { var exiterr *exec.ExitError if errors.As(err, &exiterr) && exiterr.ExitCode() == 1{ exitFail = true } else { log.Printf("%v\n", err) os.Exit(exiterr.ExitCode()) } } if exitFail { fmt.Printf("Failure detected") } results = append(results, decodeResults(f)...) } fmt.Println(results) }