]> xenbits.xensource.com Git - people/gdunlap/xsatool.git/commitdiff
meta: Allow absolute pathnames in .xsatool
authorGeorge Dunlap <george.dunlap@citrix.com>
Fri, 8 Dec 2017 14:36:25 +0000 (14:36 +0000)
committerGeorge Dunlap <george.dunlap@citrix.com>
Fri, 8 Dec 2017 16:29:48 +0000 (16:29 +0000)
If the path has a leading slash, treat it as an absolute pathname
rather than being relative to the directory .xsatool is in.

Also add unit tests to make sure the cases work (and continue to
work).

Signed-off-by: George Dunlap <george.dunlap@citrix.com>
meta.go
meta_test.go [new file with mode: 0644]

diff --git a/meta.go b/meta.go
index b87a50c6c7066edf7f07230a3aa6039a721164cb..0a9736ea05568ed9faf0b99af5ee4091bd54a6f2 100644 (file)
--- a/meta.go
+++ b/meta.go
@@ -166,8 +166,17 @@ func (tc *ToolConfig) Save() (err error) {
 }
 
 func (tc ToolConfig) GetPath(t Tree) (s string) {
-       if tc.rootDir != "" && tc.Paths[t] != "" {
-               return path.Clean(tc.rootDir + "/" + tc.Paths[t])
+       if tc.Paths[t] != "" {
+               s = tc.Paths[t]
+               if !path.IsAbs(s) {
+                       if tc.rootDir != "" {
+                               s = tc.rootDir + "/" + s
+                       } else {
+                               s = ""
+                               return
+                       }
+               }
+               s = path.Clean(s)
        }
        return
 }
diff --git a/meta_test.go b/meta_test.go
new file mode 100644 (file)
index 0000000..971b959
--- /dev/null
@@ -0,0 +1,35 @@
+package main
+
+import "testing"
+
+func TestGetPath(t *testing.T) {
+       tests := []struct {
+               xenpath string
+               rootDir string
+               expected string
+       }{
+               {"xen.git", "/build/hg/security", "/build/hg/security/xen.git"},
+               {"../xen.git", "/build/hg/security", "/build/hg/xen.git"},
+               {"/build/hg/xen.git", "/build/hg/security", "/build/hg/xen.git"},
+               {"/build/hg/xen.git", "", "/build/hg/xen.git"},
+               {"xen.git", "", ""},
+       }
+
+       tc := ToolConfig{}
+       tc.Paths = make(map[Tree]string)
+
+       for i := range tests {
+               t.Logf("Checking [\"%s\" \"%s\"]\n",
+                       tests[i].xenpath, tests[i].rootDir)
+               tc.Paths[TreeXen] = tests[i].xenpath
+               tc.rootDir = tests[i].rootDir
+
+               out := tc.GetPath(TreeXen)
+
+               if out != tests[i].expected {
+                       t.Errorf("Expected %s, got %s\n",
+                               tests[i].expected,
+                               out)
+               }
+       }
+}