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.
|
|
|
|
|
|
2015-05-20 18:55:24 -04:00
|
|
|
|
package hugolib
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"path/filepath"
|
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
"github.com/spf13/hugo/helpers"
|
|
|
|
|
"github.com/spf13/hugo/hugofs"
|
|
|
|
|
"github.com/spf13/hugo/source"
|
|
|
|
|
"github.com/spf13/hugo/target"
|
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestDefaultHandler(t *testing.T) {
|
2016-04-12 12:11:24 -04:00
|
|
|
|
viper.Reset()
|
|
|
|
|
defer viper.Reset()
|
2015-05-20 18:55:24 -04:00
|
|
|
|
|
2016-03-21 19:28:42 -04:00
|
|
|
|
hugofs.InitMemFs()
|
2015-05-20 18:55:24 -04:00
|
|
|
|
sources := []source.ByteSource{
|
2016-08-19 07:22:19 -04:00
|
|
|
|
{Name: filepath.FromSlash("sect/doc1.html"), Content: []byte("---\nmarkup: markdown\n---\n# title\nsome *content*")},
|
|
|
|
|
{Name: filepath.FromSlash("sect/doc2.html"), Content: []byte("<!doctype html><html><body>more content</body></html>")},
|
|
|
|
|
{Name: filepath.FromSlash("sect/doc3.md"), Content: []byte("# doc3\n*some* content")},
|
|
|
|
|
{Name: filepath.FromSlash("sect/doc4.md"), Content: []byte("---\ntitle: doc4\n---\n# doc4\n*some content*")},
|
|
|
|
|
{Name: filepath.FromSlash("sect/doc3/img1.png"), Content: []byte("‰PNG <20><><EFBFBD> IHDR<44><52><EFBFBD><01><><EFBFBD><08><><EFBFBD><EFBFBD>:~›U<E280BA><55><EFBFBD> IDATWcø<0F><01>ZMoñ<6F><C3B1><EFBFBD><EFBFBD>IEND®B`‚")},
|
|
|
|
|
{Name: filepath.FromSlash("sect/img2.gif"), Content: []byte("GIF89a<01><01>€<EFBFBD><E282AC>ÿÿÿ<C3BF><C3BF><EFBFBD>,<2C><><EFBFBD><EFBFBD><01><01><>D<01>;")},
|
|
|
|
|
{Name: filepath.FromSlash("sect/img2.spf"), Content: []byte("****FAKE-FILETYPE****")},
|
|
|
|
|
{Name: filepath.FromSlash("doc7.html"), Content: []byte("<html><body>doc7 content</body></html>")},
|
|
|
|
|
{Name: filepath.FromSlash("sect/doc8.html"), Content: []byte("---\nmarkup: md\n---\n# title\nsome *content*")},
|
2015-05-20 18:55:24 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
viper.Set("DefaultExtension", "html")
|
|
|
|
|
viper.Set("verbose", true)
|
|
|
|
|
|
|
|
|
|
s := &Site{
|
|
|
|
|
Source: &source.InMemorySource{ByteSource: sources},
|
2016-03-05 14:56:38 -05:00
|
|
|
|
targets: targetList{page: &target.PagePub{UglyURLs: true}},
|
2015-05-20 18:55:24 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
s.initializeSiteInfo()
|
|
|
|
|
|
2016-03-09 08:05:31 -05:00
|
|
|
|
s.prepTemplates(
|
|
|
|
|
"_default/single.html", "{{.Content}}",
|
|
|
|
|
"head", "<head><script src=\"script.js\"></script></head>",
|
|
|
|
|
"head_abs", "<head><script src=\"/script.js\"></script></head>")
|
2015-05-20 18:55:24 -04:00
|
|
|
|
|
|
|
|
|
// From site_test.go
|
|
|
|
|
createAndRenderPages(t, s)
|
|
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
|
doc string
|
|
|
|
|
expected string
|
|
|
|
|
}{
|
2016-04-12 13:18:02 -04:00
|
|
|
|
{filepath.FromSlash("sect/doc1.html"), "\n\n<h1 id=\"title\">title</h1>\n\n<p>some <em>content</em></p>\n"},
|
2015-05-20 18:55:24 -04:00
|
|
|
|
{filepath.FromSlash("sect/doc2.html"), "<!doctype html><html><body>more content</body></html>"},
|
2016-04-12 13:18:02 -04:00
|
|
|
|
{filepath.FromSlash("sect/doc3.html"), "\n\n<h1 id=\"doc3\">doc3</h1>\n\n<p><em>some</em> content</p>\n"},
|
2015-05-20 18:55:24 -04:00
|
|
|
|
{filepath.FromSlash("sect/doc3/img1.png"), string([]byte("‰PNG <20><><EFBFBD> IHDR<44><52><EFBFBD><01><><EFBFBD><08><><EFBFBD><EFBFBD>:~›U<E280BA><55><EFBFBD> IDATWcø<0F><01>ZMoñ<6F><C3B1><EFBFBD><EFBFBD>IEND®B`‚"))},
|
|
|
|
|
{filepath.FromSlash("sect/img2.gif"), string([]byte("GIF89a<01><01>€<EFBFBD><E282AC>ÿÿÿ<C3BF><C3BF><EFBFBD>,<2C><><EFBFBD><EFBFBD><01><01><>D<01>;"))},
|
|
|
|
|
{filepath.FromSlash("sect/img2.spf"), string([]byte("****FAKE-FILETYPE****"))},
|
|
|
|
|
{filepath.FromSlash("doc7.html"), "<html><body>doc7 content</body></html>"},
|
2016-04-12 13:18:02 -04:00
|
|
|
|
{filepath.FromSlash("sect/doc8.html"), "\n\n<h1 id=\"title\">title</h1>\n\n<p>some <em>content</em></p>\n"},
|
2015-05-20 18:55:24 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, test := range tests {
|
2016-03-21 19:28:42 -04:00
|
|
|
|
file, err := hugofs.Destination().Open(test.doc)
|
2015-05-20 18:55:24 -04:00
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("Did not find %s in target.", test.doc)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
content := helpers.ReaderToString(file)
|
|
|
|
|
|
|
|
|
|
if content != test.expected {
|
|
|
|
|
t.Errorf("%s content expected:\n%q\ngot:\n%q", test.doc, test.expected, content)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|