github.com/cloudfoundry/libcfbuildpack@v1.91.23/test/be_a_symlink.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 "os" 22 23 "github.com/onsi/gomega/types" 24 ) 25 26 // BeASymlink asserts that a file is a symlink and the link points to a given target. 27 func BeASymlink(target string) types.GomegaMatcher { 28 return &beASymlinkMatcher{ 29 target: target, 30 } 31 } 32 33 type beASymlinkMatcher struct { 34 target string 35 } 36 37 func (m *beASymlinkMatcher) Match(actual interface{}) (bool, error) { 38 path, ok := actual.(string) 39 if !ok { 40 return false, fmt.Errorf("BeASymlink matcher expects a path") 41 } 42 43 fi, err := os.Lstat(path) 44 if err != nil { 45 return false, fmt.Errorf("Unable to stat file: %s", err.Error()) 46 } 47 48 if fi.Mode()&os.ModeSymlink != os.ModeSymlink { 49 return false, nil 50 } 51 52 target, err := os.Readlink(path) 53 if err != nil { 54 return false, fmt.Errorf("Unable to read link :%s", err.Error()) 55 } 56 57 return target == m.target, nil 58 } 59 60 func (m *beASymlinkMatcher) FailureMessage(actual interface{}) string { 61 return fmt.Sprintf("Expected\n\t%#v\nto be a symlink to\n\t%#v", actual, m.target) 62 } 63 64 func (m *beASymlinkMatcher) NegatedFailureMessage(actual interface{}) string { 65 return fmt.Sprintf("Expected\n\t%#v\nnot to be a symlink to\n\t%#v", actual, m.target) 66 }