diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 93288a482b..909ddc9200 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -135,4 +135,5 @@ jobs: - name: Run tests env: DIFF_FILE: get_diff_files.txt + CHANGELOG_CACHE_FILE_PATH: tests/fixtures/changelog-feed.json run: npm test -- tests/${{ matrix.test-group }}/ diff --git a/lib/changelog.js b/lib/changelog.js index 34ae239800..36f7784395 100644 --- a/lib/changelog.js +++ b/lib/changelog.js @@ -4,6 +4,8 @@ import path from 'path' import Parser from 'rss-parser' +const CHANGELOG_CACHE_FILE_PATH = process.env.CHANGELOG_CACHE_FILE_PATH + async function getRssFeed(url) { const parser = new Parser({ timeout: 5000 }) const feedUrl = `${url}/feed` @@ -19,9 +21,11 @@ async function getRssFeed(url) { return feed } -export async function getChangelogItems(prefix, feedUrl) { - const fromCache = getChangelogItemsFromCache(prefix, feedUrl) - if (fromCache) return fromCache +export async function getChangelogItems(prefix, feedUrl, ignoreCache = false) { + if (!ignoreCache) { + const fromCache = getChangelogItemsFromCache(prefix, feedUrl) + if (fromCache) return fromCache + } const feed = await getRssFeed(feedUrl) @@ -62,6 +66,9 @@ function getChangelogCacheKey(prefix, feedUrl) { function getDiskCachePath(prefix, feedUrl) { // When in local development or in tests, use disk caching if (process.env.NODE_ENV === 'test' || process.env.NODE_ENV === 'development') { + if (CHANGELOG_CACHE_FILE_PATH) { + return CHANGELOG_CACHE_FILE_PATH + } const cacheKey = getChangelogCacheKey(prefix, feedUrl) const date = new Date().toISOString().split('T')[0] const fileName = `changelogcache-${cacheKey}-${date}.json` diff --git a/tests/fixtures/changelog-feed.json b/tests/fixtures/changelog-feed.json new file mode 100644 index 0000000000..6af168c61c --- /dev/null +++ b/tests/fixtures/changelog-feed.json @@ -0,0 +1 @@ +[{"title":"Authentication token format updates are generally available","date":"2021-03-31T22:22:03.000Z","href":"https://github.blog/changelog/2021-03-31-authentication-token-format-updates-are-generally-available"},{"title":"Compare REST API now supports pagination","date":"2021-03-23T02:49:54.000Z","href":"https://github.blog/changelog/2021-03-22-compare-rest-api-now-supports-pagination"},{"title":"GitHub Discussions GraphQL API public beta","date":"2021-02-23T18:21:40.000Z","href":"https://github.blog/changelog/2021-02-23-github-discussions-graphql-api-public-beta"}] \ No newline at end of file diff --git a/tests/unit/get-rss-feeds.js b/tests/unit/get-rss-feeds.js index f1b936804a..d419aab1b8 100644 --- a/tests/unit/get-rss-feeds.js +++ b/tests/unit/get-rss-feeds.js @@ -18,7 +18,16 @@ describe('getChangelogItems module', () => { changelog = await getChangelogItems( 'GitHub Actions:', - 'https://github.blog/changelog/label/packages' + 'https://github.blog/changelog/label/packages', + // This means: Don't use the cache even if it's present. + // The reason we're doing this is because all other tests, the + // cache is prepopulated from a file from the test fixtures. But + // in this particular file, we really do want to execute that code + // that executes on a cache miss. But this particular file special + // because it explicitly uses nock() to mock the HTTP socket. + // So even if we say "Don't use the cache" here, it still won't + // depend on Internet access because we're using `nock` here. + true ) })