refactor: use wrapper functions for easier testing

This commit is contained in:
Arik Jones (aider)
2024-09-19 16:25:02 -05:00
parent df1178cb03
commit 1b696ce9c6
2 changed files with 14 additions and 12 deletions

View File

@@ -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 {

View File

@@ -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 {