Skip to content

Commit 7580cff

Browse files
committed
Changing API to maintain consistency
Fixes #30, #26 Make Join(...) less opinionated Making Join() simpler so that it simply uses strings.Join() to join the paths together. This will help with really weird cases where cleaning the resulting path might create an incorrect result. I am creating a Clean() function that will allow people to still clean the path if they really want. We shouldn't enforce people to use it. Fixing test
1 parent 9ac76de commit 7580cff

File tree

2 files changed

+39
-14
lines changed

2 files changed

+39
-14
lines changed

path.go

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,8 @@ func (p *Path) RemoveAll() error {
158158
return p.Fs().RemoveAll(p.String())
159159
}
160160

161-
// Rename renames a file
162-
func (p *Path) Rename(newname string) error {
161+
// RenameStr renames a file
162+
func (p *Path) RenameStr(newname string) error {
163163
if err := p.Fs().Rename(p.String(), newname); err != nil {
164164
return err
165165
}
@@ -169,10 +169,9 @@ func (p *Path) Rename(newname string) error {
169169
return nil
170170
}
171171

172-
// RenamePath is the same as Rename except the argument is a Path object. The attributes
173-
// of the path object is retained and does not inherit anything from target.
174-
func (p *Path) RenamePath(target *Path) error {
175-
return p.Rename(target.String())
172+
// Rename renames a file
173+
func (p *Path) Rename(target *Path) error {
174+
return p.RenameStr(target.String())
176175
}
177176

178177
// Stat returns the os.FileInfo of the given path
@@ -392,7 +391,7 @@ func (p *Path) Join(elems ...string) *Path {
392391
for _, path := range elems {
393392
paths = append(paths, path)
394393
}
395-
return NewPathAfero(filepath.Join(paths...), p.Fs())
394+
return NewPathAfero(strings.Join(paths, p.Sep), p.Fs())
396395
}
397396

398397
// JoinPath is the same as Join() except it accepts a path object
@@ -451,6 +450,13 @@ func (p *Path) RelativeTo(other *Path) (*Path, error) {
451450
return NewPathAfero(strings.Join(relativePath, "/"), p.Fs()), nil
452451
}
453452

453+
// RelativeToStr computes a relative version of path to the other path. For instance,
454+
// if the object is /path/to/foo.txt and you provide /path/ as the argment, the
455+
// returned Path object will represent to/foo.txt.
456+
func (p *Path) RelativeToStr(other string) (*Path, error) {
457+
return p.RelativeTo(NewPathAfero(other, p.Fs()))
458+
}
459+
454460
// Lstat lstat's the path if the underlying afero filesystem supports it. If
455461
// the filesystem does not support afero.Lstater, or if the filesystem implements
456462
// afero.Lstater but returns false for the "lstat called" return value.
@@ -474,6 +480,14 @@ func (p *Path) Lstat() (os.FileInfo, error) {
474480
// * filesystem-specific functions *
475481
// *********************************
476482

483+
// SymlinkStr symlinks to the target location. This will fail if the underlying
484+
// afero filesystem does not implement afero.Linker.
485+
//
486+
// THIS METHOD IS NOT TYPE SAFE.
487+
func (p *Path) SymlinkStr(target string) error {
488+
return p.Symlink(NewPathAfero(target, p.Fs()))
489+
}
490+
477491
// Symlink symlinks to the target location. This will fail if the underlying
478492
// afero filesystem does not implement afero.Linker.
479493
//
@@ -539,7 +553,7 @@ func (p *Path) DeepEquals(other *Path) (bool, error) {
539553
return false, err
540554
}
541555

542-
return selfResolved.Equals(otherResolved), nil
556+
return selfResolved.Clean().Equals(otherResolved.Clean()), nil
543557
}
544558

545559
// Equals returns whether or not the object's path is identical
@@ -589,6 +603,12 @@ func (p *Path) Glob(pattern string) ([]*Path, error) {
589603
return Glob(p.Fs(), p.Join(pattern).String())
590604
}
591605

606+
// Clean returns a new object that is a lexically-cleaned
607+
// version of Path.
608+
func (p *Path) Clean() *Path {
609+
return NewPathAfero(filepath.Clean(p.String()), p.Fs())
610+
}
611+
592612
// Mtime returns the modification time of the given path.
593613
func (p *Path) Mtime() (time.Time, error) {
594614
stat, err := p.Stat()

path_test.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ func (p *PathSuite) TestRenameString() {
118118

119119
newPath := p.tmpdir.Join("file2.txt")
120120

121-
err := file.RenamePath(newPath)
121+
err := file.Rename(newPath)
122122
assert.NoError(p.T(), err)
123123
assert.Equal(p.T(), file.String(), p.tmpdir.Join("file2.txt").String())
124124

@@ -265,15 +265,20 @@ func (p *PathSuite) TestIsSymlink() {
265265
}
266266

267267
func (p *PathSuite) TestResolveAll() {
268-
require.NoError(p.T(), p.tmpdir.Join("mnt", "nfs", "data", "users", "home", "LandonTClipp").MkdirAll())
268+
home := p.tmpdir.Join("mnt", "nfs", "data", "users", "home", "LandonTClipp")
269+
require.NoError(p.T(), home.MkdirAll())
269270
require.NoError(p.T(), p.tmpdir.Join("mnt", "nfs", "symlinks").MkdirAll())
270271
require.NoError(p.T(), p.tmpdir.Join("mnt", "nfs", "symlinks", "home").Symlink(NewPath("../data/users/home")))
271272
require.NoError(p.T(), p.tmpdir.Join("home").Symlink(NewPath("./mnt/nfs/symlinks/home")))
272273

273274
resolved, err := p.tmpdir.Join("home/LandonTClipp").ResolveAll()
274-
p.NoError(err)
275-
resolvedParts := resolved.Parts()
276-
p.Equal("mnt/nfs/data/users/home/LandonTClipp", strings.Join(resolvedParts[len(resolvedParts)-6:], resolved.Sep))
275+
p.T().Log(resolved.String())
276+
require.NoError(p.T(), err)
277+
278+
homeResolved, err := home.ResolveAll()
279+
require.NoError(p.T(), err)
280+
281+
p.Equal(homeResolved.Clean().String(), resolved.Clean().String())
277282
}
278283

279284
func (p *PathSuite) TestResolveAllAbsolute() {
@@ -396,7 +401,7 @@ func TestPath_Join(t *testing.T) {
396401
a := afero.NewMemMapFs()
397402
p := NewPathAfero(tt.fields, a)
398403
want := NewPathAfero(tt.want, a)
399-
if got := p.Join(tt.args.elems...); !reflect.DeepEqual(got, want) {
404+
if got := p.Join(tt.args.elems...).Clean(); !reflect.DeepEqual(got, want) {
400405
t.Errorf("Path.Join() = %v, want %v", got, want)
401406
}
402407
})

0 commit comments

Comments
 (0)