Use error values in tests
This commit is contained in:
parent
72f5c01749
commit
b218223d02
6
parse.go
6
parse.go
@ -9,6 +9,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var errUnsupportedListLine = errors.New("unsupported LIST line")
|
var errUnsupportedListLine = errors.New("unsupported LIST line")
|
||||||
|
var errUnsupportedListDate = errors.New("unsupported LIST date")
|
||||||
|
var errUnknownListEntryType = errors.New("unknown entry type")
|
||||||
|
|
||||||
type parseFunc func(string, time.Time, *time.Location) (*Entry, error)
|
type parseFunc func(string, time.Time, *time.Location) (*Entry, error)
|
||||||
|
|
||||||
@ -132,7 +134,7 @@ func parseLsListLine(line string, now time.Time, loc *time.Location) (*Entry, er
|
|||||||
case 'l':
|
case 'l':
|
||||||
e.Type = EntryTypeLink
|
e.Type = EntryTypeLink
|
||||||
default:
|
default:
|
||||||
return nil, errors.New("unknown entry type")
|
return nil, errUnknownListEntryType
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := e.setTime(fields[5:8], now, loc); err != nil {
|
if err := e.setTime(fields[5:8], now, loc); err != nil {
|
||||||
@ -246,7 +248,7 @@ func (e *Entry) setTime(fields []string, now time.Time, loc *time.Location) (err
|
|||||||
|
|
||||||
} else { // only the date
|
} else { // only the date
|
||||||
if len(fields[2]) != 4 {
|
if len(fields[2]) != 4 {
|
||||||
return errors.New("invalid year format in time string")
|
return errUnsupportedListDate
|
||||||
}
|
}
|
||||||
timeStr := fmt.Sprintf("%s %s %s 00:00", fields[1], fields[0], fields[2])
|
timeStr := fmt.Sprintf("%s %s %s 00:00", fields[1], fields[0], fields[2])
|
||||||
e.Time, err = time.ParseInLocation("_2 Jan 2006 15:04", timeStr, loc)
|
e.Time, err = time.ParseInLocation("_2 Jan 2006 15:04", timeStr, loc)
|
||||||
|
@ -24,7 +24,7 @@ type line struct {
|
|||||||
|
|
||||||
type unsupportedLine struct {
|
type unsupportedLine struct {
|
||||||
line string
|
line string
|
||||||
err string
|
err error
|
||||||
}
|
}
|
||||||
|
|
||||||
var listTests = []line{
|
var listTests = []line{
|
||||||
@ -72,14 +72,14 @@ var listTests = []line{
|
|||||||
|
|
||||||
// Not supported, we expect a specific error message
|
// Not supported, we expect a specific error message
|
||||||
var listTestsFail = []unsupportedLine{
|
var listTestsFail = []unsupportedLine{
|
||||||
{"d [R----F--] supervisor 512 Jan 16 18:53 login", "Unsupported LIST line"},
|
{"d [R----F--] supervisor 512 Jan 16 18:53 login", errUnsupportedListLine},
|
||||||
{"- [R----F--] rhesus 214059 Oct 20 15:27 cx.exe", "Unsupported LIST line"},
|
{"- [R----F--] rhesus 214059 Oct 20 15:27 cx.exe", errUnsupportedListLine},
|
||||||
{"drwxr-xr-x 3 110 1002 3 Dec 02 209 pub", "Invalid year format in time string"},
|
{"drwxr-xr-x 3 110 1002 3 Dec 02 209 pub", errUnsupportedListDate},
|
||||||
{"modify=20150806235817;invalid;UNIX.owner=0; movies", "Unsupported LIST line"},
|
{"modify=20150806235817;invalid;UNIX.owner=0; movies", errUnsupportedListLine},
|
||||||
{"Zrwxrwxrwx 1 root other 7 Jan 25 00:17 bin -> usr/bin", "Unknown entry type"},
|
{"Zrwxrwxrwx 1 root other 7 Jan 25 00:17 bin -> usr/bin", errUnknownListEntryType},
|
||||||
{"total 1", "Unsupported LIST line"},
|
{"total 1", errUnsupportedListLine},
|
||||||
{"000000000x ", "Unsupported LIST line"}, // see https://github.com/jlaffaye/ftp/issues/97
|
{"000000000x ", errUnsupportedListLine}, // see https://github.com/jlaffaye/ftp/issues/97
|
||||||
{"", "Unsupported LIST line"},
|
{"", errUnsupportedListLine},
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestParseValidListLine(t *testing.T) {
|
func TestParseValidListLine(t *testing.T) {
|
||||||
@ -110,8 +110,8 @@ func TestParseUnsupportedListLine(t *testing.T) {
|
|||||||
if err == nil {
|
if err == nil {
|
||||||
t.Errorf("parseListLine(%v) expected to fail", lt.line)
|
t.Errorf("parseListLine(%v) expected to fail", lt.line)
|
||||||
}
|
}
|
||||||
if err.Error() != lt.err {
|
if err != lt.err {
|
||||||
t.Errorf("parseListLine(%v) expected to fail with error: '%s'; was: '%s'", lt.line, lt.err, err.Error())
|
t.Errorf("parseListLine(%v) expected to fail with error: '%s'; was: '%s'", lt.line, lt.err.Error(), err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user