test/test_build.go (view raw)
1package main
2
3import (
4 "fmt"
5 "net/http"
6 "net/http/httptest"
7 "os"
8 "os/exec"
9 "path/filepath"
10)
11
12func main() {
13 fmt.Println("Testing build and execution...")
14
15 allPassed := true
16
17 // Test 1: Build the binary
18 fmt.Print("Test 1: Build mdget binary... ")
19
20 // Create temporary directory for binary
21 tempDir, err := createTempDir("mdget-build-test-")
22 if err != nil {
23 fmt.Printf("❌ Failed to create temp dir: %v\n", err)
24 os.Exit(1)
25 }
26 defer cleanup(tempDir)
27
28 binaryPath := filepath.Join(tempDir, "mdget")
29
30 // Build the binary
31 cmd := exec.Command("go", "build", "-o", binaryPath, "../cmd/mdget.go")
32 output, err := cmd.CombinedOutput()
33 if err != nil {
34 fmt.Printf("❌ Failed to build mdget: %v\nOutput: %s\n", err, output)
35 os.Exit(1)
36 }
37
38 // Check if binary was created
39 if _, err := os.Stat(binaryPath); os.IsNotExist(err) {
40 fmt.Printf("❌ Binary not created at %s\n", binaryPath)
41 allPassed = false
42 } else {
43 fmt.Println("✅")
44 }
45
46 // Test 2: Run the built binary
47 fmt.Print("Test 2: Run built binary... ")
48
49 server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
50 w.Header().Set("Content-Type", "text/html")
51 fmt.Fprint(w, "<html><body><h1>Built Test</h1><p>Running from compiled binary.</p></body></html>")
52 }))
53 defer server.Close()
54
55 runOutput, err := runBuiltMdget(binaryPath, server.URL)
56 if !assertError("Run built binary", err, false) {
57 allPassed = false
58 } else if !assertContains("Built binary output", runOutput, "# Built Test") {
59 allPassed = false
60 } else if !assertContains("Built binary paragraph", runOutput, "Running from compiled binary") {
61 allPassed = false
62 } else {
63 fmt.Println("✅")
64 }
65
66 // Test 3: Built binary with arguments
67 fmt.Print("Test 3: Built binary with arguments (help flag)... ")
68
69 runOutput, err = runBuiltMdget(binaryPath, "--help")
70 if !assertError("Built binary help", err, false) {
71 allPassed = false
72 } else if !assertContains("Built binary help text", runOutput, "Fetch a URL and convert its HTML content to Markdown.") {
73 allPassed = false
74 } else {
75 fmt.Println("✅")
76 }
77
78 // Test 4: Built binary with version flag
79 fmt.Print("Test 4: Built binary with version flag... ")
80
81 runOutput, err = runBuiltMdget(binaryPath, "--version")
82 if !assertError("Built binary version", err, false) {
83 allPassed = false
84 } else if !assertContains("Built binary version text", runOutput, "mdget v0.1.0") {
85 allPassed = false
86 } else {
87 fmt.Println("✅")
88 }
89
90 // Test 5: Built binary with output file
91 fmt.Print("Test 5: Built binary with output file... ")
92
93 server2 := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
94 w.Header().Set("Content-Type", "text/html")
95 fmt.Fprint(w, "<html><body><h1>File Output Test</h1></body></html>")
96 }))
97 defer server2.Close()
98
99 outputFile := filepath.Join(tempDir, "test-output.md")
100 runOutput, err = runBuiltMdget(binaryPath, "--output", outputFile, server2.URL)
101 if !assertError("Built binary file output", err, false) {
102 allPassed = false
103 } else {
104 // Check if file was created
105 content, err := readFile(outputFile)
106 if err != nil {
107 fmt.Printf("❌ Failed to read output file: %v\n", err)
108 allPassed = false
109 } else if !assertContains("Output file content", content, "# File Output Test") {
110 allPassed = false
111 } else {
112 fmt.Println("✅")
113 }
114 }
115
116 // Test 6: Built binary error handling
117 fmt.Print("Test 6: Built binary error handling (invalid URL)... ")
118
119 runOutput, err = runBuiltMdget(binaryPath, "://invalid-url")
120 if !assertError("Built binary invalid URL", err, true) {
121 allPassed = false
122 } else if !assertContains("Built binary error message", runOutput, "Error fetching URL") {
123 allPassed = false
124 } else {
125 fmt.Println("✅")
126 }
127
128 // Test 7: Compare go run vs built binary output
129 fmt.Print("Test 7: Compare go run vs built binary output... ")
130
131 server3 := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
132 w.Header().Set("Content-Type", "text/html")
133 fmt.Fprint(w, "<html><body><h1>Comparison Test</h1><p>Same content.</p></body></html>")
134 }))
135 defer server3.Close()
136
137 // Get output from go run
138 goRunOutput, err := runMdget(server3.URL)
139 if err != nil {
140 fmt.Printf("❌ go run failed: %v\n", err)
141 allPassed = false
142 } else {
143 // Get output from built binary
144 builtOutput, err := runBuiltMdget(binaryPath, server3.URL)
145 if err != nil {
146 fmt.Printf("❌ built binary failed: %v\n", err)
147 allPassed = false
148 } else if goRunOutput != builtOutput {
149 // They might differ slightly due to timing or other factors
150 // Check if both contain the essential content
151 if !assertContains("go run output check", goRunOutput, "# Comparison Test") ||
152 !assertContains("built output check", builtOutput, "# Comparison Test") {
153 allPassed = false
154 } else {
155 fmt.Println("✅ (outputs functionally equivalent)")
156 }
157 } else {
158 fmt.Println("✅ (outputs identical)")
159 }
160 }
161
162 if allPassed {
163 fmt.Println("\n✅ All build and execution tests passed!")
164 os.Exit(0)
165 } else {
166 fmt.Println("\n❌ Some build and execution tests failed!")
167 os.Exit(1)
168 }
169}