Improved error handling and removing / restriction from ChangeDirectory

This commit is contained in:
Denis-Cosmin Nutiu 2017-11-09 23:02:11 +02:00
parent acc1b82b8a
commit 0f365c8a15
3 changed files with 12 additions and 17 deletions

View file

@ -39,10 +39,16 @@ var (
ErrStackCast = StackError{"StackCastError", errors.New("stack can't be casted to selected type")} ErrStackCast = StackError{"StackCastError", errors.New("stack can't be casted to selected type")}
) )
type PathError struct {
Err error
}
func (e PathError) Error() string { return "Error: " + e.Err.Error() }
// PathErrors // PathErrors
var ( var (
ErrInvalidDirectoryName = errors.New("names should not contain / character") ErrInvalidDirectoryName = PathError{errors.New("names should not contain / character")}
ErrNotADirectory = errors.New("file name is not a valid directory") ErrNotADirectory = PathError{errors.New("file name is not a valid directory")}
ErrAlreadyAtBaseDirectory = errors.New("can't go past beyond root directory") ErrAlreadyAtBaseDirectory = PathError{errors.New("can't go past beyond root directory")}
ErrSlashNotAllowed = errors.New("slash is not allowed in file names") ErrSlashNotAllowed = PathError{errors.New("slash is not allowed in file names")}
) )

View file

@ -39,16 +39,13 @@ func MakePathFromStringStack(stack *StringStack) string {
// ChangeDirectory changes the current working directory with respect to BasePath // ChangeDirectory changes the current working directory with respect to BasePath
func ChangeDirectory(stack *StringStack, directory string) error { func ChangeDirectory(stack *StringStack, directory string) error {
if containsSlash(directory) == true {
return ErrInvalidDirectoryName
}
stack.Push(directory) stack.Push(directory)
path := MakePathFromStringStack(stack) path := MakePathFromStringStack(stack)
fileInfo, err := os.Stat(path) fileInfo, err := os.Stat(path)
if err != nil { if err != nil {
stack.Pop() stack.Pop()
return err return PathError{err}
} }
if fileInfo.IsDir() == false { if fileInfo.IsDir() == false {
@ -64,7 +61,7 @@ func ChangeDirectory(stack *StringStack, directory string) error {
return nil return nil
} }
stack.Pop() stack.Pop()
return os.ErrPermission return PathError{err}
} }
// ChangeDirectoryToPrevious changes the current working directory to the previous one, // ChangeDirectoryToPrevious changes the current working directory to the previous one,

View file

@ -84,14 +84,6 @@ func TestChangeDirectory_InvalidDirectoryIsNotInStack(t *testing.T) {
} }
func TestChangeDirectory_InvalidDirectoryName(t *testing.T) {
st := MakeStringStack(1)
err := ChangeDirectory(st, "some/not/cool/directory/")
if err != ErrInvalidDirectoryName {
t.Error("TestChangeDirectory: Changed directory to something containing the '/' character!")
}
}
func TestChangeDirectoryToPrevious_StackIsEmpty(t *testing.T) { func TestChangeDirectoryToPrevious_StackIsEmpty(t *testing.T) {
st := MakeStringStack(1) st := MakeStringStack(1)
err := ChangeDirectoryToPrevious(st) err := ChangeDirectoryToPrevious(st)