]> Cypherpunks repositories - gostls13.git/commitdiff
filepath: new Abs function
authorGustavo Niemeyer <gustavo@niemeyer.net>
Tue, 5 Apr 2011 01:29:24 +0000 (18:29 -0700)
committerRob Pike <r@golang.org>
Tue, 5 Apr 2011 01:29:24 +0000 (18:29 -0700)
R=golang-dev, rsc1, peterGo, bsiegert, r, mattn
CC=golang-dev
https://golang.org/cl/4271057

src/pkg/path/filepath/path.go
src/pkg/path/filepath/path_test.go

index 6cd6cf2ab094034a4ac55ad8befbbae83c6a185a..4907dac937d60170cd35da05e5d9b77c3b4f79a0 100644 (file)
@@ -231,6 +231,21 @@ func EvalSymlinks(path string) (string, os.Error) {
        return Clean(b.String()), nil
 }
 
+// Abs returns an absolute representation of path.
+// If the path is not absolute it will be joined with the current
+// working directory to turn it into an absolute path.  The absolute
+// path name for a given file is not guaranteed to be unique.
+func Abs(path string) (string, os.Error) {
+       if IsAbs(path) {
+               return path, nil
+       }
+       wd, err := os.Getwd()
+       if err != nil {
+               return "", err
+       }
+       return Join(wd, path), nil
+}
+
 // Visitor methods are invoked for corresponding file tree entries
 // visited by Walk. The parameter path is the full path of f relative
 // to root.
index 2af6e5132438403e493888fd39b2bf381a0b1480..7ef69461ee25defb4a2860991f33b336b7a38f54 100644 (file)
@@ -9,6 +9,7 @@ import (
        "path/filepath"
        "reflect"
        "runtime"
+       "strings"
        "testing"
 )
 
@@ -459,9 +460,9 @@ func TestEvalSymlinks(t *testing.T) {
        // relative
        for _, d := range EvalSymlinksTests {
                if p, err := filepath.EvalSymlinks(d.path); err != nil {
-                       t.Errorf("EvalSymlinks(%v) error: %v", d.path, err)
+                       t.Errorf("EvalSymlinks(%q) error: %v", d.path, err)
                } else if p != d.dest {
-                       t.Errorf("EvalSymlinks(%v)=%v, want %v", d.path, p, d.dest)
+                       t.Errorf("EvalSymlinks(%q)=%q, want %q", d.path, p, d.dest)
                }
        }
        // absolute
@@ -476,9 +477,49 @@ func TestEvalSymlinks(t *testing.T) {
                        filepath.Join(testroot, d.dest),
                }
                if p, err := filepath.EvalSymlinks(a.path); err != nil {
-                       t.Errorf("EvalSymlinks(%v) error: %v", a.path, err)
+                       t.Errorf("EvalSymlinks(%q) error: %v", a.path, err)
                } else if p != a.dest {
-                       t.Errorf("EvalSymlinks(%v)=%v, want %v", a.path, p, a.dest)
+                       t.Errorf("EvalSymlinks(%q)=%q, want %q", a.path, p, a.dest)
+               }
+       }
+}
+
+// Test paths relative to $GOROOT/src
+var abstests = []string{
+       "../AUTHORS",
+       "pkg/../../AUTHORS",
+       "Make.pkg",
+       "pkg/Makefile",
+
+       // Already absolute
+       "$GOROOT/src/Make.pkg",
+}
+
+func TestAbs(t *testing.T) {
+       oldwd, err := os.Getwd()
+       if err != nil {
+               t.Fatal("Getwd failed: " + err.String())
+       }
+       defer os.Chdir(oldwd)
+       goroot := os.Getenv("GOROOT")
+       cwd := filepath.Join(goroot, "src")
+       os.Chdir(cwd)
+       for _, path := range abstests {
+               path = strings.Replace(path, "$GOROOT", goroot, -1)
+               abspath, err := filepath.Abs(path)
+               if err != nil {
+                       t.Errorf("Abs(%q) error: %v", path, err)
+               }
+               info, err := os.Stat(path)
+               if err != nil {
+                       t.Errorf("%s: %s", path, err)
+               }
+               absinfo, err := os.Stat(abspath)
+               if err != nil || absinfo.Ino != info.Ino {
+                       t.Errorf("Abs(%q)=%q, not the same file", path, abspath)
+               }
+               if !filepath.IsAbs(abspath) {
+                       t.Errorf("Abs(%q)=%q, not an absolute path", path, abspath)
                }
        }
 }