OSDN Git Service

new repo
[bytom/vapor.git] / vendor / google.golang.org / grpc / testdata / testdata.go
1 /*
2  * Copyright 2017 gRPC 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  *     http://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
18 package testdata
19
20 import (
21         "log"
22         "os"
23         "path/filepath"
24 )
25
26 // Path returns the absolute path the given relative file or directory path,
27 // relative to the google.golang.org/grpc/testdata directory in the user's GOPATH.
28 // If rel is already absolute, it is returned unmodified.
29 func Path(rel string) string {
30         if filepath.IsAbs(rel) {
31                 return rel
32         }
33
34         v, err := goPackagePath("google.golang.org/grpc/testdata")
35         if err != nil {
36                 log.Fatalf("Error finding google.golang.org/grpc/testdata directory: %v", err)
37         }
38
39         return filepath.Join(v, rel)
40 }
41
42 func goPackagePath(pkg string) (path string, err error) {
43         gp := os.Getenv("GOPATH")
44         if gp == "" {
45                 return path, os.ErrNotExist
46         }
47
48         for _, p := range filepath.SplitList(gp) {
49                 dir := filepath.Join(p, "src", filepath.FromSlash(pkg))
50                 fi, err := os.Stat(dir)
51                 if os.IsNotExist(err) {
52                         continue
53                 }
54                 if err != nil {
55                         return "", err
56                 }
57                 if !fi.IsDir() {
58                         continue
59                 }
60                 return dir, nil
61         }
62         return path, os.ErrNotExist
63 }