summaryrefslogtreecommitdiff
path: root/adapters/gtest/adapter.go
blob: 09e6ac1edfbfe598d78fa9a8d453f12f88e92c0e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
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)
}