github.com/danhper/asdf-exec@v0.1.3-0.20230723223931-b71340b83071/shim_test.go (about)

     1  package main
     2  
     3  import (
     4  	"os"
     5  	"path"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  func TestParseExecutableLine(t *testing.T) {
    12  	executable, err := ParseExecutableLine("python", "# asdf-plugin: python 3.6.7")
    13  	assert.Nil(t, err)
    14  	assert.Equal(t, executable.PluginName, "python")
    15  	assert.Equal(t, executable.PluginVersion, "3.6.7")
    16  }
    17  
    18  func TestGetExecutablesFromShim(t *testing.T) {
    19  	shimContent := `
    20  	#!/usr/bin/env bash
    21      # asdf-plugin: python 3.6.7
    22  	# asdf-plugin: python 2.7.11
    23  	exec /home/daniel/.asdf/bin/asdf exec "python" "$@"
    24  	`
    25  	executables, err := GetExecutablesFromShim("python", shimContent)
    26  	assert.Nil(t, err)
    27  	assert.Len(t, executables, 2)
    28  }
    29  
    30  func TestFindExecutable(t *testing.T) {
    31  	config := Config{LegacyVersionFile: false}
    32  	cwd, err := os.Getwd()
    33  	assert.Nil(t, err)
    34  	currentHome := os.Getenv("HOME")
    35  	os.Setenv("HOME", "/tmp")
    36  	os.Setenv("ASDF_DATA_DIR", path.Join(cwd, "fixtures", "asdf"))
    37  
    38  	defer os.Setenv("HOME", currentHome)
    39  	defer os.Chdir(cwd)
    40  	defer os.Unsetenv("ASDF_DATA_DIR")
    41  
    42  	assert.Nil(t, os.Chdir("/tmp"))
    43  
    44  	_, found, err := FindExecutable("flask", config)
    45  	assert.Nil(t, err)
    46  	assert.False(t, found)
    47  
    48  	assert.Nil(t, os.Chdir(path.Join(cwd, "fixtures", "some-dir", "nested-dir")))
    49  	executablePath, found, err := FindExecutable("flask", config)
    50  	assert.Nil(t, err)
    51  	assert.True(t, found)
    52  	expectedPath := path.Join(GetAsdfDataPath(), "installs", "python", "3.6.7", "bin", "flask")
    53  	assert.Equal(t, expectedPath, executablePath)
    54  }
    55  
    56  func TestGetExecutablePath(t *testing.T) {
    57  	cwd, err := os.Getwd()
    58  	assert.Nil(t, err)
    59  	os.Setenv("ASDF_DATA_DIR", path.Join(cwd, "fixtures", "asdf"))
    60  	defer os.Unsetenv("ASDF_DATA_DIR")
    61  
    62  	executable := Executable{Name: "2to3", PluginName: "python", PluginVersion: "2.7.11"}
    63  	executablePath, err := GetExecutablePath(executable)
    64  	assert.Nil(t, err)
    65  	expected := path.Join(os.Getenv("ASDF_DATA_DIR"), "installs", "python", "2.7.11", "bin", "2to3")
    66  	assert.Equal(t, expected, executablePath)
    67  
    68  	// check it works with list-bin-paths
    69  	executable = Executable{Name: "go", PluginName: "go", PluginVersion: "1.9.1"}
    70  	executablePath, err = GetExecutablePath(executable)
    71  	assert.Nil(t, err)
    72  	expected = path.Join(os.Getenv("ASDF_DATA_DIR"), "installs", "go", "1.9.1", "go", "bin", "go")
    73  	assert.Equal(t, expected, executablePath)
    74  }