From 465e7d6689bf434d5b0774f45f75420cb6363a33 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Thu, 24 Feb 2022 08:38:08 -0500 Subject: [PATCH 1/2] Look up on productMap keys as a set (#25590) --- lib/page.js | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/lib/page.js b/lib/page.js index 75312ca33d52..0041c699abbc 100644 --- a/lib/page.js +++ b/lib/page.js @@ -18,6 +18,12 @@ import getLinkData from './get-link-data.js' import getDocumentType from './get-document-type.js' import { union } from 'lodash-es' +// We're going to check a lot of pages' "ID" (the first part of +// the relativePath) against `productMap` to make sure it's valid. +// To avoid having to do `Object.keys(productMap).includes(id)` +// every single time, we turn it into a Set once. +const productMapKeysAsSet = new Set(Object.keys(productMap)) + // Wrapper on renderContent() that caches the output depending on the // `context` by extracting information about the page's current permalink const _renderContentCache = new Map() @@ -162,10 +168,7 @@ class Page { // make sure the ID is valid if (process.env.NODE_ENV !== 'test') { - assert( - Object.keys(productMap).includes(id), - `page ${this.fullPath} has an invalid product ID: ${id}` - ) + assert(productMapKeysAsSet.has(id), `page ${this.fullPath} has an invalid product ID: ${id}`) } return id From 6bc2600b3b2cea87e2e9b7a79d97a4778bfc87ef Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Thu, 24 Feb 2022 08:42:49 -0500 Subject: [PATCH 2/2] renable backfills missing translation site data test (#25303) * renable backfills missing translation site data test * adding a code comment to explain how finally relates to jest --- tests/content/site-data.js | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/tests/content/site-data.js b/tests/content/site-data.js index 40398c7dd4aa..469885e456ca 100644 --- a/tests/content/site-data.js +++ b/tests/content/site-data.js @@ -48,15 +48,19 @@ describe('siteData module (English)', () => { expect(reusable.includes('任意のページの左上で')).toBe(true) }) - // TODO: re-enable once Janky flakyness is resolved - // Docs Engineering issue: 964 - test.skip('backfills missing translated site data with English values', async () => { + test('backfills missing translated site data with English values', async () => { const newFile = path.join(__dirname, '../../data/newfile.yml') - await fs.writeFile(newFile, 'newvalue: bar') - const data = await loadSiteData() - expect(get(data, 'en.site.data.newfile.newvalue')).toEqual('bar') - expect(get(data, 'ja.site.data.newfile.newvalue')).toEqual('bar') - await fs.unlink(newFile) + fs.writeFileSync(newFile, 'newvalue: bar') + try { + const data = loadSiteData() + expect(get(data, 'en.site.data.newfile.newvalue')).toEqual('bar') + expect(get(data, 'ja.site.data.newfile.newvalue')).toEqual('bar') + } finally { + // If an error is thrown above, it will still "bubble up" + // to the jest reporter, but we still always need to clean up + // the temporary file. + fs.unlinkSync(newFile) + } }) test('all Liquid templating is valid', async () => {