Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

3 changes: 0 additions & 3 deletions packages/docusaurus-utils-validation/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@
"lodash": "^4.17.21",
"tslib": "^2.6.0"
},
"devDependencies": {
"tmp-promise": "^3.0.3"
},
"engines": {
"node": ">=24.14"
}
Expand Down
53 changes: 32 additions & 21 deletions packages/docusaurus-utils-validation/src/__tests__/tagsFile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
import {describe, expect, it} from 'vitest';
import * as path from 'path';
import * as fs from 'fs-extra';
import * as tmp from 'tmp-promise';
import {mkdtempDisposable, realpath} from 'node:fs/promises';
import {tmpdir} from 'node:os';
import {join} from 'node:path';
import * as YAML from 'js-yaml';
import {
ensureUniquePermalinks,
Expand Down Expand Up @@ -291,25 +293,24 @@ describe('getTagsFile', () => {
}: {
filePath: string;
tagsFileInput: TagsFileInput;
}): Promise<{dir: string}> {
async function createTmpDir() {
return (
await tmp.dir({
prefix: 'jest-createTmpSiteDir',
})
).path;
}
const contentPath = await createTmpDir();
}): Promise<{dir: string} & AsyncDisposable> {
const tmpDir = await mkdtempDisposable(
join(await realpath(tmpdir()), 'docusaurus-tmp-'),
);
const contentPath = tmpDir.path;
const finalFilePath = path.join(contentPath, filePath);
const fileContent = YAML.dump(tagsFileInput);
await fs.writeFile(finalFilePath, fileContent);
return {dir: contentPath};
return {
dir: contentPath,
[Symbol.asyncDispose]: tmpDir[Symbol.asyncDispose],
};
}

type Params = Parameters<typeof getTagsFile>[0];

