Skip to content

Commit effc818

Browse files
authored
Add tests to make sure applicable versions of each category/subcategory match the openapi (#44077)
1 parent 805c503 commit effc818

File tree

1 file changed

+66
-16
lines changed

1 file changed

+66
-16
lines changed

src/rest/tests/openapi-schema.js

+66-16
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import fs from 'fs/promises'
1+
import fs from 'fs'
22
import path from 'path'
33

44
import { describe } from '@jest/globals'
@@ -8,6 +8,8 @@ import { isPlainObject, difference } from 'lodash-es'
88
import { isApiVersioned, allVersions } from '#src/versions/lib/all-versions.js'
99
import getRest from '../lib/index.js'
1010
import readFrontmatter from '../../../lib/read-frontmatter.js'
11+
import frontmatter from '../../../lib/frontmatter.js'
12+
import getApplicableVersions from '../../versions/lib/get-applicable-versions.js'
1113

1214
const schemasPath = 'src/rest/data'
1315

@@ -42,34 +44,63 @@ function createCategoryList(operations) {
4244
}
4345

4446
describe('markdown for each rest version', () => {
45-
test('markdown file exists for every operationId prefix in all versions of the OpenAPI schema', async () => {
46-
// list of REST markdown files that do not correspond to REST API resources
47-
// TODO could we get this list dynamically, say via page frontmatter?
48-
const excludeFromResourceNameCheck = [
49-
'README.md',
50-
'index.md',
51-
'guides',
52-
'overview',
53-
'quickstart.md',
54-
]
47+
// Unique set of all categories across all versions of the OpenAPI schema
48+
const allCategories = new Set()
49+
// Entire schema including categories and subcategories
50+
const openApiSchema = {}
51+
// All applicable version of categories based on frontmatter in the categories index.md file
52+
const categoryApplicableVersions = {}
53+
const referenceDir = path.join(process.cwd(), 'content/rest')
54+
const excludeFromResourceNameCheck = [
55+
'README.md',
56+
'index.md',
57+
'guides',
58+
'overview',
59+
'quickstart.md',
60+
]
61+
62+
function getApplicableVersionFromFile(file) {
63+
const currentFile = fs.readFileSync(file, 'utf8')
64+
const { data } = frontmatter(currentFile)
65+
return getApplicableVersions(data.versions, file)
66+
}
5567

56-
// Unique set of all categories across all versions of the OpenAPI schema
57-
const allCategories = new Set()
68+
function getCategorySubcategory(file) {
69+
const fileSplit = file.split('/')
70+
const cat = fileSplit[fileSplit.length - 2]
71+
const subCat = fileSplit[fileSplit.length - 1].replace('.md', '')
72+
return { category: cat, subCategory: subCat }
73+
}
5874

75+
beforeAll(async () => {
5976
for (const version in allVersions) {
6077
if (isApiVersioned(version)) {
6178
for (const apiVersion of allVersions[version].apiVersions) {
6279
const apiOperations = await getRest(version, apiVersion)
6380
Object.keys(apiOperations).forEach((category) => allCategories.add(category))
81+
openApiSchema[version] = apiOperations
6482
}
6583
} else {
6684
const apiOperations = await getRest(version)
6785
Object.keys(apiOperations).forEach((category) => allCategories.add(category))
86+
openApiSchema[version] = apiOperations
6887
}
6988
}
7089

71-
const referenceDir = path.join('content/rest')
72-
const filenames = (await fs.readdir(referenceDir))
90+
walk(referenceDir, { includeBasePath: true, directories: false })
91+
.filter((filename) => filename.includes('index.md'))
92+
.forEach((file) => {
93+
const applicableVersions = getApplicableVersionFromFile(file)
94+
const { category } = getCategorySubcategory(file)
95+
categoryApplicableVersions[category] = applicableVersions
96+
})
97+
})
98+
99+
test('markdown file exists for every operationId prefix in all versions of the OpenAPI schema', async () => {
100+
// list of REST markdown files that do not correspond to REST API resources
101+
// TODO could we get this list dynamically, say via page frontmatter?
102+
const filenames = fs
103+
.readdirSync(referenceDir)
73104
.filter(
74105
(filename) =>
75106
!excludeFromResourceNameCheck.find((excludedFile) => filename.endsWith(excludedFile)),
@@ -84,11 +115,30 @@ describe('markdown for each rest version', () => {
84115
'Found an OpenAPI REST operation category that is not represented by a markdown file in content/rest.'
85116
expect(difference([...allCategories], filenames), missingFile).toEqual([])
86117
})
118+
119+
test('category and subcategory exist in OpenAPI schema for every applicable version in markdown frontmatter', async () => {
120+
walk(referenceDir, { includeBasePath: true, directories: false })
121+
.filter(
122+
(filename) => !excludeFromResourceNameCheck.some((pattern) => filename.includes(pattern)),
123+
)
124+
.forEach((file) => {
125+
const applicableVersions = getApplicableVersionFromFile(file)
126+
const { category, subCategory } = getCategorySubcategory(file)
127+
128+
for (const version of applicableVersions) {
129+
expect(
130+
Object.keys(openApiSchema[version][category]),
131+
`The REST version: ${version}'s category: ${category} does not include the subcategory: ${subCategory}. Please check file: ${file}`,
132+
).toContain(subCategory)
133+
expect(categoryApplicableVersions[category]).toContain(version)
134+
}
135+
})
136+
})
87137
})
88138

89139
describe('rest file structure', () => {
90140
test('children of content/rest/index.md are in alphabetical order', async () => {
91-
const indexContent = await fs.readFile('content/rest/index.md', 'utf8')
141+
const indexContent = fs.readFileSync('content/rest/index.md', 'utf8')
92142
const { data } = readFrontmatter(indexContent)
93143
const sortableChildren = data.children.filter(
94144
(child) => child !== '/quickstart' && child !== '/overview' && child !== '/guides',

0 commit comments

Comments
 (0)