Skip to content

Commit 43a84a0

Browse files
fix: correct handling of ASDF_FORCE_PREPEND environment variable (#2011)
Co-authored-by: Trevor Brown <[email protected]>
1 parent b9d5791 commit 43a84a0

File tree

4 files changed

+61
-2
lines changed

4 files changed

+61
-2
lines changed

internal/config/config.go

+9-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ package config
55
import (
66
"context"
77
"io/fs"
8+
"os"
9+
"runtime"
810
"strconv"
911
"strings"
1012

@@ -39,7 +41,7 @@ type Config struct {
3941
// Unclear if this value will be needed with the golang implementation.
4042
// AsdfDir string
4143
DataDir string `env:"ASDF_DATA_DIR, overwrite"`
42-
ForcePrepend bool `env:"ASDF_FORCE_PREPEND, overwrite"`
44+
ForcePrepend bool
4345
// Field that stores the settings struct if it is loaded
4446
Settings Settings
4547
PluginIndexURL string
@@ -59,8 +61,13 @@ type Settings struct {
5961
}
6062

6163
func defaultConfig(dataDir, configFile string) *Config {
64+
forcePrepend := forcePrependDefault
65+
forcePrependEnv := os.Getenv("ASDF_FORCE_PREPEND")
66+
if forcePrependEnv == "yes" || (forcePrependEnv == "" && runtime.GOOS == "darwin") {
67+
forcePrepend = true
68+
}
6269
return &Config{
63-
ForcePrepend: forcePrependDefault,
70+
ForcePrepend: forcePrepend,
6471
DataDir: dataDir,
6572
ConfigFile: configFile,
6673
DefaultToolVersionsFilename: defaultToolVersionsFilenameDefault,

internal/config/config_darwin_test.go

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//go:build darwin
2+
3+
package config
4+
5+
import (
6+
"testing"
7+
8+
"github.com/stretchr/testify/assert"
9+
)
10+
11+
func TestLoadConfigEnv_WithForcePrependEnv_OnDarwin(t *testing.T) {
12+
t.Run("When ASDF_FORCE_PREPEND env does not given on macOS", func(t *testing.T) {
13+
config, _ := loadConfigEnv()
14+
15+
assert.True(t, config.ForcePrepend, "Then ForcePrepend property is true")
16+
})
17+
}

internal/config/config_linux_test.go

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//go:build linux
2+
3+
package config
4+
5+
import (
6+
"testing"
7+
8+
"github.com/stretchr/testify/assert"
9+
)
10+
11+
func TestLoadConfigEnv_WithForcePrependEnv_OnLinux(t *testing.T) {
12+
t.Run("When ASDF_FORCE_PREPEND env does not given on Linux", func(t *testing.T) {
13+
config, _ := loadConfigEnv()
14+
15+
assert.False(t, config.ForcePrepend, "Then ForcePrepend property is false")
16+
})
17+
}

internal/config/config_test.go

+18
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,24 @@ func TestLoadConfigEnv(t *testing.T) {
2222
assert.Zero(t, config.Home, "Shouldn't set Home property when loading config")
2323
}
2424

25+
func TestLoadConfigEnv_WithForcePrependEnv(t *testing.T) {
26+
t.Run("When ASDF_FORCE_PREPEND env given yes", func(t *testing.T) {
27+
t.Setenv("ASDF_FORCE_PREPEND", "yes")
28+
29+
config, _ := loadConfigEnv()
30+
31+
assert.True(t, config.ForcePrepend, "Then ForcePrepend property is true")
32+
})
33+
34+
t.Run("When ASDF_FORCE_PREPEND env given any string other than yes", func(t *testing.T) {
35+
t.Setenv("ASDF_FORCE_PREPEND", "no")
36+
37+
config, _ := loadConfigEnv()
38+
39+
assert.False(t, config.ForcePrepend, "Then ForcePrepend property is false")
40+
})
41+
}
42+
2543
func TestLoadSettings(t *testing.T) {
2644
t.Run("When given invalid path returns error", func(t *testing.T) {
2745
settings, err := loadSettings("./foobar")

0 commit comments

Comments
 (0)