mirror of
https://github.com/golang/go
synced 2024-11-22 02:44:39 -07:00
filepath: new Abs function
R=golang-dev, rsc1, peterGo, bsiegert, r, mattn CC=golang-dev https://golang.org/cl/4271057
This commit is contained in:
parent
7c9c4fc3a1
commit
94e60061eb
@ -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.
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user