summaryrefslogtreecommitdiff
path: root/lib/helpers_test.go
blob: 2ea80692f4186f120d915ab7c972773eb1fba502 (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
package lib

import (
	"bytes"
	"io"
	"os"
	"testing"
)

type mockInputConverter struct {
	typ      string
	action   Action
	desc     string
	err      error
	inputFn  func(Container) (Container, error)
}

func (m mockInputConverter) GetType() string {
	return m.typ
}

func (m mockInputConverter) GetAction() Action {
	return m.action
}

func (m mockInputConverter) GetDescription() string {
	if m.desc != "" {
		return m.desc
	}
	return "mock input converter"
}

func (m mockInputConverter) Input(c Container) (Container, error) {
	if m.inputFn != nil {
		return m.inputFn(c)
	}
	return c, m.err
}

type mockOutputConverter struct {
	typ     string
	action  Action
	desc    string
	err     error
	outFn   func(Container) error
}

func (m mockOutputConverter) GetType() string {
	return m.typ
}

func (m mockOutputConverter) GetAction() Action {
	return m.action
}

func (m mockOutputConverter) GetDescription() string {
	if m.desc != "" {
		return m.desc
	}
	return "mock output converter"
}

func (m mockOutputConverter) Output(c Container) error {
	if m.outFn != nil {
		return m.outFn(c)
	}
	return m.err
}

func captureOutput(t *testing.T, fn func()) string {
	t.Helper()

	stdout := os.Stdout
	r, w, _ := os.Pipe()
	os.Stdout = w

	fn()

	_ = w.Close()
	os.Stdout = stdout

	var buf bytes.Buffer
	_, _ = io.Copy(&buf, r)
	_ = r.Close()

	return buf.String()
}

func resetInputConverters() {
	inputConverterMap = make(map[string]InputConverter)
}

func resetOutputConverters() {
	outputConverterMap = make(map[string]OutputConverter)
}

func resetConfigCreators() {
	inputConfigCreatorCache = make(map[string]inputConfigCreator)
	outputConfigCreatorCache = make(map[string]outputConfigCreator)
}