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

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