aboutsummaryrefslogtreecommitdiff
path: root/adapters/gtest/adapter.go
diff options
context:
space:
mode:
authorFlu0r1ne <flur01ne@flu0r1ne.net>2021-08-05 15:56:10 -0500
committerFlu0r1ne <flur01ne@flu0r1ne.net>2021-08-05 15:56:10 -0500
commit72a70b127636ae8a83e2a2bd53b69143e3c5ded0 (patch)
treefdddf5cccdb487d63909f97074d111e67c79ded7 /adapters/gtest/adapter.go
parentf90a14d5d723c5d2b87f2eaa19f441dec33bb9b2 (diff)
downloadplanr-72a70b127636ae8a83e2a2bd53b69143e3c5ded0.tar.xz
planr-72a70b127636ae8a83e2a2bd53b69143e3c5ded0.zip
Runtime & reorganziation
Diffstat (limited to 'adapters/gtest/adapter.go')
-rw-r--r--adapters/gtest/adapter.go106
1 files changed, 106 insertions, 0 deletions
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)
+}