Added CurrentDir() and ChangeDirToParent()

Added more tests.
This commit is contained in:
jlaffaye 2011-09-07 16:52:52 +02:00
parent 68f080c45f
commit 6110501482
2 changed files with 48 additions and 2 deletions

View File

@ -8,6 +8,7 @@ import (
const ( const (
testData = "Just some text" testData = "Just some text"
testDir = "mydir"
) )
func TestConn(t *testing.T) { func TestConn(t *testing.T) {
@ -61,15 +62,39 @@ func TestConn(t *testing.T) {
t.Error(err) t.Error(err)
} }
err = c.MakeDir("mydir") err = c.MakeDir(testDir)
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
err = c.RemoveDir("mydir") err = c.ChangeDir(testDir)
if err != nil {
t.Error(err)
}
dir, err := c.CurrentDir()
if err != nil {
t.Error(err)
} else {
if dir != "/" + testDir {
t.Error("Wrong dir: " + dir)
}
}
err = c.ChangeDirToParent()
if err != nil {
t.Error(err)
}
err = c.RemoveDir(testDir)
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
c.Quit() c.Quit()
err = c.NoOp()
if err == nil {
t.Error("Expected error")
}
} }

21
ftp.go
View File

@ -188,6 +188,27 @@ func (c *ServerConn) ChangeDir(path string) os.Error {
return err return err
} }
func (c *ServerConn) ChangeDirToParent() os.Error {
_, _, err := c.cmd(StatusRequestedFileActionOK, "CDUP")
return err
}
func (c *ServerConn) CurrentDir() (string, os.Error) {
_, msg, err := c.cmd(StatusPathCreated, "PWD")
if err != nil {
return "", err
}
start := strings.Index(msg, "\"")
end := strings.LastIndex(msg, "\"")
if start == -1 || end == -1 {
return "", os.NewError("Unsuported PWD response format")
}
return msg[start+1:end], nil
}
// Retrieves a remote file // Retrieves a remote file
func (c *ServerConn) Retr(path string) (io.ReadCloser, os.Error) { func (c *ServerConn) Retr(path string) (io.ReadCloser, os.Error) {
conn, err := c.cmdDataConn("RETR %s", path) conn, err := c.cmdDataConn("RETR %s", path)