github.com/cloudfoundry/libcfbuildpack@v1.91.23/test/have_environment.go (about) 1 /* 2 * Copyright 2018-2020 the original author or authors. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * https://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package test 18 19 import ( 20 "fmt" 21 "io/ioutil" 22 "path/filepath" 23 "reflect" 24 25 "github.com/onsi/gomega/types" 26 ) 27 28 // HaveAppendBuildEnvironment tests that a layer has an append build environment variable with the expected 29 // content. 30 func HaveAppendBuildEnvironment(name string, format string, args ...interface{}) types.GomegaMatcher { 31 return haveBuildEnvironment(fmt.Sprintf("%s.append", name), format, args...) 32 } 33 34 // HaveAppendLaunchEnvironment tests that a layer has an append launch environment variable with the expected 35 // content. 36 func HaveAppendLaunchEnvironment(name string, format string, args ...interface{}) types.GomegaMatcher { 37 return haveLaunchEnvironment(fmt.Sprintf("%s.append", name), format, args...) 38 } 39 40 // HaveAppendSharedEnvironment tests that a layer has an append shared environment variable with the expected 41 // content. 42 func HaveAppendSharedEnvironment(name string, format string, args ...interface{}) types.GomegaMatcher { 43 return haveSharedEnvironment(fmt.Sprintf("%s.append", name), format, args...) 44 } 45 46 // HaveAppendPathBuildEnvironment tests that a layer has an append path build environment variable with the expected 47 // content. 48 // 49 // Deprecated: Use HavePrependPathBuildEnvironment 50 func HaveAppendPathBuildEnvironment(name string, format string, args ...interface{}) types.GomegaMatcher { 51 return HavePrependPathBuildEnvironment(name, format, args...) 52 } 53 54 // HaveAppendPathLaunchEnvironment tests that a layer has an append path launch environment variable with the expected 55 // content. 56 // 57 // Deprecated: Use HavePrependPathLaunchEnvironment 58 func HaveAppendPathLaunchEnvironment(name string, format string, args ...interface{}) types.GomegaMatcher { 59 return HavePrependPathLaunchEnvironment(name, format, args...) 60 } 61 62 // HaveAppendPathSharedEnvironment tests that a layer has an append path shared environment variable with the expected 63 // content. 64 // 65 // Deprecated: Use HavePrependPathSharedEnvironment 66 func HaveAppendPathSharedEnvironment(name string, format string, args ...interface{}) types.GomegaMatcher { 67 return HavePrependPathSharedEnvironment(name, format, args...) 68 } 69 70 // HaveDefaultBuildEnvironment tests that a layer has a default build environment variable with the expected 71 // content. 72 func HaveDefaultBuildEnvironment(name string, format string, args ...interface{}) types.GomegaMatcher { 73 return haveBuildEnvironment(fmt.Sprintf("%s.default", name), format, args...) 74 } 75 76 // HaveDefaultLaunchEnvironment tests that a layer has a default launch environment variable with the expected 77 // content. 78 func HaveDefaultLaunchEnvironment(name string, format string, args ...interface{}) types.GomegaMatcher { 79 return haveLaunchEnvironment(fmt.Sprintf("%s.default", name), format, args...) 80 } 81 82 // HaveDefaultSharedEnvironment tests that a layer has a default shared environment variable with the expected 83 // content. 84 func HaveDefaultSharedEnvironment(name string, format string, args ...interface{}) types.GomegaMatcher { 85 return haveSharedEnvironment(fmt.Sprintf("%s.default", name), format, args...) 86 } 87 88 // HaveDelimiterBuildEnvironment tests that a layer has a delimiter for a build environment variable with the expected 89 // content. 90 func HaveDelimiterBuildEnvironment(name string, format string, args ...interface{}) types.GomegaMatcher { 91 return haveBuildEnvironment(fmt.Sprintf("%s.delim", name), format, args...) 92 } 93 94 // HaveDelimiterLaunchEnvironment tests that a layer has a delimiter for a launch environment variable with the expected 95 // content. 96 func HaveDelimiterLaunchEnvironment(name string, format string, args ...interface{}) types.GomegaMatcher { 97 return haveLaunchEnvironment(fmt.Sprintf("%s.delim", name), format, args...) 98 } 99 100 // HaveDelimiterSharedEnvironment tests that a layer has a delimiter for a shared environment variable with the expected 101 // content. 102 func HaveDelimiterSharedEnvironment(name string, format string, args ...interface{}) types.GomegaMatcher { 103 return haveSharedEnvironment(fmt.Sprintf("%s.delim", name), format, args...) 104 } 105 106 // HaveOverrideBuildEnvironment tests that a layer has an override build environment variable with the expected 107 // content. 108 func HaveOverrideBuildEnvironment(name string, format string, args ...interface{}) types.GomegaMatcher { 109 return haveBuildEnvironment(fmt.Sprintf("%s.override", name), format, args...) 110 } 111 112 // HaveOverrideLaunchEnvironment tests that a layer has an override launch environment variable with the expected 113 // content. 114 func HaveOverrideLaunchEnvironment(name string, format string, args ...interface{}) types.GomegaMatcher { 115 return haveLaunchEnvironment(fmt.Sprintf("%s.override", name), format, args...) 116 } 117 118 // HaveOverrideSharedEnvironment tests that a layer has an override shared environment variable with the expected 119 // content. 120 func HaveOverrideSharedEnvironment(name string, format string, args ...interface{}) types.GomegaMatcher { 121 return haveSharedEnvironment(fmt.Sprintf("%s.override", name), format, args...) 122 } 123 124 // HavePrependBuildEnvironment tests that a layer has a prepend build environment variable with the expected 125 // content. 126 func HavePrependBuildEnvironment(name string, format string, args ...interface{}) types.GomegaMatcher { 127 return haveBuildEnvironment(fmt.Sprintf("%s.prepend", name), format, args...) 128 } 129 130 // HavePrependLaunchEnvironment tests that a layer has a prepend launch environment variable with the expected 131 // content. 132 func HavePrependLaunchEnvironment(name string, format string, args ...interface{}) types.GomegaMatcher { 133 return haveLaunchEnvironment(fmt.Sprintf("%s.prepend", name), format, args...) 134 } 135 136 // HavePrependSharedEnvironment tests that a layer has a prepend shared environment variable with the expected 137 // content. 138 func HavePrependSharedEnvironment(name string, format string, args ...interface{}) types.GomegaMatcher { 139 return haveSharedEnvironment(fmt.Sprintf("%s.prepend", name), format, args...) 140 } 141 142 // HavePrependPathBuildEnvironment tests that a layer has a prepend path build environment variable with the expected 143 // content. 144 func HavePrependPathBuildEnvironment(name string, format string, args ...interface{}) types.GomegaMatcher { 145 return haveBuildEnvironment(name, format, args...) 146 } 147 148 // HavePrependPathLaunchEnvironment tests that a layer has a prepend path launch environment variable with the expected 149 // content. 150 func HavePrependPathLaunchEnvironment(name string, format string, args ...interface{}) types.GomegaMatcher { 151 return haveLaunchEnvironment(name, format, args...) 152 } 153 154 // HavePrependPathSharedEnvironment tests that a layer has a prepend path shared environment variable with the expected 155 // content. 156 func HavePrependPathSharedEnvironment(name string, format string, args ...interface{}) types.GomegaMatcher { 157 return haveSharedEnvironment(name, format, args...) 158 } 159 160 func haveBuildEnvironment(name string, format string, args ...interface{}) types.GomegaMatcher { 161 return &haveEnvironmentMatcher{ 162 filepath.Join("env.build", name), 163 fmt.Sprintf(format, args...), 164 } 165 } 166 167 func haveLaunchEnvironment(name string, format string, args ...interface{}) types.GomegaMatcher { 168 return &haveEnvironmentMatcher{ 169 filepath.Join("env.launch", name), 170 fmt.Sprintf(format, args...), 171 } 172 } 173 174 func haveSharedEnvironment(name string, format string, args ...interface{}) types.GomegaMatcher { 175 return &haveEnvironmentMatcher{ 176 filepath.Join("env", name), 177 fmt.Sprintf(format, args...), 178 } 179 } 180 181 type haveEnvironmentMatcher struct { 182 relativePath string 183 expected string 184 } 185 186 func (m *haveEnvironmentMatcher) Match(actual interface{}) (bool, error) { 187 path, err := m.path(actual, m.relativePath) 188 if err != nil { 189 return false, err 190 } 191 192 b, err := ioutil.ReadFile(path) 193 if err != nil { 194 return false, fmt.Errorf("failed to read file: %s", err.Error()) 195 } 196 197 return string(b) == m.expected, nil 198 } 199 200 func (m *haveEnvironmentMatcher) FailureMessage(actual interface{}) string { 201 return fmt.Sprintf("Expected\n\t%#v\nto have layer environment %#v\n\t%#v", actual, m.relativePath, m.expected) 202 } 203 204 func (m *haveEnvironmentMatcher) NegatedFailureMessage(actual interface{}) string { 205 return fmt.Sprintf("Expected\n\t%#v\nnot to have layer environment %#v\n\t%#v", actual, m.relativePath, m.expected) 206 } 207 208 func (m *haveEnvironmentMatcher) path(actual interface{}, relativePath string) (string, error) { 209 v := reflect.ValueOf(actual).FieldByName("Root") 210 if v == (reflect.Value{}) { 211 return "", fmt.Errorf("HaveEnvironment matcher expects a layer") 212 } 213 214 return filepath.Join(v.Interface().(string), relativePath), nil 215 }