2017-03-07 08:20:39 -05:00
|
|
|
// Copyright 2017-present 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.
|
|
|
|
|
|
|
|
package hugolib
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
2017-03-22 04:54:56 -04:00
|
|
|
"strings"
|
2017-03-07 08:20:39 -05:00
|
|
|
"testing"
|
|
|
|
|
2017-03-08 07:45:33 -05:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
|
|
"fmt"
|
|
|
|
|
2017-03-22 04:54:56 -04:00
|
|
|
"github.com/spf13/hugo/helpers"
|
2017-03-07 08:20:39 -05:00
|
|
|
"github.com/spf13/hugo/output"
|
2017-03-09 13:19:29 -05:00
|
|
|
"github.com/spf13/viper"
|
2017-03-07 08:20:39 -05:00
|
|
|
)
|
|
|
|
|
2017-03-22 06:34:17 -04:00
|
|
|
func TestDefaultOutputFormats(t *testing.T) {
|
2017-03-07 11:26:22 -05:00
|
|
|
t.Parallel()
|
2017-03-22 06:34:17 -04:00
|
|
|
defs, err := createDefaultOutputFormats(viper.New())
|
|
|
|
|
|
|
|
require.NoError(t, err)
|
2017-03-07 08:20:39 -05:00
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
kind string
|
2017-03-22 06:34:17 -04:00
|
|
|
want output.Formats
|
2017-03-07 08:20:39 -05:00
|
|
|
}{
|
2017-03-22 06:34:17 -04:00
|
|
|
{"RSS not for regular pages", KindPage, output.Formats{output.HTMLType}},
|
|
|
|
{"Home Sweet Home", KindHome, output.Formats{output.HTMLType, output.RSSType}},
|
2017-03-07 08:20:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2017-03-22 06:34:17 -04:00
|
|
|
if got := defs[tt.kind]; !reflect.DeepEqual(got, tt.want) {
|
|
|
|
t.Errorf("createDefaultOutputFormats(%v) = %v, want %v", tt.kind, got, tt.want)
|
2017-03-07 08:20:39 -05:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2017-03-08 07:45:33 -05:00
|
|
|
|
2017-03-22 04:54:56 -04:00
|
|
|
func TestSiteWithPageOutputs(t *testing.T) {
|
2017-03-23 15:05:10 -04:00
|
|
|
for _, outputs := range [][]string{{"html", "json", "calendar"}, {"json"}} {
|
2017-03-22 04:54:56 -04:00
|
|
|
t.Run(fmt.Sprintf("%v", outputs), func(t *testing.T) {
|
|
|
|
doTestSiteWithPageOutputs(t, outputs)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-22 06:34:17 -04:00
|
|
|
// TODO(bep) output add test for site outputs config
|
2017-03-22 04:54:56 -04:00
|
|
|
func doTestSiteWithPageOutputs(t *testing.T, outputs []string) {
|
2017-03-08 07:45:33 -05:00
|
|
|
t.Parallel()
|
|
|
|
|
2017-03-22 04:54:56 -04:00
|
|
|
outputsStr := strings.Replace(fmt.Sprintf("%q", outputs), " ", ", ", -1)
|
|
|
|
|
2017-03-08 07:45:33 -05:00
|
|
|
siteConfig := `
|
|
|
|
baseURL = "http://example.com/blog"
|
|
|
|
|
|
|
|
paginate = 1
|
|
|
|
defaultContentLanguage = "en"
|
|
|
|
|
|
|
|
disableKinds = ["page", "section", "taxonomy", "taxonomyTerm", "RSS", "sitemap", "robotsTXT", "404"]
|
|
|
|
|
|
|
|
[Taxonomies]
|
|
|
|
tag = "tags"
|
|
|
|
category = "categories"
|
|
|
|
`
|
|
|
|
|
|
|
|
pageTemplate := `---
|
|
|
|
title: "%s"
|
2017-03-22 04:54:56 -04:00
|
|
|
outputs: %s
|
2017-03-08 07:45:33 -05:00
|
|
|
---
|
|
|
|
# Doc
|
|
|
|
`
|
|
|
|
|
2017-03-09 13:19:29 -05:00
|
|
|
th, h := newTestSitesFromConfig(t, siteConfig,
|
2017-03-22 04:54:56 -04:00
|
|
|
"layouts/_default/list.json", `List JSON|{{ .Title }}|{{ .Content }}|Alt formats: {{ len .AlternativeOutputFormats -}}|
|
|
|
|
{{- range .AlternativeOutputFormats -}}
|
|
|
|
Alt Output: {{ .Name -}}|
|
|
|
|
{{- end -}}|
|
|
|
|
{{- range .OutputFormats -}}
|
|
|
|
Output/Rel: {{ .Name -}}/{{ .Rel }}|
|
|
|
|
{{- end -}}
|
|
|
|
`,
|
2017-03-09 13:19:29 -05:00
|
|
|
)
|
2017-03-08 07:45:33 -05:00
|
|
|
require.Len(t, h.Sites, 1)
|
|
|
|
|
|
|
|
fs := th.Fs
|
|
|
|
|
2017-03-22 04:54:56 -04:00
|
|
|
writeSource(t, fs, "content/_index.md", fmt.Sprintf(pageTemplate, "JSON Home", outputsStr))
|
2017-03-08 07:45:33 -05:00
|
|
|
|
|
|
|
err := h.Build(BuildCfg{})
|
|
|
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
s := h.Sites[0]
|
|
|
|
home := s.getPage(KindHome)
|
|
|
|
|
|
|
|
require.NotNil(t, home)
|
|
|
|
|
2017-03-22 04:54:56 -04:00
|
|
|
lenOut := len(outputs)
|
|
|
|
|
|
|
|
require.Len(t, home.outputFormats, lenOut)
|
2017-03-08 07:45:33 -05:00
|
|
|
|
2017-03-09 13:19:29 -05:00
|
|
|
// TODO(bep) output assert template/text
|
2017-03-22 04:54:56 -04:00
|
|
|
// There is currently always a JSON output to make it simpler ...
|
|
|
|
altFormats := lenOut - 1
|
|
|
|
hasHTML := helpers.InStringArray(outputs, "html")
|
|
|
|
th.assertFileContent("public/index.json",
|
|
|
|
"List JSON",
|
|
|
|
fmt.Sprintf("Alt formats: %d", altFormats),
|
|
|
|
)
|
2017-03-09 13:19:29 -05:00
|
|
|
|
2017-03-22 04:54:56 -04:00
|
|
|
if hasHTML {
|
|
|
|
th.assertFileContent("public/index.json",
|
|
|
|
"Alt Output: HTML",
|
|
|
|
"Output/Rel: JSON/alternate|",
|
|
|
|
"Output/Rel: HTML/canonical|",
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
th.assertFileContent("public/index.json",
|
|
|
|
"Output/Rel: JSON/canonical|",
|
|
|
|
)
|
|
|
|
}
|
2017-03-08 07:45:33 -05:00
|
|
|
|
2017-03-21 19:25:55 -04:00
|
|
|
of := home.OutputFormats()
|
2017-03-22 04:54:56 -04:00
|
|
|
require.Len(t, of, lenOut)
|
2017-03-21 19:25:55 -04:00
|
|
|
require.Nil(t, of.Get("Hugo"))
|
|
|
|
require.NotNil(t, of.Get("json"))
|
|
|
|
json := of.Get("JSON")
|
2017-03-22 04:54:56 -04:00
|
|
|
_, err = home.AlternativeOutputFormats()
|
|
|
|
require.Error(t, err)
|
2017-03-21 19:25:55 -04:00
|
|
|
require.NotNil(t, json)
|
|
|
|
require.Equal(t, "/blog/index.json", json.RelPermalink())
|
|
|
|
require.Equal(t, "http://example.com/blog/index.json", json.Permalink())
|
|
|
|
|
2017-03-23 15:05:10 -04:00
|
|
|
if helpers.InStringArray(outputs, "cal") {
|
|
|
|
// TODO(bep) output have do some protocil handling for the default too if set.
|
|
|
|
cal := of.Get("calendar")
|
|
|
|
require.NotNil(t, cal)
|
|
|
|
require.Equal(t, "/blog/index.ics", cal.RelPermalink())
|
|
|
|
require.Equal(t, "webcal://example.com/blog/index.ics", cal.Permalink())
|
|
|
|
}
|
|
|
|
|
2017-03-08 07:45:33 -05:00
|
|
|
}
|