fix(tools): update failing tests on main (#51980)

This commit is contained in:
Naomi Carrigan
2023-10-15 11:22:54 -07:00
committed by GitHub
parent 696da4a555
commit dbcc2af39b
9 changed files with 382 additions and 252 deletions

View File

@@ -7,7 +7,7 @@ test.describe('Legacy Challenge Path Redirection Tests', () => {
for (const { input, expected } of pathsToTest) { for (const { input, expected } of pathsToTest) {
test(`should redirect from ${input} to ${expected}`, async ({ page }) => { test(`should redirect from ${input} to ${expected}`, async ({ page }) => {
await page.goto(input); await page.goto(input);
await expect(page).toHaveURL(expected); await expect(page).toHaveURL(new RegExp(`${expected}/?`));
}); });
} }
}); });

View File

@@ -1,49 +1,70 @@
import fs from 'fs';
import { join } from 'path'; import { join } from 'path';
import mock from 'mock-fs';
import { import {
getChallengeOrderFromFileTree, getChallengeOrderFromFileTree,
getChallengeOrderFromMeta getChallengeOrderFromMeta
} from './get-challenge-order'; } from './get-challenge-order';
describe('getChallengeOrderFromMeta helper', () => { const metaPath = join(
beforeEach(() => { process.cwd(),
mock({ 'curriculum',
curriculum: { 'challenges',
challenges: { '_meta',
english: { 'project'
superblock: { );
'mock-project': { const superBlockPath = join(
'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"}]}
`
}
}
}
}
});
});
it('should load the file order', () => {
process.env.CALLING_DIR = join(
process.cwd(), process.cwd(),
'curriculum', 'curriculum',
'challenges', 'challenges',
'english', 'english',
'superblock', 'superblock'
'mock-project' );
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(() => {
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 = projectPath;
const challengeOrder = getChallengeOrderFromMeta(); const challengeOrder = getChallengeOrderFromMeta();
expect(challengeOrder).toEqual([ expect(challengeOrder).toEqual([
{ id: '1', title: 'This title is wrong' }, { id: '1', title: 'This title is wrong' },
@@ -53,51 +74,42 @@ describe('getChallengeOrderFromMeta helper', () => {
}); });
afterEach(() => { afterEach(() => {
mock.restore();
delete process.env.CALLING_DIR; delete process.env.CALLING_DIR;
cleanFiles();
}); });
}); });
describe('getChallengeOrderFromFileTree helper', () => { describe('getChallengeOrderFromFileTree helper', () => {
beforeEach(() => { beforeEach(() => {
mock({ fs.mkdirSync(superBlockPath);
curriculum: { fs.mkdirSync(projectPath);
challenges: { fs.mkdirSync(metaPath);
english: { fs.writeFileSync(
superblock: { join(projectPath, 'step-001.md'),
'mock-project': {
'step-001.md':
'---\nid: a8d97bd4c764e91f9d2bda01\ntitle: Step 1\n---', '---\nid: a8d97bd4c764e91f9d2bda01\ntitle: Step 1\n---',
'step-002.md': 'utf-8'
);
fs.writeFileSync(
join(projectPath, 'step-002.md'),
'---\nid: a6b0bb188d873cb2c8729495\ntitle: Step 2\n---', '---\nid: a6b0bb188d873cb2c8729495\ntitle: Step 2\n---',
'step-003.md': 'utf-8'
);
fs.writeFileSync(
join(projectPath, 'step-003.md'),
'---\nid: a5de63ebea8dbee56860f4f2\ntitle: Step 3\n---' '---\nid: a5de63ebea8dbee56860f4f2\ntitle: Step 3\n---'
} );
} fs.writeFileSync(
}, join(metaPath, 'meta.json'),
_meta: { `{
'mock-project': {
'meta.json': `{
"id": "mock-id", "id": "mock-id",
"challengeOrder": [{"id": "a8d97bd4c764e91f9d2bda01", "title": "Step 1"}, {"id": "a6b0bb188d873cb2c8729495", "title": "Step 3"}, {"id": "a5de63ebea8dbee56860f4f2", "title": "Step 2"}]} "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 () => { it('should load the file order', async () => {
expect.assertions(1); expect.assertions(1);
process.env.CALLING_DIR = join( process.env.CALLING_DIR = projectPath;
process.cwd(),
'curriculum',
'challenges',
'english',
'superblock',
'mock-project'
);
const challengeOrder = await getChallengeOrderFromFileTree(); const challengeOrder = await getChallengeOrderFromFileTree();
expect(challengeOrder).toEqual([ expect(challengeOrder).toEqual([
{ id: 'a8d97bd4c764e91f9d2bda01', title: 'Step 1' }, { id: 'a8d97bd4c764e91f9d2bda01', title: 'Step 1' },
@@ -107,7 +119,7 @@ describe('getChallengeOrderFromFileTree helper', () => {
}); });
afterEach(() => { afterEach(() => {
mock.restore(); cleanFiles();
delete process.env.CALLING_DIR; delete process.env.CALLING_DIR;
}); });
}); });

View File

@@ -12,7 +12,9 @@ export const getChallengeOrderFromFileTree = async (): Promise<
const path = getProjectPath(); const path = getProjectPath();
const fileList = await readdir(path); const fileList = await readdir(path);
const challengeOrder = fileList const challengeOrder = fileList
.map(file => matter.read(join(path, file))) .map(file => {
return matter.read(join(path, file));
})
.map(({ data }) => ({ .map(({ data }) => ({
id: data.id as string, id: data.id as string,
title: data.title as string title: data.title as string

View File

@@ -1,67 +1,82 @@
import fs from 'fs';
import { join } from 'path'; import { join } from 'path';
import mock from 'mock-fs';
import { getFileName } from './get-file-name'; import { getFileName } from './get-file-name';
describe('getFileName helper', () => { const metaPath = join(
beforeEach(() => { process.cwd(),
mock({ 'curriculum',
curriculum: { 'challenges',
challenges: { '_meta',
english: { 'project'
superblock: { );
'mock-project': { const superBlockPath = join(
'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"}}}]}
`
}
}
}
}
});
});
it('should return the file name if found', async () => {
expect.assertions(1);
process.env.CALLING_DIR = join(
process.cwd(), process.cwd(),
'curriculum', 'curriculum',
'challenges', 'challenges',
'english', 'english',
'superblock', 'superblock'
'mock-project' );
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(() => {
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'
); );
const fileName = await getFileName('1'); 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 = projectPath;
const fileName = await getFileName('a');
expect(fileName).toEqual('this-is-a-challenge.md'); expect(fileName).toEqual('this-is-a-challenge.md');
}); });
it('should return null if not found', async () => { it('should return null if not found', async () => {
expect.assertions(1); expect.assertions(1);
process.env.CALLING_DIR = join( process.env.CALLING_DIR = projectPath;
process.cwd(), const fileName = await getFileName('d');
'curriculum',
'challenges',
'english',
'superblock',
'mock-project'
);
const fileName = await getFileName('42');
expect(fileName).toBeNull(); expect(fileName).toBeNull();
}); });
afterEach(() => { afterEach(() => {
mock.restore();
delete process.env.CALLING_DIR; delete process.env.CALLING_DIR;
cleanFiles();
}); });
}); });

