all repos — mdget @ master

Unnamed repository; edit this file 'description' to name the repository.

test/test_output.go (view raw)

  1package main
  2
  3import (
  4	"fmt"
  5	"net/http"
  6	"net/http/httptest"
  7	"os"
  8	"path/filepath"
  9)
 10
 11func main() {
 12	fmt.Println("Testing file output...")
 13
 14	allPassed := true
 15
 16	// Test 1: Write output to file
 17	fmt.Print("Test 1: Write output to file... ")
 18
 19	// Create a test server
 20	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
 21		w.Header().Set("Content-Type", "text/html")
 22		fmt.Fprint(w, "<html><body><h1>File Test</h1><p>This content should be in a file.</p></body></html>")
 23	}))
 24	defer server.Close()
 25
 26	// Create temporary directory for test files
 27	tempDir, err := createTempDir("mdget-test-output-")
 28	if err != nil {
 29		fmt.Printf("❌ Failed to create temp dir: %v\n", err)
 30		os.Exit(1)
 31	}
 32	defer cleanup(tempDir)
 33
 34	outputFile := filepath.Join(tempDir, "output.md")
 35
 36	// Run mdget with output file
 37	output, err := runMdget("--output", outputFile, server.URL)
 38	if !assertError("File output", err, false) {
 39		allPassed = false
 40	} else {
 41		// Check if file was created
 42		content, err := readFile(outputFile)
 43		if err != nil {
 44			fmt.Printf("❌ Failed to read output file: %v\n", err)
 45			allPassed = false
 46		} else if !assertContains("File content", content, "# File Test") {
 47			allPassed = false
 48		} else if !assertContains("File content paragraph", content, "This content should be in a file") {
 49			allPassed = false
 50		} else if !assertNotContains("File should not contain HTML tags", content, "<html>") {
 51			allPassed = false
 52		} else {
 53			fmt.Println("✅")
 54		}
 55	}
 56
 57	// Test 2: Output to stdout when no --output flag
 58	fmt.Print("Test 2: Output to stdout (no --output flag)... ")
 59
 60	server2 := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
 61		w.Header().Set("Content-Type", "text/html")
 62		fmt.Fprint(w, "<html><body><h1>Stdout Test</h1></body></html>")
 63	}))
 64	defer server2.Close()
 65
 66	output, err = runMdget(server2.URL)
 67	if !assertError("Stdout output", err, false) {
 68		allPassed = false
 69	} else if !assertContains("Stdout output", output, "# Stdout Test") {
 70		allPassed = false
 71	} else if !assertNotContains("Stdout should not contain HTML", output, "<html>") {
 72		allPassed = false
 73	} else {
 74		fmt.Println("✅")
 75	}
 76
 77	// Test 3: Output to non-existent directory should fail
 78	fmt.Print("Test 3: Output to non-existent directory should fail... ")
 79
 80	output, err = runMdget("--output", "/tmp/nonexistent12345/output.md", server.URL)
 81	if !assertError("Non-existent directory", err, true) {
 82		allPassed = false
 83	} else if !assertContains("Error message for non-existent dir", output, "Error") {
 84		allPassed = false
 85	} else {
 86		fmt.Println("✅")
 87	}
 88
 89	// Test 4: Empty output filename should fail
 90	fmt.Print("Test 4: Empty output filename should fail... ")
 91
 92	output, err = runMdget("--output", "", server.URL)
 93	// This is tricky because empty string might be interpreted as "no output file"
 94	// Let's check if it at least runs without crashing
 95	if err != nil {
 96		// If it fails, that's okay as long as it's a reasonable error
 97		if !assertContains("Empty filename error", output, "Error") {
 98			allPassed = false
 99		} else {
100			fmt.Println("✅ (failed with error as expected)")
101		}
102	} else {
103		// If it succeeds, that means it probably wrote to stdout
104		if assertContains("Should have some output", output, "") {
105			fmt.Println("✅ (wrote to stdout)")
106		} else {
107			allPassed = false
108		}
109	}
110
111	if allPassed {
112		fmt.Println("\n✅ All file output tests passed!")
113		os.Exit(0)
114	} else {
115		fmt.Println("\n❌ Some file output tests failed!")
116		os.Exit(1)
117	}
118}
119