|
| 1 | +/* |
| 2 | +Copyright 2025 Tim Ebert. |
| 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 test |
| 18 | + |
| 19 | +import ( |
| 20 | + "os" |
| 21 | + "path/filepath" |
| 22 | + |
| 23 | + utilruntime "k8s.io/apimachinery/pkg/util/runtime" |
| 24 | +) |
| 25 | + |
| 26 | +// RepoRoot returns a relative path from the current working directory to the repository root. For use in tests only. |
| 27 | +func RepoRoot() string { |
| 28 | + relativePath := "." |
| 29 | + |
| 30 | + dir, err := os.Getwd() |
| 31 | + utilruntime.Must(err) |
| 32 | + |
| 33 | + for dir != "/" { |
| 34 | + if _, err := os.Stat(filepath.Join(dir, "go.work")); err == nil { |
| 35 | + break |
| 36 | + } else if !os.IsNotExist(err) { |
| 37 | + panic(err) |
| 38 | + } |
| 39 | + |
| 40 | + // navigate one directory up, build up relative path |
| 41 | + dir = filepath.Dir(dir) |
| 42 | + relativePath = filepath.Join(relativePath, "..") |
| 43 | + } |
| 44 | + |
| 45 | + return filepath.Clean(relativePath) |
| 46 | +} |
| 47 | + |
| 48 | +// PathShardingCRDs returns the path to the directory containing the sharding CRDs. For use in tests only. |
| 49 | +func PathShardingCRDs() string { |
| 50 | + return filepath.Join(RepoRoot(), "config", "crds") |
| 51 | +} |
0 commit comments