mirror of
https://github.com/tnypxl/rollup.git
synced 2025-12-15 15:03:17 +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
|
visited[urlStr] = true
|
||||||
|
|
||||||
content, err := extractAndConvertContent(urlStr)
|
content, err := testExtractAndConvertContent(urlStr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
if depth > 0 {
|
if depth > 0 {
|
||||||
links, err := scraper.ExtractLinks(urlStr)
|
links, err := testExtractLinks(urlStr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return content, fmt.Errorf("error extracting links: %v", err)
|
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
|
return content, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var testExtractAndConvertContent = extractAndConvertContent
|
||||||
|
var testExtractLinks = scraper.ExtractLinks
|
||||||
|
|
||||||
func extractAndConvertContent(urlStr string) (string, error) {
|
func extractAndConvertContent(urlStr string) (string, error) {
|
||||||
content, err := scraper.FetchWebpageContent(urlStr)
|
content, err := scraper.FetchWebpageContent(urlStr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -7,6 +7,9 @@ import (
|
|||||||
"github.com/tnypxl/rollup/internal/scraper"
|
"github.com/tnypxl/rollup/internal/scraper"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var testExtractAndConvertContent = extractAndConvertContent
|
||||||
|
var testExtractLinks = scraper.ExtractLinks
|
||||||
|
|
||||||
func TestConvertPathOverrides(t *testing.T) {
|
func TestConvertPathOverrides(t *testing.T) {
|
||||||
configOverrides := []config.PathOverride{
|
configOverrides := []config.PathOverride{
|
||||||
{
|
{
|
||||||
@@ -97,25 +100,21 @@ func mockExtractLinks(urlStr string) ([]string, error) {
|
|||||||
|
|
||||||
func TestScrapeURL(t *testing.T) {
|
func TestScrapeURL(t *testing.T) {
|
||||||
// Store the original functions
|
// Store the original functions
|
||||||
originalExtractAndConvertContent := extractAndConvertContent
|
originalExtractAndConvertContent := testExtractAndConvertContent
|
||||||
originalExtractLinks := scraper.ExtractLinks
|
originalExtractLinks := testExtractLinks
|
||||||
|
|
||||||
// Define mock functions
|
// Define mock functions
|
||||||
mockExtractAndConvertContent := func(urlStr string) (string, error) {
|
testExtractAndConvertContent = func(urlStr string) (string, error) {
|
||||||
return "Mocked content for " + urlStr, nil
|
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
|
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 the restoration of original functions
|
||||||
defer func() {
|
defer func() {
|
||||||
extractAndConvertContent = originalExtractAndConvertContent
|
testExtractAndConvertContent = originalExtractAndConvertContent
|
||||||
scraper.ExtractLinks = originalExtractLinks
|
testExtractLinks = originalExtractLinks
|
||||||
}()
|
}()
|
||||||
|
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
|
|||||||
Reference in New Issue
Block a user