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