This repository has been archived by the owner on Aug 10, 2019. It is now read-only.
/
framework.go
119 lines (106 loc) · 2.89 KB
/
framework.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
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
package framework
import (
"fmt"
"strings"
"testing"
"github.com/onsi/ginkgo"
"github.com/onsi/ginkgo/reporters"
"github.com/onsi/gomega"
"github.com/urfave/cli"
)
const (
testAuthor = "Author"
testName = "Name"
testDescription = "Description"
testUsageText = "This is a usage text."
)
// TestFramework is used to support commonnly used test features
type TestFramework struct {
setup func(*TestFramework) error
teardown func(*TestFramework) error
Sut *cli.App
}
// NewTestFramework creates a new test framework instance for a given `setup`
// and `teardown` function
func NewTestFramework(setup, teardown func(*TestFramework) error) *TestFramework {
sut := cli.NewApp()
sut.Author = testAuthor
sut.Name = testName
sut.Description = testDescription
sut.UsageText = testUsageText
sut.Flags = []cli.Flag{
cli.StringFlag{
Name: "socket, s",
Usage: "some usage text",
Value: "value",
},
cli.StringFlag{Name: "flag, fl, f"},
cli.BoolFlag{
Name: "another-flag, b",
Usage: "another usage text",
},
}
sut.Commands = []cli.Command{{
Aliases: []string{"c"},
Flags: []cli.Flag{
cli.StringFlag{Name: "flag, fl, f"},
cli.BoolFlag{
Name: "another-flag, b",
Usage: "another usage text",
},
},
Name: "config",
Usage: "another usage test",
Subcommands: []cli.Command{{
Aliases: []string{"s", "ss"},
Flags: []cli.Flag{
cli.StringFlag{Name: "sub-flag, sub-fl, s"},
cli.BoolFlag{
Name: "sub-command-flag, s",
Usage: "some usage text",
},
},
Name: "sub-config",
Usage: "another usage test",
}},
}, {
Aliases: []string{"i", "in"},
Name: "info",
Usage: "retrieve generic information",
}, {
Name: "some-command",
}}
return &TestFramework{
setup,
teardown,
sut,
}
}
// NilFunc is a convenience function which simply does nothing
func NilFunc(f *TestFramework) error {
return nil
}
// Setup is the global initialization function which runs before each test
// suite
func (t *TestFramework) Setup() {
// Global initialization for the whole framework goes in here
// Setup the actual test suite
gomega.Expect(t.setup(t)).To(gomega.Succeed())
}
// Teardown is the global deinitialization function which runs after each test
// suite
func (t *TestFramework) Teardown() {
// Global deinitialization for the whole framework goes in here
// Teardown the actual test suite
gomega.Expect(t.teardown(t)).To(gomega.Succeed())
}
// Describe is a convenience wrapper around the `ginkgo.Describe` function
func (t *TestFramework) Describe(text string, body func()) bool {
return ginkgo.Describe("docgen: "+text, body)
}
// RunFrameworkSpecs is a convenience wrapper for running tests
func RunFrameworkSpecs(t *testing.T, suiteName string) {
ginkgo.RunSpecsWithDefaultAndCustomReporters(t, suiteName,
[]ginkgo.Reporter{reporters.NewJUnitReporter(
fmt.Sprintf("%v_junit.xml", strings.ToLower(suiteName)))})
}