it('reads tags file - regular', async () => {
const {dir} = await createTestTagsFile({
await using tagsFile = await createTestTagsFile({
filePath: 'tags.yml',
tagsFileInput: {
tag1: {label: 'Tag1 Label'},
Expand All @@ -321,6 +322,7 @@ describe('getTagsFile', () => {
},
},
});
const {dir} = tagsFile;

const params: Params = {
contentPaths: {contentPath: dir, contentPathLocalized: dir},
Expand Down Expand Up @@ -349,12 +351,13 @@ describe('getTagsFile', () => {
});

it('reads tags file - only keys', async () => {
const {dir} = await createTestTagsFile({
await using tagsFile = await createTestTagsFile({
filePath: 'tags.yml',
tagsFileInput: {
tagKey: null,
},
});
const {dir} = tagsFile;

const params: Params = {
contentPaths: {contentPath: dir, contentPathLocalized: dir},
Expand All @@ -373,12 +376,13 @@ describe('getTagsFile', () => {
});

it('reads tags file - tags option undefined', async () => {
const {dir} = await createTestTagsFile({
await using tagsFile = await createTestTagsFile({
filePath: 'tags.yml',
tagsFileInput: {
tag: {label: 'tag label'},
},
});
const {dir} = tagsFile;

const params: Params = {
contentPaths: {contentPath: dir, contentPathLocalized: dir},
Expand All @@ -397,10 +401,11 @@ describe('getTagsFile', () => {
});

it('reads tags file - empty file', async () => {
const {dir} = await createTestTagsFile({
await using tagsFile = await createTestTagsFile({
filePath: 'tags.yml',
tagsFileInput: {},
});
const {dir} = tagsFile;

const params: Params = {
contentPaths: {contentPath: dir, contentPathLocalized: dir},
Expand All @@ -411,19 +416,21 @@ describe('getTagsFile', () => {
});

it('reads tags file - prioritizes reading from localized content path', async () => {
const {dir} = await createTestTagsFile({
await using tagsFile = await createTestTagsFile({
filePath: 'tags.yml',
tagsFileInput: {
tag: {label: 'tag label'},
},
});
const {dir} = tagsFile;

const {dir: dirLocalized} = await createTestTagsFile({
await using tagsFileLocalized = await createTestTagsFile({
filePath: 'tags.yml',
tagsFileInput: {
tag: {label: 'tag label (localized)'},
},
});
const {dir: dirLocalized} = tagsFileLocalized;

const params: Params = {
contentPaths: {contentPath: dir, contentPathLocalized: dirLocalized},
Expand All @@ -442,12 +449,13 @@ describe('getTagsFile', () => {
});

it('reads tags file - custom tags file path', async () => {
const {dir} = await createTestTagsFile({
await using tagsFile = await createTestTagsFile({
filePath: 'custom-tags-path.yml',
tagsFileInput: {
tag: {label: 'tag label'},
},
});
const {dir} = tagsFile;

const params: Params = {
contentPaths: {contentPath: dir, contentPathLocalized: dir},
Expand All @@ -466,13 +474,14 @@ describe('getTagsFile', () => {
});

it('throws if duplicate permalink', async () => {
const {dir} = await createTestTagsFile({
await using tagsFile = await createTestTagsFile({
filePath: 'tags.yml',
tagsFileInput: {
tag1: {permalink: '/duplicate'},
tag2: {permalink: '/duplicate'},
},
});
const {dir} = tagsFile;

const params: Params = {
contentPaths: {contentPath: dir, contentPathLocalized: dir},
Expand Down Expand Up @@ -500,12 +509,13 @@ describe('getTagsFile', () => {
});

it('does not read tags file - tags option null/false', async () => {
const {dir} = await createTestTagsFile({
await using tagsFile = await createTestTagsFile({
filePath: 'tags.yml',
tagsFileInput: {
tag: {label: 'tag label'},
},
});
const {dir} = tagsFile;

await expect(
getTagsFile({
Expand All @@ -522,12 +532,13 @@ describe('getTagsFile', () => {
});

it('does not read tags file - tags files has non-default name', async () => {
const {dir} = await createTestTagsFile({
await using tagsFile = await createTestTagsFile({
filePath: 'bad-tags-file-name.yml',
tagsFileInput: {
tag: {label: 'tag label'},
},
});
const {dir} = tagsFile;

const params: Params = {
contentPaths: {contentPath: dir, contentPathLocalized: dir},
Expand Down
3 changes: 1 addition & 2 deletions packages/docusaurus-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
"@types/dedent": "^0.7.0",
"@types/micromatch": "^4.0.10",
"@types/react-dom": "^19.2.3",
"dedent": "^0.7.0",
"tmp-promise": "^3.0.3"
"dedent": "^0.7.0"
}
}
17 changes: 8 additions & 9 deletions packages/docusaurus-utils/src/__tests__/moduleUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,21 @@
import {describe, expect, it} from 'vitest';
import fs from 'fs-extra';
import path from 'path';
import tmp from 'tmp-promise';
import {mkdtempDisposable, realpath} from 'node:fs/promises';
import {join} from 'node:path';
import {tmpdir} from 'node:os';
import dedent from 'dedent';
import {loadFreshModule} from '../moduleUtils';

async function createTmpDir() {
return (
await tmp.dir({
prefix: 'jest-tmp-moduleUtils-tests',
})
).path;
return mkdtempDisposable(join(await realpath(tmpdir()), 'docusaurus-tmp-'));
}

async function moduleGraphHelpers() {
const dir = await createTmpDir();

async function fileHelper(name: string, initialContent?: string) {
const filePath = path.resolve(dir, name);
const filePath = path.resolve(dir.path, name);
if (initialContent) {
await fs.outputFile(filePath, initialContent);
}
Expand All @@ -35,7 +33,7 @@ async function moduleGraphHelpers() {
};
}

return {fileHelper};
return {fileHelper, [Symbol.asyncDispose]: dir[Symbol.asyncDispose]};
}

async function loadModule(modulePath: string, withDefault: boolean) {
Expand Down Expand Up @@ -136,7 +134,8 @@ describe('loadFreshModule', () => {

describe('module graph', () => {
it('can load and reload fresh module graph', async () => {
const {fileHelper} = await moduleGraphHelpers();
await using helpers = await moduleGraphHelpers();
const {fileHelper} = helpers;

const dependency1 = await fileHelper(
'dependency1.js',
Expand Down
4 changes: 2 additions & 2 deletions packages/docusaurus-utils/src/urlUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,8 @@ export function fileToPath(file: string): string {
* Similar to `encodeURI`, but uses `encodeURIComponent` and assumes there's no
* query.
*
* `encodeURI("/question?/answer")` => `"/question?/answer#section"`;
* `encodePath("/question?/answer#section")` => `"/question%3F/answer%23foo"`
* `encodeURI("/question?/answer")` => `"/question?/answer"`;
* `encodePath("/question?/answer#section")` => `"/question%3F/answer%23section"`
*/
export function encodePath(userPath: string): string {
return userPath
Expand Down
1 change: 0 additions & 1 deletion packages/docusaurus/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@
"@types/update-notifier": "^6.0.4",
"@types/webpack-bundle-analyzer": "^4.7.0",
"@types/webpack-env": "^1.18.8",
"tmp-promise": "^3.0.3",
"tree-node-cli": "^1.6.0"
},
"peerDependencies": {
Expand Down
Loading
Loading