package main import ( "fmt" "net/http" "os" "os/exec" "strings" ) // runMdget runs mdget with the given arguments and returns output and error func runMdget(args ...string) (string, error) { cmd := exec.Command("go", append([]string{"run", "../cmd/mdget.go"}, args...)...) output, err := cmd.CombinedOutput() return string(output), err } // runBuiltMdget runs the built mdget binary with given arguments func runBuiltMdget(binaryPath string, args ...string) (string, error) { cmd := exec.Command(binaryPath, args...) output, err := cmd.CombinedOutput() return string(output), err } // createTestServer creates an HTTP test server with the given handler func createTestServer(handler http.HandlerFunc) *http.Server { server := &http.Server{ Addr: ":0", // Let OS choose port Handler: handler, } return server } // createTempDir creates a temporary directory for testing func createTempDir(prefix string) (string, error) { tempDir, err := os.MkdirTemp("", prefix) if err != nil { return "", fmt.Errorf("failed to create temp dir: %v", err) } return tempDir, nil } // assertContains checks if a string contains a substring, prints error if not func assertContains(t string, actual, expected string) bool { if !strings.Contains(actual, expected) { fmt.Printf("❌ Test failed: %s\n", t) fmt.Printf(" Expected to contain: %q\n", expected) fmt.Printf(" Got: %q\n", actual) return false } return true } // assertNotContains checks if a string does NOT contain a substring func assertNotContains(t string, actual, expected string) bool { if strings.Contains(actual, expected) { fmt.Printf("❌ Test failed: %s\n", t) fmt.Printf(" Expected NOT to contain: %q\n", expected) fmt.Printf(" Got: %q\n", actual) return false } return true } // assertError checks if an error occurred as expected func assertError(t string, err error, expectError bool) bool { hasError := err != nil if expectError && !hasError { fmt.Printf("❌ Test failed: %s\n", t) fmt.Printf(" Expected error but got none\n") return false } if !expectError && hasError { fmt.Printf("❌ Test failed: %s\n", t) fmt.Printf(" Unexpected error: %v\n", err) return false } return true } // readFile reads the entire contents of a file func readFile(path string) (string, error) { content, err := os.ReadFile(path) if err != nil { return "", err } return string(content), nil } // writeFile writes content to a file func writeFile(path, content string) error { return os.WriteFile(path, []byte(content), 0644) } // cleanup removes a file or directory func cleanup(path string) { os.RemoveAll(path) }