all repos — mdget @ master

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

test/test_helper.go (view raw)

 1package main
 2
 3import (
 4	"fmt"
 5	"net/http"
 6	"os"
 7	"os/exec"
 8	"strings"
 9)
10
11// runMdget runs mdget with the given arguments and returns output and error
12func runMdget(args ...string) (string, error) {
13	cmd := exec.Command("go", append([]string{"run", "../cmd/mdget.go"}, args...)...)
14	output, err := cmd.CombinedOutput()
15	return string(output), err
16}
17
18// runBuiltMdget runs the built mdget binary with given arguments
19func runBuiltMdget(binaryPath string, args ...string) (string, error) {
20	cmd := exec.Command(binaryPath, args...)
21	output, err := cmd.CombinedOutput()
22	return string(output), err
23}
24
25// createTestServer creates an HTTP test server with the given handler
26func createTestServer(handler http.HandlerFunc) *http.Server {
27	server := &http.Server{
28		Addr:    ":0", // Let OS choose port
29		Handler: handler,
30	}
31	return server
32}
33
34// createTempDir creates a temporary directory for testing
35func createTempDir(prefix string) (string, error) {
36	tempDir, err := os.MkdirTemp("", prefix)
37	if err != nil {
38		return "", fmt.Errorf("failed to create temp dir: %v", err)
39	}
40	return tempDir, nil
41}
42
43// assertContains checks if a string contains a substring, prints error if not
44func assertContains(t string, actual, expected string) bool {
45	if !strings.Contains(actual, expected) {
46		fmt.Printf("❌ Test failed: %s\n", t)
47		fmt.Printf("   Expected to contain: %q\n", expected)
48		fmt.Printf("   Got: %q\n", actual)
49		return false
50	}
51	return true
52}
53
54// assertNotContains checks if a string does NOT contain a substring
55func assertNotContains(t string, actual, expected string) bool {
56	if strings.Contains(actual, expected) {
57		fmt.Printf("❌ Test failed: %s\n", t)
58		fmt.Printf("   Expected NOT to contain: %q\n", expected)
59		fmt.Printf("   Got: %q\n", actual)
60		return false
61	}
62	return true
63}
64
65// assertError checks if an error occurred as expected
66func assertError(t string, err error, expectError bool) bool {
67	hasError := err != nil
68	if expectError && !hasError {
69		fmt.Printf("❌ Test failed: %s\n", t)
70		fmt.Printf("   Expected error but got none\n")
71		return false
72	}
73	if !expectError && hasError {
74		fmt.Printf("❌ Test failed: %s\n", t)
75		fmt.Printf("   Unexpected error: %v\n", err)
76		return false
77	}
78	return true
79}
80
81// readFile reads the entire contents of a file
82func readFile(path string) (string, error) {
83	content, err := os.ReadFile(path)
84	if err != nil {
85		return "", err
86	}
87	return string(content), nil
88}
89
90// writeFile writes content to a file
91func writeFile(path, content string) error {
92	return os.WriteFile(path, []byte(content), 0644)
93}
94
95// cleanup removes a file or directory
96func cleanup(path string) {
97	os.RemoveAll(path)
98}