From 74cf8fcabd075553600e132f67aef8f9f1271677 Mon Sep 17 00:00:00 2001 From: Emil Hessman Date: Tue, 27 Jan 2015 08:03:12 +0100 Subject: [PATCH] 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. --- config/module/detect_file.go | 4 +- config/module/detect_file_test.go | 82 ++++++++++++++++++++----------- 2 files changed, 56 insertions(+), 30 deletions(-) diff --git a/config/module/detect_file.go b/config/module/detect_file.go index ac9ad8e67..68ff43913 100644 --- a/config/module/detect_file.go +++ b/config/module/detect_file.go @@ -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] == '/' { diff --git a/config/module/detect_file_test.go b/config/module/detect_file_test.go index 02a6ccec2..d20271bd6 100644 --- a/config/module/detect_file_test.go +++ b/config/module/detect_file_test.go @@ -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) } } }