k8s.io/registry.k8s.io@v0.3.1/internal/integration/paths.go (about) 1 /* 2 Copyright 2022 The Kubernetes 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 package integration 18 19 import ( 20 "errors" 21 "os" 22 "path/filepath" 23 ) 24 25 func ModuleRootDir() (string, error) { 26 return moduleRootDir(os.Getwd) 27 } 28 29 func moduleRootDir(getWD func() (string, error)) (string, error) { 30 // in a test, the working directory will be the test package source dir 31 wd, err := getWD() 32 if err != nil { 33 return "", err 34 } 35 // walk upwards looking for module file 36 currDir := wd 37 for { 38 // stop at first existing go.mod 39 _, err := os.Stat(filepath.Join(currDir, "go.mod")) 40 if err == nil { 41 return currDir, nil 42 } 43 // if we get back the same path, we've hit the disk / volume root 44 nextDir := filepath.Dir(currDir) 45 if nextDir == currDir { 46 return "", errors.New("walked to disk root without finding module root") 47 } 48 currDir = nextDir 49 } 50 }