Skip to content

Commit 86a0117

Browse files
authored
Merge pull request github#16168 from github/repo-sync
repo sync
2 parents ade67ab + 09c40b1 commit 86a0117

File tree

8 files changed

+77
-60
lines changed

8 files changed

+77
-60
lines changed

.github/workflows/test.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,11 @@ jobs:
132132
- name: Run build script
133133
run: npm run build
134134

135+
- name: Warm possible disk caching
136+
env:
137+
NODE_ENV: test
138+
run: ./script/warm-before-tests.mjs
139+
135140
- name: Run tests
136141
env:
137142
DIFF_FILE: get_diff_files.txt

lib/site-data.js

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,48 @@
1+
import fs from 'fs'
2+
import os from 'os'
13
import path from 'path'
24
import flat from 'flat'
35
import { get, set } from 'lodash-es'
46
import languages from './languages.js'
57
import dataDirectory from './data-directory.js'
68
import encodeBracketedParentheses from './encode-bracketed-parentheses.js'
79

8-
const loadSiteDataFromDir = (dir) => ({
9-
site: {
10-
data: dataDirectory(path.join(dir, 'data'), {
11-
preprocess: (dataString) => encodeBracketedParentheses(dataString.trimEnd()),
12-
ignorePatterns: [/README\.md$/],
13-
}),
14-
},
10+
const TEMP_DIRECTORY = process.env.RUNNER_TEMP || os.tmpdir()
11+
12+
function diskMemoize(prefix, fn) {
13+
const useCache = process.env.NODE_ENV !== 'development'
14+
return (dir) => {
15+
const cacheFileName = `${prefix}.${dir.replace(/[^\w]+/g, '-').toLowerCase() || 'en'}.json`
16+
if (useCache) {
17+
try {
18+
return JSON.parse(fs.readFileSync(cacheFileName, 'utf-8'))
19+
} catch (err) {
20+
if (!(err.code === 'ENOENT' || err instanceof SyntaxError)) throw err
21+
}
22+
}
23+
24+
const result = fn(dir)
25+
if (useCache) {
26+
fs.writeFileSync(cacheFileName, JSON.stringify(result), 'utf-8')
27+
console.log(`Disk-cache miss on ${cacheFileName}`, new Date())
28+
}
29+
return result
30+
}
31+
}
32+
33+
const loadSiteDataFromDir = diskMemoize(path.join(TEMP_DIRECTORY, 'docs-site-data'), (dir) => {
34+
return {
35+
site: {
36+
data: dataDirectory(path.join(dir, 'data'), {
37+
preprocess: (dataString) => encodeBracketedParentheses(dataString.trimEnd()),
38+
ignorePatterns: [/README\.md$/],
39+
}),
40+
},
41+
}
1542
})
1643

1744
export default function loadSiteData() {
18-
// load english site data
45+
// load English site data
1946
const siteData = {
2047
en: loadSiteDataFromDir(languages.en.dir),
2148
}

script/warm-before-tests.mjs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/usr/bin/env node
2+
3+
// [start-readme]
4+
//
5+
// It runs the warmServer() function because that function can do things
6+
// like writing to disk as a caching mechanism.
7+
// When jest runs tests, it starts multiple concurrent processes,
8+
// even if it runs it serially (`--runInBand`) so it's highly likely
9+
// that two concurrent processes both attempt to writing to
10+
// the same exact file. By running this script before anything
11+
// begins, we can be certain that files that should have been created
12+
// are created.
13+
//
14+
// [end-readme]
15+
16+
import warmServer from '../lib/warm-server.js'
17+
18+
main()
19+
20+
async function main() {
21+
await warmServer()
22+
}

tests/content/category-pages.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,7 @@ const slugger = new GithubSlugger()
1717
const contentDir = path.join(__dirname, '../../content')
1818

1919
describe('category pages', () => {
20-
let siteData
21-
22-
beforeAll(async () => {
23-
// Load the English site data
24-
const allSiteData = await loadSiteData()
25-
siteData = allSiteData.en.site
26-
})
20+
const siteData = loadSiteData().en.site
2721

2822
const walkOptions = {
2923
globs: ['*/index.md', 'enterprise/*/index.md'],

tests/content/glossary.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
import loadSiteData from '../../lib/site-data.js'
22

33
describe('glossaries', () => {
4-
let glossaries
5-
beforeAll(async () => {
6-
glossaries = (await loadSiteData()).en.site.data.glossaries
7-
})
4+
const glossaries = loadSiteData().en.site.data.glossaries
85

96
test('are broken into external, internal, and candidates', async () => {
107
const keys = Object.keys(glossaries)
@@ -41,7 +38,7 @@ describe('glossaries', () => {
4138
})
4239

4340
test('non-English external glossary is in correct order', async () => {
44-
const vals = (await loadSiteData()).ja.site.data.glossaries.external
41+
const vals = loadSiteData().ja.site.data.glossaries.external
4542
vals.forEach((val, i) => {
4643
expect(val.term.localeCompare(vals[i + 1], 'ja')).toBeGreaterThan(0)
4744
})

tests/content/site-data-references.js

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,12 @@ import { jest } from '@jest/globals'
1111

1212
const __dirname = path.dirname(fileURLToPath(import.meta.url))
1313

14+
const siteData = loadSiteData()
15+
const pages = (await loadPages()).filter((page) => page.languageCode === 'en')
16+
1417
describe('data references', () => {
1518
jest.setTimeout(60 * 1000)
1619

17-
let data, pages
18-
19-
beforeAll(async () => {
20-
data = await loadSiteData()
21-
pages = await loadPages()
22-
pages = pages.filter((page) => page.languageCode === 'en')
23-
})
24-
2520
test('every data reference found in English content files is defined and has a value', () => {
2621
let errors = []
2722
expect(pages.length).toBeGreaterThan(0)
@@ -30,7 +25,7 @@ describe('data references', () => {
3025
const file = path.join('content', page.relativePath)
3126
const pageRefs = getDataReferences(page.markdown)
3227
pageRefs.forEach((key) => {
33-
const value = get(data.en, key)
28+
const value = get(siteData.en, key)
3429
if (typeof value !== 'string') errors.push({ key, value, file })
3530
})
3631
})
@@ -50,7 +45,7 @@ describe('data references', () => {
5045
const { data: metadata } = frontmatter(fileContents, { filepath: page.fullPath })
5146
const metadataRefs = getDataReferences(JSON.stringify(metadata))
5247
metadataRefs.forEach((key) => {
53-
const value = get(data.en, key)
48+
const value = get(siteData.en, key)
5449
if (typeof value !== 'string') errors.push({ key, value, metadataFile })
5550
})
5651
})
@@ -62,7 +57,7 @@ describe('data references', () => {
6257

6358
test('every data reference found in English reusable files is defined and has a value', async () => {
6459
let errors = []
65-
const allReusables = data.en.site.data.reusables
60+
const allReusables = siteData.en.site.data.reusables
6661
const reusables = Object.values(allReusables)
6762
expect(reusables.length).toBeGreaterThan(0)
6863

@@ -78,7 +73,7 @@ describe('data references', () => {
7873
const reusableRefs = getDataReferences(JSON.stringify(reusablesPerFile))
7974

8075
reusableRefs.forEach((key) => {
81-
const value = get(data.en, key)
76+
const value = get(siteData.en, key)
8277
if (typeof value !== 'string') errors.push({ key, value, reusableFile })
8378
})
8479
})
@@ -90,7 +85,7 @@ describe('data references', () => {
9085

9186
test('every data reference found in English variable files is defined and has a value', async () => {
9287
let errors = []
93-
const allVariables = data.en.site.data.variables
88+
const allVariables = siteData.en.site.data.variables
9489
const variables = Object.values(allVariables)
9590
expect(variables.length).toBeGreaterThan(0)
9691

@@ -106,7 +101,7 @@ describe('data references', () => {
106101
const variableRefs = getDataReferences(JSON.stringify(variablesPerFile))
107102

108103
variableRefs.forEach((key) => {
109-
const value = get(data.en, key)
104+
const value = get(siteData.en, key)
110105
if (typeof value !== 'string') errors.push({ key, value, variableFile })
111106
})
112107
})

tests/content/site-data.js

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,17 @@
11
import { fileURLToPath } from 'url'
22
import path from 'path'
3-
import fs from 'fs'
43
import { get, isPlainObject, has } from 'lodash-es'
54
import flat from 'flat'
5+
import walkSync from 'walk-sync'
66
import { ParseError } from 'liquidjs'
77
import loadSiteData from '../../lib/site-data.js'
88
import patterns from '../../lib/patterns.js'
99
import { liquid } from '../../lib/render-content/index.js'
10-
import walkSync from 'walk-sync'
1110

1211
const __dirname = path.dirname(fileURLToPath(import.meta.url))
1312

1413
describe('siteData module (English)', () => {
15-
let data
16-
beforeAll(async () => {
17-
data = await loadSiteData()
18-
})
14+
const data = loadSiteData()
1915

2016
test('makes an object', async () => {
2117
expect(isPlainObject(data)).toBe(true)
@@ -49,21 +45,6 @@ describe('siteData module (English)', () => {
4945
expect(reusable.includes('任意のページの左上で')).toBe(true)
5046
})
5147

52-
test('backfills missing translated site data with English values', async () => {
53-
const newFile = path.join(__dirname, '../../data/newfile.yml')
54-
fs.writeFileSync(newFile, 'newvalue: bar')
55-
try {
56-
const data = loadSiteData()
57-
expect(get(data, 'en.site.data.newfile.newvalue')).toEqual('bar')
58-
expect(get(data, 'ja.site.data.newfile.newvalue')).toEqual('bar')
59-
} finally {
60-
// If an error is thrown above, it will still "bubble up"
61-
// to the jest reporter, but we still always need to clean up
62-
// the temporary file.
63-
fs.unlinkSync(newFile)
64-
}
65-
})
66-
6748
test('all Liquid tags are valid', async () => {
6849
const dataMap = flat(data)
6950
for (const key in dataMap) {

tests/unit/liquid.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -191,11 +191,7 @@ describe('liquid template parser', () => {
191191
// Create a fake req so we can test the feature versions middleware
192192
const req = { language: 'en', query: {} }
193193

194-
let siteData
195-
beforeAll(async () => {
196-
const allSiteData = await loadSiteData()
197-
siteData = allSiteData.en.site
198-
})
194+
const siteData = loadSiteData().en.site
199195

200196
test('does not render in FPT because feature is not available in FPT', async () => {
201197
req.context = {

0 commit comments

Comments
 (0)