View File

@@ -9,8 +9,13 @@ export const getFileName = async (id: string): Promise<string | null> => {
if (!file.endsWith('.md')) { if (!file.endsWith('.md')) {
continue; continue;
} }
const frontMatter = matter.read(`${path}${file}`); let frontMatter = null;
if (String(frontMatter.data.id) === id) { try {
frontMatter = matter.read(`${path}${file}`);
} catch (err) {
frontMatter = null;
}
if (String(frontMatter?.data.id) === id) {
return file; return file;
} }
} }

View File

@@ -1,55 +1,82 @@
import path from 'path'; import fs from 'fs';
import mock from 'mock-fs'; import { join } from 'path';
import { import {
getMetaData, getMetaData,
getProjectMetaPath, getProjectMetaPath,
validateMetaData validateMetaData
} from './project-metadata'; } from './project-metadata';
describe('getProjectMetaPath helper', () => { const metaPath = join(
it('should return the meta path', () => { process.cwd(),
const expected = path.join(
'curriculum', 'curriculum',
'challenges', 'challenges',
`_meta/mock-project/meta.json` '_meta',
); 'project'
);
const superBlockPath = join(
process.cwd(),
'curriculum',
'challenges',
'english',
'superblock'
);
const projectPath = join(superBlockPath, 'project');
process.env.CALLING_DIR = const cleanFiles = () => {
'curriculum/challenges/english/superblock/mock-project'; 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 = join(metaPath, 'meta.json');
process.env.CALLING_DIR = projectPath;
expect(getProjectMetaPath()).toEqual(expected); expect(getProjectMetaPath()).toEqual(expected);
}); });
afterEach(() => { afterEach(() => {
cleanFiles();
delete process.env.CALLING_DIR; delete process.env.CALLING_DIR;
}); });
}); });
describe('getMetaData helper', () => { describe('getMetaData helper', () => {
beforeEach(() => { beforeEach(() => {
mock({ fs.mkdirSync(superBlockPath);
curriculum: { fs.mkdirSync(projectPath);
challenges: { fs.writeFileSync(
english: { join(projectPath, 'step-001.md'),
superblock: { 'Lorem ipsum...',
'mock-project': { 'utf-8'
'step-001.md': 'Lorem ipsum...', );
'step-002.md': 'Lorem ipsum...', fs.writeFileSync(
'step-003.md': 'Lorem ipsum...' join(projectPath, 'step-002.md'),
} 'Lorem ipsum...',
} 'utf-8'
}, );
_meta: { fs.writeFileSync(
'mock-project': { join(projectPath, 'step-003.md'),
'meta.json': `{ 'Lorem ipsum...',
'utf-8'
);
fs.mkdirSync(metaPath);
fs.writeFileSync(
join(metaPath, 'meta.json'),
`{
"id": "mock-id", "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', () => { it('should process requested file', () => {
@@ -61,8 +88,7 @@ describe('getMetaData helper', () => {
{ id: '1', title: 'Step 3' } { id: '1', title: 'Step 3' }
] ]
}; };
process.env.CALLING_DIR = process.env.CALLING_DIR = projectPath;
'curriculum/challenges/english/superblock/mock-project';
expect(getMetaData()).toEqual(expected); expect(getMetaData()).toEqual(expected);
}); });
@@ -70,7 +96,7 @@ describe('getMetaData helper', () => {
process.env.CALLING_DIR = process.env.CALLING_DIR =
'curriculum/challenges/english/superblock/mick-priject'; 'curriculum/challenges/english/superblock/mick-priject';
const errorPath = path.join( const errorPath = join(
'curriculum', 'curriculum',
'challenges', 'challenges',
'_meta', '_meta',
@@ -85,83 +111,107 @@ describe('getMetaData helper', () => {
}); });
afterEach(() => { afterEach(() => {
mock.restore(); cleanFiles();
delete process.env.CALLING_DIR; delete process.env.CALLING_DIR;
}); });
}); });
describe('validateMetaData helper', () => { describe('validateMetaData helper', () => {
it('should throw if a stepfile is missing', () => { it('should throw if a stepfile is missing', () => {
mock({ fs.mkdirSync(superBlockPath);
'_meta/project/': { fs.mkdirSync(projectPath);
'meta.json': fs.writeFileSync(
'{"id": "mock-id", "challengeOrder": [{"id": "id-1", "title": "Step 1"}, {"id": "id-2", "title": "Step 2"}, {"id": "id-3", "title": "Step 3"}]}' join(projectPath, 'step-001.md'),
}, `---
'english/superblock/project/': {
'id-1.md': `---
id: id-1 id: id-1
title: Step 2 title: Step 2
challengeType: a challengeType: a
dashedName: step-2 dashedName: step-2
--- ---
`, `,
'id-3.md': `--- 'utf-8'
);
fs.writeFileSync(
join(projectPath, 'step-003.md'),
`---
id: id-3 id: id-3
title: Step 3 title: Step 3
challengeType: c challengeType: c
dashedName: step-3 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( 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', () => { it('should throw if a step is present in the project, but not the meta', () => {
mock({ fs.mkdirSync(superBlockPath);
'_meta/project/': { fs.mkdirSync(projectPath);
'meta.json': fs.writeFileSync(
'{"id": "mock-id", "challengeOrder": [{"id": "id-1", "title": "Step 1"}, {"id": "id-2", "title": "Step 2"}]}' join(projectPath, '1.md'),
}, `---
'english/superblock/project/': {
'id-1.md': `---
id: id-1 id: id-1
title: Step 2 title: Step 2
challengeType: a challengeType: a
dashedName: step-2 dashedName: step-2
--- ---
`, `,
'id-2.md': `--- 'utf-8'
);
fs.writeFileSync(
join(projectPath, '2.md'),
`---
id: id-2 id: id-2
title: Step 1 title: Step 1
challengeType: b challengeType: b
dashedName: step-1 dashedName: step-1
--- ---
`, `,
'id-3.md': `--- 'utf-8'
);
fs.writeFileSync(
join(projectPath, '3.md'),
`---
id: id-3 id: id-3
title: Step 3 title: Step 3
challengeType: c challengeType: c
dashedName: step-3 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( 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(() => { afterEach(() => {
mock.restore();
delete process.env.CALLING_DIR; delete process.env.CALLING_DIR;
cleanFiles();
}); });
}); });

View File

@@ -19,7 +19,7 @@ export type Meta = {
}; };
function getMetaData(): Meta { function getMetaData(): Meta {
const metaData = fs.readFileSync(getProjectMetaPath(), 'utf8'); const metaData = fs.readFileSync(getProjectMetaPath(), 'utf-8');
return JSON.parse(metaData) as Meta; return JSON.parse(metaData) as Meta;
} }

View File

@@ -1,14 +1,9 @@
/* eslint-disable @typescript-eslint/no-unsafe-assignment */ /* eslint-disable @typescript-eslint/no-unsafe-assignment */
import fs from 'fs'; import fs from 'fs';
import { join } from 'path';
import ObjectID from 'bson-objectid'; import ObjectID from 'bson-objectid';
import glob from 'glob'; import glob from 'glob';
import * as matter from 'gray-matter'; import matter from 'gray-matter';
import mock from 'mock-fs';
// NOTE:
// Use `console.log()` before mocking the filesystem or use
// `process.stdout.write()` instead. There are issues when using `mock-fs` and
// `require`.
jest.mock('bson-objectid', () => { jest.mock('bson-objectid', () => {
return jest.fn(() => ({ toString: () => mockChallengeId })); return jest.fn(() => ({ toString: () => mockChallengeId }));
@@ -29,18 +24,39 @@ import {
updateStepTitles updateStepTitles
} from './utils'; } from './utils';
const metaPath = join(
process.cwd(),
'curriculum',
'challenges',
'_meta',
'project'
);
const superBlockPath = join(
process.cwd(),
'curriculum',
'challenges',
'english',
'superblock'
);
const projectPath = join(superBlockPath, 'project');
describe('Challenge utils helper scripts', () => { describe('Challenge utils helper scripts', () => {
describe('createStepFile util', () => { describe('createStepFile util', () => {
it('should create next step and return its identifier', () => { it('should create next step and return its identifier', () => {
mock({ fs.mkdirSync(superBlockPath);
'project/': { fs.mkdirSync(projectPath);
'step-001.md': 'Lorem ipsum...', fs.writeFileSync(
'step-002.md': 'Lorem ipsum...' join(projectPath, 'step-001.md'),
} 'Lorem ipsum...',
}); 'utf-8'
);
fs.writeFileSync(
join(projectPath, 'step-002.md'),
'Lorem ipsum...',
'utf-8'
);
process.env.CALLING_DIR = projectPath;
const step = createStepFile({ const step = createStepFile({
projectPath: 'project/',
stepNum: 3 stepNum: 3
}); });
@@ -52,42 +68,51 @@ describe('Challenge utils helper scripts', () => {
expect(getStepTemplate).toHaveBeenCalledTimes(1); expect(getStepTemplate).toHaveBeenCalledTimes(1);
// - Should write a file with a given name and template // - Should write a file with a given name and template
const files = glob.sync(`project/*.md`); const files = glob.sync(`${projectPath}/*.md`);
expect(files).toEqual([ expect(files).toEqual([
`project/${mockChallengeId}.md`, `${projectPath}/${mockChallengeId}.md`,
`project/step-001.md`, `${projectPath}/step-001.md`,
`project/step-002.md` `${projectPath}/step-002.md`
]); ]);
}); });
}); });
describe('createChallengeFile util', () => { describe('createChallengeFile util', () => {
it('should create the challenge', () => { it('should create the challenge', () => {
mock({ fs.mkdirSync(superBlockPath);
'project/': { fs.mkdirSync(projectPath);
'fake-challenge.md': 'Lorem ipsum...', fs.writeFileSync(
'so-many-fakes.md': 'Lorem ipsum...' join(projectPath, 'fake-challenge.md'),
} 'Lorem ipsum...',
}); 'utf-8'
);
fs.writeFileSync(
join(projectPath, 'so-many-fakes.md'),
'Lorem ipsum...',
'utf-8'
);
createChallengeFile('hi', 'pretend this is a template', 'project/'); process.env.CALLING_DIR = projectPath;
createChallengeFile('hi', 'pretend this is a template');
// - Should write a file with a given name and template // - Should write a file with a given name and template
const files = glob.sync(`project/*.md`); const files = glob.sync(`${projectPath}/*.md`);
expect(files).toEqual([ expect(files).toEqual([
`project/fake-challenge.md`, `${projectPath}/fake-challenge.md`,
`project/hi.md`, `${projectPath}/hi.md`,
`project/so-many-fakes.md` `${projectPath}/so-many-fakes.md`
]); ]);
}); });
}); });
describe('insertStepIntoMeta util', () => { describe('insertStepIntoMeta util', () => {
it('should update the meta with a new file id and name', () => { it('should update the meta with a new file id and name', () => {
mock({ fs.mkdirSync(metaPath);
'_meta/project/': { fs.writeFileSync(
'meta.json': `{"id": "mock-id", join(metaPath, 'meta.json'),
`{"id": "mock-id",
"challengeOrder": [ "challengeOrder": [
{ {
"id": "id-1", "id": "id-1",
@@ -101,15 +126,15 @@ describe('Challenge utils helper scripts', () => {
"id": "id-3", "id": "id-3",
"title": "Step 3" "title": "Step 3"
} }
]}` ]}`,
} 'utf-8'
}); );
process.env.CALLING_DIR = 'english/superblock/project'; process.env.CALLING_DIR = projectPath;
insertStepIntoMeta({ stepNum: 3, stepId: new ObjectID(mockChallengeId) }); insertStepIntoMeta({ stepNum: 3, stepId: new ObjectID(mockChallengeId) });
const meta = JSON.parse( const meta = JSON.parse(
fs.readFileSync('_meta/project/meta.json', 'utf8') fs.readFileSync(join(metaPath, 'meta.json'), 'utf-8')
); );
expect(meta).toEqual({ expect(meta).toEqual({
id: 'mock-id', id: 'mock-id',
@@ -137,53 +162,65 @@ describe('Challenge utils helper scripts', () => {
describe('updateStepTitles util', () => { describe('updateStepTitles util', () => {
it('should apply meta.challengeOrder to step files', () => { it('should apply meta.challengeOrder to step files', () => {
mock({ fs.mkdirSync(metaPath);
'_meta/project/': { fs.writeFileSync(
'meta.json': join(metaPath, 'meta.json'),
'{"id": "mock-id", "challengeOrder": [{"id": "id-1", "title": "Step 1"}, {"id": "id-3", "title": "Step 2"}, {"id": "id-2", "title": "Step 3"}]}' `{"id": "mock-id", "challengeOrder": [{"id": "id-1", "title": "Step 1"}, {"id": "id-3", "title": "Step 2"}, {"id": "id-2", "title": "Step 3"}]}`,
}, 'utf-8'
'english/superblock/project/': { );
'id-1.md': `--- fs.mkdirSync(superBlockPath);
fs.mkdirSync(projectPath);
fs.writeFileSync(
join(projectPath, 'id-1.md'),
`---
id: id-1 id: id-1
title: Step 2 title: Step 2
challengeType: a challengeType: a
dashedName: step-2 dashedName: step-2
--- ---
`, `,
'id-2.md': `--- 'utf-8'
);
fs.writeFileSync(
join(projectPath, 'id-2.md'),
`---
id: id-2 id: id-2
title: Step 1 title: Step 1
challengeType: b challengeType: b
dashedName: step-1 dashedName: step-1
--- ---
`, `,
'id-3.md': `--- 'utf-8'
);
fs.writeFileSync(
join(projectPath, 'id-3.md'),
`---
id: id-3 id: id-3
title: Step 3 title: Step 3
challengeType: c challengeType: c
dashedName: step-3 dashedName: step-3
--- ---
` `,
} 'utf-8'
}); );
process.env.CALLING_DIR = 'english/superblock/project'; process.env.CALLING_DIR = projectPath;
updateStepTitles(); updateStepTitles();
expect(matter.read('english/superblock/project/id-1.md').data).toEqual({ expect(matter.read(join(projectPath, 'id-1.md')).data).toEqual({
id: 'id-1', id: 'id-1',
title: 'Step 1', title: 'Step 1',
challengeType: 'a', challengeType: 'a',
dashedName: 'step-1' dashedName: 'step-1'
}); });
expect(matter.read('english/superblock/project/id-2.md').data).toEqual({ expect(matter.read(join(projectPath, 'id-2.md')).data).toEqual({
id: 'id-2', id: 'id-2',
title: 'Step 3', title: 'Step 3',
challengeType: 'b', challengeType: 'b',
dashedName: 'step-3' dashedName: 'step-3'
}); });
expect(matter.read('english/superblock/project/id-3.md').data).toEqual({ expect(matter.read(join(projectPath, 'id-3.md')).data).toEqual({
id: 'id-3', id: 'id-3',
title: 'Step 2', title: 'Step 2',
challengeType: 'c', challengeType: 'c',
@@ -192,7 +229,16 @@ dashedName: step-3
}); });
}); });
afterEach(() => { afterEach(() => {
mock.restore();
delete process.env.CALLING_DIR; delete process.env.CALLING_DIR;
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.');
}
}); });
}); });

View File

@@ -1,7 +1,7 @@
import fs from 'fs'; import fs from 'fs';
import path from 'path'; import path from 'path';
import ObjectID from 'bson-objectid'; import ObjectID from 'bson-objectid';
import * as matter from 'gray-matter'; import matter from 'gray-matter';
import { parseMDSync } from '../challenge-parser/parser'; import { parseMDSync } from '../challenge-parser/parser';
import { getMetaData, updateMetaData } from './helpers/project-metadata'; import { getMetaData, updateMetaData } from './helpers/project-metadata';
import { getProjectPath } from './helpers/get-project-info'; import { getProjectPath } from './helpers/get-project-info';