Skip to content

Commit

Permalink
test: add env var to test offline only
Browse files Browse the repository at this point in the history
  • Loading branch information
TypicalAM committed Apr 15, 2024
1 parent 4014a33 commit be62b6d
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
27 changes: 27 additions & 0 deletions internal/backend/backend_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,32 @@
package backend

import (
"os"
"strings"
"testing"

"github.com/TypicalAM/goread/internal/backend/rss"
)

const TEST_OFFLINE_ENV = "TEST_OFFLINE_ONLY"

// testOffline checks if the tests should be in offline mode
func testOffline() bool {
offline, ok := os.LookupEnv(TEST_OFFLINE_ENV)
if !ok {
return false
}

truthy := []string{"1", "YES", "Y", "TRUE", "ON"}
for _, val := range truthy {
if strings.ToUpper(offline) == val {
return true
}
}

return false
}

// getBackend creates a fake backend
func getBackend() (*Backend, error) {
b, err := New("../test/data/urls.yml", "", false)
Expand Down Expand Up @@ -90,6 +111,12 @@ func TestBackendGetFeeds(t *testing.T) {

// TestBackendGetArticles if we get an error getting items from a feed doesn't work
func TestBackendGetArticles(t *testing.T) {
// This test should only run online
if testOffline() {
t.Skip()
return
}

// Create a backend with a valid file
b, err := getBackend()
if err != nil {
Expand Down
33 changes: 33 additions & 0 deletions internal/backend/cache/cache_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,31 @@
package cache

import (
"os"
"strings"
"testing"
"time"
)

const TEST_OFFLINE_ENV = "TEST_OFFLINE_ONLY"

// testOffline checks if the tests should be in offline mode
func testOffline() bool {
offline, ok := os.LookupEnv(TEST_OFFLINE_ENV)
if !ok {
return false
}

truthy := []string{"1", "YES", "Y", "TRUE", "ON"}
for _, val := range truthy {
if strings.ToUpper(offline) == val {
return true
}
}

return false
}

// getCache returns a new cache with the fake data
func getCache() (*Cache, error) {
cache, err := New("../../test/data")
Expand Down Expand Up @@ -53,6 +74,12 @@ func TestCacheLoadCorrectly(t *testing.T) {

// TestCacheGetArticles if we get an error when there's a cache miss but the cache doesn't change
func TestCacheGetArticles(t *testing.T) {
// This test should only run online
if testOffline() {
t.Skip()
return
}

// Create the cache object with a valid file
cache, err := getCache()
if err != nil {
Expand Down Expand Up @@ -86,6 +113,12 @@ func TestCacheGetArticles(t *testing.T) {

// TestCacheGetArticleExpired if we get an error then the store doesn't delete expired cache when getting data
func TestCacheGetArticleExpired(t *testing.T) {
// This test should only run online
if testOffline() {
t.Skip()
return
}

// Create the cache object with a valid file
cache, err := getCache()
if err != nil {
Expand Down

0 comments on commit be62b6d

Please sign in to comment.