aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGustavo Niemeyer <gustavo@niemeyer.net>2011-04-04 18:29:24 -0700
committerRob Pike <r@golang.org>2011-04-04 18:29:24 -0700
commit94e60061eb3a2c4ea3627cf9ae52193cc1fbe5a3 (patch)
tree237732ec8c15578201aaa2263a2baed435ea78ae
parent7c9c4fc3a189399385960254facfab6feff55d5f (diff)
downloadgo-94e60061eb3a2c4ea3627cf9ae52193cc1fbe5a3.tar.gz
go-94e60061eb3a2c4ea3627cf9ae52193cc1fbe5a3.zip
filepath: new Abs function
R=golang-dev, rsc1, peterGo, bsiegert, r, mattn CC=golang-dev https://golang.org/cl/4271057
-rw-r--r--src/pkg/path/filepath/path.go15
-rw-r--r--src/pkg/path/filepath/path_test.go49
2 files changed, 60 insertions, 4 deletions
diff --git a/src/pkg/path/filepath/path.go b/src/pkg/path/filepath/path.go
index 6cd6cf2ab0..4907dac937 100644
--- a/src/pkg/path/filepath/path.go
+++ b/src/pkg/path/filepath/path.go
@@ -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.
diff --git a/src/pkg/path/filepath/path_test.go b/src/pkg/path/filepath/path_test.go
index 2af6e51324..7ef69461ee 100644
--- a/src/pkg/path/filepath/path_test.go
+++ b/src/pkg/path/filepath/path_test.go
@@ -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)
}
}
}