mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-13 09:07:29 +02:00
refactor: mark all functions that import external modules as async (#6521)
This commit is contained in:
parent
aa446b7a9c
commit
c56e6194b4
15 changed files with 125 additions and 112 deletions
|
@ -32,159 +32,159 @@ describe('docsVersion', () => {
|
|||
sidebarCollapsible: true,
|
||||
};
|
||||
|
||||
test('no version tag provided', () => {
|
||||
expect(() =>
|
||||
test('no version tag provided', async () => {
|
||||
await expect(() =>
|
||||
cliDocsVersionCommand(
|
||||
null,
|
||||
simpleSiteDir,
|
||||
DEFAULT_PLUGIN_ID,
|
||||
DEFAULT_OPTIONS,
|
||||
),
|
||||
).toThrowErrorMatchingInlineSnapshot(
|
||||
).rejects.toThrowErrorMatchingInlineSnapshot(
|
||||
`"[docs]: no version tag specified! Pass the version you wish to create as an argument, for example: 1.0.0."`,
|
||||
);
|
||||
expect(() =>
|
||||
await expect(() =>
|
||||
cliDocsVersionCommand(
|
||||
undefined,
|
||||
simpleSiteDir,
|
||||
DEFAULT_PLUGIN_ID,
|
||||
DEFAULT_OPTIONS,
|
||||
),
|
||||
).toThrowErrorMatchingInlineSnapshot(
|
||||
).rejects.toThrowErrorMatchingInlineSnapshot(
|
||||
`"[docs]: no version tag specified! Pass the version you wish to create as an argument, for example: 1.0.0."`,
|
||||
);
|
||||
expect(() =>
|
||||
await expect(() =>
|
||||
cliDocsVersionCommand(
|
||||
'',
|
||||
simpleSiteDir,
|
||||
DEFAULT_PLUGIN_ID,
|
||||
DEFAULT_OPTIONS,
|
||||
),
|
||||
).toThrowErrorMatchingInlineSnapshot(
|
||||
).rejects.toThrowErrorMatchingInlineSnapshot(
|
||||
`"[docs]: no version tag specified! Pass the version you wish to create as an argument, for example: 1.0.0."`,
|
||||
);
|
||||
});
|
||||
|
||||
test('version tag should not have slash', () => {
|
||||
expect(() =>
|
||||
test('version tag should not have slash', async () => {
|
||||
await expect(() =>
|
||||
cliDocsVersionCommand(
|
||||
'foo/bar',
|
||||
simpleSiteDir,
|
||||
DEFAULT_PLUGIN_ID,
|
||||
DEFAULT_OPTIONS,
|
||||
),
|
||||
).toThrowErrorMatchingInlineSnapshot(
|
||||
).rejects.toThrowErrorMatchingInlineSnapshot(
|
||||
`"[docs]: invalid version tag specified! Do not include slash (/) or backslash (\\\\). Try something like: 1.0.0."`,
|
||||
);
|
||||
expect(() =>
|
||||
await expect(() =>
|
||||
cliDocsVersionCommand(
|
||||
'foo\\bar',
|
||||
simpleSiteDir,
|
||||
DEFAULT_PLUGIN_ID,
|
||||
DEFAULT_OPTIONS,
|
||||
),
|
||||
).toThrowErrorMatchingInlineSnapshot(
|
||||
).rejects.toThrowErrorMatchingInlineSnapshot(
|
||||
`"[docs]: invalid version tag specified! Do not include slash (/) or backslash (\\\\). Try something like: 1.0.0."`,
|
||||
);
|
||||
});
|
||||
|
||||
test('version tag should not be too long', () => {
|
||||
expect(() =>
|
||||
test('version tag should not be too long', async () => {
|
||||
await expect(() =>
|
||||
cliDocsVersionCommand(
|
||||
'a'.repeat(255),
|
||||
simpleSiteDir,
|
||||
DEFAULT_PLUGIN_ID,
|
||||
DEFAULT_OPTIONS,
|
||||
),
|
||||
).toThrowErrorMatchingInlineSnapshot(
|
||||
).rejects.toThrowErrorMatchingInlineSnapshot(
|
||||
`"[docs]: invalid version tag specified! Length cannot exceed 32 characters. Try something like: 1.0.0."`,
|
||||
);
|
||||
});
|
||||
|
||||
test('version tag should not be a dot or two dots', () => {
|
||||
expect(() =>
|
||||
test('version tag should not be a dot or two dots', async () => {
|
||||
await expect(() =>
|
||||
cliDocsVersionCommand(
|
||||
'..',
|
||||
simpleSiteDir,
|
||||
DEFAULT_PLUGIN_ID,
|
||||
DEFAULT_OPTIONS,
|
||||
),
|
||||
).toThrowErrorMatchingInlineSnapshot(
|
||||
).rejects.toThrowErrorMatchingInlineSnapshot(
|
||||
`"[docs]: invalid version tag specified! Do not name your version \\".\\" or \\"..\\". Try something like: 1.0.0."`,
|
||||
);
|
||||
expect(() =>
|
||||
await expect(() =>
|
||||
cliDocsVersionCommand(
|
||||
'.',
|
||||
simpleSiteDir,
|
||||
DEFAULT_PLUGIN_ID,
|
||||
DEFAULT_OPTIONS,
|
||||
),
|
||||
).toThrowErrorMatchingInlineSnapshot(
|
||||
).rejects.toThrowErrorMatchingInlineSnapshot(
|
||||
`"[docs]: invalid version tag specified! Do not name your version \\".\\" or \\"..\\". Try something like: 1.0.0."`,
|
||||
);
|
||||
});
|
||||
|
||||
test('version tag should be a valid pathname', () => {
|
||||
expect(() =>
|
||||
test('version tag should be a valid pathname', async () => {
|
||||
await expect(() =>
|
||||
cliDocsVersionCommand(
|
||||
'<foo|bar>',
|
||||
simpleSiteDir,
|
||||
DEFAULT_PLUGIN_ID,
|
||||
DEFAULT_OPTIONS,
|
||||
),
|
||||
).toThrowErrorMatchingInlineSnapshot(
|
||||
).rejects.toThrowErrorMatchingInlineSnapshot(
|
||||
`"[docs]: invalid version tag specified! Please ensure its a valid pathname too. Try something like: 1.0.0."`,
|
||||
);
|
||||
expect(() =>
|
||||
await expect(() =>
|
||||
cliDocsVersionCommand(
|
||||
'foo\x00bar',
|
||||
simpleSiteDir,
|
||||
DEFAULT_PLUGIN_ID,
|
||||
DEFAULT_OPTIONS,
|
||||
),
|
||||
).toThrowErrorMatchingInlineSnapshot(
|
||||
).rejects.toThrowErrorMatchingInlineSnapshot(
|
||||
`"[docs]: invalid version tag specified! Please ensure its a valid pathname too. Try something like: 1.0.0."`,
|
||||
);
|
||||
expect(() =>
|
||||
await expect(() =>
|
||||
cliDocsVersionCommand(
|
||||
'foo:bar',
|
||||
simpleSiteDir,
|
||||
DEFAULT_PLUGIN_ID,
|
||||
DEFAULT_OPTIONS,
|
||||
),
|
||||
).toThrowErrorMatchingInlineSnapshot(
|
||||
).rejects.toThrowErrorMatchingInlineSnapshot(
|
||||
`"[docs]: invalid version tag specified! Please ensure its a valid pathname too. Try something like: 1.0.0."`,
|
||||
);
|
||||
});
|
||||
|
||||
test('version tag already exist', () => {
|
||||
expect(() =>
|
||||
test('version tag already exist', async () => {
|
||||
await expect(() =>
|
||||
cliDocsVersionCommand(
|
||||
'1.0.0',
|
||||
versionedSiteDir,
|
||||
DEFAULT_PLUGIN_ID,
|
||||
DEFAULT_OPTIONS,
|
||||
),
|
||||
).toThrowErrorMatchingInlineSnapshot(
|
||||
).rejects.toThrowErrorMatchingInlineSnapshot(
|
||||
`"[docs]: this version already exists! Use a version tag that does not already exist."`,
|
||||
);
|
||||
});
|
||||
|
||||
test('no docs file to version', () => {
|
||||
test('no docs file to version', async () => {
|
||||
const emptySiteDir = path.join(fixtureDir, 'empty-site');
|
||||
expect(() =>
|
||||
await expect(() =>
|
||||
cliDocsVersionCommand(
|
||||
'1.0.0',
|
||||
emptySiteDir,
|
||||
DEFAULT_PLUGIN_ID,
|
||||
DEFAULT_OPTIONS,
|
||||
),
|
||||
).toThrowErrorMatchingInlineSnapshot(
|
||||
).rejects.toThrowErrorMatchingInlineSnapshot(
|
||||
`"[docs]: there is no docs to version!"`,
|
||||
);
|
||||
});
|
||||
|
||||
test('first time versioning', () => {
|
||||
test('first time versioning', async () => {
|
||||
const copyMock = jest.spyOn(fs, 'copySync').mockImplementation();
|
||||
const ensureMock = jest.spyOn(fs, 'ensureDirSync').mockImplementation();
|
||||
const writeMock = jest.spyOn(fs, 'writeFileSync');
|
||||
|
@ -205,7 +205,12 @@ describe('docsVersion', () => {
|
|||
...DEFAULT_OPTIONS,
|
||||
sidebarPath: path.join(simpleSiteDir, 'sidebars.json'),
|
||||
};
|
||||
cliDocsVersionCommand('1.0.0', simpleSiteDir, DEFAULT_PLUGIN_ID, options);
|
||||
await cliDocsVersionCommand(
|
||||
'1.0.0',
|
||||
simpleSiteDir,
|
||||
DEFAULT_PLUGIN_ID,
|
||||
options,
|
||||
);
|
||||
expect(copyMock).toHaveBeenCalledWith(
|
||||
path.join(simpleSiteDir, options.path),
|
||||
path.join(
|
||||
|
@ -236,7 +241,7 @@ describe('docsVersion', () => {
|
|||
ensureMock.mockRestore();
|
||||
});
|
||||
|
||||
test('not the first time versioning', () => {
|
||||
test('not the first time versioning', async () => {
|
||||
const copyMock = jest.spyOn(fs, 'copySync').mockImplementation();
|
||||
const ensureMock = jest.spyOn(fs, 'ensureDirSync').mockImplementation();
|
||||
const writeMock = jest.spyOn(fs, 'writeFileSync');
|
||||
|
@ -257,7 +262,7 @@ describe('docsVersion', () => {
|
|||
...DEFAULT_OPTIONS,
|
||||
sidebarPath: path.join(versionedSiteDir, 'sidebars.json'),
|
||||
};
|
||||
cliDocsVersionCommand(
|
||||
await cliDocsVersionCommand(
|
||||
'2.0.0',
|
||||
versionedSiteDir,
|
||||
DEFAULT_PLUGIN_ID,
|
||||
|
@ -293,7 +298,7 @@ describe('docsVersion', () => {
|
|||
ensureMock.mockRestore();
|
||||
});
|
||||
|
||||
test('second docs instance versioning', () => {
|
||||
test('second docs instance versioning', async () => {
|
||||
const pluginId = 'community';
|
||||
|
||||
const copyMock = jest.spyOn(fs, 'copySync').mockImplementation();
|
||||
|
@ -317,7 +322,7 @@ describe('docsVersion', () => {
|
|||
path: 'community',
|
||||
sidebarPath: path.join(versionedSiteDir, 'community_sidebars.json'),
|
||||
};
|
||||
cliDocsVersionCommand('2.0.0', versionedSiteDir, pluginId, options);
|
||||
await cliDocsVersionCommand('2.0.0', versionedSiteDir, pluginId, options);
|
||||
expect(copyMock).toHaveBeenCalledWith(
|
||||
path.join(versionedSiteDir, options.path),
|
||||
path.join(
|
||||
|
|
|
@ -20,7 +20,7 @@ import {loadSidebarsFile, resolveSidebarPathOption} from './sidebars';
|
|||
import {DEFAULT_PLUGIN_ID} from '@docusaurus/utils';
|
||||
import logger from '@docusaurus/logger';
|
||||
|
||||
function createVersionedSidebarFile({
|
||||
async function createVersionedSidebarFile({
|
||||
siteDir,
|
||||
pluginId,
|
||||
sidebarPath,
|
||||
|
@ -34,7 +34,7 @@ function createVersionedSidebarFile({
|
|||
// Load current sidebar and create a new versioned sidebars file (if needed).
|
||||
// Note: we don't need the sidebars file to be normalized: it's ok to let
|
||||
// plugin option changes to impact older, versioned sidebars
|
||||
const sidebars = loadSidebarsFile(sidebarPath);
|
||||
const sidebars = await loadSidebarsFile(sidebarPath);
|
||||
|
||||
// Do not create a useless versioned sidebars file if sidebars file is empty
|
||||
// or sidebars are disabled/false)
|
||||
|
@ -56,12 +56,12 @@ function createVersionedSidebarFile({
|
|||
}
|
||||
|
||||
// Tests depend on non-default export for mocking.
|
||||
export function cliDocsVersionCommand(
|
||||
export async function cliDocsVersionCommand(
|
||||
version: string | null | undefined,
|
||||
siteDir: string,
|
||||
pluginId: string,
|
||||
options: PathOptions & SidebarOptions,
|
||||
): void {
|
||||
): Promise<void> {
|
||||
// It wouldn't be very user-friendly to show a [default] log prefix,
|
||||
// so we use [docs] instead of [default]
|
||||
const pluginIdLogPrefix =
|
||||
|
@ -127,7 +127,7 @@ export function cliDocsVersionCommand(
|
|||
throw new Error(`${pluginIdLogPrefix}: there is no docs to version!`);
|
||||
}
|
||||
|
||||
createVersionedSidebarFile({
|
||||
await createVersionedSidebarFile({
|
||||
siteDir,
|
||||
pluginId,
|
||||
version,
|
||||
|
|
|
@ -25,13 +25,13 @@ describe('loadNormalizedSidebars', () => {
|
|||
};
|
||||
test('sidebars with known sidebar item type', async () => {
|
||||
const sidebarPath = path.join(fixtureDir, 'sidebars.json');
|
||||
const result = loadNormalizedSidebars(sidebarPath, options);
|
||||
const result = await loadNormalizedSidebars(sidebarPath, options);
|
||||
expect(result).toMatchSnapshot();
|
||||
});
|
||||
|
||||
test('sidebars with deep level of category', async () => {
|
||||
const sidebarPath = path.join(fixtureDir, 'sidebars-category.js');
|
||||
const result = loadNormalizedSidebars(sidebarPath, options);
|
||||
const result = await loadNormalizedSidebars(sidebarPath, options);
|
||||
expect(result).toMatchSnapshot();
|
||||
});
|
||||
|
||||
|
@ -41,8 +41,8 @@ describe('loadNormalizedSidebars', () => {
|
|||
fixtureDir,
|
||||
'sidebars-category-shorthand.js',
|
||||
);
|
||||
const sidebar1 = loadNormalizedSidebars(sidebarPath1, options);
|
||||
const sidebar2 = loadNormalizedSidebars(sidebarPath2, options);
|
||||
const sidebar1 = await loadNormalizedSidebars(sidebarPath1, options);
|
||||
const sidebar2 = await loadNormalizedSidebars(sidebarPath2, options);
|
||||
expect(sidebar1).toEqual(sidebar2);
|
||||
});
|
||||
|
||||
|
@ -51,7 +51,7 @@ describe('loadNormalizedSidebars', () => {
|
|||
fixtureDir,
|
||||
'sidebars-category-wrong-items.json',
|
||||
);
|
||||
expect(() => loadNormalizedSidebars(sidebarPath, options))
|
||||
await expect(() => loadNormalizedSidebars(sidebarPath, options)).rejects
|
||||
.toThrowErrorMatchingInlineSnapshot(`
|
||||
"{
|
||||
\\"type\\": \\"category\\",
|
||||
|
@ -68,7 +68,7 @@ describe('loadNormalizedSidebars', () => {
|
|||
fixtureDir,
|
||||
'sidebars-category-wrong-label.json',
|
||||
);
|
||||
expect(() => loadNormalizedSidebars(sidebarPath, options))
|
||||
await expect(() => loadNormalizedSidebars(sidebarPath, options)).rejects
|
||||
.toThrowErrorMatchingInlineSnapshot(`
|
||||
"{
|
||||
\\"type\\": \\"category\\",
|
||||
|
@ -87,7 +87,7 @@ describe('loadNormalizedSidebars', () => {
|
|||
fixtureDir,
|
||||
'sidebars-doc-id-not-string.json',
|
||||
);
|
||||
expect(() => loadNormalizedSidebars(sidebarPath, options))
|
||||
await expect(() => loadNormalizedSidebars(sidebarPath, options)).rejects
|
||||
.toThrowErrorMatchingInlineSnapshot(`
|
||||
"{
|
||||
\\"type\\": \\"doc\\",
|
||||
|
@ -105,19 +105,19 @@ describe('loadNormalizedSidebars', () => {
|
|||
fixtureDir,
|
||||
'sidebars-first-level-not-category.js',
|
||||
);
|
||||
const result = loadNormalizedSidebars(sidebarPath, options);
|
||||
const result = await loadNormalizedSidebars(sidebarPath, options);
|
||||
expect(result).toMatchSnapshot();
|
||||
});
|
||||
|
||||
test('sidebars link', async () => {
|
||||
const sidebarPath = path.join(fixtureDir, 'sidebars-link.json');
|
||||
const result = loadNormalizedSidebars(sidebarPath, options);
|
||||
const result = await loadNormalizedSidebars(sidebarPath, options);
|
||||
expect(result).toMatchSnapshot();
|
||||
});
|
||||
|
||||
test('sidebars link wrong label', async () => {
|
||||
const sidebarPath = path.join(fixtureDir, 'sidebars-link-wrong-label.json');
|
||||
expect(() => loadNormalizedSidebars(sidebarPath, options))
|
||||
await expect(() => loadNormalizedSidebars(sidebarPath, options)).rejects
|
||||
.toThrowErrorMatchingInlineSnapshot(`
|
||||
"{
|
||||
\\"type\\": \\"link\\",
|
||||
|
@ -131,7 +131,7 @@ describe('loadNormalizedSidebars', () => {
|
|||
|
||||
test('sidebars link wrong href', async () => {
|
||||
const sidebarPath = path.join(fixtureDir, 'sidebars-link-wrong-href.json');
|
||||
expect(() => loadNormalizedSidebars(sidebarPath, options))
|
||||
await expect(() => loadNormalizedSidebars(sidebarPath, options)).rejects
|
||||
.toThrowErrorMatchingInlineSnapshot(`
|
||||
"{
|
||||
\\"type\\": \\"link\\",
|
||||
|
@ -147,7 +147,7 @@ describe('loadNormalizedSidebars', () => {
|
|||
|
||||
test('sidebars with unknown sidebar item type', async () => {
|
||||
const sidebarPath = path.join(fixtureDir, 'sidebars-unknown-type.json');
|
||||
expect(() => loadNormalizedSidebars(sidebarPath, options))
|
||||
await expect(() => loadNormalizedSidebars(sidebarPath, options)).rejects
|
||||
.toThrowErrorMatchingInlineSnapshot(`
|
||||
"{
|
||||
\\"type\\": \\"superman\\",
|
||||
|
@ -160,7 +160,7 @@ describe('loadNormalizedSidebars', () => {
|
|||
|
||||
test('sidebars with known sidebar item type but wrong field', async () => {
|
||||
const sidebarPath = path.join(fixtureDir, 'sidebars-wrong-field.json');
|
||||
expect(() => loadNormalizedSidebars(sidebarPath, options))
|
||||
await expect(() => loadNormalizedSidebars(sidebarPath, options)).rejects
|
||||
.toThrowErrorMatchingInlineSnapshot(`
|
||||
"{
|
||||
\\"type\\": \\"category\\",
|
||||
|
@ -173,23 +173,27 @@ describe('loadNormalizedSidebars', () => {
|
|||
`);
|
||||
});
|
||||
|
||||
test('unexisting path', () => {
|
||||
expect(loadNormalizedSidebars('badpath', options)).toEqual(
|
||||
test('unexisting path', async () => {
|
||||
await expect(loadNormalizedSidebars('badpath', options)).resolves.toEqual(
|
||||
DisabledSidebars,
|
||||
);
|
||||
});
|
||||
|
||||
test('undefined path', () => {
|
||||
expect(loadNormalizedSidebars(undefined, options)).toEqual(DefaultSidebars);
|
||||
test('undefined path', async () => {
|
||||
await expect(loadNormalizedSidebars(undefined, options)).resolves.toEqual(
|
||||
DefaultSidebars,
|
||||
);
|
||||
});
|
||||
|
||||
test('literal false path', () => {
|
||||
expect(loadNormalizedSidebars(false, options)).toEqual(DisabledSidebars);
|
||||
test('literal false path', async () => {
|
||||
await expect(loadNormalizedSidebars(false, options)).resolves.toEqual(
|
||||
DisabledSidebars,
|
||||
);
|
||||
});
|
||||
|
||||
test('sidebars with category.collapsed property', async () => {
|
||||
const sidebarPath = path.join(fixtureDir, 'sidebars-collapsed.json');
|
||||
const result = loadNormalizedSidebars(sidebarPath, options);
|
||||
const result = await loadNormalizedSidebars(sidebarPath, options);
|
||||
expect(result).toMatchSnapshot();
|
||||
});
|
||||
|
||||
|
@ -198,7 +202,7 @@ describe('loadNormalizedSidebars', () => {
|
|||
fixtureDir,
|
||||
'sidebars-collapsed-first-level.json',
|
||||
);
|
||||
const result = loadNormalizedSidebars(sidebarPath, options);
|
||||
const result = await loadNormalizedSidebars(sidebarPath, options);
|
||||
expect(result).toMatchSnapshot();
|
||||
});
|
||||
});
|
||||
|
|
|
@ -38,9 +38,9 @@ export function resolveSidebarPathOption(
|
|||
: sidebarPathOption;
|
||||
}
|
||||
|
||||
function loadSidebarsFileUnsafe(
|
||||
async function loadSidebarsFileUnsafe(
|
||||
sidebarFilePath: string | false | undefined,
|
||||
): SidebarsConfig {
|
||||
): Promise<SidebarsConfig> {
|
||||
// false => no sidebars
|
||||
if (sidebarFilePath === false) {
|
||||
return DisabledSidebars;
|
||||
|
@ -62,19 +62,19 @@ function loadSidebarsFileUnsafe(
|
|||
return importFresh(sidebarFilePath);
|
||||
}
|
||||
|
||||
export function loadSidebarsFile(
|
||||
export async function loadSidebarsFile(
|
||||
sidebarFilePath: string | false | undefined,
|
||||
): SidebarsConfig {
|
||||
const sidebarsConfig = loadSidebarsFileUnsafe(sidebarFilePath);
|
||||
): Promise<SidebarsConfig> {
|
||||
const sidebarsConfig = await loadSidebarsFileUnsafe(sidebarFilePath);
|
||||
validateSidebars(sidebarsConfig);
|
||||
return sidebarsConfig;
|
||||
}
|
||||
|
||||
export function loadNormalizedSidebars(
|
||||
export async function loadNormalizedSidebars(
|
||||
sidebarFilePath: string | false | undefined,
|
||||
params: NormalizeSidebarsParams,
|
||||
): NormalizedSidebars {
|
||||
return normalizeSidebars(loadSidebarsFile(sidebarFilePath), params);
|
||||
): Promise<NormalizedSidebars> {
|
||||
return normalizeSidebars(await loadSidebarsFile(sidebarFilePath), params);
|
||||
}
|
||||
|
||||
// Note: sidebarFilePath must be absolute, use resolveSidebarPathOption
|
||||
|
@ -87,7 +87,7 @@ export async function loadSidebars(
|
|||
version: options.version,
|
||||
categoryLabelSlugger: createSlugger(),
|
||||
};
|
||||
const normalizedSidebars = loadNormalizedSidebars(
|
||||
const normalizedSidebars = await loadNormalizedSidebars(
|
||||
sidebarFilePath,
|
||||
normalizeSidebarsParams,
|
||||
);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue