diff --git a/htesting/testdata_builder.go b/htesting/testdata_builder.go new file mode 100644 index 00000000000..437c2ad2f62 --- /dev/null +++ b/htesting/testdata_builder.go @@ -0,0 +1,39 @@ +// Copyright 2018 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 htesting + +import ( + "path/filepath" + "testing" + + "github.com/spf13/afero" +) + +type testdataBuilder struct { + t testing.TB + fs afero.Fs + workingDir string +} + +func NewTestdataBuilder(fs afero.Fs, workingDir string, t testing.TB) *testdataBuilder { + workingDir = filepath.Clean(workingDir) + return &testdataBuilder{fs: fs, workingDir: workingDir, t: t} +} + +func (b *testdataBuilder) Add(filename, content string) *testdataBuilder { + if err := afero.WriteFile(b.fs, filepath.Join(b.workingDir, filename), []byte(content), 0666); err != nil { + b.t.Fatalf("failed to add %q: %s", filename, err) + } + return b +} diff --git a/hugolib/config.go b/hugolib/config.go index 77ebb42ae6f..aa7a78e0ce7 100644 --- a/hugolib/config.go +++ b/hugolib/config.go @@ -74,6 +74,9 @@ type ConfigSourceDescriptor struct { // The project's working dir. Is used to look for additional theme config. WorkingDir string + + // The (optional) directory for additional configuration files. + AbsConfigDir string } func (d ConfigSourceDescriptor) configFilenames() []string { diff --git a/hugolib/configdir_test.go b/hugolib/configdir_test.go new file mode 100644 index 00000000000..03c21de3f29 --- /dev/null +++ b/hugolib/configdir_test.go @@ -0,0 +1,46 @@ +// Copyright 2018 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 ( + "testing" + + "github.com/spf13/afero" + "github.com/stretchr/testify/require" +) + +func TestLoadConfigDir(t *testing.T) { + t.Parallel() + + assert := require.New(t) + + // Add a random config variable for testing. + // side = page in Norwegian. + configContent := ` +paginatePath = "side" +` + + mm := afero.NewMemMapFs() + + writeToFs(t, mm, "hugo.toml", configContent) + + cfg, _, err := LoadConfig(ConfigSourceDescriptor{Fs: mm, Filename: "hugo.toml"}) + require.NoError(t, err) + + assert.Equal("side", cfg.GetString("paginatePath")) + // default + assert.Equal("layouts", cfg.GetString("layoutDir")) + // no themes + assert.False(cfg.IsSet("allThemes")) +}