2015-12-10 17:19:38 -05:00
|
|
|
// 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.
|
|
|
|
|
2013-09-01 00:13:04 -04:00
|
|
|
package hugolib
|
|
|
|
|
|
|
|
import (
|
2013-09-01 10:43:29 -04:00
|
|
|
"bytes"
|
2014-12-07 13:48:00 -05:00
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
2015-05-20 02:21:21 -04:00
|
|
|
|
|
|
|
"github.com/spf13/hugo/helpers"
|
|
|
|
"github.com/spf13/hugo/source"
|
|
|
|
"github.com/spf13/hugo/target"
|
|
|
|
"github.com/spf13/viper"
|
2013-09-01 00:13:04 -04:00
|
|
|
)
|
|
|
|
|
2016-04-07 09:22:41 -04:00
|
|
|
const aliasDoc1 = "---\ntitle: alias doc\naliases:\n - \"alias1/\"\n - \"alias-2/\"\n---\naliases\n"
|
2013-09-12 19:17:53 -04:00
|
|
|
|
2013-09-20 20:03:43 -04:00
|
|
|
var fakeSource = []source.ByteSource{
|
|
|
|
{
|
2014-12-07 13:48:00 -05:00
|
|
|
Name: filepath.FromSlash("foo/bar/file.md"),
|
2016-03-24 09:42:03 -04:00
|
|
|
Content: []byte(simplePage),
|
2013-09-20 20:03:43 -04:00
|
|
|
},
|
|
|
|
{
|
2014-12-07 13:48:00 -05:00
|
|
|
Name: filepath.FromSlash("alias/test/file1.md"),
|
2016-04-07 09:22:41 -04:00
|
|
|
Content: []byte(aliasDoc1),
|
2013-09-20 20:03:43 -04:00
|
|
|
},
|
|
|
|
{
|
2014-12-07 13:48:00 -05:00
|
|
|
Name: filepath.FromSlash("section/somecontent.html"),
|
2016-03-24 09:42:03 -04:00
|
|
|
Content: []byte(renderNoFrontmatter),
|
2013-09-20 20:03:43 -04:00
|
|
|
},
|
2013-09-01 10:43:29 -04:00
|
|
|
}
|
|
|
|
|
2013-09-05 01:28:59 -04:00
|
|
|
func checkShowPlanExpected(t *testing.T, s *Site, expected string) {
|
2014-12-07 13:48:00 -05:00
|
|
|
|
2013-09-01 10:43:29 -04:00
|
|
|
out := new(bytes.Buffer)
|
|
|
|
if err := s.ShowPlan(out); err != nil {
|
2013-09-05 01:28:59 -04:00
|
|
|
t.Fatalf("ShowPlan unexpectedly returned an error: %s", err)
|
2013-09-01 10:43:29 -04:00
|
|
|
}
|
|
|
|
got := out.String()
|
2014-04-08 21:57:25 -04:00
|
|
|
|
2014-12-07 13:48:00 -05:00
|
|
|
expected = filepath.FromSlash(expected)
|
|
|
|
// hackety hack: alias is an Url
|
|
|
|
expected = strings.Replace(expected, (helpers.FilePathSeparator + " =>"), "/ =>", -1)
|
|
|
|
expected = strings.Replace(expected, "n"+(helpers.FilePathSeparator+"a"), "n/a", -1)
|
2014-04-08 21:57:25 -04:00
|
|
|
gotList := strings.Split(got, "\n")
|
|
|
|
expectedList := strings.Split(expected, "\n")
|
|
|
|
|
2014-12-07 13:48:00 -05:00
|
|
|
diff := DiffStringSlices(gotList, expectedList)
|
2014-04-08 21:57:25 -04:00
|
|
|
|
2014-12-07 13:48:00 -05:00
|
|
|
if len(diff) > 0 {
|
|
|
|
t.Errorf("Got diff in show plan: %s", diff)
|
2013-09-05 01:28:59 -04:00
|
|
|
}
|
2013-09-01 10:43:29 -04:00
|
|
|
}
|
|
|
|
|
2013-09-05 01:28:59 -04:00
|
|
|
func TestDegenerateNoFiles(t *testing.T) {
|
|
|
|
checkShowPlanExpected(t, new(Site), "No source files provided.\n")
|
|
|
|
}
|
2013-09-01 10:43:29 -04:00
|
|
|
|
2013-09-05 01:28:59 -04:00
|
|
|
func TestDegenerateNoTarget(t *testing.T) {
|
2013-09-12 19:17:53 -04:00
|
|
|
s := &Site{
|
2014-03-01 00:31:21 -05:00
|
|
|
Source: &source.InMemorySource{ByteSource: fakeSource},
|
2013-09-12 19:17:53 -04:00
|
|
|
}
|
2016-04-07 14:03:03 -04:00
|
|
|
must(s.createPages())
|
2013-09-18 14:52:30 -04:00
|
|
|
expected := "foo/bar/file.md (renderer: markdown)\n canonical => !no target specified!\n\n" +
|
|
|
|
"alias/test/file1.md (renderer: markdown)\n canonical => !no target specified!\n\n" +
|
|
|
|
"section/somecontent.html (renderer: n/a)\n canonical => !no target specified!\n\n"
|
2013-09-05 01:28:59 -04:00
|
|
|
checkShowPlanExpected(t, s, expected)
|
2013-09-01 00:13:04 -04:00
|
|
|
}
|
2013-09-03 23:52:50 -04:00
|
|
|
|
|
|
|
func TestFileTarget(t *testing.T) {
|
2015-05-20 02:21:21 -04:00
|
|
|
viper.Reset()
|
|
|
|
defer viper.Reset()
|
|
|
|
|
|
|
|
viper.Set("DefaultExtension", "html")
|
|
|
|
|
2013-09-05 01:28:59 -04:00
|
|
|
s := &Site{
|
2014-03-01 00:31:21 -05:00
|
|
|
Source: &source.InMemorySource{ByteSource: fakeSource},
|
2013-09-05 01:28:59 -04:00
|
|
|
}
|
2016-03-05 14:56:38 -05:00
|
|
|
s.aliasTarget()
|
|
|
|
s.pageTarget()
|
2016-04-07 14:03:03 -04:00
|
|
|
must(s.createPages())
|
2013-09-18 14:52:30 -04:00
|
|
|
expected := "foo/bar/file.md (renderer: markdown)\n canonical => foo/bar/file/index.html\n\n" +
|
2013-09-18 13:27:56 -04:00
|
|
|
"alias/test/file1.md (renderer: markdown)\n" +
|
2013-09-12 19:17:53 -04:00
|
|
|
" canonical => alias/test/file1/index.html\n" +
|
|
|
|
" alias1/ => alias1/index.html\n" +
|
2013-09-18 14:52:30 -04:00
|
|
|
" alias-2/ => alias-2/index.html\n\n" +
|
|
|
|
"section/somecontent.html (renderer: n/a)\n canonical => section/somecontent/index.html\n\n"
|
|
|
|
|
2013-09-12 19:17:53 -04:00
|
|
|
checkShowPlanExpected(t, s, expected)
|
2013-09-03 23:52:50 -04:00
|
|
|
}
|
|
|
|
|
2014-11-04 00:41:47 -05:00
|
|
|
func TestPageTargetUgly(t *testing.T) {
|
2015-05-20 02:21:21 -04:00
|
|
|
viper.Reset()
|
|
|
|
defer viper.Reset()
|
|
|
|
viper.Set("DefaultExtension", "html")
|
|
|
|
viper.Set("UglyURLs", true)
|
|
|
|
|
2013-09-05 01:28:59 -04:00
|
|
|
s := &Site{
|
2016-03-05 14:56:38 -05:00
|
|
|
targets: targetList{page: &target.PagePub{UglyURLs: true}},
|
2014-11-04 00:41:47 -05:00
|
|
|
Source: &source.InMemorySource{ByteSource: fakeSource},
|
2013-09-05 01:28:59 -04:00
|
|
|
}
|
2016-03-05 14:56:38 -05:00
|
|
|
s.aliasTarget()
|
2014-11-04 00:41:47 -05:00
|
|
|
|
2016-04-07 14:03:03 -04:00
|
|
|
s.createPages()
|
2013-09-18 14:52:30 -04:00
|
|
|
expected := "foo/bar/file.md (renderer: markdown)\n canonical => foo/bar/file.html\n\n" +
|
2013-09-18 13:27:56 -04:00
|
|
|
"alias/test/file1.md (renderer: markdown)\n" +
|
2013-09-12 19:17:53 -04:00
|
|
|
" canonical => alias/test/file1.html\n" +
|
|
|
|
" alias1/ => alias1/index.html\n" +
|
2013-09-18 14:52:30 -04:00
|
|
|
" alias-2/ => alias-2/index.html\n\n" +
|
|
|
|
"section/somecontent.html (renderer: n/a)\n canonical => section/somecontent.html\n\n"
|
2013-09-05 01:28:59 -04:00
|
|
|
checkShowPlanExpected(t, s, expected)
|
2013-09-03 23:52:50 -04:00
|
|
|
}
|
|
|
|
|
2013-09-05 01:28:59 -04:00
|
|
|
func TestFileTargetPublishDir(t *testing.T) {
|
2015-05-20 02:21:21 -04:00
|
|
|
viper.Reset()
|
|
|
|
defer viper.Reset()
|
|
|
|
viper.Set("DefaultExtension", "html")
|
|
|
|
|
2013-09-05 01:28:59 -04:00
|
|
|
s := &Site{
|
2014-11-04 00:41:47 -05:00
|
|
|
|
2016-03-05 14:56:38 -05:00
|
|
|
targets: targetList{
|
|
|
|
page: &target.PagePub{PublishDir: "../public"},
|
|
|
|
alias: &target.HTMLRedirectAlias{PublishDir: "../public"},
|
2014-11-04 00:41:47 -05:00
|
|
|
},
|
2014-03-01 00:31:21 -05:00
|
|
|
Source: &source.InMemorySource{ByteSource: fakeSource},
|
2013-09-05 01:28:59 -04:00
|
|
|
}
|
2013-09-03 23:52:50 -04:00
|
|
|
|
2016-04-07 14:03:03 -04:00
|
|
|
must(s.createPages())
|
2013-09-18 14:52:30 -04:00
|
|
|
expected := "foo/bar/file.md (renderer: markdown)\n canonical => ../public/foo/bar/file/index.html\n\n" +
|
2013-09-18 13:27:56 -04:00
|
|
|
"alias/test/file1.md (renderer: markdown)\n" +
|
2013-09-12 19:17:53 -04:00
|
|
|
" canonical => ../public/alias/test/file1/index.html\n" +
|
|
|
|
" alias1/ => ../public/alias1/index.html\n" +
|
2013-09-18 14:52:30 -04:00
|
|
|
" alias-2/ => ../public/alias-2/index.html\n\n" +
|
|
|
|
"section/somecontent.html (renderer: n/a)\n canonical => ../public/section/somecontent/index.html\n\n"
|
2013-09-05 01:28:59 -04:00
|
|
|
checkShowPlanExpected(t, s, expected)
|
|
|
|
}
|
2014-12-07 13:48:00 -05:00
|
|
|
|
|
|
|
// DiffStringSlices returns the difference between two string slices.
|
|
|
|
// See:
|
|
|
|
// http://stackoverflow.com/questions/19374219/how-to-find-the-difference-between-two-slices-of-strings-in-golang
|
|
|
|
func DiffStringSlices(slice1 []string, slice2 []string) []string {
|
|
|
|
diffStr := []string{}
|
|
|
|
m := map[string]int{}
|
|
|
|
|
|
|
|
for _, s1Val := range slice1 {
|
|
|
|
m[s1Val] = 1
|
|
|
|
}
|
|
|
|
for _, s2Val := range slice2 {
|
|
|
|
m[s2Val] = m[s2Val] + 1
|
|
|
|
}
|
|
|
|
|
|
|
|
for mKey, mVal := range m {
|
|
|
|
if mVal == 1 {
|
|
|
|
diffStr = append(diffStr, mKey)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return diffStr
|
|
|
|
}
|