Skip to content

feat(tests): add cross-platform test utilities #24

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions tests/common_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package tests

import (
"os"
"runtime"
"testing"
)

func testBinaryName(name string) string {
if runtime.GOOS == "windows" {
name += ".exe"
}
return name
}

func cleanupTestPath(t *testing.T, path string) {
t.Helper()
if err := os.RemoveAll(path); err != nil && !os.IsNotExist(err) {
t.Fatalf("Failed to remove %s: %v", path, err)
}
}
10 changes: 6 additions & 4 deletions tests/golden_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func TestGoldenFiles(t *testing.T) {
}

// Make sure the CLI binary exists
cliPath := "../rules-cli"
cliPath := testBinaryName("../rules-cli")
if _, err := os.Stat(cliPath); os.IsNotExist(err) {
// Build the CLI if it doesn't exist
// Get version from package.json
Expand All @@ -159,7 +159,8 @@ func TestGoldenFiles(t *testing.T) {
for _, goldenFile := range goldenFiles {
t.Run(goldenFile, func(t *testing.T) {
// Look up the command for this golden file
cmd, found := goldenToCommand["tests/"+goldenFile]
slashPath := filepath.ToSlash(goldenFile)
cmd, found := goldenToCommand["tests/"+slashPath]
if !found {
t.Skipf("No command configured for golden file: %s", goldenFile)
return
Expand All @@ -170,10 +171,10 @@ func TestGoldenFiles(t *testing.T) {
if err != nil {
t.Fatalf("Failed to create temporary directory: %v", err)
}
defer os.RemoveAll(tempDir)
defer cleanupTestPath(t, tempDir)

// Copy the CLI binary to the temporary directory
tempCliPath := filepath.Join(tempDir, "rules-cli")
tempCliPath := filepath.Join(tempDir, testBinaryName("rules-cli"))
if err := copyFile(cliPath, tempCliPath); err != nil {
t.Fatalf("Failed to copy CLI binary: %v", err)
}
Expand Down Expand Up @@ -243,6 +244,7 @@ func TestGoldenFiles(t *testing.T) {
// Normalize line endings for cross-platform compatibility
expected = strings.ReplaceAll(expected, "\r\n", "\n")
actual = strings.ReplaceAll(actual, "\r\n", "\n")
actual = filepath.ToSlash(actual)

// Special case for login test which has placeholders
if strings.Contains(expected, "<STATE_PLACEHOLDER>") {
Expand Down
16 changes: 10 additions & 6 deletions tests/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,18 @@ func TestCLIIntegration(t *testing.T) {
// Get the parent directory (project root)
parentDir := filepath.Join("..", ".")

binaryName := testBinaryName("rules-test")
// Build the CLI from the parent directory
buildCmd := exec.Command("go", "build", "-o", "rules-test", ".")
buildCmd := exec.Command("go", "build", "-o", binaryName, ".")
buildCmd.Dir = parentDir
if err := buildCmd.Run(); err != nil {
t.Fatalf("Failed to build CLI: %v", err)
}
defer exec.Command("rm", "-f", filepath.Join(parentDir, "rules-test")).Run()
binaryPath := filepath.Join(parentDir, binaryName)
defer cleanupTestPath(t, binaryPath)

// Run the CLI command
cmd := exec.Command(filepath.Join(parentDir, "rules-test"), tt.args...)
cmd := exec.Command(binaryPath, tt.args...)
output, err := cmd.CombinedOutput()

// For help and version commands, exit code 0 is expected
Expand All @@ -58,14 +60,16 @@ func TestCLICommandsExist(t *testing.T) {
// Test that expected commands exist
parentDir := filepath.Join("..", ".")

buildCmd := exec.Command("go", "build", "-o", "rules-test", ".")
binaryName := testBinaryName("rules-test")
buildCmd := exec.Command("go", "build", "-o", binaryName, ".")
buildCmd.Dir = parentDir
if err := buildCmd.Run(); err != nil {
t.Fatalf("Failed to build CLI: %v", err)
}
defer exec.Command("rm", "-f", filepath.Join(parentDir, "rules-test")).Run()
binaryPath := filepath.Join(parentDir, binaryName)
defer cleanupTestPath(t, binaryPath)

cmd := exec.Command(filepath.Join(parentDir, "rules-test"), "--help")
cmd := exec.Command(binaryPath, "--help")
output, _ := cmd.CombinedOutput()

// Check for expected subcommands
Expand Down