mirror of
https://github.com/tnypxl/rollup.git
synced 2025-12-13 06:23:18 +00:00
refactor: use wrapper functions for easier testing
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user