Skip to content

Commit

Permalink
fix: use version in home dir when no version found in root dir (#1883)
Browse files Browse the repository at this point in the history
  • Loading branch information
Stratus3D authored Feb 4, 2025
1 parent e63aec6 commit 5ae5f76
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 20 deletions.
10 changes: 10 additions & 0 deletions internal/resolve/resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,17 @@ func Version(conf config.Config, plugin plugins.Plugin, directory string) (versi
}

nextDir := path.Dir(directory)
// If current dir and next dir are the same it means we've reached `/` and
// have no more parent directories to search.
if nextDir == directory {
// If no version found, try current users home directory. I'd like to
// eventually remove this feature.
homeDir, osErr := os.UserHomeDir()
if osErr != nil {
break
}

versions, found, err = findVersionsInDir(conf, plugin, homeDir)
break
}
directory = nextDir
Expand Down
29 changes: 16 additions & 13 deletions internal/resolve/resolve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"os"
"path/filepath"
"strings"
"testing"

"github.com/asdf-vm/asdf/internal/config"
Expand All @@ -12,13 +13,15 @@ import (
"github.com/stretchr/testify/assert"
)

const testPluginName = "test-plugin"

func TestVersion(t *testing.T) {
testDataDir := t.TempDir()
currentDir := t.TempDir()
conf := config.Config{DataDir: testDataDir, DefaultToolVersionsFilename: ".tool-versions", ConfigFile: "testdata/asdfrc"}
_, err := repotest.InstallPlugin("dummy_plugin", conf.DataDir, "lua")
_, err := repotest.InstallPlugin("dummy_plugin", conf.DataDir, testPluginName)
assert.Nil(t, err)
plugin := plugins.New(conf, "lua")
plugin := plugins.New(conf, testPluginName)

t.Run("returns empty slice when non-existent version passed", func(t *testing.T) {
toolVersion, found, err := Version(conf, plugin, t.TempDir())
Expand All @@ -29,7 +32,7 @@ func TestVersion(t *testing.T) {

t.Run("returns single version from .tool-versions file", func(t *testing.T) {
// write a version file
data := []byte("lua 1.2.3")
data := []byte(fmt.Sprintf("%s 1.2.3", testPluginName))
err = os.WriteFile(filepath.Join(currentDir, ".tool-versions"), data, 0o666)

toolVersion, found, err := Version(conf, plugin, currentDir)
Expand All @@ -40,10 +43,10 @@ func TestVersion(t *testing.T) {

t.Run("returns version from env when env variable set", func(t *testing.T) {
// Set env
t.Setenv("ASDF_LUA_VERSION", "2.3.4")
t.Setenv(fmt.Sprintf("ASDF_%s_VERSION", strings.ToUpper(testPluginName)), "2.3.4")

// write a version file
data := []byte("lua 1.2.3")
data := []byte(fmt.Sprintf("%s 1.2.3", testPluginName))
err = os.WriteFile(filepath.Join(currentDir, ".tool-versions"), data, 0o666)

// assert env variable takes precedence
Expand All @@ -55,7 +58,7 @@ func TestVersion(t *testing.T) {

t.Run("returns single version from .tool-versions file in parent directory", func(t *testing.T) {
// write a version file
data := []byte("lua 1.2.3")
data := []byte(fmt.Sprintf("%s 1.2.3", testPluginName))
err = os.WriteFile(filepath.Join(currentDir, ".tool-versions"), data, 0o666)

subDir := filepath.Join(currentDir, "subdir")
Expand All @@ -72,9 +75,9 @@ func TestVersion(t *testing.T) {
func TestFindVersionsInDir(t *testing.T) {
testDataDir := t.TempDir()
conf := config.Config{DataDir: testDataDir, DefaultToolVersionsFilename: ".tool-versions", ConfigFile: "testdata/asdfrc"}
_, err := repotest.InstallPlugin("dummy_plugin", conf.DataDir, "lua")
_, err := repotest.InstallPlugin("dummy_plugin", conf.DataDir, testPluginName)
assert.Nil(t, err)
plugin := plugins.New(conf, "lua")
plugin := plugins.New(conf, testPluginName)

t.Run("when no versions set returns found false", func(t *testing.T) {
currentDir := t.TempDir()
Expand All @@ -89,7 +92,7 @@ func TestFindVersionsInDir(t *testing.T) {
t.Run("when version is set returns found true and version", func(t *testing.T) {
currentDir := t.TempDir()

data := []byte("lua 1.2.3")
data := []byte(fmt.Sprintf("%s 1.2.3", testPluginName))
err = os.WriteFile(filepath.Join(currentDir, ".tool-versions"), data, 0o666)

toolVersion, found, err := findVersionsInDir(conf, plugin, currentDir)
Expand All @@ -102,7 +105,7 @@ func TestFindVersionsInDir(t *testing.T) {
t.Run("when multiple versions present in .tool-versions returns found true and versions", func(t *testing.T) {
currentDir := t.TempDir()

data := []byte("lua 1.2.3 2.3.4")
data := []byte(fmt.Sprintf("%s 1.2.3 2.3.4", testPluginName))
err = os.WriteFile(filepath.Join(currentDir, ".tool-versions"), data, 0o666)

toolVersion, found, err := findVersionsInDir(conf, plugin, currentDir)
Expand All @@ -116,7 +119,7 @@ func TestFindVersionsInDir(t *testing.T) {
conf := config.Config{DataDir: testDataDir, DefaultToolVersionsFilename: "custom-file"}
currentDir := t.TempDir()

data := []byte("lua 1.2.3 2.3.4")
data := []byte(fmt.Sprintf("%s 1.2.3 2.3.4", testPluginName))
err = os.WriteFile(filepath.Join(currentDir, "custom-file"), data, 0o666)

toolVersion, found, err := findVersionsInDir(conf, plugin, currentDir)
Expand All @@ -143,9 +146,9 @@ func TestFindVersionsInDir(t *testing.T) {
func TestFindVersionsLegacyFiles(t *testing.T) {
testDataDir := t.TempDir()
conf := config.Config{DataDir: testDataDir}
_, err := repotest.InstallPlugin("dummy_plugin", conf.DataDir, "lua")
_, err := repotest.InstallPlugin("dummy_plugin", conf.DataDir, testPluginName)
assert.Nil(t, err)
plugin := plugins.New(conf, "lua")
plugin := plugins.New(conf, testPluginName)

t.Run("when given tool that lacks list-legacy-filenames callback returns empty versions list", func(t *testing.T) {
pluginName := "foobar"
Expand Down
8 changes: 5 additions & 3 deletions internal/shims/shims.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,9 @@ func FindExecutable(conf config.Config, shimName, currentDirectory string) (stri
}

versions.Versions = tempVersions
existingPluginToolVersions[plugin] = versions
if len(versions.Versions) > 0 {
existingPluginToolVersions[plugin] = versions
}
}
}
}
Expand Down Expand Up @@ -131,9 +133,9 @@ func FindExecutable(conf config.Config, shimName, currentDirectory string) (stri

tools := []string{}
versions := []string{}
for plugin := range existingPluginToolVersions {
for plugin, toolVersions := range existingPluginToolVersions {
tools = append(tools, plugin.Name)
versions = append(versions, existingPluginToolVersions[plugin].Versions...)
versions = append(versions, toolVersions.Versions...)
}

return "", plugins.Plugin{}, "", false, NoExecutableForPluginError{shim: shimName, tools: tools, versions: versions}
Expand Down
6 changes: 3 additions & 3 deletions internal/versions/testdata/asdfrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pre_asdf_download_lua = echo pre_asdf_download_lua $@
pre_asdf_install_lua = echo pre_asdf_install_lua $@
post_asdf_install_lua = echo post_asdf_install_lua $@
pre_asdf_download_testlua = echo pre_asdf_download_lua $@
pre_asdf_install_testlua = echo pre_asdf_install_lua $@
post_asdf_install_testlua = echo post_asdf_install_lua $@
always_keep_download = yes
2 changes: 1 addition & 1 deletion internal/versions/versions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/stretchr/testify/assert"
)

const testPluginName = "lua"
const testPluginName = "testlua"

func TestInstallAll(t *testing.T) {
t.Run("installs multiple tools when multiple tool versions are specified", func(t *testing.T) {
Expand Down

0 comments on commit 5ae5f76

Please sign in to comment.