github.com/bartle-stripe/trillian@v1.2.1/testonly/paths.go (about) 1 // Copyright 2017 Google Inc. All Rights Reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package testonly 16 17 import ( 18 "path" 19 "path/filepath" 20 "runtime" 21 ) 22 23 // RelativeToPackage returns the input path p as an absolute path, resolved relative to the caller's package. 24 // The working directory for Go tests is the dir of the test file. Using "plain" relative paths in test 25 // utilities is, therefore, brittle, as the directory structure may change depending on where the tests are placed. 26 func RelativeToPackage(p string) string { 27 _, file, _, ok := runtime.Caller(1) 28 if !ok { 29 panic("cannot get caller information") 30 } 31 32 absPath, err := filepath.Abs(filepath.Join(path.Dir(file), p)) 33 if err != nil { 34 panic(err) 35 } 36 return absPath 37 }