From 4cc29e50d59daf445f4152d10f9f41cb1828f702 Mon Sep 17 00:00:00 2001
From: Flu0r1ne <flur01ne@flu0r1ne.net>
Date: Sun, 5 Sep 2021 01:00:12 -0500
Subject: Remove pointers and cleanup templating, add version info

---
 adapters/gtest/config.go     | 27 +++++++++++++--------------
 adapters/gtest/executable.go | 15 +++++++--------
 adapters/gtest/templating.go | 41 ++++++++++++++++++++++++++++++++++-------
 3 files changed, 54 insertions(+), 29 deletions(-)

(limited to 'adapters/gtest')

diff --git a/adapters/gtest/config.go b/adapters/gtest/config.go
index 04d426c..533f266 100644
--- a/adapters/gtest/config.go
+++ b/adapters/gtest/config.go
@@ -13,21 +13,21 @@ const (
 )
 
 type Defaults struct {
-  Name          *string
-  Suite         *string
-  Testfile      *string
+  Name          string
+  Suite         string
+  Testfile      string
   Srcs          []string
-  Timeout       *uint
+  Timeout       uint
 }
 
 func (child *Defaults) Inherit(p interface{}) {
   parent := p.(*Defaults)
 
-  if(child.Name == nil)          { child.Name = parent.Name }
-  if(child.Suite == nil)         { child.Suite = parent.Suite }
-  if(child.Testfile == nil)      { child.Testfile = parent.Testfile }
+  if(child.Name == "")           { child.Name = parent.Name }
+  if(child.Suite == "")          { child.Suite = parent.Suite }
+  if(child.Testfile == "")       { child.Testfile = parent.Testfile }
   if(len(child.Srcs) == 0)       { child.Srcs = parent.Srcs }
-  if(child.Timeout == nil)       { child.Timeout = parent.Timeout }
+  if(child.Timeout == 0)         { child.Timeout = parent.Timeout }
 }
 
 
