summaryrefslogtreecommitdiff
path: root/adapters/gtest/executable.go
blob: 80560c5fcc86ab811e252873fdbe9098a596fa17 (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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package gtest

import (
        "os"
	"errors"
        "time"
	"io/ioutil"
	"log"
	"os/exec"
	"path"
	"reflect"
	"sort"
        "context"

	"golang.flu0r1ne.net/planr")

type executable struct {
  exeNm           string
  testpath        string
  srcs            []string
  includeSrc      bool
  compilerOptions string
  tcs             []planr.TestCase
}

func dieConflictingExeProperty(exe executable, tc planr.TestCase, property string) {
  log.Fatalf(
    "Two test cases (including %s) belonging to the same executable (%s) have one or more conflicting properties\nProperty :%s",
    tc.Cname, exe.testpath, property,
  )
}

func createExecutables(tcs []planr.TestCase) []executable {
  exes := make(map[string] executable, 0)

  for _, tc := range tcs {
    cfg := tc.AdapterConfig().(*Config)
    file := cfg.Testfile
    exe, contained := exes[file]

    // For set comparison
    sort.Strings(cfg.Srcs)

    if !contained {
      exeTcs := make([]planr.TestCase, 1)
      exeTcs[0] = tc


      exe := executable {
        exeNm: planr.Cname("", file),
        testpath: file,
        srcs: cfg.Srcs,
        includeSrc: *cfg.Include_src,
        compilerOptions: cfg.Compiler_options,
        tcs: exeTcs,
      }

      exes[file] = exe
      
      continue
    }

    // We could create two different executables for each source list
    // But, that would be confusing so we're going to disallow it
    if !reflect.DeepEqual(exe.srcs, cfg.Srcs) {
      dieConflictingExeProperty(exe, tc, "srcs")
    }

    if exe.compilerOptions != cfg.Compiler_options {
      dieConflictingExeProperty(exe, tc, "compiler_options")
    }

    if exe.includeSrc != *cfg.Include_src {
      dieConflictingExeProperty(exe, tc, "include_src")
    }

    exe.tcs = append(exe.tcs, tc)
    
    exes[file] = exe
  }

  exesList := make([]executable, 0)

  for _, exe := range exes {
    exesList = append(exesList, exe)
  }

  return exesList
}

func (exe executable) compile(builddir string) (succeeded bool, buildFailures []planr.TestResult) {
  cmd := exec.Command("make", "-C", builddir, exe.exeNm)
  out, err := cmd.CombinedOutput()
  buildFailures = make([]planr.TestResult, 0)

  outputLog := string(out)

  if err != nil{
    var exiterr *exec.ExitError
    if errors.As(err, &exiterr) && exiterr.ExitCode() == 0 {
      log.Fatalf("Unrecoverable build failure: %v", err)
    }

    for i := range exe.tcs {
      res := planr.TestResult {}
      res.Tc = exe.tcs[i]
      res.DebugOutput = outputLog
      res.Status = planr.COMPILATION_FAILURE

      buildFailures = append(buildFailures, res)
    }

    succeeded = false

    return
  }

  succeeded = true
  return
}

const TMPFILENAME = "gtest_adapter_*.json"

func runGtest(exe string, tc planr.TestCase, builddir string) planr.TestResult {
  result := planr.TestResult {}
  result.Tc = tc

  exePath := path.Join(builddir, exe)
  cfg := tc.AdapterConfig().(*Config)

  f, err := ioutil.TempFile(builddir, TMPFILENAME)
      
  if err != nil {
    log.Fatal(err)
  }

  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

  cmd := exec.CommandContext(ctx, exePath, jsonFlag, testFlag)

  defer cancel()
  defer os.Remove(f.Name())

  out, err := cmd.CombinedOutput()
  if err != nil {
    var exiterr *exec.ExitError

    if !errors.As(err, &exiterr) {
      log.Printf("%v\n", err)
      os.Exit(exiterr.ExitCode())
    }
  }

  results, err := decodeResults(f)

  if err != nil {
    log.Fatalf("Could not collect results from %s: %v", exe, err)
  }

  if len(results) < 1 {
    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,
    )
  }

  if len(results) > 1 {
    log.Fatalf("Unexpected number of results, filter should have produced one result")
  }

  decodeResult := results[0]
  
  result.TestOutput = string(out)

  if decodeResult.pass {
    result.Status = planr.PASSING
  } else {
    result.Status = planr.RUNTIME_FAILURE
  }

  return result
}

func (exe executable) execute(builddir string) []planr.TestResult {
  results := make([]planr.TestResult, len(exe.tcs))

  for i := range exe.tcs {
    results[i] = runGtest(exe.exeNm, exe.tcs[i], builddir)
  }

  return results
}