2013-10-07 04:57:45 +00:00
|
|
|
package hugolib
|
|
|
|
|
|
|
|
import (
|
2013-10-07 05:53:18 +00:00
|
|
|
"html/template"
|
2014-12-07 18:48:00 +00:00
|
|
|
"path/filepath"
|
2013-10-07 04:57:45 +00:00
|
|
|
"testing"
|
2014-04-07 15:44:13 +00:00
|
|
|
|
2014-10-17 00:20:09 +00:00
|
|
|
"github.com/spf13/hugo/source"
|
2014-04-07 15:44:13 +00:00
|
|
|
"github.com/spf13/viper"
|
2013-10-07 04:57:45 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestPermalink(t *testing.T) {
|
2015-05-20 06:21:21 +00:00
|
|
|
viper.Reset()
|
|
|
|
defer viper.Reset()
|
|
|
|
|
2013-10-07 05:53:18 +00:00
|
|
|
tests := []struct {
|
2014-12-12 19:28:28 +00:00
|
|
|
file string
|
|
|
|
base template.URL
|
|
|
|
slug string
|
|
|
|
url string
|
2015-03-11 17:34:57 +00:00
|
|
|
uglyURLs bool
|
|
|
|
canonifyURLs bool
|
2014-12-12 19:28:28 +00:00
|
|
|
expectedAbs string
|
|
|
|
expectedRel string
|
2013-10-07 05:53:18 +00:00
|
|
|
}{
|
2015-05-28 21:05:13 +00:00
|
|
|
{"x/y/z/boofar.md", "", "", "", false, false, "/x/y/z/boofar/", "/x/y/z/boofar/"},
|
|
|
|
{"x/y/z/boofar.md", "", "", "", false, false, "/x/y/z/boofar/", "/x/y/z/boofar/"},
|
2015-05-27 18:41:25 +00:00
|
|
|
// Issue #1174
|
2015-05-28 21:05:13 +00:00
|
|
|
{"x/y/z/boofar.md", "http://gopher.com/", "", "", false, true, "http://gopher.com/x/y/z/boofar/", "/x/y/z/boofar/"},
|
|
|
|
{"x/y/z/boofar.md", "http://gopher.com/", "", "", true, true, "http://gopher.com/x/y/z/boofar.html", "/x/y/z/boofar.html"},
|
|
|
|
{"x/y/z/boofar.md", "", "boofar", "", false, false, "/x/y/z/boofar/", "/x/y/z/boofar/"},
|
|
|
|
{"x/y/z/boofar.md", "http://barnew/", "", "", false, false, "http://barnew/x/y/z/boofar/", "/x/y/z/boofar/"},
|
|
|
|
{"x/y/z/boofar.md", "http://barnew/", "boofar", "", false, false, "http://barnew/x/y/z/boofar/", "/x/y/z/boofar/"},
|
|
|
|
{"x/y/z/boofar.md", "", "", "", true, false, "/x/y/z/boofar.html", "/x/y/z/boofar.html"},
|
|
|
|
{"x/y/z/boofar.md", "", "", "", true, false, "/x/y/z/boofar.html", "/x/y/z/boofar.html"},
|
|
|
|
{"x/y/z/boofar.md", "", "boofar", "", true, false, "/x/y/z/boofar.html", "/x/y/z/boofar.html"},
|
|
|
|
{"x/y/z/boofar.md", "http://barnew/", "", "", true, false, "http://barnew/x/y/z/boofar.html", "/x/y/z/boofar.html"},
|
|
|
|
{"x/y/z/boofar.md", "http://barnew/", "boofar", "", true, false, "http://barnew/x/y/z/boofar.html", "/x/y/z/boofar.html"},
|
|
|
|
{"x/y/z/boofar.md", "http://barnew/boo/", "boofar", "", true, false, "http://barnew/boo/x/y/z/boofar.html", "/boo/x/y/z/boofar.html"},
|
|
|
|
{"x/y/z/boofar.md", "http://barnew/boo/", "boofar", "", false, true, "http://barnew/boo/x/y/z/boofar/", "/x/y/z/boofar/"},
|
|
|
|
{"x/y/z/boofar.md", "http://barnew/boo/", "boofar", "", false, false, "http://barnew/boo/x/y/z/boofar/", "/boo/x/y/z/boofar/"},
|
|
|
|
{"x/y/z/boofar.md", "http://barnew/boo/", "boofar", "", true, true, "http://barnew/boo/x/y/z/boofar.html", "/x/y/z/boofar.html"},
|
|
|
|
{"x/y/z/boofar.md", "http://barnew/boo", "boofar", "", true, true, "http://barnew/boo/x/y/z/boofar.html", "/x/y/z/boofar.html"},
|
2014-02-28 07:30:12 +00:00
|
|
|
|
2015-03-18 05:16:54 +00:00
|
|
|
// test URL overrides
|
2015-05-28 21:05:13 +00:00
|
|
|
{"x/y/z/boofar.md", "", "", "/z/y/q/", false, false, "/z/y/q/", "/z/y/q/"},
|
2013-10-07 05:53:18 +00:00
|
|
|
}
|
2013-10-07 04:57:45 +00:00
|
|
|
|
2014-10-17 00:20:09 +00:00
|
|
|
viper.Set("DefaultExtension", "html")
|
|
|
|
|
2014-08-22 11:59:59 +00:00
|
|
|
for i, test := range tests {
|
2015-03-11 17:34:57 +00:00
|
|
|
viper.Set("uglyurls", test.uglyURLs)
|
|
|
|
viper.Set("canonifyurls", test.canonifyURLs)
|
2013-10-07 05:53:18 +00:00
|
|
|
p := &Page{
|
|
|
|
Node: Node{
|
2015-03-18 05:16:54 +00:00
|
|
|
URLPath: URLPath{
|
2014-02-28 07:30:12 +00:00
|
|
|
Section: "z",
|
2015-03-18 05:16:54 +00:00
|
|
|
URL: test.url,
|
2014-02-28 07:30:12 +00:00
|
|
|
},
|
2014-05-28 23:11:54 +00:00
|
|
|
Site: &SiteInfo{
|
2015-03-18 05:16:54 +00:00
|
|
|
BaseURL: test.base,
|
2014-02-02 00:58:14 +00:00
|
|
|
},
|
2013-10-07 05:53:18 +00:00
|
|
|
},
|
2014-12-07 18:48:00 +00:00
|
|
|
Source: Source{File: *source.NewFile(filepath.FromSlash(test.file))},
|
2013-11-19 13:10:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if test.slug != "" {
|
|
|
|
p.update(map[string]interface{}{
|
|
|
|
"slug": test.slug,
|
|
|
|
})
|
2013-10-07 05:53:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
u, err := p.Permalink()
|
|
|
|
if err != nil {
|
2014-08-22 11:59:59 +00:00
|
|
|
t.Errorf("Test %d: Unable to process permalink: %s", i, err)
|
2013-10-07 05:53:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
expected := test.expectedAbs
|
|
|
|
if u != expected {
|
2014-08-22 11:59:59 +00:00
|
|
|
t.Errorf("Test %d: Expected abs url: %s, got: %s", i, expected, u)
|
2013-10-07 05:53:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
u, err = p.RelPermalink()
|
|
|
|
if err != nil {
|
2014-08-22 11:59:59 +00:00
|
|
|
t.Errorf("Test %d: Unable to process permalink: %s", i, err)
|
2013-10-07 05:53:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
expected = test.expectedRel
|
|
|
|
if u != expected {
|
2014-12-12 19:28:28 +00:00
|
|
|
t.Errorf("Test %d: Expected rel url: %s, got: %s", i, expected, u)
|
2013-10-07 05:53:18 +00:00
|
|
|
}
|
|
|
|
}
|
2013-10-07 04:57:45 +00:00
|
|
|
}
|