Add test package with GetModuleRoot and PrependToPath function

Signed-off-by: Evan Lezar <elezar@nvidia.com>
This commit is contained in:
Evan Lezar
2022-03-10 13:37:08 +02:00
parent c5c2ffd68f
commit bf8c3bab72
3 changed files with 57 additions and 50 deletions

View File

@@ -1,17 +1,16 @@
package oci
import (
"fmt"
"os"
"path/filepath"
"runtime"
"testing"
"github.com/NVIDIA/nvidia-container-toolkit/internal/test"
"github.com/stretchr/testify/require"
)
func TestMaintainSpec(t *testing.T) {
moduleRoot, err := getModuleRoot()
moduleRoot, err := test.GetModuleRoot()
require.NoError(t, err)
files := []string{
@@ -38,21 +37,3 @@ func TestMaintainSpec(t *testing.T) {
require.JSONEq(t, string(inputContents), string(outputContents))
}
}
func getModuleRoot() (string, error) {
_, filename, _, _ := runtime.Caller(0)
return hasGoMod(filename)
}
func hasGoMod(dir string) (string, error) {
if dir == "" || dir == "/" {
return "", fmt.Errorf("module root not found")
}
_, err := os.Stat(filepath.Join(dir, "go.mod"))
if err != nil {
return hasGoMod(filepath.Dir(dir))
}
return dir, nil
}