From 72a70b127636ae8a83e2a2bd53b69143e3c5ded0 Mon Sep 17 00:00:00 2001 From: Flu0r1ne Date: Thu, 5 Aug 2021 15:56:10 -0500 Subject: Runtime & reorganziation --- adapters/gtest/adapter.go | 106 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 adapters/gtest/adapter.go (limited to 'adapters/gtest/adapter.go') diff --git a/adapters/gtest/adapter.go b/adapters/gtest/adapter.go new file mode 100644 index 0000000..09e6ac1 --- /dev/null +++ b/adapters/gtest/adapter.go @@ -0,0 +1,106 @@ +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) +} -- cgit v1.2.3