2024-02-10 13:02:36 -07:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-07-03 17:27:13 -07:00
|
|
|
"fmt"
|
2024-02-10 13:02:36 -07:00
|
|
|
"os/exec"
|
2024-07-03 17:27:13 -07:00
|
|
|
"strings"
|
2024-02-10 13:02:36 -07:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Basic integration tests using the legacy BATS test scripts. This ensures the
|
|
|
|
// new Golang implementation matches the existing Bash implementation.
|
|
|
|
|
|
|
|
func TestBatsTests(t *testing.T) {
|
2024-03-07 12:15:24 -07:00
|
|
|
dir := t.TempDir()
|
2024-02-10 14:05:48 -07:00
|
|
|
|
|
|
|
// Build asdf and put in temp directory
|
2024-03-01 06:43:52 -07:00
|
|
|
buildAsdf(t, dir)
|
2024-02-10 14:05:48 -07:00
|
|
|
|
|
|
|
// Run tests with the asdf binary in the temp directory
|
2024-02-10 13:02:36 -07:00
|
|
|
|
2024-02-10 17:58:13 -07:00
|
|
|
// Uncomment these as they are implemented
|
|
|
|
//t.Run("current_command", func(t *testing.T) {
|
2024-03-01 06:43:52 -07:00
|
|
|
// runBatsFile(t, dir, "current_command.bats")
|
2024-02-10 17:58:13 -07:00
|
|
|
//})
|
2024-02-10 13:02:36 -07:00
|
|
|
|
2024-02-10 17:58:13 -07:00
|
|
|
//t.Run("help_command", func(t *testing.T) {
|
2024-03-01 06:43:52 -07:00
|
|
|
// runBatsFile(t, dir, "help_command.bats")
|
2024-02-10 17:58:13 -07:00
|
|
|
//})
|
2024-02-10 13:02:36 -07:00
|
|
|
|
2024-08-20 05:15:05 -07:00
|
|
|
t.Run("info_command", func(t *testing.T) {
|
|
|
|
runBatsFile(t, dir, "info_command.bats")
|
|
|
|
})
|
2024-02-10 13:02:36 -07:00
|
|
|
|
2024-02-10 17:58:13 -07:00
|
|
|
//t.Run("install_command", func(t *testing.T) {
|
2024-03-01 06:43:52 -07:00
|
|
|
// runBatsFile(t, dir, "install_command.bats")
|
2024-02-10 17:58:13 -07:00
|
|
|
//})
|
2024-02-10 13:02:36 -07:00
|
|
|
|
2024-08-20 05:15:05 -07:00
|
|
|
t.Run("latest_command", func(t *testing.T) {
|
|
|
|
runBatsFile(t, dir, "latest_command.bats")
|
|
|
|
})
|
2024-02-10 13:02:36 -07:00
|
|
|
|
2024-02-10 17:58:13 -07:00
|
|
|
//t.Run("list_command", func(t *testing.T) {
|
2024-03-01 06:43:52 -07:00
|
|
|
// runBatsFile(t, dir, "list_command.bats")
|
2024-02-10 17:58:13 -07:00
|
|
|
//})
|
2024-02-10 13:02:36 -07:00
|
|
|
|
2024-07-03 17:27:13 -07:00
|
|
|
t.Run("plugin_add_command", func(t *testing.T) {
|
|
|
|
runBatsFile(t, dir, "plugin_add_command.bats")
|
|
|
|
})
|
2024-02-10 13:02:36 -07:00
|
|
|
|
2024-02-10 17:58:13 -07:00
|
|
|
//t.Run("plugin_extension_command", func(t *testing.T) {
|
2024-03-01 06:43:52 -07:00
|
|
|
// runBatsFile(t, dir, "plugin_extension_command.bats")
|
2024-02-10 17:58:13 -07:00
|
|
|
//})
|
2024-02-10 13:02:36 -07:00
|
|
|
|
2024-02-10 17:58:13 -07:00
|
|
|
//t.Run("plugin_list_all_command", func(t *testing.T) {
|
2024-03-01 06:43:52 -07:00
|
|
|
// runBatsFile(t, dir, "plugin_list_all_command.bats")
|
2024-02-10 17:58:13 -07:00
|
|
|
//})
|
2024-02-10 13:02:36 -07:00
|
|
|
|
2024-08-22 06:16:28 -07:00
|
|
|
t.Run("plugin_remove_command", func(t *testing.T) {
|
|
|
|
runBatsFile(t, dir, "plugin_remove_command.bats")
|
|
|
|
})
|
2024-02-10 13:02:36 -07:00
|
|
|
|
2024-02-10 17:58:13 -07:00
|
|
|
//t.Run("plugin_test_command", func(t *testing.T) {
|
2024-03-01 06:43:52 -07:00
|
|
|
// runBatsFile(t, dir, "plugin_test_command.bats")
|
2024-02-10 17:58:13 -07:00
|
|
|
//})
|
2024-02-10 13:02:36 -07:00
|
|
|
|
2024-02-10 17:58:13 -07:00
|
|
|
//t.Run("plugin_update_command", func(t *testing.T) {
|
2024-03-01 06:43:52 -07:00
|
|
|
// runBatsFile(t, dir, "plugin_update_command.bats")
|
2024-02-10 17:58:13 -07:00
|
|
|
//})
|
2024-02-10 13:02:36 -07:00
|
|
|
|
2024-02-10 17:58:13 -07:00
|
|
|
//t.Run("remove_command", func(t *testing.T) {
|
2024-03-01 06:43:52 -07:00
|
|
|
// runBatsFile(t, dir, "remove_command.bats")
|
2024-02-10 17:58:13 -07:00
|
|
|
//})
|
2024-02-10 13:02:36 -07:00
|
|
|
|
2024-09-05 19:12:08 -07:00
|
|
|
t.Run("reshim_command", func(t *testing.T) {
|
|
|
|
runBatsFile(t, dir, "reshim_command.bats")
|
|
|
|
})
|
2024-02-10 13:02:36 -07:00
|
|
|
|
2024-02-10 17:58:13 -07:00
|
|
|
//t.Run("shim_env_command", func(t *testing.T) {
|
2024-03-01 06:43:52 -07:00
|
|
|
// runBatsFile(t, dir, "shim_env_command.bats")
|
2024-02-10 17:58:13 -07:00
|
|
|
//})
|
2024-02-10 13:02:36 -07:00
|
|
|
|
2024-02-10 17:58:13 -07:00
|
|
|
//t.Run("shim_exec", func(t *testing.T) {
|
2024-03-01 06:43:52 -07:00
|
|
|
// runBatsFile(t, dir, "shim_exec.bats")
|
2024-02-10 17:58:13 -07:00
|
|
|
//})
|
2024-02-10 13:02:36 -07:00
|
|
|
|
2024-02-10 17:58:13 -07:00
|
|
|
//t.Run("shim_versions_command", func(t *testing.T) {
|
2024-03-01 06:43:52 -07:00
|
|
|
// runBatsFile(t, dir, "shim_versions_command.bats")
|
2024-02-10 17:58:13 -07:00
|
|
|
//})
|
2024-02-10 13:02:36 -07:00
|
|
|
|
2024-02-10 17:58:13 -07:00
|
|
|
//t.Run("uninstall_command", func(t *testing.T) {
|
2024-03-01 06:43:52 -07:00
|
|
|
// runBatsFile(t, dir, "uninstall_command.bats")
|
2024-02-10 17:58:13 -07:00
|
|
|
//})
|
2024-02-10 13:02:36 -07:00
|
|
|
|
2024-02-10 17:58:13 -07:00
|
|
|
//t.Run("version_commands", func(t *testing.T) {
|
2024-03-01 06:43:52 -07:00
|
|
|
// runBatsFile(t, dir, "version_commands.bats")
|
2024-02-10 17:58:13 -07:00
|
|
|
//})
|
|
|
|
|
|
|
|
//t.Run("where_command", func(t *testing.T) {
|
2024-03-01 06:43:52 -07:00
|
|
|
// runBatsFile(t, dir, "where_command.bats")
|
2024-02-10 17:58:13 -07:00
|
|
|
//})
|
|
|
|
|
|
|
|
//t.Run("which_command", func(t *testing.T) {
|
2024-03-01 06:43:52 -07:00
|
|
|
// runBatsFile(t, dir, "which_command.bats")
|
2024-02-10 17:58:13 -07:00
|
|
|
//})
|
2024-02-10 13:02:36 -07:00
|
|
|
}
|
|
|
|
|
2024-07-03 17:27:13 -07:00
|
|
|
func runBatsFile(t *testing.T, dir, filename string) {
|
|
|
|
t.Helper()
|
2024-02-10 13:02:36 -07:00
|
|
|
|
2024-07-03 17:27:13 -07:00
|
|
|
cmd := exec.Command("bats", "--verbose-run", fmt.Sprintf("test/%s", filename))
|
2024-02-10 13:02:36 -07:00
|
|
|
|
2024-07-03 17:27:13 -07:00
|
|
|
// Capture stdout and stderr
|
|
|
|
var stdout strings.Builder
|
|
|
|
var stderr strings.Builder
|
|
|
|
cmd.Stdout = &stdout
|
|
|
|
cmd.Stderr = &stderr
|
2024-02-10 13:02:36 -07:00
|
|
|
|
2024-07-03 17:27:13 -07:00
|
|
|
// Add dir to asdf test variables
|
|
|
|
asdfTestHome := fmt.Sprintf("BASE_DIR=%s", dir)
|
|
|
|
asdfBinPath := fmt.Sprintf("ASDF_BIN=%s", dir)
|
|
|
|
cmd.Env = []string{asdfBinPath, asdfTestHome}
|
2024-02-10 14:05:48 -07:00
|
|
|
|
2024-07-03 17:27:13 -07:00
|
|
|
err := cmd.Run()
|
|
|
|
if err != nil {
|
|
|
|
// If command fails print both stderr and stdout
|
|
|
|
fmt.Println("stdout:", stdout.String())
|
|
|
|
fmt.Println("stderr:", stderr.String())
|
|
|
|
t.Fatal("bats command failed to run test file successfully")
|
2024-02-10 13:02:36 -07:00
|
|
|
|
2024-07-03 17:27:13 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2024-02-10 14:05:48 -07:00
|
|
|
|
2024-03-01 06:43:52 -07:00
|
|
|
func buildAsdf(t *testing.T, dir string) {
|
2024-02-10 14:05:48 -07:00
|
|
|
cmd := exec.Command("go", "build", "-o", dir)
|
|
|
|
|
|
|
|
err := cmd.Run()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Failed to build asdf")
|
|
|
|
}
|
|
|
|
}
|