diff --git a/cmd/substation/test.go b/cmd/substation/test.go index e7667c72..be7263b7 100644 --- a/cmd/substation/test.go +++ b/cmd/substation/test.go @@ -193,10 +193,20 @@ func testPath(arg string, extVars map[string]string, recursive bool) error { func testFile(arg string, extVars map[string]string) error { var cfg customConfig - switch filepath.Ext(arg) { + // Switching directories is required to support relative imports. + // The current directory is saved and restored after each test. + wd, _ := os.Getwd() + defer func() { + _ = os.Chdir(wd) + }() + + fileName := filepath.Base(arg) + _ = os.Chdir(filepath.Dir(arg)) + + switch filepath.Ext(fileName) { case ".jsonnet", ".libsonnet": // If the Jsonnet cannot compile, then the file is invalid. - mem, err := compileFile(arg, extVars) + mem, err := compileFile(fileName, extVars) if err != nil { fmt.Printf("?\t%s\t[error]\n", arg) fmt.Fprint(os.Stderr, transformErrStr(err, arg, cfg)) @@ -211,7 +221,7 @@ func testFile(arg string, extVars map[string]string) error { cfg = c case ".json": - c, err := fiConfig(arg) + c, err := fiConfig(fileName) if err != nil { return err } diff --git a/cmd/substation/vet.go b/cmd/substation/vet.go index 77c34aaf..ab7dab7b 100644 --- a/cmd/substation/vet.go +++ b/cmd/substation/vet.go @@ -94,9 +94,19 @@ func vetFile(arg string, extVars map[string]string) error { // This uses the custom config from the `test` command. var cfg customConfig - switch filepath.Ext(arg) { + // Switching directories is required to support relative imports. + // The current directory is saved and restored after each test. + wd, _ := os.Getwd() + defer func() { + _ = os.Chdir(wd) + }() + + fileName := filepath.Base(arg) + _ = os.Chdir(filepath.Dir(arg)) + + switch filepath.Ext(fileName) { case ".jsonnet", ".libsonnet": - mem, err := compileFile(arg, extVars) + mem, err := compileFile(fileName, extVars) if err != nil { // This is an error in the Jsonnet syntax. // The line number and column range are included. @@ -112,7 +122,7 @@ func vetFile(arg string, extVars map[string]string) error { return err } case ".json": - fi, err := fiConfig(arg) + fi, err := fiConfig(fileName) if err != nil { return err }