mirror of
https://github.com/facebook/docusaurus.git
synced 2025-04-29 02:08:36 +02:00
309 lines
11 KiB
TypeScript
309 lines
11 KiB
TypeScript
/**
|
||
* Copyright (c) Facebook, Inc. and its affiliates.
|
||
*
|
||
* This source code is licensed under the MIT license found in the
|
||
* LICENSE file in the root directory of this source tree.
|
||
*/
|
||
|
||
import {jest} from '@jest/globals';
|
||
import fs from 'fs-extra';
|
||
import path from 'path';
|
||
import {DEFAULT_PLUGIN_ID} from '@docusaurus/utils';
|
||
import {cliDocsVersionCommand} from '../cli';
|
||
import {
|
||
getVersionDocsDirPath,
|
||
getVersionsFilePath,
|
||
getVersionSidebarsPath,
|
||
} from '../versions/files';
|
||
import type {PluginOptions} from '@docusaurus/plugin-content-docs';
|
||
|
||
const fixtureDir = path.join(__dirname, '__fixtures__');
|
||
|
||
describe('docsVersion', () => {
|
||
const simpleSiteDir = path.join(fixtureDir, 'simple-site');
|
||
const versionedSiteDir = path.join(fixtureDir, 'versioned-site');
|
||
|
||
const DEFAULT_OPTIONS: PluginOptions = {
|
||
id: 'default',
|
||
path: 'docs',
|
||
sidebarPath: '',
|
||
sidebarCollapsed: true,
|
||
sidebarCollapsible: true,
|
||
};
|
||
|
||
it('no version tag provided', async () => {
|
||
await expect(() =>
|
||
cliDocsVersionCommand(null, DEFAULT_OPTIONS, {siteDir: simpleSiteDir}),
|
||
).rejects.toThrowErrorMatchingInlineSnapshot(
|
||
`"Versions should be strings. Found type "object" for version "null"."`,
|
||
);
|
||
await expect(() =>
|
||
cliDocsVersionCommand(undefined, DEFAULT_OPTIONS, {
|
||
siteDir: simpleSiteDir,
|
||
}),
|
||
).rejects.toThrowErrorMatchingInlineSnapshot(
|
||
`"Versions should be strings. Found type "undefined" for version "undefined"."`,
|
||
);
|
||
await expect(() =>
|
||
cliDocsVersionCommand('', DEFAULT_OPTIONS, {siteDir: simpleSiteDir}),
|
||
).rejects.toThrowErrorMatchingInlineSnapshot(
|
||
`"Invalid version name "": version name must contain at least one non-whitespace character."`,
|
||
);
|
||
});
|
||
|
||
it('version tag should not have slash', async () => {
|
||
await expect(() =>
|
||
cliDocsVersionCommand('foo/bar', DEFAULT_OPTIONS, {
|
||
siteDir: simpleSiteDir,
|
||
}),
|
||
).rejects.toThrowError(
|
||
'Invalid version name "foo/bar": version name should not include slash (/) or backslash (\\).',
|
||
);
|
||
await expect(() =>
|
||
cliDocsVersionCommand('foo\\bar', DEFAULT_OPTIONS, {
|
||
siteDir: simpleSiteDir,
|
||
}),
|
||
).rejects.toThrowError(
|
||
'Invalid version name "foo\\bar": version name should not include slash (/) or backslash (\\).',
|
||
);
|
||
});
|
||
|
||
it('version tag should not be too long', async () => {
|
||
await expect(() =>
|
||
cliDocsVersionCommand('a'.repeat(255), DEFAULT_OPTIONS, {
|
||
siteDir: simpleSiteDir,
|
||
}),
|
||
).rejects.toThrowErrorMatchingInlineSnapshot(
|
||
`"Invalid version name "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa": version name cannot be longer than 32 characters."`,
|
||
);
|
||
});
|
||
|
||
it('version tag should not be a dot or two dots', async () => {
|
||
await expect(() =>
|
||
cliDocsVersionCommand('..', DEFAULT_OPTIONS, {siteDir: simpleSiteDir}),
|
||
).rejects.toThrowErrorMatchingInlineSnapshot(
|
||
`"Invalid version name "..": version name should not be "." or ".."."`,
|
||
);
|
||
await expect(() =>
|
||
cliDocsVersionCommand('.', DEFAULT_OPTIONS, {siteDir: simpleSiteDir}),
|
||
).rejects.toThrowErrorMatchingInlineSnapshot(
|
||
`"Invalid version name ".": version name should not be "." or ".."."`,
|
||
);
|
||
});
|
||
|
||
it('version tag should be a valid pathname', async () => {
|
||
await expect(() =>
|
||
cliDocsVersionCommand('<foo|bar>', DEFAULT_OPTIONS, {
|
||
siteDir: simpleSiteDir,
|
||
}),
|
||
).rejects.toThrowErrorMatchingInlineSnapshot(
|
||
`"Invalid version name "<foo|bar>": version name should be a valid file path."`,
|
||
);
|
||
await expect(() =>
|
||
cliDocsVersionCommand('foo\x00bar', DEFAULT_OPTIONS, {
|
||
siteDir: simpleSiteDir,
|
||
}),
|
||
).rejects.toThrowErrorMatchingInlineSnapshot(
|
||
`"Invalid version name "foo |