github.com/pietrocarrara/hugo@v0.47.1/hugolib/page_time_integration_test.go (about)

     1  // Copyright 2015 The Hugo Authors. All rights reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  // http://www.apache.org/licenses/LICENSE-2.0
     7  //
     8  // Unless required by applicable law or agreed to in writing, software
     9  // distributed under the License is distributed on an "AS IS" BASIS,
    10  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package hugolib
    15  
    16  import (
    17  	"fmt"
    18  	"os"
    19  	"strings"
    20  	"sync"
    21  	"testing"
    22  	"time"
    23  
    24  	"github.com/spf13/cast"
    25  )
    26  
    27  const (
    28  	pageWithInvalidDate = `---
    29  date: 2010-05-02_15:29:31+08:00
    30  ---
    31  Page With Invalid Date (replace T with _ for RFC 3339)`
    32  
    33  	pageWithDateRFC3339 = `---
    34  date: 2010-05-02T15:29:31+08:00
    35  ---
    36  Page With Date RFC3339`
    37  
    38  	pageWithDateRFC3339NoT = `---
    39  date: 2010-05-02 15:29:31+08:00
    40  ---
    41  Page With Date RFC3339_NO_T`
    42  
    43  	pageWithRFC1123 = `---
    44  date: Sun, 02 May 2010 15:29:31 PST
    45  ---
    46  Page With Date RFC1123`
    47  
    48  	pageWithDateRFC1123Z = `---
    49  date: Sun, 02 May 2010 15:29:31 +0800
    50  ---
    51  Page With Date RFC1123Z`
    52  
    53  	pageWithDateRFC822 = `---
    54  date: 02 May 10 15:29 PST
    55  ---
    56  Page With Date RFC822`
    57  
    58  	pageWithDateRFC822Z = `---
    59  date: 02 May 10 15:29 +0800
    60  ---
    61  Page With Date RFC822Z`
    62  
    63  	pageWithDateANSIC = `---
    64  date: Sun May 2 15:29:31 2010
    65  ---
    66  Page With Date ANSIC`
    67  
    68  	pageWithDateUnixDate = `---
    69  date: Sun May 2 15:29:31 PST 2010
    70  ---
    71  Page With Date UnixDate`
    72  
    73  	pageWithDateRubyDate = `---
    74  date: Sun May 02 15:29:31 +0800 2010
    75  ---
    76  Page With Date RubyDate`
    77  
    78  	pageWithDateHugoYearNumeric = `---
    79  date: 2010-05-02
    80  ---
    81  Page With Date HugoYearNumeric`
    82  
    83  	pageWithDateHugoYear = `---
    84  date: 02 May 2010
    85  ---
    86  Page With Date HugoYear`
    87  
    88  	pageWithDateHugoLong = `---
    89  date: 02 May 2010 15:29 PST
    90  ---
    91  Page With Date HugoLong`
    92  )
    93  
    94  func TestDegenerateDateFrontMatter(t *testing.T) {
    95  	t.Parallel()
    96  	s := newTestSite(t)
    97  	p, _ := s.NewPageFrom(strings.NewReader(pageWithInvalidDate), "page/with/invalid/date")
    98  	if p.Date != *new(time.Time) {
    99  		t.Fatalf("Date should be set to time.Time zero value.  Got: %s", p.Date)
   100  	}
   101  }
   102  
   103  func TestParsingDateInFrontMatter(t *testing.T) {
   104  	t.Parallel()
   105  	s := newTestSite(t)
   106  	tests := []struct {
   107  		buf string
   108  		dt  string
   109  	}{
   110  		{pageWithDateRFC3339, "2010-05-02T15:29:31+08:00"},
   111  		{pageWithDateRFC3339NoT, "2010-05-02T15:29:31+08:00"},
   112  		{pageWithDateRFC1123Z, "2010-05-02T15:29:31+08:00"},
   113  		{pageWithDateRFC822Z, "2010-05-02T15:29:00+08:00"},
   114  		{pageWithDateANSIC, "2010-05-02T15:29:31Z"},
   115  		{pageWithDateRubyDate, "2010-05-02T15:29:31+08:00"},
   116  		{pageWithDateHugoYearNumeric, "2010-05-02T00:00:00Z"},
   117  		{pageWithDateHugoYear, "2010-05-02T00:00:00Z"},
   118  	}
   119  
   120  	tzShortCodeTests := []struct {
   121  		buf string
   122  		dt  string
   123  	}{
   124  		{pageWithRFC1123, "2010-05-02T15:29:31-08:00"},
   125  		{pageWithDateRFC822, "2010-05-02T15:29:00-08:00Z"},
   126  		{pageWithDateUnixDate, "2010-05-02T15:29:31-08:00"},
   127  		{pageWithDateHugoLong, "2010-05-02T15:21:00+08:00"},
   128  	}
   129  
   130  	if _, err := time.LoadLocation("PST"); err == nil {
   131  		tests = append(tests, tzShortCodeTests...)
   132  	} else {
   133  		fmt.Fprintf(os.Stderr, "Skipping shortname timezone tests.\n")
   134  	}
   135  
   136  	for _, test := range tests {
   137  		dt, e := time.Parse(time.RFC3339, test.dt)
   138  		if e != nil {
   139  			t.Fatalf("Unable to parse date time (RFC3339) for running the test: %s", e)
   140  		}
   141  		p, err := s.NewPageFrom(strings.NewReader(test.buf), "page/with/date")
   142  		if err != nil {
   143  			t.Fatalf("Expected to be able to parse page.")
   144  		}
   145  		if !dt.Equal(p.Date) {
   146  			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)
   147  		}
   148  	}
   149  }
   150  
   151  // Temp test https://github.com/gohugoio/hugo/issues/3059
   152  func TestParsingDateParallel(t *testing.T) {
   153  	t.Parallel()
   154  
   155  	var wg sync.WaitGroup
   156  
   157  	for j := 0; j < 100; j++ {
   158  		wg.Add(1)
   159  		go func() {
   160  			defer wg.Done()
   161  			for j := 0; j < 100; j++ {
   162  				dateStr := "2010-05-02 15:29:31 +08:00"
   163  
   164  				dt, err := time.Parse("2006-01-02 15:04:05 -07:00", dateStr)
   165  				if err != nil {
   166  					t.Fatal(err)
   167  				}
   168  
   169  				if dt.Year() != 2010 {
   170  					t.Fatal("time.Parse: Invalid date:", dt)
   171  				}
   172  
   173  				dt2 := cast.ToTime(dateStr)
   174  
   175  				if dt2.Year() != 2010 {
   176  					t.Fatal("cast.ToTime: Invalid date:", dt2.Year())
   177  				}
   178  			}
   179  		}()
   180  	}
   181  	wg.Wait()
   182  
   183  }