feat(content-docs): make docs:version command work on localized docs (#7106)

Co-authored-by: Sébastien Lorber <slorber@users.noreply.github.com>
This commit is contained in:
Joshua Chen 2022-04-08 01:43:24 +08:00 committed by GitHub
parent 0a3aad618e
commit 4134ebb3fb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 165 additions and 183 deletions

View file

@ -8,10 +8,7 @@
import {jest} from '@jest/globals';
import path from 'path';
import {cliDocsVersionCommand} from '../cli';
import type {
PathOptions,
SidebarOptions,
} from '@docusaurus/plugin-content-docs';
import type {PluginOptions} from '@docusaurus/plugin-content-docs';
import fs from 'fs-extra';
import {
getVersionedDocsDirPath,
@ -26,7 +23,8 @@ describe('docsVersion', () => {
const simpleSiteDir = path.join(fixtureDir, 'simple-site');
const versionedSiteDir = path.join(fixtureDir, 'versioned-site');
const DEFAULT_OPTIONS: PathOptions & SidebarOptions = {
const DEFAULT_OPTIONS: PluginOptions = {
id: 'default',
path: 'docs',
sidebarPath: '',
sidebarCollapsed: true,
@ -35,32 +33,19 @@ describe('docsVersion', () => {
it('no version tag provided', async () => {
await expect(() =>
cliDocsVersionCommand(
null,
simpleSiteDir,
DEFAULT_PLUGIN_ID,
DEFAULT_OPTIONS,
),
cliDocsVersionCommand(null, DEFAULT_OPTIONS, {siteDir: simpleSiteDir}),
).rejects.toThrowErrorMatchingInlineSnapshot(
`"[docs]: no version tag specified! Pass the version you wish to create as an argument, for example: 1.0.0."`,
);
await expect(() =>
cliDocsVersionCommand(
undefined,
simpleSiteDir,
DEFAULT_PLUGIN_ID,
DEFAULT_OPTIONS,
),
cliDocsVersionCommand(undefined, DEFAULT_OPTIONS, {
siteDir: simpleSiteDir,
}),
).rejects.toThrowErrorMatchingInlineSnapshot(
`"[docs]: no version tag specified! Pass the version you wish to create as an argument, for example: 1.0.0."`,
);
await expect(() =>
cliDocsVersionCommand(
'',
simpleSiteDir,
DEFAULT_PLUGIN_ID,
DEFAULT_OPTIONS,
),
cliDocsVersionCommand('', DEFAULT_OPTIONS, {siteDir: simpleSiteDir}),
).rejects.toThrowErrorMatchingInlineSnapshot(
`"[docs]: no version tag specified! Pass the version you wish to create as an argument, for example: 1.0.0."`,
);
@ -68,22 +53,16 @@ describe('docsVersion', () => {
it('version tag should not have slash', async () => {
await expect(() =>
cliDocsVersionCommand(
'foo/bar',
simpleSiteDir,
DEFAULT_PLUGIN_ID,
DEFAULT_OPTIONS,
),
cliDocsVersionCommand('foo/bar', DEFAULT_OPTIONS, {
siteDir: simpleSiteDir,
}),
).rejects.toThrowError(
'[docs]: invalid version tag specified! Do not include slash (/) or backslash (\\). Try something like: 1.0.0.',
);
await expect(() =>
cliDocsVersionCommand(
'foo\\bar',
simpleSiteDir,
DEFAULT_PLUGIN_ID,
DEFAULT_OPTIONS,
),
cliDocsVersionCommand('foo\\bar', DEFAULT_OPTIONS, {
siteDir: simpleSiteDir,
}),
).rejects.toThrowError(
'[docs]: invalid version tag specified! Do not include slash (/) or backslash (\\). Try something like: 1.0.0.',
);
@ -91,12 +70,9 @@ describe('docsVersion', () => {
it('version tag should not be too long', async () => {
await expect(() =>
cliDocsVersionCommand(
'a'.repeat(255),
simpleSiteDir,
DEFAULT_PLUGIN_ID,
DEFAULT_OPTIONS,
),
cliDocsVersionCommand('a'.repeat(255), DEFAULT_OPTIONS, {
siteDir: simpleSiteDir,
}),
).rejects.toThrowErrorMatchingInlineSnapshot(
`"[docs]: invalid version tag specified! Length cannot exceed 32 characters. Try something like: 1.0.0."`,
);
@ -104,22 +80,12 @@ describe('docsVersion', () => {
it('version tag should not be a dot or two dots', async () => {
await expect(() =>
cliDocsVersionCommand(
'..',
simpleSiteDir,
DEFAULT_PLUGIN_ID,
DEFAULT_OPTIONS,
),
cliDocsVersionCommand('..', DEFAULT_OPTIONS, {siteDir: simpleSiteDir}),
).rejects.toThrowErrorMatchingInlineSnapshot(
`"[docs]: invalid version tag specified! Do not name your version \\".\\" or \\"..\\". Try something like: 1.0.0."`,
);
await expect(() =>
cliDocsVersionCommand(
'.',
simpleSiteDir,
DEFAULT_PLUGIN_ID,
DEFAULT_OPTIONS,
),
cliDocsVersionCommand('.', DEFAULT_OPTIONS, {siteDir: simpleSiteDir}),
).rejects.toThrowErrorMatchingInlineSnapshot(
`"[docs]: invalid version tag specified! Do not name your version \\".\\" or \\"..\\". Try something like: 1.0.0."`,
);
@ -127,32 +93,23 @@ describe('docsVersion', () => {
it('version tag should be a valid pathname', async () => {
await expect(() =>
cliDocsVersionCommand(
'<foo|bar>',
simpleSiteDir,
DEFAULT_PLUGIN_ID,
DEFAULT_OPTIONS,
),
cliDocsVersionCommand('<foo|bar>', DEFAULT_OPTIONS, {
siteDir: simpleSiteDir,
}),
).rejects.toThrowErrorMatchingInlineSnapshot(
`"[docs]: invalid version tag specified! Please ensure its a valid pathname too. Try something like: 1.0.0."`,
);
await expect(() =>
cliDocsVersionCommand(
'foo\x00bar',
simpleSiteDir,
DEFAULT_PLUGIN_ID,
DEFAULT_OPTIONS,
),
cliDocsVersionCommand('foo\x00bar', DEFAULT_OPTIONS, {
siteDir: simpleSiteDir,
}),
).rejects.toThrowErrorMatchingInlineSnapshot(
`"[docs]: invalid version tag specified! Please ensure its a valid pathname too. Try something like: 1.0.0."`,
);
await expect(() =>
cliDocsVersionCommand(
'foo:bar',
simpleSiteDir,
DEFAULT_PLUGIN_ID,
DEFAULT_OPTIONS,
),
cliDocsVersionCommand('foo:bar', DEFAULT_OPTIONS, {
siteDir: simpleSiteDir,
}),
).rejects.toThrowErrorMatchingInlineSnapshot(
`"[docs]: invalid version tag specified! Please ensure its a valid pathname too. Try something like: 1.0.0."`,
);
@ -160,12 +117,9 @@ describe('docsVersion', () => {
it('version tag already exist', async () => {
await expect(() =>
cliDocsVersionCommand(
'1.0.0',
versionedSiteDir,
DEFAULT_PLUGIN_ID,
DEFAULT_OPTIONS,
),
cliDocsVersionCommand('1.0.0', DEFAULT_OPTIONS, {
siteDir: versionedSiteDir,
}),
).rejects.toThrowErrorMatchingInlineSnapshot(
`"[docs]: this version already exists! Use a version tag that does not already exist."`,
);
@ -174,14 +128,12 @@ describe('docsVersion', () => {
it('no docs file to version', async () => {
const emptySiteDir = path.join(fixtureDir, 'empty-site');
await expect(() =>
cliDocsVersionCommand(
'1.0.0',
emptySiteDir,
DEFAULT_PLUGIN_ID,
DEFAULT_OPTIONS,
),
cliDocsVersionCommand('1.0.0', DEFAULT_OPTIONS, {
siteDir: emptySiteDir,
i18n: {locales: ['en', 'zh-Hans'], defaultLocale: 'en'},
}),
).rejects.toThrowErrorMatchingInlineSnapshot(
`"[docs]: no docs found in <PROJECT_ROOT>/packages/docusaurus-plugin-content-docs/src/__tests__/__fixtures__/empty-site/docs."`,
`"[docs]: no docs found in \\"<PROJECT_ROOT>/packages/docusaurus-plugin-content-docs/src/__tests__/__fixtures__/empty-site/docs\\"."`,
);
});
@ -205,12 +157,10 @@ describe('docsVersion', () => {
...DEFAULT_OPTIONS,
sidebarPath: path.join(simpleSiteDir, 'sidebars.json'),
};
await cliDocsVersionCommand(
'1.0.0',
simpleSiteDir,
DEFAULT_PLUGIN_ID,
options,
);
await cliDocsVersionCommand('1.0.0', options, {
siteDir: simpleSiteDir,
i18n: {locales: ['en', 'zh-Hans'], defaultLocale: 'en'},
});
expect(copyMock).toHaveBeenCalledWith(
path.join(simpleSiteDir, options.path),
path.join(
@ -218,6 +168,16 @@ describe('docsVersion', () => {
'version-1.0.0',
),
);
expect(copyMock).toHaveBeenCalledWith(
path.join(
simpleSiteDir,
'i18n/zh-Hans/docusaurus-plugin-content-docs/current',
),
path.join(
simpleSiteDir,
'i18n/zh-Hans/docusaurus-plugin-content-docs/version-1.0.0',
),
);
expect(versionedSidebar).toMatchSnapshot();
expect(versionedSidebarPath).toEqual(
path.join(
@ -256,16 +216,15 @@ describe('docsVersion', () => {
versions = JSON.parse(content as string);
});
const consoleMock = jest.spyOn(console, 'log').mockImplementation(() => {});
const warnMock = jest.spyOn(console, 'warn').mockImplementation(() => {});
const options = {
...DEFAULT_OPTIONS,
sidebarPath: path.join(versionedSiteDir, 'sidebars.json'),
};
await cliDocsVersionCommand(
'2.0.0',
versionedSiteDir,
DEFAULT_PLUGIN_ID,
options,
);
await cliDocsVersionCommand('2.0.0', options, {
siteDir: versionedSiteDir,
i18n: {locales: ['en', 'zh-Hans'], defaultLocale: 'en'},
});
expect(copyMock).toHaveBeenCalledWith(
path.join(versionedSiteDir, options.path),
path.join(
@ -289,7 +248,11 @@ describe('docsVersion', () => {
/.*\[SUCCESS\].*\[docs\].*: version .*2\.0\.0.* created!.*/,
),
);
expect(warnMock.mock.calls[0][0]).toMatchInlineSnapshot(
`"[WARNING] [docs]: no docs found in \\"<PROJECT_ROOT>/packages/docusaurus-plugin-content-docs/src/__tests__/__fixtures__/versioned-site/i18n/zh-Hans/docusaurus-plugin-content-docs/current\\". Skipping."`,
);
warnMock.mockRestore();
copyMock.mockRestore();
writeMock.mockRestore();
consoleMock.mockRestore();
@ -315,10 +278,14 @@ describe('docsVersion', () => {
const consoleMock = jest.spyOn(console, 'log').mockImplementation(() => {});
const options = {
...DEFAULT_OPTIONS,
id: pluginId,
path: 'community',
sidebarPath: path.join(versionedSiteDir, 'community_sidebars.json'),
};
await cliDocsVersionCommand('2.0.0', versionedSiteDir, pluginId, options);
await cliDocsVersionCommand('2.0.0', options, {
siteDir: versionedSiteDir,
i18n: {locales: ['en', 'fr'], defaultLocale: 'en'},
});
expect(copyMock).toHaveBeenCalledWith(
path.join(versionedSiteDir, options.path),
path.join(
@ -326,6 +293,16 @@ describe('docsVersion', () => {
'version-2.0.0',
),
);
expect(copyMock).toHaveBeenCalledWith(
path.join(
versionedSiteDir,
'i18n/fr/docusaurus-plugin-content-docs-community/current',
),
path.join(
versionedSiteDir,
'i18n/fr/docusaurus-plugin-content-docs-community/version-2.0.0',
),
);
expect(versionedSidebar).toMatchSnapshot();
expect(versionedSidebarPath).toEqual(
path.join(

View file

@ -16,7 +16,7 @@ import pluginContentDocs from '../index';
import {loadContext} from '@docusaurus/core/src/server/index';
import {applyConfigureWebpack} from '@docusaurus/core/src/webpack/utils';
import type {RouteConfig} from '@docusaurus/types';
import {posixPath, DEFAULT_PLUGIN_ID} from '@docusaurus/utils';
import {posixPath} from '@docusaurus/utils';
import {sortConfig} from '@docusaurus/core/src/server/plugins/routeConfig';
import * as cliDocs from '../cli';
@ -230,23 +230,21 @@ describe('simple website', () => {
const siteDir = path.join(__dirname, '__fixtures__', 'simple-site');
const context = await loadContext({siteDir});
const sidebarPath = path.join(siteDir, 'sidebars.json');
const plugin = await pluginContentDocs(
context,
validateOptions({
validate: normalizePluginOptions,
options: {
path: 'docs',
sidebarPath,
},
}),
);
const options = validateOptions({
validate: normalizePluginOptions,
options: {
path: 'docs',
sidebarPath,
},
});
const plugin = await pluginContentDocs(context, options);
const pluginContentDir = path.join(context.generatedFilesDir, plugin.name);
return {siteDir, context, sidebarPath, plugin, pluginContentDir};
return {siteDir, context, sidebarPath, plugin, options, pluginContentDir};
}
it('extendCli - docsVersion', async () => {
const {siteDir, sidebarPath, plugin} = await loadSite();
const {plugin, options, context} = await loadSite();
const mock = jest
.spyOn(cliDocs, 'cliDocsVersionCommand')
.mockImplementation(async () => {});
@ -256,12 +254,7 @@ describe('simple website', () => {
plugin.extendCli!(cli);
cli.parse(['node', 'test', 'docs:version', '1.0.0']);
expect(mock).toHaveBeenCalledTimes(1);
expect(mock).toHaveBeenCalledWith('1.0.0', siteDir, DEFAULT_PLUGIN_ID, {
path: 'docs',
sidebarPath,
sidebarCollapsed: true,
sidebarCollapsible: true,
});
expect(mock).toHaveBeenCalledWith('1.0.0', options, context);
mock.mockRestore();
});
@ -344,29 +337,28 @@ describe('versioned website', () => {
const context = await loadContext({siteDir});
const sidebarPath = path.join(siteDir, 'sidebars.json');
const routeBasePath = 'docs';
const plugin = await pluginContentDocs(
context,
validateOptions({
validate: normalizePluginOptions,
options: {
routeBasePath,
sidebarPath,
},
}),
);
const options = validateOptions({
validate: normalizePluginOptions,
options: {
routeBasePath,
sidebarPath,
},
});
const plugin = await pluginContentDocs(context, options);
const pluginContentDir = path.join(context.generatedFilesDir, plugin.name);
return {
siteDir,
context,
routeBasePath,
sidebarPath,
options,
plugin,
pluginContentDir,
};
}
it('extendCli - docsVersion', async () => {
const {siteDir, routeBasePath, sidebarPath, plugin} = await loadSite();
const {plugin, context, options} = await loadSite();
const mock = jest
.spyOn(cliDocs, 'cliDocsVersionCommand')
.mockImplementation(async () => {});
@ -376,12 +368,7 @@ describe('versioned website', () => {
plugin.extendCli!(cli);
cli.parse(['node', 'test', 'docs:version', '2.0.0']);
expect(mock).toHaveBeenCalledTimes(1);
expect(mock).toHaveBeenCalledWith('2.0.0', siteDir, DEFAULT_PLUGIN_ID, {
path: routeBasePath,
sidebarPath,
sidebarCollapsed: true,
sidebarCollapsible: true,
});
expect(mock).toHaveBeenCalledWith('2.0.0', options, context);
mock.mockRestore();
});
@ -474,18 +461,16 @@ describe('versioned website (community)', () => {
const sidebarPath = path.join(siteDir, 'community_sidebars.json');
const routeBasePath = 'community';
const pluginId = 'community';
const plugin = await pluginContentDocs(
context,
validateOptions({
validate: normalizePluginOptions,
options: {
id: 'community',
path: 'community',
routeBasePath,
sidebarPath,
},
}),
);
const options = validateOptions({
validate: normalizePluginOptions,
options: {
id: 'community',
path: 'community',
routeBasePath,
sidebarPath,
},
});
const plugin = await pluginContentDocs(context, options);
const pluginContentDir = path.join(context.generatedFilesDir, plugin.name);
return {
siteDir,
@ -493,14 +478,14 @@ describe('versioned website (community)', () => {
routeBasePath,
sidebarPath,
pluginId,
options,
plugin,
pluginContentDir,
};
}
it('extendCli - docsVersion', async () => {
const {siteDir, routeBasePath, sidebarPath, pluginId, plugin} =
await loadSite();
const {pluginId, plugin, options, context} = await loadSite();
const mock = jest
.spyOn(cliDocs, 'cliDocsVersionCommand')
.mockImplementation(async () => {});
@ -510,12 +495,7 @@ describe('versioned website (community)', () => {
plugin.extendCli!(cli);
cli.parse(['node', 'test', `docs:version:${pluginId}`, '2.0.0']);
expect(mock).toHaveBeenCalledTimes(1);
expect(mock).toHaveBeenCalledWith('2.0.0', siteDir, pluginId, {
path: routeBasePath,
sidebarPath,
sidebarCollapsed: true,
sidebarCollapsible: true,
});
expect(mock).toHaveBeenCalledWith('2.0.0', options, context);
mock.mockRestore();
});