Skip to content

Commit 6a7966f

Browse files
authored
Merge pull request #83 from bentranter/formatting-nil-time
Format nil time.Time's without panicking
2 parents 98f2ecb + 2fefd0a commit 6a7966f

File tree

5 files changed

+22
-8
lines changed

5 files changed

+22
-8
lines changed

.travis.yml

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
language: go
22

33
go:
4-
- 1.4
5-
- 1.5
6-
- 1.6
4+
- 1.7
5+
- 1.8
6+
- 1.9
7+
- "1.10"
8+
- tip
79

810
install:
911
- go get -t ./...

app/handler/auth_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,8 @@ func TestUserWithoutAuthentication(t *testing.T) {
187187
ctx := mockContext(nil, "GET", "/admin/")
188188
ctx.App.ServeHTTP(ctx.Response, ctx.Request)
189189

190-
Convey("Status code should be 301 redirection", func() {
191-
So(ctx.Response.(*httptest.ResponseRecorder).Code, ShouldEqual, 301)
190+
Convey("Status code should be 302 redirection", func() {
191+
So(ctx.Response.(*httptest.ResponseRecorder).Code, ShouldEqual, 302)
192192
})
193193
})
194194

app/utils/date.go

+4
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ var conversion = map[rune]string{
5656
// %z numbers representing the timezone, ex: "-0700"
5757
// %L milliseconds, ex: ".000"
5858
func DateFormat(t *time.Time, format string) string {
59+
if t == nil {
60+
return ""
61+
}
62+
5963
retval := make([]byte, 0, len(format))
6064
for i, ni := 0, 0; i < len(format); i = ni + 2 {
6165
ni = strings.IndexByte(format[i:], '%')

app/utils/date_test.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ package utils
22

33
import (
44
"fmt"
5-
. "github.com/smartystreets/goconvey/convey"
65
"testing"
76
"time"
7+
8+
. "github.com/smartystreets/goconvey/convey"
89
)
910

1011
func ExampleDateFormat() {
@@ -22,4 +23,11 @@ func TestDateFormat(t *testing.T) {
2223
So(dateFmt, ShouldEqual, "2009-11-10 23:00")
2324
})
2425
})
26+
27+
Convey("It should not panic when trying to format nil dates", t, func() {
28+
dateFmt := DateFormat(nil, "%Y-%m-%d %H:%M")
29+
Convey("Test DateFormat", func() {
30+
So(dateFmt, ShouldEqual, "")
31+
})
32+
})
2533
}

app/utils/file.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,13 @@ func CopyDir(source string, dest string) (err error) {
107107
if entry.IsDir() {
108108
err = CopyDir(sfp, dfp)
109109
if err != nil {
110-
log.Println("[Error]: %v", err)
110+
log.Printf("[Error]: %v", err)
111111
}
112112
} else {
113113
// perform copy
114114
err = CopyFile(sfp, dfp)
115115
if err != nil {
116-
log.Println("[Error]: %v", err)
116+
log.Printf("[Error]: %v", err)
117117
}
118118
}
119119

0 commit comments

Comments
 (0)