Skip to content

Commit

Permalink
added normalize
Browse files Browse the repository at this point in the history
  • Loading branch information
vishvamsinh28 committed Oct 18, 2024
1 parent 0c957d3 commit 7e7b82b
Showing 1 changed file with 26 additions and 25 deletions.
51 changes: 26 additions & 25 deletions tests/build-post-list.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { existsSync, readFileSync, writeFileSync, mkdirSync, rmSync } = require('fs');
const { join, resolve } = require('path');
const { join, resolve, normalize } = require('path');
const { buildPostList, slugifyToC } = require('../scripts/build-post-list');
const os = require('os');

Expand All @@ -11,25 +11,25 @@ describe('buildPostList', () => {

beforeEach(() => {
tempDir = resolve(__dirname, 'test-config');
writeFilePath = join(tempDir, 'posts.json');
writeFilePath = normalize(join(tempDir, 'posts.json'));
postDirectories = [
[join(tempDir, 'blog'), '/blog'],
[join(tempDir, 'docs'), '/docs'],
[join(tempDir, 'about'), '/about'],
[normalize(join(tempDir, 'blog')), '/blog'],
[normalize(join(tempDir, 'docs')), '/docs'],
[normalize(join(tempDir, 'about')), '/about'],
];

mkdirSync(tempDir, { recursive: true });

mkdirSync(join(tempDir, 'blog'), { recursive: true });
writeFileSync(join(tempDir, 'blog', 'release-notes-2.1.0.mdx'), '---' + lineBreak + 'title: Release Notes 2.1.0' + lineBreak + '---' + lineBreak + 'This is a release note.');
mkdirSync(normalize(join(tempDir, 'blog')), { recursive: true });
writeFileSync(normalize(join(tempDir, 'blog', 'release-notes-2.1.0.mdx')), '---' + lineBreak + 'title: Release Notes 2.1.0' + lineBreak + '---' + lineBreak + 'This is a release note.');

mkdirSync(join(tempDir, 'docs'), { recursive: true });
writeFileSync(join(tempDir, 'docs', 'index.mdx'), '---' + lineBreak + 'title: Docs Home' + lineBreak + '---' + lineBreak + 'This is the documentation homepage.');
mkdirSync(normalize(join(tempDir, 'docs')), { recursive: true });
writeFileSync(normalize(join(tempDir, 'docs', 'index.mdx')), '---' + lineBreak + 'title: Docs Home' + lineBreak + '---' + lineBreak + 'This is the documentation homepage.');

mkdirSync(join(tempDir, 'about'), { recursive: true });
writeFileSync(join(tempDir, 'about', 'index.mdx'), '---' + lineBreak + 'title: About Us' + lineBreak + '---' + lineBreak + 'This is the about page.');
mkdirSync(normalize(join(tempDir, 'about')), { recursive: true });
writeFileSync(normalize(join(tempDir, 'about', 'index.mdx')), '---' + lineBreak + 'title: About Us' + lineBreak + '---' + lineBreak + 'This is the about page.');

mkdirSync(join(tempDir, 'docs', 'reference', 'specification'), { recursive: true });
mkdirSync(normalize(join(tempDir, 'docs', 'reference', 'specification')), { recursive: true });
});

afterEach(() => {
Expand All @@ -43,20 +43,22 @@ describe('buildPostList', () => {
expect(outputExists).toBe(true);

const output = JSON.parse(readFileSync(writeFilePath, 'utf-8'));
console.log(output)

expect(output).toHaveProperty('docs');
expect(output).toHaveProperty('blog');
expect(output).toHaveProperty('about');
expect(output).toHaveProperty('docsTree');

const blogEntry = output.blog.find(item => item.slug === '/blog/release-notes-2.1.0');
console.log(blogEntry)
expect(blogEntry).toBeDefined();
expect(blogEntry.title).toBe('Release Notes 2.1.0');
});

it('handles a directory with only section files', async () => {
mkdirSync(join(tempDir, 'docs', 'section1'), { recursive: true });
writeFileSync(join(tempDir, 'docs', 'section1', '_section.mdx'), '---' + lineBreak + 'title: Section 1' + lineBreak + '---' + lineBreak + 'This is section 1.');
mkdirSync(normalize(join(tempDir, 'docs', 'section1')), { recursive: true });
writeFileSync(normalize(join(tempDir, 'docs', 'section1', '_section.mdx')), '---' + lineBreak + 'title: Section 1' + lineBreak + '---' + lineBreak + 'This is section 1.');

await buildPostList(postDirectories, tempDir, writeFilePath);

Expand All @@ -67,7 +69,7 @@ describe('buildPostList', () => {
});

it('handles multiple release notes correctly', async () => {
writeFileSync(join(tempDir, 'blog', 'release-notes-2.1.1.mdx'), '---' + lineBreak + 'title: Release Notes 2.1.1' + lineBreak + '---' + lineBreak + 'This is a release note.');
writeFileSync(normalize(join(tempDir, 'blog', 'release-notes-2.1.1.mdx')), '---' + lineBreak + 'title: Release Notes 2.1.1' + lineBreak + '---' + lineBreak + 'This is a release note.');

await buildPostList(postDirectories, tempDir, writeFilePath);

Expand All @@ -84,7 +86,7 @@ describe('buildPostList', () => {
});

it('handles errors gracefully', async () => {
const invalidDir = [join(tempDir, 'non-existent-dir'), '/invalid'];
const invalidDir = [normalize(join(tempDir, 'non-existent-dir')), '/invalid'];
await expect(buildPostList([invalidDir], tempDir, writeFilePath)).rejects.toThrow();
});

Expand All @@ -103,9 +105,9 @@ describe('buildPostList', () => {
});

it('does not process specification files without a title', async () => {
const specDir = join(tempDir, 'docs', 'reference', 'specification');
const specDir = normalize(join(tempDir, 'docs', 'reference', 'specification'));
writeFileSync(
join(specDir, 'v2.1.0-no-title.mdx'),
normalize(join(specDir, 'v2.1.0-no-title.mdx')),
'---' + lineBreak + '---' + lineBreak + 'Content of specification without a title.'
);

Expand All @@ -118,9 +120,9 @@ describe('buildPostList', () => {
});

it('does not process specification files with "next-spec" in the filename', async () => {
const specDir = join(tempDir, 'docs', 'reference', 'specification');
const specDir = normalize(join(tempDir, 'docs', 'reference', 'specification'));
writeFileSync(
join(specDir, 'v2.1.0-next-spec.1.mdx'),
normalize(join(specDir, 'v2.1.0-next-spec.1.mdx')),
'---' + lineBreak + '---' + lineBreak + 'Content of pre-release specification v2.1.0-next-spec.1.'
);

Expand All @@ -133,9 +135,9 @@ describe('buildPostList', () => {
});

it('does not process specification files with "explorer" in the filename', async () => {
const specDir = join(tempDir, 'docs', 'reference', 'specification');
const specDir = normalize(join(tempDir, 'docs', 'reference', 'specification'));
writeFileSync(
join(specDir, 'explorer.mdx'),
normalize(join(specDir, 'explorer.mdx')),
'---' + lineBreak + '---' + lineBreak + 'Content of explorer specification.'
);

Expand All @@ -148,7 +150,7 @@ describe('buildPostList', () => {
});

it('throws an error if the directory cannot be read', async () => {
const invalidDir = [join(tempDir, 'non-existent-dir'), '/invalid'];
const invalidDir = [normalize(join(tempDir, 'non-existent-dir')), '/invalid'];

let error;
try {
Expand All @@ -162,7 +164,7 @@ describe('buildPostList', () => {
});

it('throws an error if the front matter cannot be parsed', async () => {
writeFileSync(join(tempDir, 'docs', 'invalid.mdx'), '---' + lineBreak + 'invalid front matter' + lineBreak + '---' + lineBreak + 'Content');
writeFileSync(normalize(join(tempDir, 'docs', 'invalid.mdx')), '---' + lineBreak + 'invalid front matter' + lineBreak + '---' + lineBreak + 'Content');

let error;
try {
Expand All @@ -187,5 +189,4 @@ describe('buildPostList', () => {
expect(error).toBeDefined();
expect(error.message).toMatch(/Error while building post list/);
});

});

0 comments on commit 7e7b82b

Please sign in to comment.