From 15b64d51da48807c5f896f17b33d8c0d054c9461 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Sat, 25 Mar 2017 14:37:04 +0100 Subject: all: Propagate baseURL error to the callers --- helpers/baseURL.go | 1 - helpers/baseURL_test.go | 10 ++++++++++ helpers/path_test.go | 6 +++--- helpers/pathspec.go | 12 ++++++++---- helpers/pathspec_test.go | 3 ++- helpers/testhelpers_test.go | 3 ++- helpers/url_test.go | 8 ++++---- 7 files changed, 29 insertions(+), 14 deletions(-) (limited to 'helpers') diff --git a/helpers/baseURL.go b/helpers/baseURL.go index 9a4b77edd..50265fe36 100644 --- a/helpers/baseURL.go +++ b/helpers/baseURL.go @@ -69,6 +69,5 @@ func newBaseURLFromString(b string) (BaseURL, error) { return result, err } - // TODO(bep) output consider saving original URL? return BaseURL{url: base, urlStr: base.String()}, nil } diff --git a/helpers/baseURL_test.go b/helpers/baseURL_test.go index eaa27ddb9..437152f34 100644 --- a/helpers/baseURL_test.go +++ b/helpers/baseURL_test.go @@ -48,4 +48,14 @@ func TestBaseURL(t *testing.T) { require.NoError(t, err) require.Equal(t, "webcal://hugo@rules.com", p) + // Test with "non-URLs". Some people will try to use these as a way to get + // relative URLs working etc. + b, err = newBaseURLFromString("/") + require.NoError(t, err) + require.Equal(t, "/", b.String()) + + b, err = newBaseURLFromString("") + require.NoError(t, err) + require.Equal(t, "", b.String()) + } diff --git a/helpers/path_test.go b/helpers/path_test.go index 90dd95288..25dbdc543 100644 --- a/helpers/path_test.go +++ b/helpers/path_test.go @@ -59,7 +59,7 @@ func TestMakePath(t *testing.T) { v := viper.New() l := NewDefaultLanguage(v) v.Set("removePathAccents", test.removeAccents) - p := NewPathSpec(hugofs.NewMem(v), l) + p, _ := NewPathSpec(hugofs.NewMem(v), l) output := p.MakePath(test.input) if output != test.expected { @@ -71,7 +71,7 @@ func TestMakePath(t *testing.T) { func TestMakePathSanitized(t *testing.T) { v := viper.New() l := NewDefaultLanguage(v) - p := NewPathSpec(hugofs.NewMem(v), l) + p, _ := NewPathSpec(hugofs.NewMem(v), l) tests := []struct { input string @@ -99,7 +99,7 @@ func TestMakePathSanitizedDisablePathToLower(t *testing.T) { v.Set("disablePathToLower", true) l := NewDefaultLanguage(v) - p := NewPathSpec(hugofs.NewMem(v), l) + p, _ := NewPathSpec(hugofs.NewMem(v), l) tests := []struct { input string diff --git a/helpers/pathspec.go b/helpers/pathspec.go index de7665c87..ff4488020 100644 --- a/helpers/pathspec.go +++ b/helpers/pathspec.go @@ -60,10 +60,14 @@ func (p PathSpec) String() string { } // NewPathSpec creats a new PathSpec from the given filesystems and Language. -func NewPathSpec(fs *hugofs.Fs, cfg config.Provider) *PathSpec { +func NewPathSpec(fs *hugofs.Fs, cfg config.Provider) (*PathSpec, error) { - // TODO(bep) output error handling - baseURL, _ := newBaseURLFromString(cfg.GetString("baseURL")) + baseURLstr := cfg.GetString("baseURL") + baseURL, err := newBaseURLFromString(baseURLstr) + + if err != nil { + return nil, fmt.Errorf("Failed to create baseURL from %q: %s", baseURLstr, err) + } ps := &PathSpec{ fs: fs, @@ -87,7 +91,7 @@ func NewPathSpec(fs *hugofs.Fs, cfg config.Provider) *PathSpec { ps.language = language } - return ps + return ps, nil } // PaginatePath returns the configured root path used for paginator pages. diff --git a/helpers/pathspec_test.go b/helpers/pathspec_test.go index c67c6fbdc..2536b8f24 100644 --- a/helpers/pathspec_test.go +++ b/helpers/pathspec_test.go @@ -40,8 +40,9 @@ func TestNewPathSpecFromConfig(t *testing.T) { v.Set("staticDir", "thestatic") v.Set("theme", "thetheme") - p := NewPathSpec(hugofs.NewMem(v), l) + p, err := NewPathSpec(hugofs.NewMem(v), l) + require.NoError(t, err) require.True(t, p.canonifyURLs) require.True(t, p.defaultContentLanguageInSubdir) require.True(t, p.disablePathToLower) diff --git a/helpers/testhelpers_test.go b/helpers/testhelpers_test.go index 303f9feb6..d5a1b60ed 100644 --- a/helpers/testhelpers_test.go +++ b/helpers/testhelpers_test.go @@ -8,7 +8,8 @@ import ( func newTestPathSpec(fs *hugofs.Fs, v *viper.Viper) *PathSpec { l := NewDefaultLanguage(v) - return NewPathSpec(fs, l) + ps, _ := NewPathSpec(fs, l) + return ps } func newTestDefaultPathSpec(configKeyValues ...interface{}) *PathSpec { diff --git a/helpers/url_test.go b/helpers/url_test.go index b53e2e6cc..499388259 100644 --- a/helpers/url_test.go +++ b/helpers/url_test.go @@ -28,7 +28,7 @@ func TestURLize(t *testing.T) { v := viper.New() l := NewDefaultLanguage(v) - p := NewPathSpec(hugofs.NewMem(v), l) + p, _ := NewPathSpec(hugofs.NewMem(v), l) tests := []struct { input string @@ -89,7 +89,7 @@ func doTestAbsURL(t *testing.T, defaultInSubDir, addLanguage, multilingual bool, for _, test := range tests { v.Set("baseURL", test.baseURL) l := NewLanguage(lang, v) - p := NewPathSpec(hugofs.NewMem(v), l) + p, _ := NewPathSpec(hugofs.NewMem(v), l) output := p.AbsURL(test.input, addLanguage) expected := test.expected @@ -167,7 +167,7 @@ func doTestRelURL(t *testing.T, defaultInSubDir, addLanguage, multilingual bool, v.Set("baseURL", test.baseURL) v.Set("canonifyURLs", test.canonify) l := NewLanguage(lang, v) - p := NewPathSpec(hugofs.NewMem(v), l) + p, _ := NewPathSpec(hugofs.NewMem(v), l) output := p.RelURL(test.input, addLanguage) @@ -255,7 +255,7 @@ func TestURLPrep(t *testing.T) { v := viper.New() v.Set("uglyURLs", d.ugly) l := NewDefaultLanguage(v) - p := NewPathSpec(hugofs.NewMem(v), l) + p, _ := NewPathSpec(hugofs.NewMem(v), l) output := p.URLPrep(d.input) if d.output != output { -- cgit v1.2.3