Skip to content

Commit 98e4b78

Browse files
authored
Update tests to use fs.promises when not top-level (#16803)
* Update tests to use fs.promises when not top-level * Move two to asyncFilter * Update site-data-references.js * Update site-data-references.js * Clear out await fs.exists * Lint * A few more fixes * Can't use async when defining tests
1 parent aae3c4e commit 98e4b78

8 files changed

Lines changed: 73 additions & 61 deletions

File tree

tests/content/category-pages.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,15 @@ describe('category pages', () => {
4242
// Get links included in product index page.
4343
// Each link corresponds to a product subdirectory (category).
4444
// Example: "getting-started-with-github"
45-
const contents = fs.readFileSync(productIndex, 'utf8')
45+
const contents = fs.readFileSync(productIndex, 'utf8') // TODO move to async
4646
const { content } = matter(contents)
4747

4848
const productDir = path.dirname(productIndex)
4949

5050
const categoryLinks = getLinks(content)
5151
// Only include category directories, not standalone category files like content/actions/quickstart.md
5252
.filter(link => fs.existsSync(getPath(productDir, link, 'index')))
53+
// TODO this should move to async, but you can't asynchronously define tests with Jest...
5354

5455
// Map those to the Markdown file paths that represent that category page index
5556
const categoryPaths = categoryLinks.map(link => getPath(productDir, link, 'index'))

tests/content/remove-liquid-statements.js

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const fs = require('fs')
1+
const fs = require('fs').promises
22
const path = require('path')
33
const cheerio = require('cheerio')
44
const matter = require('gray-matter')
@@ -35,8 +35,8 @@ function processFrontmatter (contents, file) {
3535
}
3636

3737
describe('removing liquid statements only', () => {
38-
test('removes liquid statements that specify "greater than version to deprecate"', () => {
39-
let contents = fs.readFileSync(greaterThan, 'utf8')
38+
test('removes liquid statements that specify "greater than version to deprecate"', async () => {
39+
let contents = await fs.readFile(greaterThan, 'utf8')
4040
contents = removeLiquidStatements(contents, versionToDeprecate, nextOldestVersion)
4141
const $ = cheerio.load(contents)
4242
expect($('.example1').text().trim()).toBe('Alpha')
@@ -57,8 +57,8 @@ Alpha\n\n{% else %}\n\nBravo\n\n{% if currentVersion ver_gt "enterprise-server@2
5757
expect($('.example10').text().trim()).toBe('Alpha')
5858
})
5959

60-
test('removes liquid statements that specify "and greater than version to deprecate"', () => {
61-
let contents = fs.readFileSync(andGreaterThan1, 'utf8')
60+
test('removes liquid statements that specify "and greater than version to deprecate"', async () => {
61+
let contents = await fs.readFile(andGreaterThan1, 'utf8')
6262
contents = removeLiquidStatements(contents, versionToDeprecate, nextOldestVersion)
6363
const $ = cheerio.load(contents)
6464
expect($('.example1').text().trim()).toBe('{% if currentVersion != "free-pro-team@latest" %}\n\nAlpha\n\n{% endif %}')
@@ -71,8 +71,8 @@ Alpha\n\n{% if currentVersion != "free-pro-team@latest" %}\n\nBravo\n\n{% endif
7171
Alpha\n\n{% if currentVersion ver_gt "enterprise-server@2.16" %}\n\nBravo\n\n{% endif %}\n\n{% endif %}`)
7272
})
7373

74-
test('removes liquid statements that specify "and greater than version to deprecate" (alternate format)', () => {
75-
let contents = fs.readFileSync(andGreaterThan2, 'utf8')
74+
test('removes liquid statements that specify "and greater than version to deprecate" (alternate format)', async () => {
75+
let contents = await fs.readFile(andGreaterThan2, 'utf8')
7676
contents = removeLiquidStatements(contents, versionToDeprecate, nextOldestVersion)
7777
const $ = cheerio.load(contents)
7878
expect($('.example1').text().trim()).toBe('{% if currentVersion ver_lt "enterprise-server@2.16" %}\n\nAlpha\n\n{% endif %}')
@@ -85,8 +85,8 @@ Alpha\n\n{% if currentVersion ver_lt "enterprise-server@2.16" %}\n\nBravo\n\n{%
8585
Alpha\n\n{% if currentVersion != "free-pro-team@latest" %}\n\nBravo\n\n{% endif %}\n\n{% endif %}`)
8686
})
8787

88-
test('removes liquid statements that specify "not equals version to deprecate"', () => {
89-
let contents = fs.readFileSync(notEquals, 'utf8')
88+
test('removes liquid statements that specify "not equals version to deprecate"', async () => {
89+
let contents = await fs.readFile(notEquals, 'utf8')
9090
contents = removeLiquidStatements(contents, versionToDeprecate, nextOldestVersion)
9191
const $ = cheerio.load(contents)
9292
expect($('.example1').text().trim()).toBe('Alpha')
@@ -103,8 +103,8 @@ Alpha\n\n{% endif %}`)
103103
})
104104

105105
describe('removing liquid statements and content', () => {
106-
test('removes interior content and liquid statements that specify "equals version to deprecate"', () => {
107-
let contents = fs.readFileSync(equals, 'utf8')
106+
test('removes interior content and liquid statements that specify "equals version to deprecate"', async () => {
107+
let contents = await fs.readFile(equals, 'utf8')
108108
contents = removeLiquidStatements(contents, versionToDeprecate, nextOldestVersion)
109109
const $ = cheerio.load(contents)
110110
expect($('.example1').text().trim()).toBe('')
@@ -117,8 +117,8 @@ Alpha\n\n{% else %}\n\nCharlie\n\n{% endif %}`)
117117
expect($('.example6').text().trim()).toBe('Charlie\n\nBravo')
118118
})
119119

120-
test('removes interior content and liquid statements that specify "less than next oldest than version to deprecate"', () => {
121-
let contents = fs.readFileSync(lessThanNextOldest, 'utf8')
120+
test('removes interior content and liquid statements that specify "less than next oldest than version to deprecate"', async () => {
121+
let contents = await fs.readFile(lessThanNextOldest, 'utf8')
122122
contents = removeLiquidStatements(contents, versionToDeprecate, nextOldestVersion)
123123
const $ = cheerio.load(contents)
124124
expect($('.example1').text().trim()).toBe('Alpha')
@@ -137,8 +137,8 @@ Charlie\n\n{% else %}\n\nDelta\n\n{% endif %}\n\nEcho`)
137137
})
138138

139139
describe('updating frontmatter', () => {
140-
test('updates frontmatter versions Enterprise if set to greater-than-or-equal-to version to deprecate', () => {
141-
let contents = fs.readFileSync(frontmatter1, 'utf8')
140+
test('updates frontmatter versions Enterprise if set to greater-than-or-equal-to version to deprecate', async () => {
141+
let contents = await fs.readFile(frontmatter1, 'utf8')
142142
contents = processFrontmatter(contents, frontmatter1)
143143
const $ = cheerio.load(contents)
144144
// console.log('foo')
@@ -147,8 +147,8 @@ describe('updating frontmatter', () => {
147147
expect($.text().includes('enterprise-server: \'>=2.13\'')).toBe(false)
148148
})
149149

150-
test('updates frontmatter versions Enterprise if set to greater-than-or-equal-to next oldest version', () => {
151-
let contents = fs.readFileSync(frontmatter2, 'utf8')
150+
test('updates frontmatter versions Enterprise if set to greater-than-or-equal-to next oldest version', async () => {
151+
let contents = await fs.readFile(frontmatter2, 'utf8')
152152
contents = processFrontmatter(contents, frontmatter2)
153153
const $ = cheerio.load(contents)
154154
expect($.text().includes('enterprise-server: \'*\'')).toBe(true)
@@ -157,8 +157,8 @@ describe('updating frontmatter', () => {
157157
})
158158

159159
describe('whitespace', () => {
160-
test('does not add newlines when whitespace control is used', () => {
161-
let contents = fs.readFileSync(whitespace, 'utf8')
160+
test('does not add newlines when whitespace control is used', async () => {
161+
let contents = await fs.readFile(whitespace, 'utf8')
162162
contents = removeLiquidStatements(contents, versionToDeprecate, nextOldestVersion)
163163
const $ = cheerio.load(contents)
164164
expect($('.example1').text()).toBe('\n Alpha\n')
@@ -167,8 +167,8 @@ describe('whitespace', () => {
167167
expect($('.example4').text()).toBe('\n Alpha\n')
168168
})
169169

170-
test('does not add newlines when no newlines are present', () => {
171-
let contents = fs.readFileSync(whitespace, 'utf8')
170+
test('does not add newlines when no newlines are present', async () => {
171+
let contents = await fs.readFile(whitespace, 'utf8')
172172
contents = removeLiquidStatements(contents, versionToDeprecate, nextOldestVersion)
173173
const $ = cheerio.load(contents)
174174
expect($('.example5').text()).toBe('\n Alpha\n')

tests/content/site-data-references.js

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const loadSiteData = require('../../lib/site-data')
33
const { loadPages } = require('../../lib/pages')
44
const getDataReferences = require('../../lib/get-liquid-data-references')
55
const frontmatter = require('@github-docs/frontmatter')
6-
const fs = require('fs')
6+
const fs = require('fs').promises
77
const path = require('path')
88

99
describe('data references', () => {
@@ -33,64 +33,64 @@ describe('data references', () => {
3333
expect(errors.length, JSON.stringify(errors, null, 2)).toBe(0)
3434
})
3535

36-
test('every data reference found in metadata of English content files is defined and has a value', () => {
36+
test('every data reference found in metadata of English content files is defined and has a value', async () => {
3737
let errors = []
3838
expect(pages.length).toBeGreaterThan(0)
3939

40-
pages.forEach(page => {
40+
await Promise.all(pages.map(async page => {
4141
const metadataFile = path.join('content', page.relativePath)
42-
const fileContents = fs.readFileSync(path.join(__dirname, '../..', metadataFile))
42+
const fileContents = await fs.readFile(path.join(__dirname, '../..', metadataFile))
4343
const { data: metadata } = frontmatter(fileContents, { filepath: page.fullPath })
4444
const metadataRefs = getDataReferences(JSON.stringify(metadata))
4545
metadataRefs.forEach(key => {
4646
const value = get(data.en, key)
4747
if (typeof value !== 'string') errors.push({ key, value, metadataFile })
4848
})
49-
})
49+
}))
5050

5151
errors = uniqWith(errors, isEqual) // remove duplicates
5252
expect(errors.length, JSON.stringify(errors, null, 2)).toBe(0)
5353
})
5454

55-
test('every data reference found in English reusable files is defined and has a value', () => {
55+
test('every data reference found in English reusable files is defined and has a value', async () => {
5656
let errors = []
5757
const allReusables = data.en.site.data.reusables
5858
const reusables = Object.values(allReusables)
5959
expect(reusables.length).toBeGreaterThan(0)
6060

61-
reusables.forEach(reusablesPerFile => {
61+
await Promise.all(reusables.map(async reusablesPerFile => {
6262
let reusableFile = path.join(__dirname, '../../data/reusables/', getFilenameByValue(allReusables, reusablesPerFile))
63-
reusableFile = getFilepath(reusableFile)
63+
reusableFile = await getFilepath(reusableFile)
6464

6565
const reusableRefs = getDataReferences(JSON.stringify(reusablesPerFile))
6666

6767
reusableRefs.forEach(key => {
6868
const value = get(data.en, key)
6969
if (typeof value !== 'string') errors.push({ key, value, reusableFile })
7070
})
71-
})
71+
}))
7272

7373
errors = uniqWith(errors, isEqual) // remove duplicates
7474
expect(errors.length, JSON.stringify(errors, null, 2)).toBe(0)
7575
})
7676

77-
test('every data reference found in English variable files is defined and has a value', () => {
77+
test('every data reference found in English variable files is defined and has a value', async () => {
7878
let errors = []
7979
const allVariables = data.en.site.data.variables
8080
const variables = Object.values(allVariables)
8181
expect(variables.length).toBeGreaterThan(0)
8282

83-
variables.forEach(variablesPerFile => {
83+
await Promise.all(variables.map(async variablesPerFile => {
8484
let variableFile = path.join(__dirname, '../../data/variables/', getFilenameByValue(allVariables, variablesPerFile))
85-
variableFile = getFilepath(variableFile)
85+
variableFile = await getFilepath(variableFile)
8686

8787
const variableRefs = getDataReferences(JSON.stringify(variablesPerFile))
8888

8989
variableRefs.forEach(key => {
9090
const value = get(data.en, key)
9191
if (typeof value !== 'string') errors.push({ key, value, variableFile })
9292
})
93-
})
93+
}))
9494

9595
errors = uniqWith(errors, isEqual) // remove duplicates
9696
expect(errors.length, JSON.stringify(errors, null, 2)).toBe(0)
@@ -102,10 +102,13 @@ function getFilenameByValue (object, value) {
102102
}
103103

104104
// if path exists, assume it's a directory; otherwise, assume a YML extension
105-
function getFilepath (filepath) {
106-
filepath = fs.existsSync(filepath)
107-
? filepath + '/'
108-
: filepath + '.yml'
105+
async function getFilepath (filepath) {
106+
try {
107+
await fs.stat(filepath)
108+
filepath = filepath + '/'
109+
} catch (_) {
110+
filepath = filepath + '.yml'
111+
}
109112

110113
// we only need the relative path
111114
return filepath.replace(path.join(__dirname, '../../'), '')

tests/content/site-data.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,11 @@ describe('siteData module (English)', () => {
4545
// TODO: re-enable once Janky flakyness is resolved
4646
test.skip('backfills missing translated site data with English values', async () => {
4747
const newFile = path.join(__dirname, '../../data/newfile.yml')
48-
fs.writeFileSync(newFile, 'newvalue: bar')
48+
await fs.writeFile(newFile, 'newvalue: bar')
4949
const data = await loadSiteData()
5050
expect(get(data, 'en.site.data.newfile.newvalue')).toEqual('bar')
5151
expect(get(data, 'ja.site.data.newfile.newvalue')).toEqual('bar')
52-
fs.unlinkSync(newFile)
52+
await fs.unlink(newFile)
5353
})
5454

5555
test('all Liquid templating is valid', async () => {

tests/graphql/build-changelog-test.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const yaml = require('js-yaml')
22
const { createChangelogEntry, cleanPreviewTitle, previewAnchor, prependDatedEntry } = require('../../script/graphql/build-changelog')
3-
const fs = require('fs')
3+
const fs = require('fs').promises
44
const MockDate = require('mockdate')
55
const expectedChangelogEntry = require('../fixtures/changelog-entry')
66
const expectedUpdatedChangelogFile = require('../fixtures/updated-changelog-file')
@@ -111,18 +111,18 @@ describe('updating the changelog file', () => {
111111
MockDate.reset()
112112
})
113113

114-
it('modifies the entry object and the file on disk', () => {
114+
it('modifies the entry object and the file on disk', async () => {
115115
const testTargetPath = 'tests/graphql/example_changelog.json'
116-
const previousContents = fs.readFileSync(testTargetPath)
116+
const previousContents = await fs.readFile(testTargetPath)
117117

118118
const exampleEntry = { someStuff: true }
119119
const expectedDate = '2020-11-20'
120120
MockDate.set(expectedDate)
121121

122122
prependDatedEntry(exampleEntry, testTargetPath)
123-
const newContents = fs.readFileSync(testTargetPath, 'utf8')
123+
const newContents = await fs.readFile(testTargetPath, 'utf8')
124124
// reset the file:
125-
fs.writeFileSync(testTargetPath, previousContents)
125+
await fs.writeFile(testTargetPath, previousContents)
126126

127127
expect(exampleEntry).toEqual({ someStuff: true, date: expectedDate })
128128
expect(JSON.parse(newContents)).toEqual(expectedUpdatedChangelogFile)

tests/meta/orphan-tests.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,28 @@
1-
const fs = require('fs')
1+
const fs = require('fs').promises
22
const path = require('path')
3+
const { filter: asyncFilter } = require('async')
34

45
describe('check for orphan tests', () => {
5-
test('all tests are in sub-directories', () => {
6+
test('all tests are in sub-directories', async () => {
67
// A known list of exceptions that can live outside of directories
78
const EXCEPTIONS = ['README.md']
89
const pathToTests = path.join(process.cwd(), 'tests')
910

1011
// Get a list of files/directories in `/tests`
11-
const testDirectory = fs.readdirSync(pathToTests)
12+
const testDirectory = await fs.readdir(pathToTests)
1213

13-
const filteredList = testDirectory
14+
let filteredList = testDirectory
1415
// Filter out our exceptions
1516
.filter(item => !EXCEPTIONS.includes(item))
16-
// Don't include directories
17-
.filter(item => !fs.statSync(path.join(pathToTests, item)).isDirectory())
17+
// Don't include directories
18+
filteredList = await asyncFilter(
19+
filteredList,
20+
async item => !(
21+
await fs.stat(
22+
path.join(pathToTests, item)
23+
)
24+
).isDirectory()
25+
)
1826

1927
expect(filteredList).toHaveLength(0)
2028
})

tests/rendering/rest.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const fs = require('fs')
1+
const fs = require('fs').promises
22
const path = require('path')
33
const { difference, isPlainObject } = require('lodash')
44
const { getJSON } = require('../helpers/supertest')
@@ -17,7 +17,7 @@ describe('REST references docs', () => {
1717
test('markdown file exists for every operationId prefix in the api.github.com schema', async () => {
1818
const { categories } = require('../../lib/rest')
1919
const referenceDir = path.join(__dirname, '../../content/rest/reference')
20-
const filenames = fs.readdirSync(referenceDir)
20+
const filenames = (await fs.readdir(referenceDir))
2121
.filter(filename => !excludeFromResourceNameCheck.find(excludedFile => filename.endsWith(excludedFile)))
2222
.map(filename => filename.replace('.md', ''))
2323

tests/unit/early-access.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const fs = require('fs')
1+
const fs = require('fs').promises
22
const path = require('path')
33

44
const { GITHUB_ACTIONS, GITHUB_REPOSITORY } = process.env
@@ -7,17 +7,17 @@ const testViaActionsOnly = runningActionsOnInternalRepo ? test : test.skip
77

88
describe('cloning early-access', () => {
99
testViaActionsOnly('the content directory exists', async () => {
10-
const eaContentDir = path.join(process.cwd(), 'content/early-access')
11-
expect(fs.existsSync(eaContentDir)).toBe(true)
10+
const eaDir = path.join(process.cwd(), 'content/early-access')
11+
expect(await fs.stat(eaDir)).toBeTruthy()
1212
})
1313

1414
testViaActionsOnly('the data directory exists', async () => {
15-
const eaContentDir = path.join(process.cwd(), 'data/early-access')
16-
expect(fs.existsSync(eaContentDir)).toBe(true)
15+
const eaDir = path.join(process.cwd(), 'data/early-access')
16+
expect(await fs.stat(eaDir)).toBeTruthy()
1717
})
1818

1919
testViaActionsOnly('the assets/images directory exists', async () => {
20-
const eaContentDir = path.join(process.cwd(), 'assets/images/early-access')
21-
expect(fs.existsSync(eaContentDir)).toBe(true)
20+
const eaDir = path.join(process.cwd(), 'assets/images/early-access')
21+
expect(await fs.stat(eaDir)).toBeTruthy()
2222
})
2323
})

0 commit comments

Comments
 (0)