summaryrefslogtreecommitdiffstats
path: root/hugolib
diff options
context:
space:
mode:
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2016-03-22 00:28:42 +0100
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2016-03-31 21:24:18 +0200
commit4f66f790b168005efb835b2499c4a502e492b747 (patch)
tree2e5f808bf10ac18ff1735dd743aa63e5513737d5 /hugolib
parenta89035bdaaa8bb1525a74d82e068ef80bfa28aed (diff)
Add readFile template func
This also includes a refactor of the hugofs package and its usage. The motivation for that is: The Afero filesystems are brilliant. Hugo's way of adding a dozen of global variables for the different filesystems was a mistake. In readFile (and also in some other places in Hugo today) we need a way to restrict the access inside the working dir. We could use ioutil.ReadFile and implement the path checking, checking the base path and the dots ("..") etc. But it is obviously better to use an Afero BasePathFs combined witha ReadOnlyFs. We could create a use-once-filesystem and handle the initialization ourselves, but since this is also useful to others and the initialization depends on some other global state (which would mean to create a new file system on every invocation), we might as well do it properly and encapsulate the predefined set of filesystems. This change also leads the way, if needed, to encapsulate the file systems in a struct, making it possible to have several file system sets in action at once (parallel multilanguage site building? With Moore's law and all...) Fixes #1551
Diffstat (limited to 'hugolib')
-rw-r--r--hugolib/handler_test.go5
-rw-r--r--hugolib/menu_test.go5
-rw-r--r--hugolib/page.go6
-rw-r--r--hugolib/robotstxt_test.go18
-rw-r--r--hugolib/rss_test.go7
-rw-r--r--hugolib/shortcode_test.go2
-rw-r--r--hugolib/site.go4
-rw-r--r--hugolib/site_test.go37
-rw-r--r--hugolib/site_url_test.go9
-rw-r--r--hugolib/sitemap_test.go7
10 files changed, 53 insertions, 47 deletions
diff --git a/hugolib/handler_test.go b/hugolib/handler_test.go
index df5b970c9..29b1161e4 100644
--- a/hugolib/handler_test.go
+++ b/hugolib/handler_test.go
@@ -17,7 +17,6 @@ import (
"path/filepath"
"testing"
- "github.com/spf13/afero"
"github.com/spf13/hugo/helpers"
"github.com/spf13/hugo/hugofs"
"github.com/spf13/hugo/source"
@@ -29,7 +28,7 @@ func TestDefaultHandler(t *testing.T) {
viper.Reset()
defer viper.Reset()
- hugofs.DestinationFS = new(afero.MemMapFs)
+ hugofs.InitMemFs()
sources := []source.ByteSource{
{filepath.FromSlash("sect/doc1.html"), []byte("---\nmarkup: markdown\n---\n# title\nsome *content*")},
{filepath.FromSlash("sect/doc2.html"), []byte("<!doctype html><html><body>more content</body></html>")},
@@ -75,7 +74,7 @@ func TestDefaultHandler(t *testing.T) {
}
for _, test := range tests {
- file, err := hugofs.DestinationFS.Open(test.doc)
+ file, err := hugofs.Destination().Open(test.doc)
if err != nil {
t.Fatalf("Did not find %s in target.", test.doc)
}
diff --git a/hugolib/menu_test.go b/hugolib/menu_test.go
index 609a87d01..aaa172ebf 100644
--- a/hugolib/menu_test.go
+++ b/hugolib/menu_test.go
@@ -1,4 +1,4 @@
-// Copyright 2015 The Hugo Authors. All rights reserved.
+// Copyright 2016 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.
@@ -22,7 +22,6 @@ import (
"github.com/BurntSushi/toml"
"github.com/kr/pretty"
- "github.com/spf13/afero"
"github.com/spf13/hugo/hugofs"
"github.com/spf13/hugo/source"
"github.com/spf13/viper"
@@ -684,7 +683,7 @@ func setupMenuTests(t *testing.T, pageSources []source.ByteSource) *Site {
}
func createTestSite(pageSources []source.ByteSource) *Site {
- hugofs.DestinationFS = new(afero.MemMapFs)
+ hugofs.InitMemFs()
s := &Site{
Source: &source.InMemorySource{ByteSource: pageSources},
diff --git a/hugolib/page.go b/hugolib/page.go
index a2b4d3119..3d1b48738 100644
--- a/hugolib/page.go
+++ b/hugolib/page.go
@@ -1,4 +1,4 @@
-// Copyright 2015 The Hugo Authors. All rights reserved.
+// Copyright 2016 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.
@@ -890,9 +890,9 @@ func (p *Page) saveSource(by []byte, inpath string, safe bool) (err error) {
jww.INFO.Println("creating", inpath)
if safe {
- err = helpers.SafeWriteToDisk(inpath, bytes.NewReader(by), hugofs.SourceFs)
+ err = helpers.SafeWriteToDisk(inpath, bytes.NewReader(by), hugofs.Source())
} else {
- err = helpers.WriteToDisk(inpath, bytes.NewReader(by), hugofs.SourceFs)
+ err = helpers.WriteToDisk(inpath, bytes.NewReader(by), hugofs.Source())
}
if err != nil {
return
diff --git a/hugolib/robotstxt_test.go b/hugolib/robotstxt_test.go
index 31f89b847..b0a843a7e 100644
--- a/hugolib/robotstxt_test.go
+++ b/hugolib/robotstxt_test.go
@@ -1,10 +1,22 @@
+// Copyright 2016 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 (
"bytes"
"testing"
- "github.com/spf13/afero"
"github.com/spf13/hugo/helpers"
"github.com/spf13/hugo/hugofs"
"github.com/spf13/hugo/source"
@@ -21,7 +33,7 @@ func TestRobotsTXTOutput(t *testing.T) {
viper.Reset()
defer viper.Reset()
- hugofs.DestinationFS = new(afero.MemMapFs)
+ hugofs.InitMemFs()
viper.Set("baseurl", "http://auth/bub/")
@@ -53,7 +65,7 @@ func TestRobotsTXTOutput(t *testing.T) {
t.Fatalf("Unable to RenderRobotsTXT :%s", err)
}
- robotsFile, err := hugofs.DestinationFS.Open("robots.txt")
+ robotsFile, err := hugofs.Destination().Open("robots.txt")
if err != nil {
t.Fatalf("Unable to locate: robots.txt")
diff --git a/hugolib/rss_test.go b/hugolib/rss_test.go
index b8a16665f..e42202a5b 100644
--- a/hugolib/rss_test.go
+++ b/hugolib/rss_test.go
@@ -1,4 +1,4 @@
-// Copyright 2015 The Hugo Authors. All rights reserved.
+// Copyright 2016 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.
@@ -17,7 +17,6 @@ import (
"bytes"
"testing"
- "github.com/spf13/afero"
"github.com/spf13/hugo/helpers"
"github.com/spf13/hugo/hugofs"
"github.com/spf13/hugo/source"
@@ -53,7 +52,7 @@ func TestRSSOutput(t *testing.T) {
viper.Set("baseurl", "http://auth/bub/")
viper.Set("RSSUri", rssURI)
- hugofs.DestinationFS = new(afero.MemMapFs)
+ hugofs.InitMemFs()
s := &Site{
Source: &source.InMemorySource{ByteSource: WEIGHTED_SOURCES},
}
@@ -72,7 +71,7 @@ func TestRSSOutput(t *testing.T) {
t.Fatalf("Unable to RenderHomePage: %s", err)
}
- file, err := hugofs.DestinationFS.Open(rssURI)
+ file, err := hugofs.Destination().Open(rssURI)
if err != nil {
t.Fatalf("Unable to locate: %s", rssURI)
diff --git a/hugolib/shortcode_test.go b/hugolib/shortcode_test.go
index 833cdae99..ab764b845 100644
--- a/hugolib/shortcode_test.go
+++ b/hugolib/shortcode_test.go
@@ -487,7 +487,7 @@ e`,
createAndRenderPages(t, s)
for _, test := range tests {
- file, err := hugofs.DestinationFS.Open(test.outFile)
+ file, err := hugofs.Destination().Open(test.outFile)
if err != nil {
t.Fatalf("Did not find %s in target: %s", test.outFile, err)
diff --git a/hugolib/site.go b/hugolib/site.go
index b1c5090f9..192628051 100644
--- a/hugolib/site.go
+++ b/hugolib/site.go
@@ -529,7 +529,7 @@ func (s *Site) ReBuild(events []fsnotify.Event) error {
// it's been updated
if ev.Op&fsnotify.Rename == fsnotify.Rename {
// If the file is still on disk, it's only been updated, if it's not, it's been moved
- if ex, err := afero.Exists(hugofs.SourceFs, ev.Name); !ex || err != nil {
+ if ex, err := afero.Exists(hugofs.Source(), ev.Name); !ex || err != nil {
path, _ := helpers.GetRelativePath(ev.Name, s.absContentDir())
s.RemovePageByPath(path)
continue
@@ -852,7 +852,7 @@ func (s *Site) absPublishDir() string {
}
func (s *Site) checkDirectories() (err error) {
- if b, _ := helpers.DirExists(s.absContentDir(), hugofs.SourceFs); !b {
+ if b, _ := helpers.DirExists(s.absContentDir(), hugofs.Source()); !b {
return fmt.Errorf("No source directory found, expecting to find it at " + s.absContentDir())
}
return
diff --git a/hugolib/site_test.go b/hugolib/site_test.go
index d96db4323..1173d0f3d 100644
--- a/hugolib/site_test.go
+++ b/hugolib/site_test.go
@@ -1,4 +1,4 @@
-// Copyright 2015 The Hugo Authors. All rights reserved.
+// Copyright 2016 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.
@@ -25,7 +25,6 @@ import (
"bitbucket.org/pkg/inflect"
- "github.com/spf13/afero"
"github.com/spf13/hugo/helpers"
"github.com/spf13/hugo/hugofs"
"github.com/spf13/hugo/source"
@@ -200,7 +199,7 @@ func TestRenderThingOrDefault(t *testing.T) {
{false, TEMPLATE_FUNC, HTML("simple-template")},
}
- hugofs.DestinationFS = new(afero.MemMapFs)
+ hugofs.InitMemFs()
for i, test := range tests {
@@ -226,7 +225,7 @@ func TestRenderThingOrDefault(t *testing.T) {
t.Errorf("Unable to render html: %s", err)
}
- file, err := hugofs.DestinationFS.Open(filepath.FromSlash("out/index.html"))
+ file, err := hugofs.Destination().Open(filepath.FromSlash("out/index.html"))
if err != nil {
t.Errorf("Unable to open html: %s", err)
}
@@ -240,7 +239,7 @@ func TestDraftAndFutureRender(t *testing.T) {
viper.Reset()
defer viper.Reset()
- hugofs.DestinationFS = new(afero.MemMapFs)
+ hugofs.InitMemFs()
sources := []source.ByteSource{
{filepath.FromSlash("sect/doc1.md"), []byte("---\ntitle: doc1\ndraft: true\npublishdate: \"2414-05-29\"\n---\n# doc1\n*some content*")},
{filepath.FromSlash("sect/doc2.md"), []byte("---\ntitle: doc2\ndraft: true\npublishdate: \"2012-05-29\"\n---\n# doc2\n*some content*")},
@@ -299,7 +298,7 @@ func TestDraftAndFutureRender(t *testing.T) {
// Issue #957
func TestCrossrefs(t *testing.T) {
- hugofs.DestinationFS = new(afero.MemMapFs)
+ hugofs.InitMemFs()
for _, uglyURLs := range []bool{true, false} {
for _, relative := range []bool{true, false} {
doTestCrossrefs(t, relative, uglyURLs)
@@ -374,7 +373,7 @@ THE END.`, refShortcode))},
}
for _, test := range tests {
- file, err := hugofs.DestinationFS.Open(test.doc)
+ file, err := hugofs.Destination().Open(test.doc)
if err != nil {
t.Fatalf("Did not find %s in target: %s", test.doc, err)
@@ -392,7 +391,7 @@ THE END.`, refShortcode))},
// Issue #939
// Issue #1923
func TestShouldAlwaysHaveUglyURLs(t *testing.T) {
- hugofs.DestinationFS = new(afero.MemMapFs)
+ hugofs.InitMemFs()
for _, uglyURLs := range []bool{true, false} {
doTestShouldAlwaysHaveUglyURLs(t, uglyURLs)
}
@@ -462,7 +461,7 @@ func doTestShouldAlwaysHaveUglyURLs(t *testing.T, uglyURLs bool) {
}
for _, test := range tests {
- file, err := hugofs.DestinationFS.Open(test.doc)
+ file, err := hugofs.Destination().Open(test.doc)
if err != nil {
t.Fatalf("Did not find %s in target: %s", test.doc, err)
}
@@ -489,7 +488,7 @@ func TestSectionNaming(t *testing.T) {
}
func doTestSectionNaming(t *testing.T, canonify, uglify, pluralize bool) {
- hugofs.DestinationFS = new(afero.MemMapFs)
+ hugofs.InitMemFs()
viper.Reset()
defer viper.Reset()
viper.Set("baseurl", "http://auth/sub/")
@@ -539,7 +538,7 @@ func doTestSectionNaming(t *testing.T, canonify, uglify, pluralize bool) {
}
for _, test := range tests {
- file, err := hugofs.DestinationFS.Open(test.doc)
+ file, err := hugofs.Destination().Open(test.doc)
if err != nil {
t.Fatalf("Did not find %s in target: %s", test.doc, err)
}
@@ -560,7 +559,7 @@ func TestSkipRender(t *testing.T) {
viper.Reset()
defer viper.Reset()
- hugofs.DestinationFS = new(afero.MemMapFs)
+ hugofs.InitMemFs()
sources := []source.ByteSource{
{filepath.FromSlash("sect/doc1.html"), []byte("---\nmarkup: markdown\n---\n# title\nsome *content*")},
{filepath.FromSlash("sect/doc2.html"), []byte("<!doctype html><html><body>more content</body></html>")},
@@ -605,7 +604,7 @@ func TestSkipRender(t *testing.T) {
}
for _, test := range tests {
- file, err := hugofs.DestinationFS.Open(test.doc)
+ file, err := hugofs.Destination().Open(test.doc)
if err != nil {
t.Fatalf("Did not find %s in target.", test.doc)
}
@@ -624,7 +623,7 @@ func TestAbsURLify(t *testing.T) {
viper.Set("DefaultExtension", "html")
- hugofs.DestinationFS = new(afero.MemMapFs)
+ hugofs.InitMemFs()
sources := []source.ByteSource{
{filepath.FromSlash("sect/doc1.html"), []byte("<!doctype html><html><head></head><body><a href=\"#frag1\">link</a></body></html>")},
{filepath.FromSlash("content/blue/doc2.html"), []byte("---\nf: t\n---\n<!doctype html><html><body>more content</body></html>")},
@@ -662,7 +661,7 @@ func TestAbsURLify(t *testing.T) {
for _, test := range tests {
- file, err := hugofs.DestinationFS.Open(filepath.FromSlash(test.file))
+ file, err := hugofs.Destination().Open(filepath.FromSlash(test.file))
if err != nil {
t.Fatalf("Unable to locate rendered content: %s", test.file)
}
@@ -730,7 +729,7 @@ func TestOrderedPages(t *testing.T) {
viper.Reset()
defer viper.Reset()
- hugofs.DestinationFS = new(afero.MemMapFs)
+ hugofs.InitMemFs()
viper.Set("baseurl", "http://auth/bub")
s := &Site{
@@ -804,7 +803,7 @@ func TestGroupedPages(t *testing.T) {
}
}()
- hugofs.DestinationFS = new(afero.MemMapFs)
+ hugofs.InitMemFs()
viper.Set("baseurl", "http://auth/bub")
s := &Site{
@@ -984,7 +983,7 @@ func TestWeightedTaxonomies(t *testing.T) {
viper.Reset()
defer viper.Reset()
- hugofs.DestinationFS = new(afero.MemMapFs)
+ hugofs.InitMemFs()
sources := []source.ByteSource{
{filepath.FromSlash("sect/doc1.md"), PAGE_WITH_WEIGHTED_TAXONOMIES_1},
{filepath.FromSlash("sect/doc2.md"), PAGE_WITH_WEIGHTED_TAXONOMIES_2},
@@ -1039,7 +1038,7 @@ func findPage(site *Site, f string) *Page {
}
func setupLinkingMockSite(t *testing.T) *Site {
- hugofs.DestinationFS = new(afero.MemMapFs)
+ hugofs.InitMemFs()
sources := []source.ByteSource{
{filepath.FromSlash("index.md"), []byte("")},
{filepath.FromSlash("rootfile.md"), []byte("")},
diff --git a/hugolib/site_url_test.go b/hugolib/site_url_test.go
index 2bccd99ae..c4c918c88 100644
--- a/hugolib/site_url_test.go
+++ b/hugolib/site_url_test.go
@@ -1,4 +1,4 @@
-// Copyright 2015 The Hugo Authors. All rights reserved.
+// Copyright 2016 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.
@@ -19,7 +19,6 @@ import (
"html/template"
- "github.com/spf13/afero"
"github.com/spf13/hugo/hugofs"
"github.com/spf13/hugo/source"
"github.com/spf13/hugo/target"
@@ -88,7 +87,7 @@ func TestPageCount(t *testing.T) {
viper.Reset()
defer viper.Reset()
- hugofs.DestinationFS = new(afero.MemMapFs)
+ hugofs.InitMemFs()
viper.Set("uglyurls", false)
viper.Set("paginate", 10)
@@ -113,7 +112,7 @@ func TestPageCount(t *testing.T) {
t.Errorf("Unable to render site lists: %s", err)
}
- _, err := hugofs.DestinationFS.Open("blue")
+ _, err := hugofs.Destination().Open("blue")
if err != nil {
t.Errorf("No indexed rendered.")
}
@@ -129,7 +128,7 @@ func TestPageCount(t *testing.T) {
"sd3/index.html",
"sd4.html",
} {
- if _, err := hugofs.DestinationFS.Open(filepath.FromSlash(s)); err != nil {
+ if _, err := hugofs.Destination().Open(filepath.FromSlash(s)); err != nil {
t.Errorf("No alias rendered: %s", s)
}
}
diff --git a/hugolib/sitemap_test.go b/hugolib/sitemap_test.go
index 34f2d3477..3f10bbcdd 100644
--- a/hugolib/sitemap_test.go
+++ b/hugolib/sitemap_test.go
@@ -1,4 +1,4 @@
-// Copyright 2015 The Hugo Authors. All rights reserved.
+// Copyright 2016 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.
@@ -17,7 +17,6 @@ import (
"bytes"
"testing"
- "github.com/spf13/afero"
"github.com/spf13/hugo/helpers"
"github.com/spf13/hugo/hugofs"
"github.com/spf13/hugo/source"
@@ -40,7 +39,7 @@ func TestSitemapOutput(t *testing.T) {
viper.Reset()
defer viper.Reset()
- hugofs.DestinationFS = new(afero.MemMapFs)
+ hugofs.InitMemFs()
viper.Set("baseurl", "http://auth/bub/")
@@ -72,7 +71,7 @@ func TestSitemapOutput(t *testing.T) {
t.Fatalf("Unable to RenderRobotsTXT :%s", err)
}
- sitemapFile, err := hugofs.DestinationFS.Open("sitemap.xml")
+ sitemapFile, err := hugofs.Destination().Open("sitemap.xml")
if err != nil {
t.Fatalf("Unable to locate: sitemap.xml")