mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2025-12-19 18:18:27 -05:00
fix(tools): update failing tests on main (#51980)
This commit is contained in:
@@ -1,49 +1,70 @@
|
||||
import fs from 'fs';
|
||||
import { join } from 'path';
|
||||
|
||||
import mock from 'mock-fs';
|
||||
import {
|
||||
getChallengeOrderFromFileTree,
|
||||
getChallengeOrderFromMeta
|
||||
} from './get-challenge-order';
|
||||
|
||||
const metaPath = join(
|
||||
process.cwd(),
|
||||
'curriculum',
|
||||
'challenges',
|
||||
'_meta',
|
||||
'project'
|
||||
);
|
||||
const superBlockPath = join(
|
||||
process.cwd(),
|
||||
'curriculum',
|
||||
'challenges',
|
||||
'english',
|
||||
'superblock'
|
||||
);
|
||||
const projectPath = join(superBlockPath, 'project');
|
||||
|
||||
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', () => {
|
||||
beforeEach(() => {
|
||||
mock({
|
||||
curriculum: {
|
||||
challenges: {
|
||||
english: {
|
||||
superblock: {
|
||||
'mock-project': {
|
||||
'this-is-a-challenge.md':
|
||||
'---\nid: 1\ntitle: This is a Challenge\n---',
|
||||
'what-a-cool-thing.md':
|
||||
'---\nid: 100\ntitle: What a Cool Thing\n---',
|
||||
'i-dunno.md': '---\nid: 2\ntitle: I Dunno\n---'
|
||||
}
|
||||
}
|
||||
},
|
||||
_meta: {
|
||||
'mock-project': {
|
||||
'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"}]}
|
||||
`
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
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'),
|
||||
`{
|
||||
"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'
|
||||
);
|
||||
});
|
||||
|
||||
it('should load the file order', () => {
|
||||
process.env.CALLING_DIR = join(
|
||||
process.cwd(),
|
||||
'curriculum',
|
||||
'challenges',
|
||||
'english',
|
||||
'superblock',
|
||||
'mock-project'
|
||||
);
|
||||
process.env.CALLING_DIR = projectPath;
|
||||
const challengeOrder = getChallengeOrderFromMeta();
|
||||
expect(challengeOrder).toEqual([
|
||||
{ id: '1', title: 'This title is wrong' },
|
||||
@@ -53,51 +74,42 @@ describe('getChallengeOrderFromMeta helper', () => {
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
mock.restore();
|
||||
delete process.env.CALLING_DIR;
|
||||
cleanFiles();
|
||||
});
|
||||
});
|
||||
|
||||
describe('getChallengeOrderFromFileTree helper', () => {
|
||||
beforeEach(() => {
|
||||
mock({
|
||||
curriculum: {
|
||||
challenges: {
|
||||
english: {
|
||||
superblock: {
|
||||
'mock-project': {
|
||||
'step-001.md':
|
||||
'---\nid: a8d97bd4c764e91f9d2bda01\ntitle: Step 1\n---',
|
||||
'step-002.md':
|
||||
'---\nid: a6b0bb188d873cb2c8729495\ntitle: Step 2\n---',
|
||||
'step-003.md':
|
||||
'---\nid: a5de63ebea8dbee56860f4f2\ntitle: Step 3\n---'
|
||||
}
|
||||
}
|
||||
},
|
||||
_meta: {
|
||||
'mock-project': {
|
||||
'meta.json': `{
|
||||
"id": "mock-id",
|
||||
"challengeOrder": [{"id": "a8d97bd4c764e91f9d2bda01", "title": "Step 1"}, {"id": "a6b0bb188d873cb2c8729495", "title": "Step 3"}, {"id": "a5de63ebea8dbee56860f4f2", "title": "Step 2"}]}
|
||||
`
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
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'),
|
||||
`{
|
||||
"id": "mock-id",
|
||||
"challengeOrder": [{"id": "a8d97bd4c764e91f9d2bda01", "title": "Step 1"}, {"id": "a6b0bb188d873cb2c8729495", "title": "Step 3"}, {"id": "a5de63ebea8dbee56860f4f2", "title": "Step 2"}]}`,
|
||||
'utf-8'
|
||||
);
|
||||
});
|
||||
|
||||
it('should load the file order', async () => {
|
||||
expect.assertions(1);
|
||||
process.env.CALLING_DIR = join(
|
||||
process.cwd(),
|
||||
'curriculum',
|
||||
'challenges',
|
||||
'english',
|
||||
'superblock',
|
||||
'mock-project'
|
||||
);
|
||||
process.env.CALLING_DIR = projectPath;
|
||||
const challengeOrder = await getChallengeOrderFromFileTree();
|
||||
expect(challengeOrder).toEqual([
|
||||
{ id: 'a8d97bd4c764e91f9d2bda01', title: 'Step 1' },
|
||||
@@ -107,7 +119,7 @@ describe('getChallengeOrderFromFileTree helper', () => {
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
mock.restore();
|
||||
cleanFiles();
|
||||
delete process.env.CALLING_DIR;
|
||||
});
|
||||
});
|
||||
|
||||
@@ -12,7 +12,9 @@ export const getChallengeOrderFromFileTree = async (): Promise<
|
||||
const path = getProjectPath();
|
||||
const fileList = await readdir(path);
|
||||
const challengeOrder = fileList
|
||||
.map(file => matter.read(join(path, file)))
|
||||
.map(file => {
|
||||
return matter.read(join(path, file));
|
||||
})
|
||||
.map(({ data }) => ({
|
||||
id: data.id as string,
|
||||
title: data.title as string
|
||||
|
||||
@@ -1,67 +1,82 @@
|
||||
import fs from 'fs';
|
||||
import { join } from 'path';
|
||||
|
||||
import mock from 'mock-fs';
|
||||
import { getFileName } from './get-file-name';
|
||||
|
||||
const metaPath = join(
|
||||
process.cwd(),
|
||||
'curriculum',
|
||||
'challenges',
|
||||
'_meta',
|
||||
'project'
|
||||
);
|
||||
const superBlockPath = join(
|
||||
process.cwd(),
|
||||
'curriculum',
|
||||
'challenges',
|
||||
'english',
|
||||
'superblock'
|
||||
);
|
||||
const projectPath = join(superBlockPath, 'project');
|
||||
|
||||
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('getFileName helper', () => {
|
||||
beforeEach(() => {
|
||||
mock({
|
||||
curriculum: {
|
||||
challenges: {
|
||||
english: {
|
||||
superblock: {
|
||||
'mock-project': {
|
||||
'this-is-a-challenge.md':
|
||||
'---\nid: 1\ntitle: This is a Challenge\n---',
|
||||
'what-a-cool-thing.md':
|
||||
'---\nid: 100\ntitle: What a Cool Thing\n---',
|
||||
'i-dunno.md': '---\nid: 2\ntitle: I Dunno\n---'
|
||||
}
|
||||
}
|
||||
},
|
||||
_meta: {
|
||||
'mock-project': {
|
||||
'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"}}}]}
|
||||
`
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
fs.mkdirSync(superBlockPath);
|
||||
fs.mkdirSync(projectPath);
|
||||
fs.writeFileSync(
|
||||
join(projectPath, 'this-is-a-challenge.md'),
|
||||
'---\nid: a\ntitle: This is a Challenge\n---',
|
||||
'utf-8'
|
||||
);
|
||||
fs.writeFileSync(
|
||||
join(projectPath, 'what-a-cool-thing.md'),
|
||||
'---\nid: b\ntitle: What a Cool Thing\n---',
|
||||
'utf-8'
|
||||
);
|
||||
fs.writeFileSync(
|
||||
join(projectPath, 'i-dunno.md'),
|
||||
'---\nid: c\ntitle: I Dunno\n---',
|
||||
'utf-8'
|
||||
);
|
||||
fs.mkdirSync(metaPath);
|
||||
fs.writeFileSync(
|
||||
join(metaPath, 'meta.json'),
|
||||
`{
|
||||
"id": "mock-id",
|
||||
"challengeOrder": [{"id": "a", "title": "This title is wrong"}, {"id": "b", "title": "I Dunno"}, {"id": "c", "title": "What a Cool Thing"}}}]}`,
|
||||
'utf-8'
|
||||
);
|
||||
});
|
||||
|
||||
it('should return the file name if found', async () => {
|
||||
expect.assertions(1);
|
||||
process.env.CALLING_DIR = join(
|
||||
process.cwd(),
|
||||
'curriculum',
|
||||
'challenges',
|
||||
'english',
|
||||
'superblock',
|
||||
'mock-project'
|
||||
);
|
||||
const fileName = await getFileName('1');
|
||||
process.env.CALLING_DIR = projectPath;
|
||||
const fileName = await getFileName('a');
|
||||
expect(fileName).toEqual('this-is-a-challenge.md');
|
||||
});
|
||||
|
||||
it('should return null if not found', async () => {
|
||||
expect.assertions(1);
|
||||
process.env.CALLING_DIR = join(
|
||||
process.cwd(),
|
||||
'curriculum',
|
||||
'challenges',
|
||||
'english',
|
||||
'superblock',
|
||||
'mock-project'
|
||||
);
|
||||
const fileName = await getFileName('42');
|
||||
process.env.CALLING_DIR = projectPath;
|
||||
const fileName = await getFileName('d');
|
||||
expect(fileName).toBeNull();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
mock.restore();
|
||||
delete process.env.CALLING_DIR;
|
||||
cleanFiles();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -9,8 +9,13 @@ export const getFileName = async (id: string): Promise<string | null> => {
|
||||
if (!file.endsWith('.md')) {
|
||||
continue;
|
||||
}
|
||||
const frontMatter = matter.read(`${path}${file}`);
|
||||
if (String(frontMatter.data.id) === id) {
|
||||
let frontMatter = null;
|
||||
try {
|
||||
frontMatter = matter.read(`${path}${file}`);
|
||||
} catch (err) {
|
||||
frontMatter = null;
|
||||
}
|
||||
if (String(frontMatter?.data.id) === id) {
|
||||
return file;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,55 +1,82 @@
|
||||
import path from 'path';
|
||||
import mock from 'mock-fs';
|
||||
import fs from 'fs';
|
||||
import { join } from 'path';
|
||||
import {
|
||||
getMetaData,
|
||||
getProjectMetaPath,
|
||||
validateMetaData
|
||||
} from './project-metadata';
|
||||
|
||||
const metaPath = join(
|
||||
process.cwd(),
|
||||
'curriculum',
|
||||
'challenges',
|
||||
'_meta',
|
||||
'project'
|
||||
);
|
||||
const superBlockPath = join(
|
||||
process.cwd(),
|
||||
'curriculum',
|
||||
'challenges',
|
||||
'english',
|
||||
'superblock'
|
||||
);
|
||||
const projectPath = join(superBlockPath, 'project');
|
||||
|
||||
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('getProjectMetaPath helper', () => {
|
||||
it('should return the meta path', () => {
|
||||
const expected = path.join(
|
||||
'curriculum',
|
||||
'challenges',
|
||||
`_meta/mock-project/meta.json`
|
||||
);
|
||||
const expected = join(metaPath, 'meta.json');
|
||||
|
||||
process.env.CALLING_DIR =
|
||||
'curriculum/challenges/english/superblock/mock-project';
|
||||
process.env.CALLING_DIR = projectPath;
|
||||
|
||||
expect(getProjectMetaPath()).toEqual(expected);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
cleanFiles();
|
||||
delete process.env.CALLING_DIR;
|
||||
});
|
||||
});
|
||||
|
||||
describe('getMetaData helper', () => {
|
||||
beforeEach(() => {
|
||||
mock({
|
||||
curriculum: {
|
||||
challenges: {
|
||||
english: {
|
||||
superblock: {
|
||||
'mock-project': {
|
||||
'step-001.md': 'Lorem ipsum...',
|
||||
'step-002.md': 'Lorem ipsum...',
|
||||
'step-003.md': 'Lorem ipsum...'
|
||||
}
|
||||
}
|
||||
},
|
||||
_meta: {
|
||||
'mock-project': {
|
||||
'meta.json': `{
|
||||
fs.mkdirSync(superBlockPath);
|
||||
fs.mkdirSync(projectPath);
|
||||
fs.writeFileSync(
|
||||
join(projectPath, 'step-001.md'),
|
||||
'Lorem ipsum...',
|
||||
'utf-8'
|
||||
);
|
||||
fs.writeFileSync(
|
||||
join(projectPath, 'step-002.md'),
|
||||
'Lorem ipsum...',
|
||||
'utf-8'
|
||||
);
|
||||
fs.writeFileSync(
|
||||
join(projectPath, 'step-003.md'),
|
||||
'Lorem ipsum...',
|
||||
'utf-8'
|
||||
);
|
||||
fs.mkdirSync(metaPath);
|
||||
fs.writeFileSync(
|
||||
join(metaPath, 'meta.json'),
|
||||
`{
|
||||
"id": "mock-id",
|
||||
"challengeOrder": [{"id": "1", "title": "Step 1"}, {"id": "2", "title": "Step 2"}, {"id": "1", "title": "Step 3"}]}
|
||||
`
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
"challengeOrder": [{"id": "1", "title": "Step 1"}, {"id": "2", "title": "Step 2"}, {"id": "1", "title": "Step 3"}]}`,
|
||||
'utf-8'
|
||||
);
|
||||
});
|
||||
|
||||
it('should process requested file', () => {
|
||||
@@ -61,8 +88,7 @@ describe('getMetaData helper', () => {
|
||||
{ id: '1', title: 'Step 3' }
|
||||
]
|
||||
};
|
||||
process.env.CALLING_DIR =
|
||||
'curriculum/challenges/english/superblock/mock-project';
|
||||
process.env.CALLING_DIR = projectPath;
|
||||
expect(getMetaData()).toEqual(expected);
|
||||
});
|
||||
|
||||
@@ -70,7 +96,7 @@ describe('getMetaData helper', () => {
|
||||
process.env.CALLING_DIR =
|
||||
'curriculum/challenges/english/superblock/mick-priject';
|
||||
|
||||
const errorPath = path.join(
|
||||
const errorPath = join(
|
||||
'curriculum',
|
||||
'challenges',
|
||||
'_meta',
|
||||
@@ -85,83 +111,107 @@ describe('getMetaData helper', () => {
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
mock.restore();
|
||||
cleanFiles();
|
||||
delete process.env.CALLING_DIR;
|
||||
});
|
||||
});
|
||||
|
||||
describe('validateMetaData helper', () => {
|
||||
it('should throw if a stepfile is missing', () => {
|
||||
mock({
|
||||
'_meta/project/': {
|
||||
'meta.json':
|
||||
'{"id": "mock-id", "challengeOrder": [{"id": "id-1", "title": "Step 1"}, {"id": "id-2", "title": "Step 2"}, {"id": "id-3", "title": "Step 3"}]}'
|
||||
},
|
||||
'english/superblock/project/': {
|
||||
'id-1.md': `---
|
||||
fs.mkdirSync(superBlockPath);
|
||||
fs.mkdirSync(projectPath);
|
||||
fs.writeFileSync(
|
||||
join(projectPath, 'step-001.md'),
|
||||
`---
|
||||
id: id-1
|
||||
title: Step 2
|
||||
challengeType: a
|
||||
dashedName: step-2
|
||||
---
|
||||
`,
|
||||
'id-3.md': `---
|
||||
'utf-8'
|
||||
);
|
||||
fs.writeFileSync(
|
||||
join(projectPath, 'step-003.md'),
|
||||
`---
|
||||
id: id-3
|
||||
title: Step 3
|
||||
challengeType: c
|
||||
dashedName: step-3
|
||||
---
|
||||
`
|
||||
}
|
||||
});
|
||||
`,
|
||||
'utf-8'
|
||||
);
|
||||
fs.mkdirSync(metaPath);
|
||||
fs.writeFileSync(
|
||||
join(metaPath, 'meta.json'),
|
||||
`{
|
||||
"id": "mock-id",
|
||||
"challengeOrder": [{"id": "1", "title": "Step 1"}, {"id": "2", "title": "Step 2"}, {"id": "1", "title": "Step 3"}]}`,
|
||||
'utf-8'
|
||||
);
|
||||
|
||||
process.env.CALLING_DIR = 'english/superblock/project';
|
||||
process.env.CALLING_DIR = projectPath;
|
||||
|
||||
expect(() => validateMetaData()).toThrow(
|
||||
"ENOENT: no such file or directory, access 'english/superblock/project/id-2.md'"
|
||||
`ENOENT: no such file or directory, access '${projectPath}/1.md'`
|
||||
);
|
||||
});
|
||||
|
||||
it('should throw if a step is present in the project, but not the meta', () => {
|
||||
mock({
|
||||
'_meta/project/': {
|
||||
'meta.json':
|
||||
'{"id": "mock-id", "challengeOrder": [{"id": "id-1", "title": "Step 1"}, {"id": "id-2", "title": "Step 2"}]}'
|
||||
},
|
||||
'english/superblock/project/': {
|
||||
'id-1.md': `---
|
||||
fs.mkdirSync(superBlockPath);
|
||||
fs.mkdirSync(projectPath);
|
||||
fs.writeFileSync(
|
||||
join(projectPath, '1.md'),
|
||||
`---
|
||||
id: id-1
|
||||
title: Step 2
|
||||
challengeType: a
|
||||
dashedName: step-2
|
||||
---
|
||||
`,
|
||||
'id-2.md': `---
|
||||
'utf-8'
|
||||
);
|
||||
fs.writeFileSync(
|
||||
join(projectPath, '2.md'),
|
||||
`---
|
||||
id: id-2
|
||||
title: Step 1
|
||||
challengeType: b
|
||||
dashedName: step-1
|
||||
---
|
||||
`,
|
||||
'id-3.md': `---
|
||||
'utf-8'
|
||||
);
|
||||
fs.writeFileSync(
|
||||
join(projectPath, '3.md'),
|
||||
`---
|
||||
id: id-3
|
||||
title: Step 3
|
||||
challengeType: c
|
||||
dashedName: step-3
|
||||
---
|
||||
`
|
||||
}
|
||||
});
|
||||
`,
|
||||
'utf-8'
|
||||
);
|
||||
fs.mkdirSync(metaPath);
|
||||
fs.writeFileSync(
|
||||
join(metaPath, 'meta.json'),
|
||||
`{
|
||||
"id": "mock-id",
|
||||
"challengeOrder": [{"id": "1", "title": "Step 1"}, {"id": "2", "title": "Step 2"}, {"id": "1", "title": "Step 3"}]}`,
|
||||
'utf-8'
|
||||
);
|
||||
|
||||
process.env.CALLING_DIR = 'english/superblock/project';
|
||||
process.env.CALLING_DIR = projectPath;
|
||||
|
||||
expect(() => validateMetaData()).toThrow(
|
||||
"File english/superblock/project/id-3.md should be in the meta.json's challengeOrder"
|
||||
`File ${projectPath}/3.md should be in the meta.json's challengeOrder`
|
||||
);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
mock.restore();
|
||||
delete process.env.CALLING_DIR;
|
||||
cleanFiles();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -19,7 +19,7 @@ export type Meta = {
|
||||
};
|
||||
|
||||
function getMetaData(): Meta {
|
||||
const metaData = fs.readFileSync(getProjectMetaPath(), 'utf8');
|
||||
const metaData = fs.readFileSync(getProjectMetaPath(), 'utf-8');
|
||||
return JSON.parse(metaData) as Meta;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user