aboutsummaryrefslogtreecommitdiff
path: root/runner.go
blob: 11fceab5db4319235318ec03735830d51f6e6f0e (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
package planr

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

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

func (r Runner) adapterCfgs() []AdapterConfig {
  cgs := make([]AdapterConfig, 0)
  
  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 adapterBuildDir(dirs DirConfig, adapter Adapter) string {
  return path.Join(dirs.Build(), adapter.Config().Name)
}

func (r Runner) createAdapterBuildDir(adapter Adapter) {
  build := adapterBuildDir(r.dirs, adapter)

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

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) {
  wd, err := os.Getwd()
  if err != nil {
    log.Fatal(err)
  }

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

  testSets := r.groupByAdapter(tcs)

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

    adapter.Build(cases)
  }

  safeCd(wd)
}

func (r Runner) Evaluate(tcs []TestCase) []TestResult {
  wd, err := os.Getwd()
  if err != nil {
    log.Fatal(err)
  }

  testSets := r.groupByAdapter(tcs)
  results := make([]TestResult, 0)
  
  c := make(chan []TestResult)
  for _, pair := range testSets {
    go func (pair adapterTestSet) {
      adapter := pair.adapter
      cases   := pair.tcs

      resultSet := adapter.Evaluate(cases)

      c <- resultSet
    }(pair)
  }

  for range testSets {
    results = append(results, (<-c)...)
  }

  sort.Sort(ByReadIdx(results))
  
  safeCd(wd)

  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 {
    ctx := PipelineContext {
      Dirs: dirs,
      AdapterDir: adapterBuildDir(dirs, adapter),
    }

    adapter.Init(ctx)
  }

  return r
}