diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2023-05-20 15:04:22 +0200 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2023-05-20 17:45:56 +0200 |
commit | 7c7baa618325cb3d2b1ef48bdc1f97aae25f62e9 (patch) | |
tree | f80cd6e57e2f9dcb07ceae3e7c022f41d4076bfe /common | |
parent | 4f085e80da9a415725db3def70e5ce847cf06741 (diff) |
Add hugo.WorkingDir
Fixes #10969
Diffstat (limited to 'common')
-rw-r--r-- | common/hugo/hugo.go | 21 | ||||
-rw-r--r-- | common/hugo/hugo_test.go | 19 |
2 files changed, 34 insertions, 6 deletions
diff --git a/common/hugo/hugo.go b/common/hugo/hugo.go index 6402d7b88..4769852a0 100644 --- a/common/hugo/hugo.go +++ b/common/hugo/hugo.go @@ -60,6 +60,7 @@ type HugoInfo struct { // version of go that the Hugo binary was built with GoVersion string + conf ConfigProvider deps []*Dependency } @@ -81,15 +82,26 @@ func (i HugoInfo) IsExtended() bool { return IsExtended } +// WorkingDir returns the project working directory. +func (i HugoInfo) WorkingDir() string { + return i.conf.WorkingDir() +} + // Deps gets a list of dependencies for this Hugo build. func (i HugoInfo) Deps() []*Dependency { return i.deps } +// ConfigProvider represents the config options that are relevant for HugoInfo. +type ConfigProvider interface { + Environment() string + WorkingDir() string +} + // NewInfo creates a new Hugo Info object. -func NewInfo(environment string, deps []*Dependency) HugoInfo { - if environment == "" { - environment = EnvironmentProduction +func NewInfo(conf ConfigProvider, deps []*Dependency) HugoInfo { + if conf.Environment() == "" { + panic("environment not set") } var ( commitHash string @@ -107,7 +119,8 @@ func NewInfo(environment string, deps []*Dependency) HugoInfo { return HugoInfo{ CommitHash: commitHash, BuildDate: buildDate, - Environment: environment, + Environment: conf.Environment(), + conf: conf, deps: deps, GoVersion: goVersion, } diff --git a/common/hugo/hugo_test.go b/common/hugo/hugo_test.go index f2ad0f5c1..b0279f111 100644 --- a/common/hugo/hugo_test.go +++ b/common/hugo/hugo_test.go @@ -23,10 +23,12 @@ import ( func TestHugoInfo(t *testing.T) { c := qt.New(t) - hugoInfo := NewInfo("", nil) + conf := testConfig{environment: "production", workingDir: "/mywork"} + hugoInfo := NewInfo(conf, nil) c.Assert(hugoInfo.Version(), qt.Equals, CurrentVersion.Version()) c.Assert(fmt.Sprintf("%T", VersionString("")), qt.Equals, fmt.Sprintf("%T", hugoInfo.Version())) + c.Assert(hugoInfo.WorkingDir(), qt.Equals, "/mywork") bi := getBuildInfo() if bi != nil { @@ -39,6 +41,19 @@ func TestHugoInfo(t *testing.T) { c.Assert(hugoInfo.IsProduction(), qt.Equals, true) c.Assert(hugoInfo.IsExtended(), qt.Equals, IsExtended) - devHugoInfo := NewInfo("development", nil) + devHugoInfo := NewInfo(testConfig{environment: "development"}, nil) c.Assert(devHugoInfo.IsProduction(), qt.Equals, false) } + +type testConfig struct { + environment string + workingDir string +} + +func (c testConfig) Environment() string { + return c.environment +} + +func (c testConfig) WorkingDir() string { + return c.workingDir +} |