Added the path modules test files

Added the new path modules test file. This replaces the old
helpers_test.go file.

The currently failing tests are:
TestReplaceExtension
TestFilename
TestFileAndExt
TestGuessSection
TestFindCWD
TestWriteToDisk

In addition the TestSafeWriteToDisk test case is currently disabled.
It will panic if enabled.

In addition there are some minor changes to path.go. They are:
Refactored MakePathToLower to simplify it.
Commented out, pending removal, Sanitize as it appears to be unused.
Fixed the resource leak in UnicodeSanitize

Conflicts:
	helpers/path.go
This commit is contained in:
Owen Waller 2014-09-10 18:48:14 +01:00 committed by spf13
parent 6b619d2cd6
commit e8bbc44ab0
2 changed files with 541 additions and 7 deletions

View file

@ -38,20 +38,21 @@ func MakePath(s string) string {
return UnicodeSanitize(strings.Replace(strings.TrimSpace(s), " ", "-", -1))
}
// MakePathToLowerr creates a Unicode santized string, with the spaces replaced,
// MakePathToLower creates a Unicode santized string, with the spaces replaced,
// and transformed to lower case.
// E.g. Social Media -> social-media
func MakePathToLower(s string) string {
return UnicodeSanitize(strings.ToLower(strings.Replace(strings.TrimSpace(s), " ", "-", -1)))
return strings.ToLower(MakePath(s))
}
func MakeTitle(inpath string) string {
return strings.Replace(strings.TrimSpace(inpath), "-", " ", -1)
}
func Sanitize(s string) string {
return sanitizeRegexp.ReplaceAllString(s, "")
}
// unused
//func Sanitize(s string) string {
// return sanitizeRegexp.ReplaceAllString(s, "")
//}
func UnicodeSanitize(s string) string {
source := []rune(s)
@ -100,12 +101,15 @@ func IsEmpty(path string, fs afero.Fs) (bool, error) {
return false, err
}
if fi.IsDir() {
f, err := fs.Open(path)
f, err := os.Open(path)
// FIX: Resource leak - f.close() should be called here by defer or is missed
// if the err != nil branch is taken.
defer f.Close()
if err != nil {
return false, err
}
list, err := f.Readdir(-1)
f.Close()
// f.Close() - see bug fix above
return len(list) == 0, nil
} else {
return fi.Size() == 0, nil