package main import ( "fmt" "net/http" "net/http/httptest" "os" "os/exec" "path/filepath" ) func main() { fmt.Println("Testing build and execution...") allPassed := true // Test 1: Build the binary fmt.Print("Test 1: Build mdget binary... ") // Create temporary directory for binary tempDir, err := createTempDir("mdget-build-test-") if err != nil { fmt.Printf("❌ Failed to create temp dir: %v\n", err) os.Exit(1) } defer cleanup(tempDir) binaryPath := filepath.Join(tempDir, "mdget") // Build the binary cmd := exec.Command("go", "build", "-o", binaryPath, "../cmd/mdget.go") output, err := cmd.CombinedOutput() if err != nil { fmt.Printf("❌ Failed to build mdget: %v\nOutput: %s\n", err, output) os.Exit(1) } // Check if binary was created if _, err := os.Stat(binaryPath); os.IsNotExist(err) { fmt.Printf("❌ Binary not created at %s\n", binaryPath) allPassed = false } else { fmt.Println("✅") } // Test 2: Run the built binary fmt.Print("Test 2: Run built binary... ") server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "text/html") fmt.Fprint(w, "

Built Test

Running from compiled binary.

") })) defer server.Close() runOutput, err := runBuiltMdget(binaryPath, server.URL) if !assertError("Run built binary", err, false) { allPassed = false } else if !assertContains("Built binary output", runOutput, "# Built Test") { allPassed = false } else if !assertContains("Built binary paragraph", runOutput, "Running from compiled binary") { allPassed = false } else { fmt.Println("✅") } // Test 3: Built binary with arguments fmt.Print("Test 3: Built binary with arguments (help flag)... ") runOutput, err = runBuiltMdget(binaryPath, "--help") if !assertError("Built binary help", err, false) { allPassed = false } else if !assertContains("Built binary help text", runOutput, "Fetch a URL and convert its HTML content to Markdown.") { allPassed = false } else { fmt.Println("✅") } // Test 4: Built binary with version flag fmt.Print("Test 4: Built binary with version flag... ") runOutput, err = runBuiltMdget(binaryPath, "--version") if !assertError("Built binary version", err, false) { allPassed = false } else if !assertContains("Built binary version text", runOutput, "mdget v0.1.0") { allPassed = false } else { fmt.Println("✅") } // Test 5: Built binary with output file fmt.Print("Test 5: Built binary with output file... ") server2 := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "text/html") fmt.Fprint(w, "

File Output Test

") })) defer server2.Close() outputFile := filepath.Join(tempDir, "test-output.md") runOutput, err = runBuiltMdget(binaryPath, "--output", outputFile, server2.URL) if !assertError("Built binary file output", err, false) { allPassed = false } else { // Check if file was created content, err := readFile(outputFile) if err != nil { fmt.Printf("❌ Failed to read output file: %v\n", err) allPassed = false } else if !assertContains("Output file content", content, "# File Output Test") { allPassed = false } else { fmt.Println("✅") } } // Test 6: Built binary error handling fmt.Print("Test 6: Built binary error handling (invalid URL)... ") runOutput, err = runBuiltMdget(binaryPath, "://invalid-url") if !assertError("Built binary invalid URL", err, true) { allPassed = false } else if !assertContains("Built binary error message", runOutput, "Error fetching URL") { allPassed = false } else { fmt.Println("✅") } // Test 7: Compare go run vs built binary output fmt.Print("Test 7: Compare go run vs built binary output... ") server3 := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "text/html") fmt.Fprint(w, "

Comparison Test

Same content.

") })) defer server3.Close() // Get output from go run goRunOutput, err := runMdget(server3.URL) if err != nil { fmt.Printf("❌ go run failed: %v\n", err) allPassed = false } else { // Get output from built binary builtOutput, err := runBuiltMdget(binaryPath, server3.URL) if err != nil { fmt.Printf("❌ built binary failed: %v\n", err) allPassed = false } else if goRunOutput != builtOutput { // They might differ slightly due to timing or other factors // Check if both contain the essential content if !assertContains("go run output check", goRunOutput, "# Comparison Test") || !assertContains("built output check", builtOutput, "# Comparison Test") { allPassed = false } else { fmt.Println("✅ (outputs functionally equivalent)") } } else { fmt.Println("✅ (outputs identical)") } } if allPassed { fmt.Println("\n✅ All build and execution tests passed!") os.Exit(0) } else { fmt.Println("\n❌ Some build and execution tests failed!") os.Exit(1) } }