diff options
author | Evgeny Kuznetsov <evgeny@kuznetsov.md> | 2020-01-28 21:14:27 +0300 |
---|---|---|
committer | Evgeny Kuznetsov <evgeny@kuznetsov.md> | 2020-01-28 21:14:27 +0300 |
commit | f3a4c3e464285abd5db211c047f16dbc5ce82c32 (patch) | |
tree | 5afe8f674bb0b8e4f718b4102c33f51feaa83799 | |
parent | 60d88d0bc841b1b48fd2a5262dc6723d63e4d641 (diff) | |
download | radiorus-rss-f3a4c3e464285abd5db211c047f16dbc5ce82c32.tar.gz radiorus-rss-f3a4c3e464285abd5db211c047f16dbc5ce82c32.zip |
improve test coverage
-rw-r--r-- | main.go | 19 | ||||
-rw-r--r-- | main_test.go | 24 |
2 files changed, 33 insertions, 10 deletions
@@ -172,15 +172,22 @@ func populateFeed(feed *feeds.Feed, page []byte) (err error) { func describeFeed(feed *feeds.Feed, wg *sync.WaitGroup) { defer wg.Done() - programAboutUrl := strings.TrimSuffix(feed.Link.Href, "episodes") + "about" - page := getPage(programAboutUrl) - feed.Description = processFeedDesc(page) + url := strings.TrimSuffix(feed.Link.Href, "episodes") + "about" + page := getPage(url) + desc, err := processFeedDesc(page) + if err != nil { + log.Printf("could not find programme description on page %v: %v", url, err) + } + feed.Description = desc } -func processFeedDesc(page []byte) string { - programAbout := programAboutRe.FindSubmatch(page)[1] +func processFeedDesc(page []byte) (string, error) { + matches := programAboutRe.FindSubmatch(page) + if len(matches) < 2 { + return "", errCantParse + } re := regexp.MustCompile(`<(.+?)?>`) - return string(re.ReplaceAll(programAbout, []byte(``))) + return string(re.ReplaceAll(matches[1], []byte(``))), nil } func describeEpisodes(feed *feeds.Feed) { diff --git a/main_test.go b/main_test.go index f24880c..d7210be 100644 --- a/main_test.go +++ b/main_test.go @@ -63,7 +63,7 @@ func TestFeed(t *testing.T) { page = helperLoadBytes(t, "about") page = cleanText(page) - feed.Description = processFeedDesc(page) + feed.Description, _ = processFeedDesc(page) actual := createFeed(feed) golden := filepath.Join("testdata", t.Name()+".golden") @@ -94,13 +94,29 @@ func TestMissingEpisode(t *testing.T) { wg.Add(1) describeEpisode(&item, &wg) - got := string(buf.Bytes()) - want := fmt.Sprintf("could not find episode description on page %v: %v", item.Link.Href, errCantParse) + assertStringContains(t, buf.String(), fmt.Sprintf("could not find episode description on page %v: %v", item.Link.Href, errCantParse)) +} + +func assertStringContains(t *testing.T, got, want string) { if !strings.Contains(got, want) { - t.Fatalf("got %v, want %v", got, want) + t.Fatalf("%v does not contain %v", got, want) } } +func TestMissingFeedDesc(t *testing.T) { + server := helperMockServer(t) + defer helperCleanupFile(t, "episodes") + helperCleanupFile(t, "about") + + var buf bytes.Buffer + log.SetOutput(&buf) + defer func() { log.SetOutput(os.Stderr) }() + + processURL(fmt.Sprintf("%s/brand/57083/episodes", server.URL)) + + assertStringContains(t, buf.String(), fmt.Sprintf("could not find programme description on page %v: %v", server.URL+"/brand/57083/about", errCantParse)) +} + func TestServedFeed(t *testing.T) { server := helperMockServer(t) defer helperCleanupServer(t) |