dharma/testsite_test.go
Felipe M. 0ef15167d5
All checks were successful
ci/woodpecker/tag/release Pipeline was successful
initial release
2025-05-04 10:49:50 +02:00

115 lines
2.9 KiB
Go

package main
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"git.nakama.town/fmartingr/dharma/pkg/testutil"
)
// TestFullIntegration runs a full integration test of the tool against testsite
// This test uses the actual binary to verify everything works end-to-end
func TestFullIntegration(t *testing.T) {
// Skip this test if running in CI environment or if it's a short test run
if testing.Short() {
t.Skip("Skipping full integration test in short mode")
}
// Build the binary
buildCmd := exec.Command("go", "build", "-o", "dharma_test", "./cmd/dharma")
if err := buildCmd.Run(); err != nil {
t.Fatalf("Failed to build binary: %v", err)
}
defer os.Remove("dharma_test") // Clean up after test
// Build absolute path to dharma_test binary
wd, err := os.Getwd()
if err != nil {
t.Fatalf("Failed to get working directory: %v", err)
}
dharmaPath := filepath.Join(wd, "dharma_test")
// Start the testsite server
serverURL, cleanup, err := testutil.StartTestsiteServer()
if err != nil {
t.Fatalf("Failed to start test server: %v", err)
}
defer cleanup()
// Test cases for different formats
testCases := []struct {
name string
args []string
expectCode int
expectText string
}{
{
name: "Basic scan",
args: []string{serverURL},
expectCode: 0,
expectText: "not_found.html",
},
{
name: "JSON output",
args: []string{"--format", "json", serverURL},
expectCode: 0,
expectText: `"url"`,
},
{
name: "CSV output",
args: []string{"--format", "csv", serverURL},
expectCode: 0,
expectText: "Status,Type,URL",
},
{
name: "Internal links only",
args: []string{"--internal-only", serverURL},
expectCode: 0,
expectText: "not_found.html",
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
// Run the binary
cmd := exec.Command(dharmaPath, tc.args...)
output, err := cmd.CombinedOutput()
// Check exit code
var exitCode int
if err != nil {
if exitError, ok := err.(*exec.ExitError); ok {
exitCode = exitError.ExitCode()
} else {
t.Fatalf("Failed to run binary: %v", err)
}
}
if exitCode != tc.expectCode {
t.Errorf("Expected exit code %d, got %d", tc.expectCode, exitCode)
}
// Check output contains expected text
if tc.expectText != "" && !containsString(string(output), tc.expectText) {
t.Errorf("Expected output to contain %q but didn't.\nOutput:\n%s", tc.expectText, output)
}
// Verify we got some output
if len(output) == 0 {
t.Errorf("Expected output but got none")
}
// Print summary of the results
fmt.Printf("Test %s: Found %d bytes of output\n", tc.name, len(output))
})
}
}
// containsString checks if a string contains another string
func containsString(haystack, needle string) bool {
return strings.Contains(haystack, needle)
}