generated from snivilised/arcadia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-utils.go
208 lines (160 loc) · 4.29 KB
/
test-utils.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
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package helpers
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/onsi/ginkgo/v2"
"github.com/onsi/gomega"
"github.com/spf13/viper"
"github.com/pkg/errors"
"github.com/snivilised/cobrass/src/assistant/configuration"
ci18n "github.com/snivilised/cobrass/src/assistant/i18n"
"github.com/snivilised/pixa/src/app/proxy/common"
"github.com/snivilised/pixa/src/i18n"
"github.com/snivilised/pixa/src/internal/matchers"
xi18n "github.com/snivilised/extendio/i18n"
"github.com/snivilised/extendio/xfs/storage"
"golang.org/x/text/language"
)
const (
Silent = true
Verbose = false
)
func Path(parent, relative string) string {
segments := strings.Split(relative, "/")
return filepath.Join(append([]string{parent}, segments...)...)
}
func Normalise(p string) string {
return strings.ReplaceAll(p, "/", string(filepath.Separator))
}
func Reason(name string) string {
return fmt.Sprintf("❌ for item named: '%v'", name)
}
func JoinCwd(segments ...string) string {
if current, err := os.Getwd(); err == nil {
parent, _ := filepath.Split(current)
grand := filepath.Dir(parent)
great := filepath.Dir(grand)
all := append([]string{great}, segments...)
return filepath.Join(all...)
}
panic("could not get root path")
}
func Root() string {
if current, err := os.Getwd(); err == nil {
return current
}
panic("could not get root path")
}
func Repo(relative string) string {
cmd := exec.Command("git", "rev-parse", "--show-toplevel")
bytes, err := cmd.Output()
if err != nil {
panic(errors.Wrap(err, "couldn't get repo root"))
}
segments := strings.Split(relative, "/")
output := strings.TrimSuffix(string(bytes), "\n")
path := []string{output}
path = append(path, segments...)
return filepath.Join(path...)
}
func Log() string {
if current, err := os.Getwd(); err == nil {
parent, _ := filepath.Split(current)
grand := filepath.Dir(parent)
great := filepath.Dir(grand)
return filepath.Join(great, "Test", "test.log")
}
panic("could not get root path")
}
func SetupTest(
index, configPath, l10nPath string,
silent bool,
) (vfs storage.VirtualFS, root string) {
var (
err error
)
viper.Reset()
vfs, root = ResetFS(index, silent)
if err = MockConfigFile(vfs, configPath); err != nil {
ginkgo.Fail(err.Error())
}
if err = ReadGlobalConfig(configPath); err != nil {
ginkgo.Fail(err.Error())
}
if err = UseI18n(l10nPath); err != nil {
ginkgo.Fail(err.Error())
}
return vfs, root
}
func UseI18n(l10nPath string) error {
return xi18n.Use(func(uo *xi18n.UseOptions) {
uo.From = xi18n.LoadFrom{
Path: l10nPath,
Sources: xi18n.TranslationFiles{
i18n.PixaSourceID: xi18n.TranslationSource{
Name: "dummy-cobrass",
},
ci18n.CobrassSourceID: xi18n.TranslationSource{
Name: "dummy-cobrass",
},
},
}
})
}
func ReadGlobalConfig(configPath string) error {
var (
err error
)
config := &configuration.GlobalViperConfig{}
config.SetConfigType(common.Definitions.Pixa.ConfigType)
config.SetConfigName(common.Definitions.Pixa.ConfigTestFilename)
config.AddConfigPath(configPath)
if e := config.ReadInConfig(); e != nil {
err = errors.Wrap(e, "can't read config")
}
return err
}
// MockConfigFile create a dummy config file in the file system specified
func MockConfigFile(vfs storage.VirtualFS, configPath string) error {
var (
err error
)
_ = vfs.MkdirAll(configPath, common.Permissions.Beezledub)
if _, err = vfs.Create(
filepath.Join(configPath, common.Definitions.Pixa.ConfigTestFilename),
); err != nil {
ginkgo.Fail(fmt.Sprintf("🔥 can't create dummy config (err: '%v')", err))
}
gomega.Expect(matchers.AsDirectory(configPath)).To(matchers.ExistInFS(vfs))
return err
}
func ResetFS(index string, silent bool) (vfs storage.VirtualFS, root string) {
vfs = storage.UseMemFS()
root = Scientist(vfs, index, silent)
gomega.Expect(matchers.AsDirectory(root)).To(matchers.ExistInFS(vfs))
return vfs, root
}
type DetectorStub struct {
}
func (j *DetectorStub) Scan() language.Tag {
return language.BritishEnglish
}
type ExecutorStub struct {
Name string
}
func (e *ExecutorStub) ProgName() string {
return e.Name
}
func (e *ExecutorStub) Look() (string, error) {
return "", nil
}
func (e *ExecutorStub) Execute(_ ...string) error {
return nil
}
type DirectoryQuantities struct {
Files uint
Folders uint
}