asdf/hook/hook_test.go
Trevor Brown 778ab34a6f feat(golang-rewrite): create RunCallback method for Plugin struct
* Create `plugins.New` function, updating existing code to use it
* Add another test for `hook.Run` function
* Enable `plugin_add_command.bats` tests for Go implementation of asdf
* Add `RunCallback` method to `Plugin` struct
* Update `plugins.Add` function to run `post-plugin-add` plugin callback script
* Handle Bash expression and scripts properly in `execute` package so `$@` is always set
2024-12-18 11:32:01 -05:00

48 lines
1.2 KiB
Go

package hook
import (
"os/exec"
"testing"
"asdf/config"
"github.com/stretchr/testify/assert"
)
func TestRun(t *testing.T) {
// Set the asdf config file location to the test file
t.Setenv("ASDF_CONFIG_FILE", "testdata/asdfrc")
t.Run("accepts config, hook name, and a slice of string arguments", func(t *testing.T) {
config, err := config.LoadConfig()
assert.Nil(t, err)
err = Run(config, "pre_asdf_plugin_add_test", []string{})
assert.Nil(t, err)
})
t.Run("passes argument to command", func(t *testing.T) {
config, err := config.LoadConfig()
assert.Nil(t, err)
err = Run(config, "pre_asdf_plugin_add_test2", []string{"123"})
assert.Equal(t, 123, err.(*exec.ExitError).ExitCode())
})
t.Run("passes arguments to command", func(t *testing.T) {
config, err := config.LoadConfig()
assert.Nil(t, err)
err = Run(config, "pre_asdf_plugin_add_test3", []string{"exit 123"})
assert.Equal(t, 123, err.(*exec.ExitError).ExitCode())
})
t.Run("does not return error when no such hook is defined in asdfrc", func(t *testing.T) {
config, err := config.LoadConfig()
assert.Nil(t, err)
err = Run(config, "nonexistant-hook", []string{})
assert.Nil(t, err)
})
}