From 6eee9fbcb3d684e0bc64934d6025fda22ef822c4 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sun, 14 Sep 2014 19:28:18 -0700 Subject: [PATCH] config/module: file paths require pwd --- config/module/detect_file.go | 5 +++++ config/module/detect_file_test.go | 28 ++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/config/module/detect_file.go b/config/module/detect_file.go index 7f1632faa..ac9ad8e67 100644 --- a/config/module/detect_file.go +++ b/config/module/detect_file.go @@ -16,6 +16,11 @@ func (d *FileDetector) Detect(src, pwd string) (string, bool, error) { // 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( + "relative paths require a module with a pwd") + } + src = filepath.Join(pwd, src) } diff --git a/config/module/detect_file_test.go b/config/module/detect_file_test.go index 0f76bc05b..6ccd49f4f 100644 --- a/config/module/detect_file_test.go +++ b/config/module/detect_file_test.go @@ -30,3 +30,31 @@ func TestFileDetector(t *testing.T) { } } } + +func TestFileDetector_noPwd(t *testing.T) { + cases := []struct { + Input string + Output string + Err bool + }{ + {"./foo", "", true}, + {"foo", "", true}, + {"/foo", "file:///foo", false}, + } + + pwd := "" + f := new(FileDetector) + for i, tc := range cases { + output, ok, err := f.Detect(tc.Input, 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) + } + } +}