mirror of
https://github.com/opentofu/opentofu.git
synced 2025-02-25 18:45:20 -06:00
config/module: adjust FileDetector tests for Windows
"/foo" is not an absolute path on Windows. Adjust the FileDetector tests to take that into account when verifying the results. Fixes FileDetector test failures on Windows.
This commit is contained in:
parent
160e4f926e
commit
74cf8fcabd
@ -13,8 +13,6 @@ func (d *FileDetector) Detect(src, pwd string) (string, bool, error) {
|
|||||||
return "", false, nil
|
return "", false, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Make sure we're using "/" even on Windows. URLs are "/"-based.
|
|
||||||
src = filepath.ToSlash(src)
|
|
||||||
if !filepath.IsAbs(src) {
|
if !filepath.IsAbs(src) {
|
||||||
if pwd == "" {
|
if pwd == "" {
|
||||||
return "", true, fmt.Errorf(
|
return "", true, fmt.Errorf(
|
||||||
@ -23,6 +21,8 @@ func (d *FileDetector) Detect(src, pwd string) (string, bool, error) {
|
|||||||
|
|
||||||
src = filepath.Join(pwd, src)
|
src = filepath.Join(pwd, src)
|
||||||
}
|
}
|
||||||
|
// Make sure we're using "/" even on Windows. URLs are "/"-based.
|
||||||
|
src = filepath.ToSlash(src)
|
||||||
|
|
||||||
// Make sure that we don't start with "/" since we add that below
|
// Make sure that we don't start with "/" since we add that below
|
||||||
if src[0] == '/' {
|
if src[0] == '/' {
|
||||||
|
@ -1,25 +1,42 @@
|
|||||||
package module
|
package module
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"runtime"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type fileTest struct {
|
||||||
|
in, pwd, out string
|
||||||
|
err bool
|
||||||
|
}
|
||||||
|
|
||||||
|
var fileTests = []fileTest{
|
||||||
|
{"./foo", "/pwd", "file:///pwd/foo", false},
|
||||||
|
{"./foo?foo=bar", "/pwd", "file:///pwd/foo?foo=bar", false},
|
||||||
|
{"foo", "/pwd", "file:///pwd/foo", false},
|
||||||
|
}
|
||||||
|
|
||||||
|
var unixFileTests = []fileTest{
|
||||||
|
{"/foo", "/pwd", "file:///foo", false},
|
||||||
|
{"/foo?bar=baz", "/pwd", "file:///foo?bar=baz", false},
|
||||||
|
}
|
||||||
|
|
||||||
|
var winFileTests = []fileTest{
|
||||||
|
{"/foo", "/pwd", "file:///pwd/foo", false},
|
||||||
|
{`C:\`, `/pwd`, `file:///C:/`, false},
|
||||||
|
{`C:\?bar=baz`, `/pwd`, `file:///C:/?bar=baz`, false},
|
||||||
|
}
|
||||||
|
|
||||||
func TestFileDetector(t *testing.T) {
|
func TestFileDetector(t *testing.T) {
|
||||||
cases := []struct {
|
if runtime.GOOS == "windows" {
|
||||||
Input string
|
fileTests = append(fileTests, winFileTests...)
|
||||||
Output string
|
} else {
|
||||||
}{
|
fileTests = append(fileTests, unixFileTests...)
|
||||||
{"./foo", "file:///pwd/foo"},
|
|
||||||
{"./foo?foo=bar", "file:///pwd/foo?foo=bar"},
|
|
||||||
{"foo", "file:///pwd/foo"},
|
|
||||||
{"/foo", "file:///foo"},
|
|
||||||
{"/foo?bar=baz", "file:///foo?bar=baz"},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pwd := "/pwd"
|
|
||||||
f := new(FileDetector)
|
f := new(FileDetector)
|
||||||
for i, tc := range cases {
|
for i, tc := range fileTests {
|
||||||
output, ok, err := f.Detect(tc.Input, pwd)
|
out, ok, err := f.Detect(tc.in, tc.pwd)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("err: %s", err)
|
t.Fatalf("err: %s", err)
|
||||||
}
|
}
|
||||||
@ -27,36 +44,45 @@ func TestFileDetector(t *testing.T) {
|
|||||||
t.Fatal("not ok")
|
t.Fatal("not ok")
|
||||||
}
|
}
|
||||||
|
|
||||||
if output != tc.Output {
|
if out != tc.out {
|
||||||
t.Fatalf("%d: bad: %#v", i, output)
|
t.Fatalf("%d: bad: %#v", i, out)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var noPwdFileTests = []fileTest{
|
||||||
|
{in: "./foo", pwd: "", out: "", err: true},
|
||||||
|
{in: "foo", pwd: "", out: "", err: true},
|
||||||
|
}
|
||||||
|
|
||||||
|
var noPwdUnixFileTests = []fileTest{
|
||||||
|
{in: "/foo", pwd: "", out: "file:///foo", err: false},
|
||||||
|
}
|
||||||
|
|
||||||
|
var noPwdWinFileTests = []fileTest{
|
||||||
|
{in: "/foo", pwd: "", out: "", err: true},
|
||||||
|
{in: `C:\`, pwd: ``, out: `file:///C:/`, err: false},
|
||||||
|
}
|
||||||
|
|
||||||
func TestFileDetector_noPwd(t *testing.T) {
|
func TestFileDetector_noPwd(t *testing.T) {
|
||||||
cases := []struct {
|
if runtime.GOOS == "windows" {
|
||||||
Input string
|
noPwdFileTests = append(noPwdFileTests, noPwdWinFileTests...)
|
||||||
Output string
|
} else {
|
||||||
Err bool
|
noPwdFileTests = append(noPwdFileTests, noPwdUnixFileTests...)
|
||||||
}{
|
|
||||||
{"./foo", "", true},
|
|
||||||
{"foo", "", true},
|
|
||||||
{"/foo", "file:///foo", false},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pwd := ""
|
|
||||||
f := new(FileDetector)
|
f := new(FileDetector)
|
||||||
for i, tc := range cases {
|
for i, tc := range noPwdFileTests {
|
||||||
output, ok, err := f.Detect(tc.Input, pwd)
|
out, ok, err := f.Detect(tc.in, tc.pwd)
|
||||||
if (err != nil) != tc.Err {
|
if (err != nil) != tc.err {
|
||||||
t.Fatalf("%d: err: %s", i, err)
|
t.Fatalf("%d: err: %s", i, err)
|
||||||
}
|
}
|
||||||
if !ok {
|
if !ok {
|
||||||
t.Fatal("not ok")
|
t.Fatal("not ok")
|
||||||
}
|
}
|
||||||
|
|
||||||
if output != tc.Output {
|
if out != tc.out {
|
||||||
t.Fatalf("%d: bad: %#v", i, output)
|
t.Fatalf("%d: bad: %#v", i, out)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user