feat(golang-rewrite): build golang asdf for BATS integration tests

This commit is contained in:
Trevor Brown 2024-02-10 16:05:48 -05:00
parent 3cc6d8d3bb
commit ff82d9787c

View File

@ -2,6 +2,7 @@ package main
import ( import (
"fmt" "fmt"
"os"
"os/exec" "os/exec"
"strings" "strings"
"testing" "testing"
@ -11,107 +12,118 @@ import (
// new Golang implementation matches the existing Bash implementation. // new Golang implementation matches the existing Bash implementation.
func TestBatsTests(t *testing.T) { func TestBatsTests(t *testing.T) {
// Create temp directory
dir, err := os.MkdirTemp("", "golang-bats-tests")
if err != nil {
t.Fatal("Failed to create temp dir")
}
defer os.RemoveAll(dir)
// Build asdf and put in temp directory
build_asdf(t, dir)
// Run tests with the asdf binary in the temp directory
t.Run("current_command", func(t *testing.T) { t.Run("current_command", func(t *testing.T) {
run_bats_file(t, "current_command.bats") run_bats_file(t, dir, "current_command.bats")
}) })
t.Run("get_asdf_config_value", func(t *testing.T) { t.Run("get_asdf_config_value", func(t *testing.T) {
run_bats_file(t, "get_asdf_config_value.bats") run_bats_file(t, dir, "get_asdf_config_value.bats")
}) })
t.Run("help_command", func(t *testing.T) { t.Run("help_command", func(t *testing.T) {
run_bats_file(t, "help_command.bats") run_bats_file(t, dir, "help_command.bats")
}) })
t.Run("info_command", func(t *testing.T) { t.Run("info_command", func(t *testing.T) {
run_bats_file(t, "info_command.bats") run_bats_file(t, dir, "info_command.bats")
}) })
t.Run("install_command", func(t *testing.T) { t.Run("install_command", func(t *testing.T) {
run_bats_file(t, "install_command.bats") run_bats_file(t, dir, "install_command.bats")
}) })
t.Run("latest_command", func(t *testing.T) { t.Run("latest_command", func(t *testing.T) {
run_bats_file(t, "latest_command.bats") run_bats_file(t, dir, "latest_command.bats")
}) })
t.Run("list_command", func(t *testing.T) { t.Run("list_command", func(t *testing.T) {
run_bats_file(t, "list_command.bats") run_bats_file(t, dir, "list_command.bats")
}) })
t.Run("plugin_extension_command", func(t *testing.T) { t.Run("plugin_extension_command", func(t *testing.T) {
run_bats_file(t, "plugin_extension_command.bats") run_bats_file(t, dir, "plugin_extension_command.bats")
}) })
t.Run("plugin_list_all_command", func(t *testing.T) { t.Run("plugin_list_all_command", func(t *testing.T) {
run_bats_file(t, "plugin_list_all_command.bats") run_bats_file(t, dir, "plugin_list_all_command.bats")
}) })
t.Run("plugin_remove_command", func(t *testing.T) { t.Run("plugin_remove_command", func(t *testing.T) {
run_bats_file(t, "plugin_remove_command.bats") run_bats_file(t, dir, "plugin_remove_command.bats")
}) })
t.Run("plugin_test_command", func(t *testing.T) { t.Run("plugin_test_command", func(t *testing.T) {
run_bats_file(t, "plugin_test_command.bats") run_bats_file(t, dir, "plugin_test_command.bats")
}) })
t.Run("plugin_update_command", func(t *testing.T) { t.Run("plugin_update_command", func(t *testing.T) {
run_bats_file(t, "plugin_update_command.bats") run_bats_file(t, dir, "plugin_update_command.bats")
}) })
t.Run("remove_command", func(t *testing.T) { t.Run("remove_command", func(t *testing.T) {
run_bats_file(t, "remove_command.bats") run_bats_file(t, dir, "remove_command.bats")
}) })
t.Run("reshim_command", func(t *testing.T) { t.Run("reshim_command", func(t *testing.T) {
run_bats_file(t, "reshim_command.bats") run_bats_file(t, dir, "reshim_command.bats")
}) })
t.Run("reshim_command", func(t *testing.T) { t.Run("reshim_command", func(t *testing.T) {
run_bats_file(t, "reshim_command.bats") run_bats_file(t, dir, "reshim_command.bats")
}) })
t.Run("shim_env_command", func(t *testing.T) { t.Run("shim_env_command", func(t *testing.T) {
run_bats_file(t, "shim_env_command.bats") run_bats_file(t, dir, "shim_env_command.bats")
}) })
t.Run("shim_exec", func(t *testing.T) { t.Run("shim_exec", func(t *testing.T) {
run_bats_file(t, "shim_exec.bats") run_bats_file(t, dir, "shim_exec.bats")
}) })
t.Run("shim_versions_command", func(t *testing.T) { t.Run("shim_versions_command", func(t *testing.T) {
run_bats_file(t, "shim_versions_command.bats") run_bats_file(t, dir, "shim_versions_command.bats")
}) })
t.Run("shim_versions_command", func(t *testing.T) { t.Run("shim_versions_command", func(t *testing.T) {
run_bats_file(t, "shim_versions_command.bats") run_bats_file(t, dir, "shim_versions_command.bats")
}) })
t.Run("uninstall_command", func(t *testing.T) { t.Run("uninstall_command", func(t *testing.T) {
run_bats_file(t, "uninstall_command.bats") run_bats_file(t, dir, "uninstall_command.bats")
}) })
t.Run("update_command", func(t *testing.T) { t.Run("update_command", func(t *testing.T) {
run_bats_file(t, "update_command.bats") run_bats_file(t, dir, "update_command.bats")
}) })
t.Run("version_commands", func(t *testing.T) { t.Run("version_commands", func(t *testing.T) {
run_bats_file(t, "version_commands.bats") run_bats_file(t, dir, "version_commands.bats")
}) })
t.Run("where_command", func(t *testing.T) { t.Run("where_command", func(t *testing.T) {
run_bats_file(t, "where_command.bats") run_bats_file(t, dir, "where_command.bats")
}) })
t.Run("which_command", func(t *testing.T) { t.Run("which_command", func(t *testing.T) {
run_bats_file(t, "which_command.bats") run_bats_file(t, dir, "which_command.bats")
}) })
} }
func run_bats_file(t *testing.T, filename string) { func run_bats_file(t *testing.T, dir, filename string) {
t.Helper() t.Helper()
cmd := exec.Command("bats", filename) cmd := exec.Command("bats", "--verbose-run", fmt.Sprintf("test/%s", filename))
// Capture stdout and stderr // Capture stdout and stderr
var stdout strings.Builder var stdout strings.Builder
@ -119,13 +131,27 @@ func run_bats_file(t *testing.T, filename string) {
cmd.Stdout = &stdout cmd.Stdout = &stdout
cmd.Stderr = &stderr cmd.Stderr = &stderr
// Add dir to front of path
path := os.Getenv("PATH")
cmd.Env = append(cmd.Environ(), fmt.Sprintf("PATH=%s:%s", dir, path))
err := cmd.Run() err := cmd.Run()
if err != nil { if err != nil {
// If command fails print both stderr and stdout // If command fails print both stderr and stdout
fmt.Println("stderr:", stderr.String())
fmt.Println("stdout:", stdout.String()) fmt.Println("stdout:", stdout.String())
fmt.Println("stderr:", stderr.String())
t.Fatal("bats command failed to run test file successfully") t.Fatal("bats command failed to run test file successfully")
return return
} }
} }
func build_asdf(t *testing.T, dir string) {
cmd := exec.Command("go", "build", "-o", dir)
err := cmd.Run()
if err != nil {
t.Fatal("Failed to build asdf")
}
}