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) {
test(`should redirect from ${input} to ${expected}`, async ({ page }) => {
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 mock from 'mock-fs';
import {
getChallengeOrderFromFileTree,
getChallengeOrderFromMeta
} from './get-challenge-order';
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"}]}
`
}
}
}
}
});
});
it('should load the file order', () => {
process.env.CALLING_DIR = join(
const metaPath = join(
process.cwd(),
'curriculum',
'challenges',
'_meta',
'project'
);
const superBlockPath = join(
process.cwd(),
'curriculum',
'challenges',
'english',
'superblock',
'mock-project'
'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(() => {
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();
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':
fs.mkdirSync(superBlockPath);
fs.mkdirSync(projectPath);
fs.mkdirSync(metaPath);
fs.writeFileSync(
join(projectPath, 'step-001.md'),
'---\nid: a8d97bd4c764e91f9d2bda01\ntitle: Step 1\n---',
'step-002.md':
'utf-8'
);
fs.writeFileSync(
join(projectPath, 'step-002.md'),
'---\nid: a6b0bb188d873cb2c8729495\ntitle: Step 2\n---',
'step-003.md':
'utf-8'
);
fs.writeFileSync(
join(projectPath, 'step-003.md'),
'---\nid: a5de63ebea8dbee56860f4f2\ntitle: Step 3\n---'
}
}
},
_meta: {
'mock-project': {
'meta.json': `{
);
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"}]}
`
}
}
}
}
});
"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;
});
});

View File

@@ -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

View File

@@ -1,67 +1,82 @@
import fs from 'fs';
import { join } from 'path';
import mock from 'mock-fs';
import { getFileName } from './get-file-name';
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"}}}]}
`
}
}
}
}
});
});
it('should return the file name if found', async () => {
expect.assertions(1);
process.env.CALLING_DIR = join(
const metaPath = join(
process.cwd(),
'curriculum',
'challenges',
'_meta',
'project'
);
const superBlockPath = join(
process.cwd(),
'curriculum',
'challenges',
'english',
'superblock',
'mock-project'
'superblock'
);
const fileName = await getFileName('1');
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'
);
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');
});
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();
});
});

View File

@@ -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;
}
}

View 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';
describe('getProjectMetaPath helper', () => {
it('should return the meta path', () => {
const expected = path.join(
const metaPath = join(
process.cwd(),
'curriculum',
'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 =
'curriculum/challenges/english/superblock/mock-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 = join(metaPath, 'meta.json');
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();
});
});

View File

@@ -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;
}

View File

@@ -1,14 +1,9 @@
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
import fs from 'fs';
import { join } from 'path';
import ObjectID from 'bson-objectid';
import glob from 'glob';
import * as 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`.
import matter from 'gray-matter';
jest.mock('bson-objectid', () => {
return jest.fn(() => ({ toString: () => mockChallengeId }));
@@ -29,18 +24,39 @@ import {
updateStepTitles
} 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('createStepFile util', () => {
it('should create next step and return its identifier', () => {
mock({
'project/': {
'step-001.md': 'Lorem ipsum...',
'step-002.md': 'Lorem ipsum...'
}
});
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'
);
process.env.CALLING_DIR = projectPath;
const step = createStepFile({
projectPath: 'project/',
stepNum: 3
});
@@ -52,42 +68,51 @@ describe('Challenge utils helper scripts', () => {
expect(getStepTemplate).toHaveBeenCalledTimes(1);
// - 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([
`project/${mockChallengeId}.md`,
`project/step-001.md`,
`project/step-002.md`
`${projectPath}/${mockChallengeId}.md`,
`${projectPath}/step-001.md`,
`${projectPath}/step-002.md`
]);
});
});
describe('createChallengeFile util', () => {
it('should create the challenge', () => {
mock({
'project/': {
'fake-challenge.md': 'Lorem ipsum...',
'so-many-fakes.md': 'Lorem ipsum...'
}
});
fs.mkdirSync(superBlockPath);
fs.mkdirSync(projectPath);
fs.writeFileSync(
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
const files = glob.sync(`project/*.md`);
const files = glob.sync(`${projectPath}/*.md`);
expect(files).toEqual([
`project/fake-challenge.md`,
`project/hi.md`,
`project/so-many-fakes.md`
`${projectPath}/fake-challenge.md`,
`${projectPath}/hi.md`,
`${projectPath}/so-many-fakes.md`
]);
});
});
describe('insertStepIntoMeta util', () => {
it('should update the meta with a new file id and name', () => {
mock({
'_meta/project/': {
'meta.json': `{"id": "mock-id",
fs.mkdirSync(metaPath);
fs.writeFileSync(
join(metaPath, 'meta.json'),
`{"id": "mock-id",
"challengeOrder": [
{
"id": "id-1",
@@ -101,15 +126,15 @@ describe('Challenge utils helper scripts', () => {
"id": "id-3",
"title": "Step 3"
}
]}`
}
});
process.env.CALLING_DIR = 'english/superblock/project';
]}`,
'utf-8'
);
process.env.CALLING_DIR = projectPath;
insertStepIntoMeta({ stepNum: 3, stepId: new ObjectID(mockChallengeId) });
const meta = JSON.parse(
fs.readFileSync('_meta/project/meta.json', 'utf8')
fs.readFileSync(join(metaPath, 'meta.json'), 'utf-8')
);
expect(meta).toEqual({
id: 'mock-id',
@@ -137,53 +162,65 @@ describe('Challenge utils helper scripts', () => {
describe('updateStepTitles util', () => {
it('should apply meta.challengeOrder to step files', () => {
mock({
'_meta/project/': {
'meta.json':
'{"id": "mock-id", "challengeOrder": [{"id": "id-1", "title": "Step 1"}, {"id": "id-3", "title": "Step 2"}, {"id": "id-2", "title": "Step 3"}]}'
},
'english/superblock/project/': {
'id-1.md': `---
fs.mkdirSync(metaPath);
fs.writeFileSync(
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"}]}`,
'utf-8'
);
fs.mkdirSync(superBlockPath);
fs.mkdirSync(projectPath);
fs.writeFileSync(
join(projectPath, 'id-1.md'),
`---
id: id-1
title: Step 2
challengeType: a
dashedName: step-2
---
`,
'id-2.md': `---
'utf-8'
);
fs.writeFileSync(
join(projectPath, 'id-2.md'),
`---
id: id-2
title: Step 1
challengeType: b
dashedName: step-1
---
`,
'id-3.md': `---
'utf-8'
);
fs.writeFileSync(
join(projectPath, 'id-3.md'),
`---
id: id-3
title: Step 3
challengeType: c
dashedName: step-3
---
`
}
});
`,
'utf-8'
);
process.env.CALLING_DIR = 'english/superblock/project';
process.env.CALLING_DIR = projectPath;
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',
title: 'Step 1',
challengeType: 'a',
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',
title: 'Step 3',
challengeType: 'b',
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',
title: 'Step 2',
challengeType: 'c',
@@ -192,7 +229,16 @@ dashedName: step-3
});
});
afterEach(() => {
mock.restore();
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 path from 'path';
import ObjectID from 'bson-objectid';
import * as matter from 'gray-matter';
import matter from 'gray-matter';
import { parseMDSync } from '../challenge-parser/parser';
import { getMetaData, updateMetaData } from './helpers/project-metadata';
import { getProjectPath } from './helpers/get-project-info';