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:
Emil Hessman 2015-01-27 08:03:12 +01:00
parent 160e4f926e
commit 74cf8fcabd
2 changed files with 56 additions and 30 deletions

View File

@ -13,8 +13,6 @@ func (d *FileDetector) Detect(src, pwd string) (string, bool, error) {
return "", false, nil
}
// Make sure we're using "/" even on Windows. URLs are "/"-based.
src = filepath.ToSlash(src)
if !filepath.IsAbs(src) {
if pwd == "" {
return "", true, fmt.Errorf(
@ -23,6 +21,8 @@ func (d *FileDetector) Detect(src, pwd string) (string, bool, error) {
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
if src[0] == '/' {

View File

@ -1,25 +1,42 @@
package module
import (
"runtime"
"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) {
cases := []struct {
Input string
Output string
}{
{"./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"},
if runtime.GOOS == "windows" {
fileTests = append(fileTests, winFileTests...)
} else {
fileTests = append(fileTests, unixFileTests...)
}
pwd := "/pwd"
f := new(FileDetector)
for i, tc := range cases {
output, ok, err := f.Detect(tc.Input, pwd)
for i, tc := range fileTests {
out, ok, err := f.Detect(tc.in, tc.pwd)
if err != nil {
t.Fatalf("err: %s", err)
}
@ -27,36 +44,45 @@ func TestFileDetector(t *testing.T) {
t.Fatal("not ok")
}
if output != tc.Output {
t.Fatalf("%d: bad: %#v", i, output)
if out != tc.out {
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) {
cases := []struct {
Input string
Output string
Err bool
}{
{"./foo", "", true},
{"foo", "", true},
{"/foo", "file:///foo", false},
if runtime.GOOS == "windows" {
noPwdFileTests = append(noPwdFileTests, noPwdWinFileTests...)
} else {
noPwdFileTests = append(noPwdFileTests, noPwdUnixFileTests...)
}
pwd := ""
f := new(FileDetector)
for i, tc := range cases {
output, ok, err := f.Detect(tc.Input, pwd)
if (err != nil) != tc.Err {
for i, tc := range noPwdFileTests {
out, ok, err := f.Detect(tc.in, tc.pwd)
if (err != nil) != tc.err {
t.Fatalf("%d: err: %s", i, err)
}
if !ok {
t.Fatal("not ok")
}
if output != tc.Output {
t.Fatalf("%d: bad: %#v", i, output)
if out != tc.out {
t.Fatalf("%d: bad: %#v", i, out)
}
}
}