summaryrefslogtreecommitdiffstats
path: root/hugolib/page_time_integration_test.go
blob: 1bf83bdca988f03f9d96424366604783c953a6d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// Copyright 2015 The Hugo Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package hugolib

import (
	"fmt"
	"os"
	"strings"
	"sync"
	"testing"
	"time"

	"github.com/spf13/cast"
)

const (
	pageWithInvalidDate = `---
date: 2010-05-02_15:29:31+08:00
---
Page With Invalid Date (replace T with _ for RFC 3339)`

	pageWithDateRFC3339 = `---
date: 2010-05-02T15:29:31+08:00
---
Page With Date RFC3339`

	pageWithDateRFC3339NoT = `---
date: 2010-05-02 15:29:31+08:00
---
Page With Date RFC3339_NO_T`

	pageWithRFC1123 = `---
date: Sun, 02 May 2010 15:29:31 PST
---
Page With Date RFC1123`

	pageWithDateRFC1123Z = `---
date: Sun, 02 May 2010 15:29:31 +0800
---
Page With Date RFC1123Z`

	pageWithDateRFC822 = `---
date: 02 May 10 15:29 PST
---
Page With Date RFC822`

	pageWithDateRFC822Z = `---
date: 02 May 10 15:29 +0800
---
Page With Date RFC822Z`

	pageWithDateANSIC = `---
date: Sun May 2 15:29:31 2010
---
Page With Date ANSIC`

	pageWithDateUnixDate = `---
date: Sun May 2 15:29:31 PST 2010
---
Page With Date UnixDate`

	pageWithDateRubyDate = `---
date: Sun May 02 15:29:31 +0800 2010
---
Page With Date RubyDate`

	pageWithDateHugoYearNumeric = `---
date: 2010-05-02
---
Page With Date HugoYearNumeric`

	pageWithDateHugoYear = `---
date: 02 May 2010
---
Page With Date HugoYear`

	pageWithDateHugoLong = `---
date: 02 May 2010 15:29 PST
---
Page With Date HugoLong`
)

func TestDegenerateDateFrontMatter(t *testing.T) {
	t.Parallel()
	s := newTestSite(t)
	p, _ := s.NewPageFrom(strings.NewReader(pageWithInvalidDate), "page/with/invalid/date")
	if p.Date != *new(time.Time) {
		t.Fatalf("Date should be set to time.Time zero value.  Got: %s", p.Date)
	}
}

func TestParsingDateInFrontMatter(t *testing.T) {
	t.Parallel()
	s := newTestSite(t)
	tests := []struct {
		buf string
		dt  string
	}{
		{pageWithDateRFC3339, "2010-05-02T15:29:31+08:00"},
		{pageWithDateRFC3339NoT, "2010-05-02T15:29:31+08:00"},
		{pageWithDateRFC1123Z, "2010-05-02T15:29:31+08:00"},
		{pageWithDateRFC822Z, "2010-05-02T15:29:00+08:00"},
		{pageWithDateANSIC, "2010-05-02T15:29:31Z"},
		{pageWithDateRubyDate, "2010-05-02T15:29:31+08:00"},
		{pageWithDateHugoYearNumeric, "2010-05-02T00:00:00Z"},
		{pageWithDateHugoYear, "2010-05-02T00:00:00Z"},
	}

	tzShortCodeTests := []struct {
		buf string
		dt  string
	}{
		{pageWithRFC1123, "2010-05-02T15:29:31-08:00"},
		{pageWithDateRFC822, "2010-05-02T15:29:00-08:00Z"},
		{pageWithDateUnixDate, "2010-05-02T15:29:31-08:00"},
		{pageWithDateHugoLong, "2010-05-02T15:21:00+08:00"},
	}

	if _, err := time.LoadLocation("PST"); err == nil {
		tests = append(tests, tzShortCodeTests...)
	} else {
		fmt.Fprintf(os.Stderr, "Skipping shortname timezone tests.\n")
	}

	for _, test := range tests {
		dt, e := time.Parse(time.RFC3339, test.dt)
		if e != nil {
			t.Fatalf("Unable to parse date time (RFC3339) for running the test: %s", e)
		}
		p, err := s.NewPageFrom(strings.NewReader(test.buf), "page/with/date")
		if err != nil {
			t.Fatalf("Expected to be able to parse page.")
		}
		if !dt.Equal(p.Date) {
			t.Errorf("Date does not equal frontmatter:\n%s\nExpecting: %s\n      Got: %s. Diff: %s\n internal: %#v\n           %#v", test.buf, dt, p.Date, dt.Sub(p.Date), dt, p.Date)
		}
	}
}

// Temp test https://github.com/gohugoio/hugo/issues/3059
func TestParsingDateParallel(t *testing.T) {
	t.Parallel()

	var wg sync.WaitGroup

	for j := 0; j < 100; j++ {
		wg.Add(1)
		go func() {
			defer wg.Done()
			for j := 0; j < 100; j++ {
				dateStr := "2010-05-02 15:29:31 +08:00"

				dt, err := time.Parse("2006-01-02 15:04:05 -07:00", dateStr)
				if err != nil {
					t.Fatal(err)
				}

				if dt.Year() != 2010 {
					t.Fatal("time.Parse: Invalid date:", dt)
				}

				dt2 := cast.ToTime(dateStr)

				if dt2.Year() != 2010 {
					t.Fatal("cast.ToTime: Invalid date:", dt2.Year())
				}
			}
		}()
	}
	wg.Wait()

}