Skip to content

Commit

Permalink
Add /config dir support
Browse files Browse the repository at this point in the history
  • Loading branch information
bep committed Dec 5, 2018
1 parent 514e18d commit feab302
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 0 deletions.
39 changes: 39 additions & 0 deletions htesting/testdata_builder.go
Original file line number Diff line number Diff line change
@@ -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
}
3 changes: 3 additions & 0 deletions hugolib/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
46 changes: 46 additions & 0 deletions hugolib/configdir_test.go
Original file line number Diff line number Diff line change
@@ -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"))
}

0 comments on commit feab302

Please sign in to comment.