all: Propagate baseURL error to the callers

This commit is contained in:
Bjørn Erik Pedersen 2017-03-25 14:37:04 +01:00
parent 148fedd227
commit 15b64d51da
18 changed files with 124 additions and 47 deletions

View file

@ -54,7 +54,10 @@ func benchmark(cmd *cobra.Command, args []string) error {
return err return err
} }
c := newCommandeer(cfg) c, err := newCommandeer(cfg)
if err != nil {
return err
}
var memProf *os.File var memProf *os.File
if memProfileFile != "" { if memProfileFile != "" {

View file

@ -16,6 +16,7 @@ package commands
import ( import (
"github.com/spf13/hugo/deps" "github.com/spf13/hugo/deps"
"github.com/spf13/hugo/helpers" "github.com/spf13/hugo/helpers"
"github.com/spf13/hugo/hugofs"
) )
type commandeer struct { type commandeer struct {
@ -35,12 +36,14 @@ func (c *commandeer) Set(key string, value interface{}) {
// be configured before it is created. // be configured before it is created.
func (c *commandeer) PathSpec() *helpers.PathSpec { func (c *commandeer) PathSpec() *helpers.PathSpec {
c.configured = true c.configured = true
if c.pathSpec == nil {
c.pathSpec = helpers.NewPathSpec(c.Fs, c.Cfg)
}
return c.pathSpec return c.pathSpec
} }
func newCommandeer(cfg *deps.DepsCfg) *commandeer { func newCommandeer(cfg *deps.DepsCfg) (*commandeer, error) {
return &commandeer{DepsCfg: cfg} fs := hugofs.NewDefault(cfg.Language)
ps, err := helpers.NewPathSpec(fs, cfg.Cfg)
if err != nil {
return nil, err
}
return &commandeer{DepsCfg: cfg, pathSpec: ps}, nil
} }

View file

@ -118,7 +118,10 @@ Complete documentation is available at http://gohugo.io/.`,
return err return err
} }
c := newCommandeer(cfg) c, err := newCommandeer(cfg)
if err != nil {
return err
}
if buildWatch { if buildWatch {
cfg.Cfg.Set("disableLiveReload", true) cfg.Cfg.Set("disableLiveReload", true)
@ -287,7 +290,10 @@ func InitializeConfig(subCmdVs ...*cobra.Command) (*deps.DepsCfg, error) {
cfg.Cfg = config cfg.Cfg = config
c := newCommandeer(cfg) c, err := newCommandeer(cfg)
if err != nil {
return nil, err
}
for _, cmdV := range append([]*cobra.Command{hugoCmdV}, subCmdVs...) { for _, cmdV := range append([]*cobra.Command{hugoCmdV}, subCmdVs...) {
c.initializeFlags(cmdV) c.initializeFlags(cmdV)

View file

@ -49,7 +49,10 @@ var listDraftsCmd = &cobra.Command{
return err return err
} }
c := newCommandeer(cfg) c, err := newCommandeer(cfg)
if err != nil {
return err
}
c.Set("buildDrafts", true) c.Set("buildDrafts", true)
@ -87,7 +90,10 @@ posted in the future.`,
return err return err
} }
c := newCommandeer(cfg) c, err := newCommandeer(cfg)
if err != nil {
return err
}
c.Set("buildFuture", true) c.Set("buildFuture", true)
@ -125,7 +131,10 @@ expired.`,
return err return err
} }
c := newCommandeer(cfg) c, err := newCommandeer(cfg)
if err != nil {
return err
}
c.Set("buildExpired", true) c.Set("buildExpired", true)

View file

@ -93,7 +93,10 @@ func NewContent(cmd *cobra.Command, args []string) error {
return err return err
} }
c := newCommandeer(cfg) c, err := newCommandeer(cfg)
if err != nil {
return err
}
if flagChanged(cmd.Flags(), "format") { if flagChanged(cmd.Flags(), "format") {
c.Set("metaDataFormat", configFormat) c.Set("metaDataFormat", configFormat)
@ -220,7 +223,10 @@ func NewTheme(cmd *cobra.Command, args []string) error {
return newUserError("theme name needs to be provided") return newUserError("theme name needs to be provided")
} }
c := newCommandeer(cfg) c, err := newCommandeer(cfg)
if err != nil {
return err
}
createpath := c.PathSpec().AbsPathify(filepath.Join(c.Cfg.GetString("themesDir"), args[0])) createpath := c.PathSpec().AbsPathify(filepath.Join(c.Cfg.GetString("themesDir"), args[0]))
jww.INFO.Println("creating theme at", createpath) jww.INFO.Println("creating theme at", createpath)

View file

@ -106,7 +106,10 @@ func server(cmd *cobra.Command, args []string) error {
return err return err
} }
c := newCommandeer(cfg) c, err := newCommandeer(cfg)
if err != nil {
return err
}
if flagChanged(cmd.Flags(), "disableLiveReload") { if flagChanged(cmd.Flags(), "disableLiveReload") {
c.Set("disableLiveReload", disableLiveReload) c.Set("disableLiveReload", disableLiveReload)

19
deps/deps.go vendored
View file

@ -65,7 +65,7 @@ func (d *Deps) LoadResources() error {
return nil return nil
} }
func New(cfg DepsCfg) *Deps { func New(cfg DepsCfg) (*Deps, error) {
var ( var (
logger = cfg.Logger logger = cfg.Logger
fs = cfg.Fs fs = cfg.Fs
@ -92,26 +92,37 @@ func New(cfg DepsCfg) *Deps {
fs = hugofs.NewDefault(cfg.Language) fs = hugofs.NewDefault(cfg.Language)
} }
ps, err := helpers.NewPathSpec(fs, cfg.Language)
if err != nil {
return nil, err
}
d := &Deps{ d := &Deps{
Fs: fs, Fs: fs,
Log: logger, Log: logger,
templateProvider: cfg.TemplateProvider, templateProvider: cfg.TemplateProvider,
translationProvider: cfg.TranslationProvider, translationProvider: cfg.TranslationProvider,
WithTemplate: cfg.WithTemplate, WithTemplate: cfg.WithTemplate,
PathSpec: helpers.NewPathSpec(fs, cfg.Language), PathSpec: ps,
ContentSpec: helpers.NewContentSpec(cfg.Language), ContentSpec: helpers.NewContentSpec(cfg.Language),
Cfg: cfg.Language, Cfg: cfg.Language,
Language: cfg.Language, Language: cfg.Language,
} }
return d return d, nil
} }
// ForLanguage creates a copy of the Deps with the language dependent // ForLanguage creates a copy of the Deps with the language dependent
// parts switched out. // parts switched out.
func (d Deps) ForLanguage(l *helpers.Language) (*Deps, error) { func (d Deps) ForLanguage(l *helpers.Language) (*Deps, error) {
var err error
d.PathSpec, err = helpers.NewPathSpec(d.Fs, l)
if err != nil {
return nil, err
}
d.PathSpec = helpers.NewPathSpec(d.Fs, l)
d.ContentSpec = helpers.NewContentSpec(l) d.ContentSpec = helpers.NewContentSpec(l)
d.Cfg = l d.Cfg = l
d.Language = l d.Language = l

View file

@ -69,6 +69,5 @@ func newBaseURLFromString(b string) (BaseURL, error) {
return result, err return result, err
} }
// TODO(bep) output consider saving original URL?
return BaseURL{url: base, urlStr: base.String()}, nil return BaseURL{url: base, urlStr: base.String()}, nil
} }

View file

@ -48,4 +48,14 @@ func TestBaseURL(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, "webcal://hugo@rules.com", p) 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())
} }

View file

@ -59,7 +59,7 @@ func TestMakePath(t *testing.T) {
v := viper.New() v := viper.New()
l := NewDefaultLanguage(v) l := NewDefaultLanguage(v)
v.Set("removePathAccents", test.removeAccents) v.Set("removePathAccents", test.removeAccents)
p := NewPathSpec(hugofs.NewMem(v), l) p, _ := NewPathSpec(hugofs.NewMem(v), l)
output := p.MakePath(test.input) output := p.MakePath(test.input)
if output != test.expected { if output != test.expected {
@ -71,7 +71,7 @@ func TestMakePath(t *testing.T) {
func TestMakePathSanitized(t *testing.T) { func TestMakePathSanitized(t *testing.T) {
v := viper.New() v := viper.New()
l := NewDefaultLanguage(v) l := NewDefaultLanguage(v)
p := NewPathSpec(hugofs.NewMem(v), l) p, _ := NewPathSpec(hugofs.NewMem(v), l)
tests := []struct { tests := []struct {
input string input string
@ -99,7 +99,7 @@ func TestMakePathSanitizedDisablePathToLower(t *testing.T) {
v.Set("disablePathToLower", true) v.Set("disablePathToLower", true)
l := NewDefaultLanguage(v) l := NewDefaultLanguage(v)
p := NewPathSpec(hugofs.NewMem(v), l) p, _ := NewPathSpec(hugofs.NewMem(v), l)
tests := []struct { tests := []struct {
input string input string

View file

@ -60,10 +60,14 @@ func (p PathSpec) String() string {
} }
// NewPathSpec creats a new PathSpec from the given filesystems and Language. // 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 baseURLstr := cfg.GetString("baseURL")
baseURL, _ := newBaseURLFromString(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{ ps := &PathSpec{
fs: fs, fs: fs,
@ -87,7 +91,7 @@ func NewPathSpec(fs *hugofs.Fs, cfg config.Provider) *PathSpec {
ps.language = language ps.language = language
} }
return ps return ps, nil
} }
// PaginatePath returns the configured root path used for paginator pages. // PaginatePath returns the configured root path used for paginator pages.

View file

@ -40,8 +40,9 @@ func TestNewPathSpecFromConfig(t *testing.T) {
v.Set("staticDir", "thestatic") v.Set("staticDir", "thestatic")
v.Set("theme", "thetheme") 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.canonifyURLs)
require.True(t, p.defaultContentLanguageInSubdir) require.True(t, p.defaultContentLanguageInSubdir)
require.True(t, p.disablePathToLower) require.True(t, p.disablePathToLower)

View file

@ -8,7 +8,8 @@ import (
func newTestPathSpec(fs *hugofs.Fs, v *viper.Viper) *PathSpec { func newTestPathSpec(fs *hugofs.Fs, v *viper.Viper) *PathSpec {
l := NewDefaultLanguage(v) l := NewDefaultLanguage(v)
return NewPathSpec(fs, l) ps, _ := NewPathSpec(fs, l)
return ps
} }
func newTestDefaultPathSpec(configKeyValues ...interface{}) *PathSpec { func newTestDefaultPathSpec(configKeyValues ...interface{}) *PathSpec {

View file

@ -28,7 +28,7 @@ func TestURLize(t *testing.T) {
v := viper.New() v := viper.New()
l := NewDefaultLanguage(v) l := NewDefaultLanguage(v)
p := NewPathSpec(hugofs.NewMem(v), l) p, _ := NewPathSpec(hugofs.NewMem(v), l)
tests := []struct { tests := []struct {
input string input string
@ -89,7 +89,7 @@ func doTestAbsURL(t *testing.T, defaultInSubDir, addLanguage, multilingual bool,
for _, test := range tests { for _, test := range tests {
v.Set("baseURL", test.baseURL) v.Set("baseURL", test.baseURL)
l := NewLanguage(lang, v) l := NewLanguage(lang, v)
p := NewPathSpec(hugofs.NewMem(v), l) p, _ := NewPathSpec(hugofs.NewMem(v), l)
output := p.AbsURL(test.input, addLanguage) output := p.AbsURL(test.input, addLanguage)
expected := test.expected expected := test.expected
@ -167,7 +167,7 @@ func doTestRelURL(t *testing.T, defaultInSubDir, addLanguage, multilingual bool,
v.Set("baseURL", test.baseURL) v.Set("baseURL", test.baseURL)
v.Set("canonifyURLs", test.canonify) v.Set("canonifyURLs", test.canonify)
l := NewLanguage(lang, v) l := NewLanguage(lang, v)
p := NewPathSpec(hugofs.NewMem(v), l) p, _ := NewPathSpec(hugofs.NewMem(v), l)
output := p.RelURL(test.input, addLanguage) output := p.RelURL(test.input, addLanguage)
@ -255,7 +255,7 @@ func TestURLPrep(t *testing.T) {
v := viper.New() v := viper.New()
v.Set("uglyURLs", d.ugly) v.Set("uglyURLs", d.ugly)
l := NewDefaultLanguage(v) l := NewDefaultLanguage(v)
p := NewPathSpec(hugofs.NewMem(v), l) p, _ := NewPathSpec(hugofs.NewMem(v), l)
output := p.URLPrep(d.input) output := p.URLPrep(d.input)
if d.output != output { if d.output != output {

View file

@ -92,7 +92,13 @@ func applyDepsIfNeeded(cfg deps.DepsCfg, sites ...*Site) error {
if d == nil { if d == nil {
cfg.Language = s.Language cfg.Language = s.Language
cfg.WithTemplate = s.withSiteTemplates(cfg.WithTemplate) cfg.WithTemplate = s.withSiteTemplates(cfg.WithTemplate)
d = deps.New(cfg)
var err error
d, err = deps.New(cfg)
if err != nil {
return err
}
s.Deps = d s.Deps = d
if err = d.LoadResources(); err != nil { if err = d.LoadResources(); err != nil {

View file

@ -74,7 +74,8 @@ func (th testHelper) replaceDefaultContentLanguageValue(value string) string {
func newTestPathSpec(fs *hugofs.Fs, v *viper.Viper) *helpers.PathSpec { func newTestPathSpec(fs *hugofs.Fs, v *viper.Viper) *helpers.PathSpec {
l := helpers.NewDefaultLanguage(v) l := helpers.NewDefaultLanguage(v)
return helpers.NewPathSpec(fs, l) ps, _ := helpers.NewPathSpec(fs, l)
return ps
} }
func newTestDefaultPathSpec() *helpers.PathSpec { func newTestDefaultPathSpec() *helpers.PathSpec {
@ -82,7 +83,8 @@ func newTestDefaultPathSpec() *helpers.PathSpec {
// Easier to reason about in tests. // Easier to reason about in tests.
v.Set("disablePathToLower", true) v.Set("disablePathToLower", true)
fs := hugofs.NewDefault(v) fs := hugofs.NewDefault(v)
return helpers.NewPathSpec(fs, v) ps, _ := helpers.NewPathSpec(fs, v)
return ps
} }
func newTestCfg() (*viper.Viper, *hugofs.Fs) { func newTestCfg() (*viper.Viper, *hugofs.Fs) {

View file

@ -289,12 +289,16 @@ urlize: bat-man
} }
config.Fs = fs config.Fs = fs
d := deps.New(config) d, err := deps.New(config)
if err != nil {
t.Fatal(err)
}
if err := d.LoadResources(); err != nil { if err := d.LoadResources(); err != nil {
t.Fatal(err) t.Fatal(err)
} }
err := d.Tmpl.Lookup("test").Execute(&b, &data) err = d.Tmpl.Lookup("test").Execute(&b, &data)
if err != nil { if err != nil {
t.Fatal("Got error on execute", err) t.Fatal("Got error on execute", err)
@ -2902,12 +2906,13 @@ func TestPartialCached(t *testing.T) {
return nil return nil
} }
de := deps.New(config) de, err := deps.New(config)
require.NoError(t, err)
require.NoError(t, de.LoadResources()) require.NoError(t, de.LoadResources())
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
templ := de.Tmpl.Lookup("testroot") templ := de.Tmpl.Lookup("testroot")
err := templ.Execute(buf, &data) err = templ.Execute(buf, &data)
if err != nil { if err != nil {
t.Fatalf("[%d] error executing template: %s", i, err) t.Fatalf("[%d] error executing template: %s", i, err)
} }
@ -2941,7 +2946,8 @@ func BenchmarkPartial(b *testing.B) {
return nil return nil
} }
de := deps.New(config) de, err := deps.New(config)
require.NoError(b, err)
require.NoError(b, de.LoadResources()) require.NoError(b, de.LoadResources())
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
@ -2972,7 +2978,8 @@ func BenchmarkPartialCached(b *testing.B) {
return nil return nil
} }
de := deps.New(config) de, err := deps.New(config)
require.NoError(b, err)
require.NoError(b, de.LoadResources()) require.NoError(b, de.LoadResources())
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
@ -2994,7 +3001,10 @@ func newTestFuncster() *templateFuncster {
func newTestFuncsterWithViper(v *viper.Viper) *templateFuncster { func newTestFuncsterWithViper(v *viper.Viper) *templateFuncster {
config := newDepsConfig(v) config := newDepsConfig(v)
d := deps.New(config) d, err := deps.New(config)
if err != nil {
panic(err)
}
if err := d.LoadResources(); err != nil { if err := d.LoadResources(); err != nil {
panic(err) panic(err)
@ -3013,7 +3023,8 @@ func newTestTemplate(t *testing.T, name, template string) *template.Template {
return nil return nil
} }
de := deps.New(config) de, err := deps.New(config)
require.NoError(t, err)
require.NoError(t, de.LoadResources()) require.NoError(t, de.LoadResources())
return de.Tmpl.Lookup(name) return de.Tmpl.Lookup(name)

View file

@ -79,7 +79,8 @@ html lang=en
[]byte(this.baseContent), []byte(this.innerContent)) []byte(this.baseContent), []byte(this.innerContent))
} }
a := deps.New(config) a, err := deps.New(config)
require.NoError(t, err)
if err := a.LoadResources(); err != nil { if err := a.LoadResources(); err != nil {
t.Fatal(err) t.Fatal(err)
@ -94,7 +95,7 @@ html lang=en
} }
var buff bytes.Buffer var buff bytes.Buffer
err := a.Tmpl.ExecuteTemplate(&buff, "mytemplate.html", d) err = a.Tmpl.ExecuteTemplate(&buff, "mytemplate.html", d)
if err != nil && this.expectErr == 0 { if err != nil && this.expectErr == 0 {
t.Errorf("Test %d with root '%s' errored: %s", i, root, err) t.Errorf("Test %d with root '%s' errored: %s", i, root, err)
@ -288,7 +289,8 @@ func TestTplGoFuzzReports(t *testing.T) {
return templ.AddTemplate("fuzz", this.data) return templ.AddTemplate("fuzz", this.data)
} }
de := deps.New(config) de, err := deps.New(config)
require.NoError(t, err)
require.NoError(t, de.LoadResources()) require.NoError(t, de.LoadResources())
templ := de.Tmpl.(*GoHTMLTemplate) templ := de.Tmpl.(*GoHTMLTemplate)
@ -299,7 +301,7 @@ func TestTplGoFuzzReports(t *testing.T) {
t.Errorf("#1 Test %d should have errored", i) t.Errorf("#1 Test %d should have errored", i)
} }
err := de.Tmpl.ExecuteTemplate(ioutil.Discard, "fuzz", d) err = de.Tmpl.ExecuteTemplate(ioutil.Discard, "fuzz", d)
if err != nil && this.expectErr == 0 { if err != nil && this.expectErr == 0 {
t.Fatalf("Test %d errored: %s", i, err) t.Fatalf("Test %d errored: %s", i, err)