This repository has been archived by the owner on Apr 23, 2020. It is now read-only.
/
have_environment.go
215 lines (180 loc) · 8.89 KB
/
have_environment.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
209
210
211
212
213
214
215
/*
* Copyright 2018-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package test
import (
"fmt"
"io/ioutil"
"path/filepath"
"reflect"
"github.com/onsi/gomega/types"
)
// HaveAppendBuildEnvironment tests that a layer has an append build environment variable with the expected
// content.
func HaveAppendBuildEnvironment(name string, format string, args ...interface{}) types.GomegaMatcher {
return haveBuildEnvironment(fmt.Sprintf("%s.append", name), format, args...)
}
// HaveAppendLaunchEnvironment tests that a layer has an append launch environment variable with the expected
// content.
func HaveAppendLaunchEnvironment(name string, format string, args ...interface{}) types.GomegaMatcher {
return haveLaunchEnvironment(fmt.Sprintf("%s.append", name), format, args...)
}
// HaveAppendSharedEnvironment tests that a layer has an append shared environment variable with the expected
// content.
func HaveAppendSharedEnvironment(name string, format string, args ...interface{}) types.GomegaMatcher {
return haveSharedEnvironment(fmt.Sprintf("%s.append", name), format, args...)
}
// HaveAppendPathBuildEnvironment tests that a layer has an append path build environment variable with the expected
// content.
//
// Deprecated: Use HavePrependPathBuildEnvironment
func HaveAppendPathBuildEnvironment(name string, format string, args ...interface{}) types.GomegaMatcher {
return HavePrependPathBuildEnvironment(name, format, args...)
}
// HaveAppendPathLaunchEnvironment tests that a layer has an append path launch environment variable with the expected
// content.
//
// Deprecated: Use HavePrependPathLaunchEnvironment
func HaveAppendPathLaunchEnvironment(name string, format string, args ...interface{}) types.GomegaMatcher {
return HavePrependPathLaunchEnvironment(name, format, args...)
}
// HaveAppendPathSharedEnvironment tests that a layer has an append path shared environment variable with the expected
// content.
//
// Deprecated: Use HavePrependPathSharedEnvironment
func HaveAppendPathSharedEnvironment(name string, format string, args ...interface{}) types.GomegaMatcher {
return HavePrependPathSharedEnvironment(name, format, args...)
}
// HaveDefaultBuildEnvironment tests that a layer has a default build environment variable with the expected
// content.
func HaveDefaultBuildEnvironment(name string, format string, args ...interface{}) types.GomegaMatcher {
return haveBuildEnvironment(fmt.Sprintf("%s.default", name), format, args...)
}
// HaveDefaultLaunchEnvironment tests that a layer has a default launch environment variable with the expected
// content.
func HaveDefaultLaunchEnvironment(name string, format string, args ...interface{}) types.GomegaMatcher {
return haveLaunchEnvironment(fmt.Sprintf("%s.default", name), format, args...)
}
// HaveDefaultSharedEnvironment tests that a layer has a default shared environment variable with the expected
// content.
func HaveDefaultSharedEnvironment(name string, format string, args ...interface{}) types.GomegaMatcher {
return haveSharedEnvironment(fmt.Sprintf("%s.default", name), format, args...)
}
// HaveDelimiterBuildEnvironment tests that a layer has a delimiter for a build environment variable with the expected
// content.
func HaveDelimiterBuildEnvironment(name string, format string, args ...interface{}) types.GomegaMatcher {
return haveBuildEnvironment(fmt.Sprintf("%s.delim", name), format, args...)
}
// HaveDelimiterLaunchEnvironment tests that a layer has a delimiter for a launch environment variable with the expected
// content.
func HaveDelimiterLaunchEnvironment(name string, format string, args ...interface{}) types.GomegaMatcher {
return haveLaunchEnvironment(fmt.Sprintf("%s.delim", name), format, args...)
}
// HaveDelimiterSharedEnvironment tests that a layer has a delimiter for a shared environment variable with the expected
// content.
func HaveDelimiterSharedEnvironment(name string, format string, args ...interface{}) types.GomegaMatcher {
return haveSharedEnvironment(fmt.Sprintf("%s.delim", name), format, args...)
}
// HaveOverrideBuildEnvironment tests that a layer has an override build environment variable with the expected
// content.
func HaveOverrideBuildEnvironment(name string, format string, args ...interface{}) types.GomegaMatcher {
return haveBuildEnvironment(fmt.Sprintf("%s.override", name), format, args...)
}
// HaveOverrideLaunchEnvironment tests that a layer has an override launch environment variable with the expected
// content.
func HaveOverrideLaunchEnvironment(name string, format string, args ...interface{}) types.GomegaMatcher {
return haveLaunchEnvironment(fmt.Sprintf("%s.override", name), format, args...)
}
// HaveOverrideSharedEnvironment tests that a layer has an override shared environment variable with the expected
// content.
func HaveOverrideSharedEnvironment(name string, format string, args ...interface{}) types.GomegaMatcher {
return haveSharedEnvironment(fmt.Sprintf("%s.override", name), format, args...)
}
// HavePrependBuildEnvironment tests that a layer has a prepend build environment variable with the expected
// content.
func HavePrependBuildEnvironment(name string, format string, args ...interface{}) types.GomegaMatcher {
return haveBuildEnvironment(fmt.Sprintf("%s.prepend", name), format, args...)
}
// HavePrependLaunchEnvironment tests that a layer has a prepend launch environment variable with the expected
// content.
func HavePrependLaunchEnvironment(name string, format string, args ...interface{}) types.GomegaMatcher {
return haveLaunchEnvironment(fmt.Sprintf("%s.prepend", name), format, args...)
}
// HavePrependSharedEnvironment tests that a layer has a prepend shared environment variable with the expected
// content.
func HavePrependSharedEnvironment(name string, format string, args ...interface{}) types.GomegaMatcher {
return haveSharedEnvironment(fmt.Sprintf("%s.prepend", name), format, args...)
}
// HavePrependPathBuildEnvironment tests that a layer has a prepend path build environment variable with the expected
// content.
func HavePrependPathBuildEnvironment(name string, format string, args ...interface{}) types.GomegaMatcher {
return haveBuildEnvironment(name, format, args...)
}
// HavePrependPathLaunchEnvironment tests that a layer has a prepend path launch environment variable with the expected
// content.
func HavePrependPathLaunchEnvironment(name string, format string, args ...interface{}) types.GomegaMatcher {
return haveLaunchEnvironment(name, format, args...)
}
// HavePrependPathSharedEnvironment tests that a layer has a prepend path shared environment variable with the expected
// content.
func HavePrependPathSharedEnvironment(name string, format string, args ...interface{}) types.GomegaMatcher {
return haveSharedEnvironment(name, format, args...)
}
func haveBuildEnvironment(name string, format string, args ...interface{}) types.GomegaMatcher {
return &haveEnvironmentMatcher{
filepath.Join("env.build", name),
fmt.Sprintf(format, args...),
}
}
func haveLaunchEnvironment(name string, format string, args ...interface{}) types.GomegaMatcher {
return &haveEnvironmentMatcher{
filepath.Join("env.launch", name),
fmt.Sprintf(format, args...),
}
}
func haveSharedEnvironment(name string, format string, args ...interface{}) types.GomegaMatcher {
return &haveEnvironmentMatcher{
filepath.Join("env", name),
fmt.Sprintf(format, args...),
}
}
type haveEnvironmentMatcher struct {
relativePath string
expected string
}
func (m *haveEnvironmentMatcher) Match(actual interface{}) (bool, error) {
path, err := m.path(actual, m.relativePath)
if err != nil {
return false, err
}
b, err := ioutil.ReadFile(path)
if err != nil {
return false, fmt.Errorf("failed to read file: %s", err.Error())
}
return string(b) == m.expected, nil
}
func (m *haveEnvironmentMatcher) FailureMessage(actual interface{}) string {
return fmt.Sprintf("Expected\n\t%#v\nto have layer environment %#v\n\t%#v", actual, m.relativePath, m.expected)
}
func (m *haveEnvironmentMatcher) NegatedFailureMessage(actual interface{}) string {
return fmt.Sprintf("Expected\n\t%#v\nnot to have layer environment %#v\n\t%#v", actual, m.relativePath, m.expected)
}
func (m *haveEnvironmentMatcher) path(actual interface{}, relativePath string) (string, error) {
v := reflect.ValueOf(actual).FieldByName("Root")
if v == (reflect.Value{}) {
return "", fmt.Errorf("HaveEnvironment matcher expects a layer")
}
return filepath.Join(v.Interface().(string), relativePath), nil
}