package main import ( "fmt" "net/http" "net/http/httptest" "os" "path/filepath" ) func main() { fmt.Println("Testing file output...") allPassed := true // Test 1: Write output to file fmt.Print("Test 1: Write output to file... ") // Create a test server server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "text/html") fmt.Fprint(w, "

File Test

This content should be in a file.

") })) defer server.Close() // Create temporary directory for test files tempDir, err := createTempDir("mdget-test-output-") if err != nil { fmt.Printf("❌ Failed to create temp dir: %v\n", err) os.Exit(1) } defer cleanup(tempDir) outputFile := filepath.Join(tempDir, "output.md") // Run mdget with output file output, err := runMdget("--output", outputFile, server.URL) if !assertError("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("File content", content, "# File Test") { allPassed = false } else if !assertContains("File content paragraph", content, "This content should be in a file") { allPassed = false } else if !assertNotContains("File should not contain HTML tags", content, "") { allPassed = false } else { fmt.Println("✅") } } // Test 2: Output to stdout when no --output flag fmt.Print("Test 2: Output to stdout (no --output flag)... ") server2 := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "text/html") fmt.Fprint(w, "

Stdout Test

") })) defer server2.Close() output, err = runMdget(server2.URL) if !assertError("Stdout output", err, false) { allPassed = false } else if !assertContains("Stdout output", output, "# Stdout Test") { allPassed = false } else if !assertNotContains("Stdout should not contain HTML", output, "") { allPassed = false } else { fmt.Println("✅") } // Test 3: Output to non-existent directory should fail fmt.Print("Test 3: Output to non-existent directory should fail... ") output, err = runMdget("--output", "/tmp/nonexistent12345/output.md", server.URL) if !assertError("Non-existent directory", err, true) { allPassed = false } else if !assertContains("Error message for non-existent dir", output, "Error") { allPassed = false } else { fmt.Println("✅") } // Test 4: Empty output filename should fail fmt.Print("Test 4: Empty output filename should fail... ") output, err = runMdget("--output", "", server.URL) // This is tricky because empty string might be interpreted as "no output file" // Let's check if it at least runs without crashing if err != nil { // If it fails, that's okay as long as it's a reasonable error if !assertContains("Empty filename error", output, "Error") { allPassed = false } else { fmt.Println("✅ (failed with error as expected)") } } else { // If it succeeds, that means it probably wrote to stdout if assertContains("Should have some output", output, "") { fmt.Println("✅ (wrote to stdout)") } else { allPassed = false } } if allPassed { fmt.Println("\n✅ All file output tests passed!") os.Exit(0) } else { fmt.Println("\n❌ Some file output tests failed!") os.Exit(1) } }