From 1b696ce9c6cf44f5b832cb3d2b3eee94d48137ba Mon Sep 17 00:00:00 2001 From: "Arik Jones (aider)" Date: Thu, 19 Sep 2024 16:25:02 -0500 Subject: [PATCH] refactor: use wrapper functions for easier testing --- cmd/web.go | 7 +++++-- cmd/web_test.go | 19 +++++++++---------- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/cmd/web.go b/cmd/web.go index 6a92289..b4c1d26 100644 --- a/cmd/web.go +++ b/cmd/web.go @@ -149,13 +149,13 @@ func scrapeURL(urlStr string, depth int, visited map[string]bool) (string, error visited[urlStr] = true - content, err := extractAndConvertContent(urlStr) + content, err := testExtractAndConvertContent(urlStr) if err != nil { return "", err } if depth > 0 { - links, err := scraper.ExtractLinks(urlStr) + links, err := testExtractLinks(urlStr) if err != nil { return content, fmt.Errorf("error extracting links: %v", err) } @@ -173,6 +173,9 @@ func scrapeURL(urlStr string, depth int, visited map[string]bool) (string, error return content, nil } +var testExtractAndConvertContent = extractAndConvertContent +var testExtractLinks = scraper.ExtractLinks + func extractAndConvertContent(urlStr string) (string, error) { content, err := scraper.FetchWebpageContent(urlStr) if err != nil { diff --git a/cmd/web_test.go b/cmd/web_test.go index 5d585b4..21e2403 100644 --- a/cmd/web_test.go +++ b/cmd/web_test.go @@ -7,6 +7,9 @@ import ( "github.com/tnypxl/rollup/internal/scraper" ) +var testExtractAndConvertContent = extractAndConvertContent +var testExtractLinks = scraper.ExtractLinks + func TestConvertPathOverrides(t *testing.T) { configOverrides := []config.PathOverride{ { @@ -97,25 +100,21 @@ func mockExtractLinks(urlStr string) ([]string, error) { func TestScrapeURL(t *testing.T) { // Store the original functions - originalExtractAndConvertContent := extractAndConvertContent - originalExtractLinks := scraper.ExtractLinks + originalExtractAndConvertContent := testExtractAndConvertContent + originalExtractLinks := testExtractLinks // Define mock functions - mockExtractAndConvertContent := func(urlStr string) (string, error) { + testExtractAndConvertContent = func(urlStr string) (string, error) { return "Mocked content for " + urlStr, nil } - mockExtractLinks := func(urlStr string) ([]string, error) { + testExtractLinks = func(urlStr string) ([]string, error) { return []string{"http://example.com/link1", "http://example.com/link2"}, nil } - // Replace the actual functions with mocks - extractAndConvertContent = mockExtractAndConvertContent - scraper.ExtractLinks = mockExtractLinks - // Defer the restoration of original functions defer func() { - extractAndConvertContent = originalExtractAndConvertContent - scraper.ExtractLinks = originalExtractLinks + testExtractAndConvertContent = originalExtractAndConvertContent + testExtractLinks = originalExtractLinks }() tests := []struct {