Resolve Windows rooted search paths with Go
This commit is contained in:
@@ -370,7 +370,9 @@ func (iterator *searchCandidateIterator) absoluteCandidate(path string) (string,
|
||||
if filepath.IsAbs(path) {
|
||||
return filepath.Clean(path), nil
|
||||
}
|
||||
if filepath.VolumeName(path) != "" {
|
||||
// Drive-relative and drive-root-relative Windows paths need native
|
||||
// resolution; joining a rooted path to the whole working directory changes it.
|
||||
if filepath.VolumeName(path) != "" || runtime.GOOS == "windows" && len(path) > 0 && os.IsPathSeparator(path[0]) {
|
||||
return filepath.Abs(path)
|
||||
}
|
||||
working, err := iterator.helpers.workingDir()
|
||||
|
||||
@@ -4,6 +4,8 @@ package engine
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
@@ -30,3 +32,60 @@ func TestWindowsSearchRejectsReservedComponentsBeforeAcquisition(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestWindowsSearchPathsMatchHostAbsoluteResolution(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
file := writeImportFile(t, root, "settings/config.yml", "value: selected\n")
|
||||
working := t.TempDir()
|
||||
t.Chdir(working)
|
||||
relative, err := filepath.Rel(working, file)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
volume := filepath.VolumeName(file)
|
||||
rooted := strings.TrimPrefix(file, volume)
|
||||
for _, entry := range []struct {
|
||||
name string
|
||||
path string
|
||||
}{
|
||||
{"relative", relative},
|
||||
{"drive root relative", rooted},
|
||||
{"slash root relative", filepath.ToSlash(rooted)},
|
||||
{"drive relative", volume + relative},
|
||||
{"absolute", file},
|
||||
} {
|
||||
t.Run(entry.name, func(t *testing.T) {
|
||||
want, err := filepath.Abs(entry.path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
explicit, err := testLoad[discoveryLoadConfig](File(entry.path))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, search := range []Search{
|
||||
Paths(entry.path),
|
||||
SearchDirectory(filepath.Dir(entry.path), DirectorySearchOptions{
|
||||
Filenames: []FilenameGroup{CustomFilenames(filepath.Base(entry.path))},
|
||||
}),
|
||||
} {
|
||||
iterator, diagnostics := newSearchCandidateIterator(search, "", discoveryDirectoryHelpers{})
|
||||
if len(diagnostics) != 0 {
|
||||
t.Fatal(diagnostics)
|
||||
}
|
||||
candidate, ok, err := iterator.next()
|
||||
if err != nil || !ok || candidate != want {
|
||||
t.Fatalf("candidate = %q, %v, %v; want %q", candidate, ok, err, want)
|
||||
}
|
||||
result, err := testLoad[discoveryLoadConfig](SearchConfig("", search))
|
||||
if err != nil || result.Config.Value != explicit.Config.Value {
|
||||
t.Fatalf("searched path = %#v, %v; explicit value %q", result, err, explicit.Config.Value)
|
||||
}
|
||||
sources := result.Sources()
|
||||
if len(sources) != 1 || sources[0].Path != explicit.Sources()[0].Path {
|
||||
t.Fatalf("searched source = %#v; explicit source %#v", sources, explicit.Sources())
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user