Skip to content

Commit

Permalink
fix: issues suggested by coderabbitai
Browse files Browse the repository at this point in the history
  • Loading branch information
Aditya0733 committed Dec 16, 2024
1 parent 75b8d02 commit 42fe248
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 49 deletions.
20 changes: 5 additions & 15 deletions scripts/markdown/check-markdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ function getConcurrencyLimit() {
*/
function isValidURL(str) {
try {
URL(str);
new URL(str);
return true;
} catch (err) {
return false;
Expand Down Expand Up @@ -142,12 +142,7 @@ function validateDocs(frontmatter) {
* @param {string} [relativePath=''] - The relative path of the folder for logging purposes.
* @param {import('p-limit').default} limit - Concurrency limiter.
*/
async function checkMarkdownFiles(
folderPath,
validateFunction,
limit,
relativePath = ''
) {
async function checkMarkdownFiles(folderPath, validateFunction, limit, relativePath = '') {
try {
const files = await fs.readdir(folderPath);
const filePromises = files.map(async (file) => {
Expand All @@ -163,14 +158,9 @@ async function checkMarkdownFiles(

// Recurse if directory, otherwise validate markdown file
if (stats.isDirectory()) {
await checkMarkdownFiles(
filePath,
validateFunction,
limit,
relativeFilePath
);
await checkMarkdownFiles(filePath, validateFunction, limit, relativeFilePath);
} else if (path.extname(file) === '.md') {
try{
try {
await limit(async () => {
const fileContent = await fs.readFile(filePath, 'utf-8');
const { data: frontmatter } = matter(fileContent);
Expand All @@ -182,7 +172,7 @@ async function checkMarkdownFiles(
process.exitCode = 1;
}
});
}catch (error) {
} catch (error) {
console.error(`Error processing file ${relativeFilePath}:`, error);
throw error;
}
Expand Down
64 changes: 30 additions & 34 deletions tests/markdown/check-markdown.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,38 +70,40 @@ describe('Frontmatter Validator', () => {
});

it('respects concurrency limit during file processing', async () => {
const processingTimes = [];
const mockValidateFunction = jest.fn().mockImplementation(() => {
const startTime = Date.now();
processingTimes.push(startTime);
// Simulate some processing time
return new Promise(resolve => setTimeout(resolve, 50));
let activeCount = 0;
let maxActiveCount = 0;
const mockValidateFunction = jest.fn().mockImplementation(async () => {
activeCount++;
try {
// Track the maximum number of concurrent executions
if (activeCount > maxActiveCount) {
maxActiveCount = activeCount;
}

// Simulate some processing time
await new Promise(resolve => setTimeout(resolve, 50));
} finally {
activeCount--;
}
});

// Create multiple test files
const writePromises = [];
for (let i = 0; i < 20; i++) {
await fs.writeFile(
path.join(tempDir, `test${i}.md`),
'---\ntitle: Test\n---'
writePromises.push(
fs.writeFile(
path.join(tempDir, `test${i}.md`),
'---\ntitle: Test\n---'
)
);
}

await Promise.all(writePromises);

const limit = pLimit(5); // Set limit to 5
await checkMarkdownFiles(tempDir, mockValidateFunction, '', limit);

// Group processing times by 5 (our limit) and verify gaps between groups
const sortedTimes = processingTimes.sort();
const groups = [];
for (let i = 0; i < sortedTimes.length; i += 5) {
groups.push(sortedTimes.slice(i, i + 5));
}

// Verify that each group of 5 started processing together
groups.forEach(group => {
const concurrentExecutions = mockValidateFunction.mock.calls.filter(call => Math.abs(call[0] - group[0]) < 10).length;
expect(concurrentExecutions).toBeLessThanOrEqual(5);
});


// Verify that the maximum number of concurrent executions never exceeds the limit
expect(maxActiveCount).toBeLessThanOrEqual(5);

// Verify that the mock validate function was called for all files
expect(mockValidateFunction).toHaveBeenCalledTimes(20);
});
Expand All @@ -114,11 +116,7 @@ describe('Frontmatter Validator', () => {
type: 'blog',
tags: ['test'],
cover: 'cover.jpg',
authors: [
{ name: 'John' },
{ photo: 'jane.jpg' },
{ name: 'Bob', photo: 'bob.jpg', link: 'not-a-url' },
],
authors: [{ name: 'John' }, { photo: 'jane.jpg' }, { name: 'Bob', photo: 'bob.jpg', link: 'not-a-url' }]
};

const errors = validateBlogs(frontmatter);
Expand Down Expand Up @@ -148,9 +146,7 @@ describe('Frontmatter Validator', () => {

await checkMarkdownFiles(tempDir, validateBlogs, '', pLimit(10));

expect(mockConsoleLog).toHaveBeenCalledWith(
expect.stringContaining('Errors in file invalid.md:'),
);
expect(mockConsoleLog).toHaveBeenCalledWith(expect.stringContaining('Errors in file invalid.md:'));
mockConsoleLog.mockRestore();
});

Expand Down

0 comments on commit 42fe248

Please sign in to comment.