summaryrefslogtreecommitdiff
path: root/adapters/bash/adapter.go
diff options
context:
space:
mode:
Diffstat (limited to 'adapters/bash/adapter.go')
-rw-r--r--adapters/bash/adapter.go103
1 files changed, 103 insertions, 0 deletions
diff --git a/adapters/bash/adapter.go b/adapters/bash/adapter.go
new file mode 100644
index 0000000..65a6c80
--- /dev/null
+++ b/adapters/bash/adapter.go
@@ -0,0 +1,103 @@
+package bash
+
+import (
+ "context"
+ "errors"
+ "log"
+ "os"
+ "os/exec"
+ "path"
+ "strings"
+ "time"
+ "fmt"
+
+ "golang.flu0r1ne.net/planr"
+)
+
+type Adapter struct {
+ dirs planr.DirConfig
+}
+
+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(dirs planr.DirConfig) {
+ a.dirs = dirs
+}
+
+func (adapter Adapter) Build(tcs []planr.TestCase) { }
+
+func executeScriptedTest(testdir string, tc planr.TestCase) planr.TestResult {
+ cfg := tc.AdapterConfig().(*Config)
+
+ 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)
+
+ 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.dirs.Tests(), tcs[i])
+ }(i)
+ }
+
+ for range tcs {
+ trs = append(trs, <-c)
+ }
+
+ return trs
+}
+
+func NewAdapter() *Adapter {
+ return new(Adapter)
+}