github.com/elmarschill/hugo_sample@v0.47.1/hugolib/page_permalink_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  	"html/template"
    19  	"path/filepath"
    20  	"testing"
    21  
    22  	"github.com/stretchr/testify/require"
    23  
    24  	"github.com/gohugoio/hugo/deps"
    25  )
    26  
    27  func TestPermalink(t *testing.T) {
    28  	t.Parallel()
    29  
    30  	tests := []struct {
    31  		file         string
    32  		base         template.URL
    33  		slug         string
    34  		url          string
    35  		uglyURLs     bool
    36  		canonifyURLs bool
    37  		expectedAbs  string
    38  		expectedRel  string
    39  	}{
    40  		{"x/y/z/boofar.md", "", "", "", false, false, "/x/y/z/boofar/", "/x/y/z/boofar/"},
    41  		{"x/y/z/boofar.md", "", "", "", false, false, "/x/y/z/boofar/", "/x/y/z/boofar/"},
    42  		// Issue #1174
    43  		{"x/y/z/boofar.md", "http://gopher.com/", "", "", false, true, "http://gopher.com/x/y/z/boofar/", "/x/y/z/boofar/"},
    44  		{"x/y/z/boofar.md", "http://gopher.com/", "", "", true, true, "http://gopher.com/x/y/z/boofar.html", "/x/y/z/boofar.html"},
    45  		{"x/y/z/boofar.md", "", "boofar", "", false, false, "/x/y/z/boofar/", "/x/y/z/boofar/"},
    46  		{"x/y/z/boofar.md", "http://barnew/", "", "", false, false, "http://barnew/x/y/z/boofar/", "/x/y/z/boofar/"},
    47  		{"x/y/z/boofar.md", "http://barnew/", "boofar", "", false, false, "http://barnew/x/y/z/boofar/", "/x/y/z/boofar/"},
    48  		{"x/y/z/boofar.md", "", "", "", true, false, "/x/y/z/boofar.html", "/x/y/z/boofar.html"},
    49  		{"x/y/z/boofar.md", "", "", "", true, false, "/x/y/z/boofar.html", "/x/y/z/boofar.html"},
    50  		{"x/y/z/boofar.md", "", "boofar", "", true, false, "/x/y/z/boofar.html", "/x/y/z/boofar.html"},
    51  		{"x/y/z/boofar.md", "http://barnew/", "", "", true, false, "http://barnew/x/y/z/boofar.html", "/x/y/z/boofar.html"},
    52  		{"x/y/z/boofar.md", "http://barnew/", "boofar", "", true, false, "http://barnew/x/y/z/boofar.html", "/x/y/z/boofar.html"},
    53  		{"x/y/z/boofar.md", "http://barnew/boo/", "booslug", "", true, false, "http://barnew/boo/x/y/z/booslug.html", "/boo/x/y/z/booslug.html"},
    54  		{"x/y/z/boofar.md", "http://barnew/boo/", "booslug", "", false, true, "http://barnew/boo/x/y/z/booslug/", "/x/y/z/booslug/"},
    55  		{"x/y/z/boofar.md", "http://barnew/boo/", "booslug", "", false, false, "http://barnew/boo/x/y/z/booslug/", "/boo/x/y/z/booslug/"},
    56  		{"x/y/z/boofar.md", "http://barnew/boo/", "booslug", "", true, true, "http://barnew/boo/x/y/z/booslug.html", "/x/y/z/booslug.html"},
    57  		{"x/y/z/boofar.md", "http://barnew/boo", "booslug", "", true, true, "http://barnew/boo/x/y/z/booslug.html", "/x/y/z/booslug.html"},
    58  
    59  		// test URL overrides
    60  		{"x/y/z/boofar.md", "", "", "/z/y/q/", false, false, "/z/y/q/", "/z/y/q/"},
    61  	}
    62  
    63  	for i, test := range tests {
    64  
    65  		cfg, fs := newTestCfg()
    66  
    67  		cfg.Set("uglyURLs", test.uglyURLs)
    68  		cfg.Set("canonifyURLs", test.canonifyURLs)
    69  		cfg.Set("baseURL", test.base)
    70  
    71  		pageContent := fmt.Sprintf(`---
    72  title: Page
    73  slug: %q
    74  url: %q
    75  ---
    76  Content
    77  `, test.slug, test.url)
    78  
    79  		writeSource(t, fs, filepath.Join("content", filepath.FromSlash(test.file)), pageContent)
    80  
    81  		s := buildSingleSite(t, deps.DepsCfg{Fs: fs, Cfg: cfg}, BuildCfg{SkipRender: true})
    82  		require.Len(t, s.RegularPages, 1)
    83  
    84  		p := s.RegularPages[0]
    85  
    86  		u := p.Permalink()
    87  
    88  		expected := test.expectedAbs
    89  		if u != expected {
    90  			t.Fatalf("[%d] Expected abs url: %s, got: %s", i, expected, u)
    91  		}
    92  
    93  		u = p.RelPermalink()
    94  
    95  		expected = test.expectedRel
    96  		if u != expected {
    97  			t.Errorf("[%d] Expected rel url: %s, got: %s", i, expected, u)
    98  		}
    99  	}
   100  }