-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branches 'pub/testutil-pkgdata', 'pub/fsutil-explicit-parents' …
…and 'pub/fsutil-dir-clean' into pub/extract-proper-parent-modes
- Loading branch information
Showing
7 changed files
with
124 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package fsutil | ||
|
||
import ( | ||
"path/filepath" | ||
"strings" | ||
) | ||
|
||
// These functions exists because we work with slash terminated directory paths | ||
// that come from deb package tarballs but standard library path functions | ||
// treat slash terminated paths as unclean. | ||
|
||
// Like filepath.Dir() but trailing slash on input is ignored and the return | ||
// value always includes trailing slash. | ||
// | ||
// Comparison of filepath.Dir() with fsutil.Dir(): | ||
// | ||
// filepath.Dir("/foo/bar/") == "/foo/bar" | ||
// filepath.Dir("/foo/bar") == "/foo" | ||
// | ||
// fsutil.Dir("/foo/bar") == "/foo/" | ||
// fsutil.Dir("/foo/bar/") == "/foo/" | ||
func Dir(path string) string { | ||
parent := filepath.Dir(filepath.Clean(path)) | ||
if parent != "/" { | ||
parent += "/" | ||
} | ||
return parent | ||
} | ||
|
||
// Like filepath.Clean() but keeps trailing slash. | ||
// | ||
// Comparison of filepath.Clean() with fsutil.Clean(): | ||
// | ||
// filepath.Clean("/foo/bar") == "/foo/bar" | ||
// filepath.Clean("/foo/bar/") == "/foo/bar" | ||
// filepath.Clean("/foo/bar/.//) == "/foo/bar" | ||
// | ||
// fsutil.Clean("/foo/bar") == "/foo/bar" | ||
// fsutil.Clean("/foo/bar/") == "/foo/bar/" | ||
// fsutil.Clean("/foo/bar/.//") == "/foo/bar/" | ||
func Clean(path string) string { | ||
clean := filepath.Clean(path) | ||
if strings.HasSuffix(path, "/") { | ||
clean += "/" | ||
} | ||
return clean | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package fsutil_test | ||
|
||
import ( | ||
. "gopkg.in/check.v1" | ||
|
||
"github.com/canonical/chisel/internal/fsutil" | ||
) | ||
|
||
var dirTests = []struct { | ||
input string | ||
output string | ||
}{ | ||
{"/a/b/c", "/a/b/"}, | ||
{"/a/b/c/", "/a/b/"}, | ||
{"/a/b/c//", "/a/b/"}, | ||
{"/a/b/c/././", "/a/b/"}, | ||
{"/a/b/c/.././", "/a/"}, | ||
{"/a/b//c", "/a/b/"}, | ||
{"/a/b/./c", "/a/b/"}, | ||
{"a/b/./c", "a/b/"}, | ||
{"./a/b/./c", "a/b/"}, | ||
{"/", "/"}, | ||
{"///.///", "/"}, | ||
{".", "./"}, | ||
{"", "./"}, | ||
} | ||
|
||
var cleanTests = []struct { | ||
input string | ||
output string | ||
}{ | ||
{"/a/b/c", "/a/b/c"}, | ||
{"/a/b/c/", "/a/b/c/"}, | ||
{"/a/b/c/.//", "/a/b/c/"}, | ||
{"/a/b//./c", "/a/b/c"}, | ||
{"/a/b//./c/", "/a/b/c/"}, | ||
{"/a/b/.././c/", "/a/c/"}, | ||
} | ||
|
||
func (s *S) TestDir(c *C) { | ||
for _, t := range dirTests { | ||
c.Logf("%s => %s", t.input, t.output) | ||
c.Assert(fsutil.Dir(t.input), Equals, t.output) | ||
} | ||
} | ||
|
||
func (s *S) TestClean(c *C) { | ||
for _, t := range cleanTests { | ||
c.Logf("%s => %s", t.input, t.output) | ||
c.Assert(fsutil.Clean(t.input), Equals, t.output) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters