forked from hashicorp/otto
/
testing.go
51 lines (40 loc) · 1.2 KB
/
testing.go
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
package otto
import (
"testing"
"github.com/hashicorp/otto/app"
"github.com/hashicorp/otto/infrastructure"
"github.com/hashicorp/otto/ui"
)
// TestCoreConfig returns a CoreConfig that can be used for testing.
func TestCoreConfig(t *testing.T) *CoreConfig {
config := &CoreConfig{
Ui: new(ui.Mock),
}
// Add some default mock implementations. These can be overwritten easily
TestInfra(t, "test", config)
TestApp(t, app.Tuple{"test", "test", "test"}, config)
return config
}
// TestInfra adds a mock infrastructure with the given name to the
// core config and returns it.
func TestInfra(t *testing.T, n string, c *CoreConfig) *infrastructure.Mock {
if c.Infrastructures == nil {
c.Infrastructures = make(map[string]infrastructure.Factory)
}
result := new(infrastructure.Mock)
c.Infrastructures[n] = func() (infrastructure.Infrastructure, error) {
return result, nil
}
return result
}
// TestApp adds a mock app with the given tuple to the core config.
func TestApp(t *testing.T, tuple app.Tuple, c *CoreConfig) *app.Mock {
if c.Apps == nil {
c.Apps = make(map[app.Tuple]app.Factory)
}
result := new(app.Mock)
c.Apps[tuple] = func() (app.App, error) {
return result, nil
}
return result
}