aboutsummaryrefslogtreecommitdiff
path: root/adapters/bash/adapter.go
blob: cfa2032bb78ad545497c338c526f1eae52e62b3d (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
package bash

import (
	"context"
	"errors"
	"fmt"
	"log"
	"os"
	"os/exec"
	"path"
	"strings"
	"time"
        "io/ioutil"

	"golang.flu0r1ne.net/planr"
)

type Adapter struct {
  dirs     planr.DirConfig
  buildDir string
}

func (a *Adapter) Config() planr.AdapterConfig {
  return planr.AdapterConfig {
    Name: "bash",
    ParseConfig: ParseConfig,
    ParseDefaultConfig: ParseDefaultConfig,
  }
}

func safeWd() string{
  wd, err := os.Getwd()

  if err != nil {
    log.Fatalf("Could not get GtestBuildDir %s %v\n", wd, err)
  }

  return wd
}

func (a *Adapter) Init(ctx planr.PipelineContext) {
  a.dirs = ctx.Dirs
  a.buildDir = ctx.AdapterDir
}

func createTmpDir(dir string) string {
  name, err := ioutil.TempDir(dir, "tmpbuild.*")

  if err != nil {
    log.Fatal(err)
  }

  return name
}

func removeTmpDir(dir string) {
  if err := os.RemoveAll(dir); err != nil {
    log.Fatal(err)
  }
}

func (adapter Adapter) Build(tcs []planr.TestCase) { }

func executeScriptedTest(builddir, testdir, srcdir string, tc planr.TestCase) planr.TestResult {
  cfg := tc.AdapterConfig().(*Config)

  tmpdir := createTmpDir(builddir)
  defer removeTmpDir(tmpdir)

  if err := planr.RecursiveCopyDir(srcdir, tmpdir); err != nil {
    log.Fatalf("Could not copy sources: %v", err)
  }

  timeout := time.Duration(cfg.Timeout) * time.Millisecond

  ctx, cancel := context.WithTimeout(context.Background(), timeout)

  defer cancel()

  path := path.Join(testdir, cfg.Testfile)

  result := planr.TestResult {}
  result.Tc = tc

  cmd := exec.CommandContext(ctx, "bash", path)

  cmd.Dir = tmpdir
    
  if out, err := cmd.CombinedOutput(); err != nil {
    result.Status = planr.RUNTIME_FAILURE  
    result.TestOutput = string(out)

    var exiterr *exec.ExitError
    if !errors.As(err, &exiterr) {
      log.Fatalf("Test script %s failed with unknown error %v\n", path, err)
    } else {
      if strings.Contains(exiterr.String(), "killed") {
        result.TestOutput += fmt.Sprintf("TEST TERMINATED (Timeout=%d)\n", cfg.Timeout)
      }
    }

    return result
  }

  result.Status = planr.PASSING


  return result
}

func (adapter Adapter) Evaluate(tcs []planr.TestCase) [] planr.TestResult {
  finalizeConfigs(tcs)
 
  trs := make([]planr.TestResult, 0)
  c := make(chan planr.TestResult, 0)
  for i := range tcs {
    go func(i int) {
      c <- executeScriptedTest(adapter.buildDir, adapter.dirs.Tests(), adapter.dirs.Src(), tcs[i])
    }(i)
  }

  for range tcs {
    trs = append(trs, <-c)
  }

  return trs
}

func NewAdapter() *Adapter {
  return new(Adapter)
}