Skip to content

Commit

Permalink
fix: sort challengeOrder when repairing meta (freeCodeCamp#53037)
Browse files Browse the repository at this point in the history
  • Loading branch information
ojeytonwilliams authored Jan 11, 2024
1 parent 576bc66 commit efb6398
Show file tree
Hide file tree
Showing 8 changed files with 342 additions and 329 deletions.
3 changes: 2 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ module.exports = {
roots: ['.', './client', './api-server'],
transformIgnorePatterns: ['node_modules/.pnpm/(?!(nanoid|uuid)@)'],
setupFilesAfterEnv: ['./jest.setup.js'],
testEnvironment: 'jsdom'
testEnvironment: 'jsdom',
watchPathIgnorePatterns: ['<rootDir>/__fixtures__.*']
};
23 changes: 21 additions & 2 deletions tools/challenge-helper-scripts/commands.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import fs from 'fs';
import { getProjectPath } from './helpers/get-project-info';
import { getMetaData } from './helpers/project-metadata';
import { getMetaData, updateMetaData } from './helpers/project-metadata';
import { getChallengeOrderFromFileTree } from './helpers/get-challenge-order';
import {
createStepFile,
deleteStepFromMeta,
Expand Down Expand Up @@ -70,4 +71,22 @@ function createEmptySteps(num: number): void {
console.log(`Successfully added ${num} steps`);
}

export { deleteStep, insertStep, createEmptySteps };
const repairMeta = async () => {
const sortByStepNum = (a: string, b: string) =>
parseInt(a.split(' ')[1]) - parseInt(b.split(' ')[1]);

const challengeOrder = await getChallengeOrderFromFileTree();
if (!challengeOrder.every(({ title }) => /Step \d+/.test(title))) {
throw new Error(
'You can only run this command on project-based blocks with step files.'
);
}
const sortedChallengeOrder = challengeOrder.sort((a, b) =>
sortByStepNum(a.title, b.title)
);
const meta = getMetaData();
meta.challengeOrder = sortedChallengeOrder;
updateMetaData(meta);
};

export { deleteStep, insertStep, createEmptySteps, repairMeta };
180 changes: 83 additions & 97 deletions tools/challenge-helper-scripts/helpers/get-challenge-order.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,120 +6,106 @@ import {
getChallengeOrderFromMeta
} from './get-challenge-order';

const metaPath = join(
const basePath = join(
process.cwd(),
'curriculum',
'challenges',
'_meta',
'project'
'__fixtures__' + process.env.JEST_WORKER_ID
);
const commonPath = join(basePath, 'curriculum', 'challenges');

const block = 'project-get-challenge-order';
const metaPath = join(commonPath, '_meta', block);
const superBlockPath = join(
process.cwd(),
'curriculum',
'challenges',
commonPath,
'english',
'superblock'
'superblock-get-challenge-order'
);
const projectPath = join(superBlockPath, 'project');
const projectPath = join(superBlockPath, block);

const cleanFiles = () => {
try {
fs.rmSync(superBlockPath, { recursive: true });
} catch (err) {
console.log('Could not remove superblock mock folder. ');
}
try {
fs.rmSync(metaPath, { recursive: true });
} catch (err) {
console.log('Could not remove meta mock folder.');
}
};

describe('getChallengeOrderFromMeta helper', () => {
describe('get-challenge-order helper', () => {
beforeEach(() => {
fs.mkdirSync(superBlockPath);
fs.mkdirSync(projectPath);
fs.mkdirSync(metaPath);
fs.writeFileSync(
join(projectPath, 'this-is-a-challenge.md'),
'---\nid: 1\ntitle: This is a Challenge\n---',
'utf-8'
);
fs.writeFileSync(
join(projectPath, 'what-a-cool-thing.md'),
'---\nid: 100\ntitle: What a Cool Thing\n---',
'utf-8'
);
fs.writeFileSync(
join(projectPath, 'i-dunno.md'),
'---\nid: 2\ntitle: I Dunno\n---'
);
fs.writeFileSync(
join(metaPath, 'meta.json'),
`{
fs.mkdirSync(superBlockPath, { recursive: true });
fs.mkdirSync(projectPath, { recursive: true });
fs.mkdirSync(metaPath, { recursive: true });
});
describe('getChallengeOrderFromMeta helper', () => {
beforeEach(() => {
fs.writeFileSync(
join(projectPath, 'this-is-a-challenge.md'),
'---\nid: 1\ntitle: This is a Challenge\n---',
'utf-8'
);
fs.writeFileSync(
join(projectPath, 'what-a-cool-thing.md'),
'---\nid: 100\ntitle: What a Cool Thing\n---',
'utf-8'
);
fs.writeFileSync(
join(projectPath, 'i-dunno.md'),
'---\nid: 2\ntitle: I Dunno\n---'
);
fs.writeFileSync(
join(metaPath, 'meta.json'),
`{
"id": "mock-id",
"challengeOrder": [{"id": "1", "title": "This title is wrong"}, {"id": "2", "title": "I Dunno"}, {"id": "100", "title": "What a Cool Thing"}]}`,
'utf-8'
);
});
'utf-8'
);
});

it('should load the file order', () => {
process.env.CALLING_DIR = projectPath;
const challengeOrder = getChallengeOrderFromMeta();
expect(challengeOrder).toEqual([
{ id: '1', title: 'This title is wrong' },
{ id: '2', title: 'I Dunno' },
{ id: '100', title: 'What a Cool Thing' }
]);
it('should load the file order', () => {
process.env.CALLING_DIR = projectPath;
const challengeOrder = getChallengeOrderFromMeta();
expect(challengeOrder).toEqual([
{ id: '1', title: 'This title is wrong' },
{ id: '2', title: 'I Dunno' },
{ id: '100', title: 'What a Cool Thing' }
]);
});
});

afterEach(() => {
delete process.env.CALLING_DIR;
cleanFiles();
});
});

describe('getChallengeOrderFromFileTree helper', () => {
beforeEach(() => {
fs.mkdirSync(superBlockPath);
fs.mkdirSync(projectPath);
fs.mkdirSync(metaPath);
fs.writeFileSync(
join(projectPath, 'step-001.md'),
'---\nid: a8d97bd4c764e91f9d2bda01\ntitle: Step 1\n---',
'utf-8'
);
fs.writeFileSync(
join(projectPath, 'step-002.md'),
'---\nid: a6b0bb188d873cb2c8729495\ntitle: Step 2\n---',
'utf-8'
);
fs.writeFileSync(
join(projectPath, 'step-003.md'),
'---\nid: a5de63ebea8dbee56860f4f2\ntitle: Step 3\n---'
);
fs.writeFileSync(
join(metaPath, 'meta.json'),
`{
describe('getChallengeOrderFromFileTree helper', () => {
beforeEach(() => {
fs.writeFileSync(
join(projectPath, 'step-001.md'),
'---\nid: a8d97bd4c764e91f9d2bda01\ntitle: Step 1\n---',
'utf-8'
);
fs.writeFileSync(
join(projectPath, 'step-002.md'),
'---\nid: a6b0bb188d873cb2c8729495\ntitle: Step 2\n---',
'utf-8'
);
fs.writeFileSync(
join(projectPath, 'step-003.md'),
'---\nid: a5de63ebea8dbee56860f4f2\ntitle: Step 3\n---'
);
fs.writeFileSync(
join(metaPath, 'meta.json'),
`{
"id": "mock-id",
"challengeOrder": [{"id": "a8d97bd4c764e91f9d2bda01", "title": "Step 1"}, {"id": "a6b0bb188d873cb2c8729495", "title": "Step 3"}, {"id": "a5de63ebea8dbee56860f4f2", "title": "Step 2"}]}`,
'utf-8'
);
});
'utf-8'
);
});

it('should load the file order', async () => {
expect.assertions(1);
process.env.CALLING_DIR = projectPath;
const challengeOrder = await getChallengeOrderFromFileTree();
expect(challengeOrder).toEqual([
{ id: 'a8d97bd4c764e91f9d2bda01', title: 'Step 1' },
{ id: 'a6b0bb188d873cb2c8729495', title: 'Step 2' },
{ id: 'a5de63ebea8dbee56860f4f2', title: 'Step 3' }
]);
it('should load the file order', async () => {
expect.assertions(1);
process.env.CALLING_DIR = projectPath;
const challengeOrder = await getChallengeOrderFromFileTree();
expect(challengeOrder).toEqual([
{ id: 'a8d97bd4c764e91f9d2bda01', title: 'Step 1' },
{ id: 'a6b0bb188d873cb2c8729495', title: 'Step 2' },
{ id: 'a5de63ebea8dbee56860f4f2', title: 'Step 3' }
]);
});
});

afterEach(() => {
cleanFiles();
delete process.env.CALLING_DIR;
try {
fs.rmSync(basePath, { recursive: true });
} catch (err) {
console.log(err);
console.log('Could not remove fixtures folder.');
}
});
});
45 changes: 16 additions & 29 deletions tools/challenge-helper-scripts/helpers/get-file-name.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,22 @@ import { join } from 'path';

import { getFileName } from './get-file-name';

const metaPath = join(
const basePath = join(
process.cwd(),
'curriculum',
'challenges',
'_meta',
'project'
'__fixtures__' + process.env.JEST_WORKER_ID
);
const superBlockPath = join(
process.cwd(),
'curriculum',
'challenges',
'english',
'superblock'
);
const projectPath = join(superBlockPath, 'project');
const commonPath = join(basePath, 'curriculum', 'challenges');

const cleanFiles = () => {
try {
fs.rmSync(superBlockPath, { recursive: true });
} catch (err) {
console.log('Could not remove superblock mock folder. ');
}
try {
fs.rmSync(metaPath, { recursive: true });
} catch (err) {
console.log('Could not remove meta mock folder.');
}
};
const block = 'project-get-file-name';
const metaPath = join(commonPath, '_meta', block);
const superBlockPath = join(commonPath, 'english', 'superblock-get-file-name');
const projectPath = join(superBlockPath, block);

describe('getFileName helper', () => {
beforeEach(() => {
fs.mkdirSync(superBlockPath);
fs.mkdirSync(projectPath);
fs.mkdirSync(superBlockPath, { recursive: true });
fs.mkdirSync(projectPath, { recursive: true });
fs.mkdirSync(metaPath, { recursive: true });
fs.writeFileSync(
join(projectPath, 'this-is-a-challenge.md'),
'---\nid: a\ntitle: This is a Challenge\n---',
Expand All @@ -51,7 +34,6 @@ describe('getFileName helper', () => {
'---\nid: c\ntitle: I Dunno\n---',
'utf-8'
);
fs.mkdirSync(metaPath);
fs.writeFileSync(
join(metaPath, 'meta.json'),
`{
Expand All @@ -77,6 +59,11 @@ describe('getFileName helper', () => {

afterEach(() => {
delete process.env.CALLING_DIR;
cleanFiles();
try {
fs.rmSync(basePath, { recursive: true });
} catch (err) {
console.log(err);
console.log('Could not remove fixtures folder.');
}
});
});
Loading

0 comments on commit efb6398

Please sign in to comment.