summaryrefslogtreecommitdiff
path: root/runner.go
blob: 2d66dc643991205c9f597fca95cfeefc6b14648d (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
package planr

import (
	"log"
	"os"
        "path"
)

type Runner struct {
  adapters      map[string] Adapter
  dirs          DirConfig
}

func (r Runner) adapterCfgs() []AdapterConfig {
  cgs := make([]AdapterConfig, len(r.adapters))
  
  for _, adapter := range r.adapters {
    cgs = append(cgs, adapter.Config())
  }

  return cgs
}

// TODO: Move into configuration parsing
func (r Runner) checkConfig(tcs []TestCase) {
  for _, tc := range tcs {
    tc.Config.ensureSatisfied(tc.Path)
  }
}

func (r Runner) setupEnv(adapter Adapter) {
  nm := adapter.Config().Name
  wd := path.Join(r.dirs.Build(), nm)

  if !directoryExists(wd) {
    if err := os.Mkdir(wd, 0755); err != nil {
      log.Fatalf("Could not create adapter config %s %v\n", wd, err)
    }
  } 
  
  safeCd(wd)
}

type adapterTestSet struct {
  adapter Adapter
  tcs     []TestCase
}

func (r Runner) groupByAdapter(tcs []TestCase) []adapterTestSet {
  r.checkConfig(tcs)
  
  pairs := make(map[string] adapterTestSet, 0)

  for _, tc := range tcs {
    // TODO: Make non-pointer
    adptNm := *tc.Config.Adapter
   
    // See if adapter if contained in map
    adapter, contained := r.adapters[adptNm]

    if !contained {
      log.Fatalf("Cannot find adapter \"%s\" for testcase \"%s\"", adptNm, tc.Cname)
    }

    pair, exists :=  pairs[adptNm]

    if !exists {
      pair.adapter = adapter 
    }
    
    pair.tcs = append(pair.tcs, tc)

    pairs[adptNm] = pair
  }


  // Convert to slice
  set := make([]adapterTestSet, 0)

  for _, pair := range pairs {
    set = append(set, pair)
  }
 
  return set
}

func (r Runner) CollectCases() []TestCase {
  return collectUnits(r.dirs.Rubric(), r.adapterCfgs())
}

func (r Runner) Build(tcs []TestCase) {

  if !directoryExists(r.dirs.Build()) {
    r.dirs.MkBuild()
  }

  testSets := r.groupByAdapter(tcs)

  for _, pair := range testSets {
    adapter := pair.adapter
    cases   := pair.tcs
    
    r.setupEnv(adapter)

    adapter.Build(cases)
  }

  safeCd(r.dirs.Config())
}

func (r Runner) Evaluate(tcs []TestCase) []TestResult {
  testSets := r.groupByAdapter(tcs)
  results := make([]TestResult, 0)

  for _, pair := range testSets {
    adapter := pair.adapter
    cases   := pair.tcs

    r.setupEnv(adapter)
    resultSet := adapter.Evaluate(cases)

    results = append(results, resultSet...)
  }
  
  safeCd(r.dirs.Config())

  return results
}

func (r Runner) Clean() {
  r.dirs.CleanBuild()
}

func (r Runner) BuildDir() string {
  return r.dirs.Build();
}

func (r Runner) ConfigDir() string {
  return r.dirs.Config()
}

func (r Runner) SrcDir() string {
  return r.dirs.Src()
}

func NewRunner(adapters map[string]Adapter, dirs DirConfig) Runner {
  r := Runner{adapters, dirs}
  
  for _, adapter := range r.adapters {
    adapter.Init(dirs)
  }

  return r
}