@@ -36,17 +36,16 @@ type Config struct {
 }
 
 func (c * Config) finalize(path string) {
-  if c.Name == nil {
+  if c.Name == "" {
     log.Fatalf("\"name\" is not defined for unit: %s\n", path)
-  } else if c.Suite == nil {
+  } else if c.Suite == "" {
     log.Fatalf("\"suite\" is not defined for unit: %s\n", path)
-  } else if c.Testfile == nil {
+  } else if c.Testfile == "" {
     log.Fatalf("\"testfile\" is not defined for unit: %s\n", path)
   }
 
-  if c.Timeout == nil {
-    c.Timeout = new(uint)
-    *c.Timeout = DEFAULT_TIMEOUT;
+  if c.Timeout == 0 {
+    c.Timeout = DEFAULT_TIMEOUT;
   }
 }
 
diff --git a/adapters/gtest/executable.go b/adapters/gtest/executable.go
index 78b0b56..25c83c1 100644
--- a/adapters/gtest/executable.go
+++ b/adapters/gtest/executable.go
@@ -27,7 +27,7 @@ func createExecutables(tcs []planr.TestCase) []executable {
 
   for _, tc := range tcs {
     cfg := tc.AdapterConfig().(*Config)
-    file := *cfg.Testfile
+    file := cfg.Testfile
     exe, contained := exes[file]
 
     // For set comparison
@@ -55,7 +55,7 @@ func createExecutables(tcs []planr.TestCase) []executable {
     if !reflect.DeepEqual(exe.srcs, cfg.Srcs) {
       log.Fatalf(
         "Two test case definitions %s and %s have different lists of sources", 
-        exe.testpath, *cfg.Testfile,
+        exe.testpath, cfg.Testfile,
       )
     }
 
@@ -119,12 +119,12 @@ func runGtest(exe string, tc planr.TestCase, builddir string) planr.TestResult {
     log.Fatal(err)
   }
 
-  timeout := time.Duration(*cfg.Timeout) * time.Millisecond
+  timeout := time.Duration(cfg.Timeout) * time.Millisecond
 
   ctx, cancel := context.WithTimeout(context.Background(), timeout)
 
   jsonFlag := "--gtest_output=json:" + f.Name()
-  testFlag := "--gtest_filter=" + *cfg.Suite + "." + *cfg.Name
+  testFlag := "--gtest_filter=" + cfg.Suite + "." + cfg.Name
 
   cmd := exec.CommandContext(ctx, exePath, jsonFlag, testFlag)
 
@@ -151,14 +151,13 @@ func runGtest(exe string, tc planr.TestCase, builddir string) planr.TestResult {
     log.Fatalf(
       "Could not find testcase %s with name=\"%s\" and suite=\"%s\". Does such a test exist in the test source?",
       tc.Cname,
-      *cfg.Name,
-      *cfg.Suite,
+      cfg.Name,
+      cfg.Suite,
     )
   }
 
-  // TODO: Cleanup -- ZERO TESTS?
   if len(results) > 1 {
-    log.Fatalf("Unexpected number of results")
+    log.Fatalf("Unexpected number of results, filter should have produced one result")
   }
 
   decodeResult := results[0]
diff --git a/adapters/gtest/templating.go b/adapters/gtest/templating.go
index 57532fa..41c54c1 100644
--- a/adapters/gtest/templating.go
+++ b/adapters/gtest/templating.go
@@ -3,8 +3,9 @@ package gtest
 import (
 	"io"
 	"log"
-	"text/template"
         "os"
+	"golang.flu0r1ne.net/planr"
+        "text/template"
 )
 
 type cmakeUnit struct {
@@ -27,7 +28,7 @@ func generateCmakeScript(out string, units []cmakeUnit) {
     log.Fatalf("Could not open CMakeFile (%s)\n%v", out, err)
   }
 
-  writeBoiler(file)
+  writeCmakeBoilerplate(file)
   
   tmpl := unitTemplate()
 
@@ -43,6 +44,10 @@ func generateCmakeScript(out string, units []cmakeUnit) {
 func unitTemplate() *template.Template {
   tmpl, err := template.New("gtest_unit").Parse(`
 
+################################################
+
+## {{.ExeNm}}
+
 add_executable(
   "{{.ExeNm}}"
   "{{.File}}"
@@ -60,14 +65,30 @@ gtest_discover_tests(
 `)
 
   if err != nil {
-    log.Fatalf("Cannot load Gtest Unit Template %v", err)
+    log.Fatalf("Cannot load Gtest unit template %v", err)
   }
 
   return tmpl
 }
 
-func writeBoiler(w io.Writer) {
-  w.Write([]byte(`
+const GOOGLE_TEST_URL = "https://github.com/google/googletest/archive/609281088cfefc76f9d0ce82e1ff6c30cc3591e5.zip"
+
+func writeCmakeBoilerplate(w io.Writer) {
+  tmpl := boilderTemplate()
+  
+  tmpl.Execute(w, struct {
+    Url     string
+    Version string
+  }{
+    Url: GOOGLE_TEST_URL,
+    Version: planr.VERSION,
+  })
+}
+
+func boilderTemplate() *template.Template {
+  tmpl, err := template.New("gtest_boilerplate").Parse(`
+# AUTOMATICALLY GENERATED BY PLANR VERSION {{.Version}}
+
 cmake_minimum_required (VERSION 3.1.0)
 
 project(PlanRGtestAdapter)
@@ -75,10 +96,16 @@ project(PlanRGtestAdapter)
 include(FetchContent)
 FetchContent_Declare(
   googletest
-  URL https://github.com/google/googletest/archive/609281088cfefc76f9d0ce82e1ff6c30cc3591e5.zip
+  URL {{.Url}}
 )
 
 include(GoogleTest)
 FetchContent_MakeAvailable(googletest)
-`))
+`)
+  
+  if err != nil {
+    log.Fatalf("Cannot load Gtest Cmake boilerplate")
+  }
+
+  return tmpl
 }
-- 
cgit v1.2.3