Skip to content

Commit

Permalink
fix: get local folder from NUV_PWD in plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
giusdp committed Aug 1, 2023
1 parent 8be5e53 commit d0eeb51
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 20 deletions.
7 changes: 2 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,8 +280,6 @@ func wskPropertySet(apihost, auth string) error {
}

func runNuv(baseDir string, args []string) error {
localFolder := os.Getenv("NUV_PWD")

err := Nuv(baseDir, args[1:])
if err == nil {
return nil
Expand All @@ -292,7 +290,7 @@ func runNuv(baseDir string, args []string) error {
var taskNotFoundErr *TaskNotFoundErr
if errors.As(err, &taskNotFoundErr) {
trace("task not found, looking for plugin:", args[1])
plgDir, err := findTaskInPlugins(localFolder, args[1])
plgDir, err := findTaskInPlugins(args[1])
if err != nil {
return taskNotFoundErr
}
Expand All @@ -317,8 +315,7 @@ func setupNuvPwd() {
}

func buildConfigMap(nuvRootPath string, configPath string) (*config.ConfigMap, error) {
localDir := os.Getenv("NUV_PWD")
plgNuvRootMap, err := GetNuvRootPlugins(localDir)
plgNuvRootMap, err := GetNuvRootPlugins()
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion nuv.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ func Nuv(base string, args []string) error {
err := help()
if !isSubCmd {
fmt.Println()
return printPluginsHelp(parent(base))
return printPluginsHelp()
}
return err
}
Expand Down
15 changes: 8 additions & 7 deletions plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ func checkGitRepo(url string) (bool, string) {
return false, ""
}

func printPluginsHelp(localDir string) error {
plgs, err := newPlugins(localDir)
func printPluginsHelp() error {
plgs, err := newPlugins()
if err != nil {
return err
}
Expand All @@ -143,8 +143,8 @@ func printPluginsHelp(localDir string) error {
// If the same plugin is found in both folders, the one in the local folder
// is used.
// Useful to build the config map including the plugin configs
func GetNuvRootPlugins(localDir string) (map[string]string, error) {
plgs, err := newPlugins(localDir)
func GetNuvRootPlugins() (map[string]string, error) {
plgs, err := newPlugins()
if err != nil {
return nil, err
}
Expand All @@ -171,8 +171,8 @@ func GetNuvRootPlugins(localDir string) (map[string]string, error) {

// findTaskInPlugins returns the path to the plugin containing the task
// or an error if the task is not found
func findTaskInPlugins(localDir string, plg string) (string, error) {
plgs, err := newPlugins(localDir)
func findTaskInPlugins(plg string) (string, error) {
plgs, err := newPlugins()
if err != nil {
return "", err
}
Expand Down Expand Up @@ -202,7 +202,8 @@ type plugins struct {
nuv []string
}

func newPlugins(localDir string) (*plugins, error) {
func newPlugins() (*plugins, error) {
localDir := os.Getenv("NUV_PWD")
localOlarisFolders := make([]string, 0)
nuvOlarisFolders := make([]string, 0)

Expand Down
22 changes: 15 additions & 7 deletions plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,17 @@ func TestGetAllNuvRootPlugins(t *testing.T) {
t.Run("success: get all the nuvroots.json from plugins with 1 plugin", func(t *testing.T) {
tempDir := t.TempDir()
plgFolder := setupPluginTest(tempDir, t)
os.Setenv("NUV_PWD", tempDir)

nuvRoots, err := GetNuvRootPlugins(tempDir)
nuvRoots, err := GetNuvRootPlugins()
require.NoError(t, err)
require.Len(t, nuvRoots, 1)
require.Equal(t, joinpath(plgFolder, NUVROOT), nuvRoots[getPluginName(plgFolder)])
})

t.Run("success: get all the nuvroots.json from plugins with 2 plugins", func(t *testing.T) {
tempDir := t.TempDir()
os.Setenv("NUV_PWD", tempDir)
plgFolder := setupPluginTest(tempDir, t)

// create the olaris-test2 folder
Expand All @@ -96,7 +98,7 @@ func TestGetAllNuvRootPlugins(t *testing.T) {
err = copyFile(nuvfileYML, filepath.Join(olarisTestDir, "nuvfile.yml"))
require.NoError(t, err)

nuvRoots, err := GetNuvRootPlugins(tempDir)
nuvRoots, err := GetNuvRootPlugins()
require.NoError(t, err)
require.Len(t, nuvRoots, 2)
require.Equal(t, joinpath(plgFolder, NUVROOT), nuvRoots[getPluginName(plgFolder)])
Expand All @@ -105,9 +107,10 @@ func TestGetAllNuvRootPlugins(t *testing.T) {

t.Run("empty: no plugins folder found (olaris-*)", func(t *testing.T) {
tempDir := t.TempDir()
os.Setenv("NUV_PWD", tempDir)

// Test when the folder is not found
nuvRoots, err := GetNuvRootPlugins(tempDir)
nuvRoots, err := GetNuvRootPlugins()
require.NoError(t, err)
require.Empty(t, nuvRoots)
})
Expand All @@ -116,18 +119,20 @@ func TestGetAllNuvRootPlugins(t *testing.T) {
func TestFindPluginTask(t *testing.T) {
t.Run("success: plugin task found in ./olaris-test", func(t *testing.T) {
tempDir := t.TempDir()
os.Setenv("NUV_PWD", tempDir)
plgFolder := setupPluginTest(tempDir, t)

fld, err := findTaskInPlugins(tempDir, "test")
fld, err := findTaskInPlugins("test")
require.NoError(t, err)
require.Equal(t, plgFolder, fld)
})

t.Run("error: no plugins folder found (olaris-*)", func(t *testing.T) {
tempDir := t.TempDir()
os.Setenv("NUV_PWD", tempDir)

// Test when the folder is not found
fld, err := findTaskInPlugins(tempDir, "grep")
fld, err := findTaskInPlugins("grep")
require.Error(t, err)
require.Empty(t, fld)
})
Expand All @@ -138,7 +143,9 @@ func TestNewPlugins(t *testing.T) {
tempDir := t.TempDir()
plgFolder := setupPluginTest(tempDir, t)

p, err := newPlugins(tempDir)
os.Setenv("NUV_PWD", tempDir)

p, err := newPlugins()
require.NoError(t, err)
require.NotNil(t, p)
require.Len(t, p.local, 1)
Expand All @@ -147,7 +154,8 @@ func TestNewPlugins(t *testing.T) {

t.Run("non existent local dir results in empty local field", func(t *testing.T) {
localDir := "/path/to/nonexistent/dir"
p, err := newPlugins(localDir)
os.Setenv("NUV_PWD", localDir)
p, err := newPlugins()
require.NoError(t, err)
require.NotNil(t, p)
require.Len(t, p.local, 0)
Expand Down

0 comments on commit d0eeb51

Please sign in to comment.