mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-05-28 18:26:54 +00:00
refactor(tools): challenge tests - checks for title and ids (#51432)
This commit is contained in:
@@ -314,9 +314,18 @@ function populateTestsForLang({ lang, challenges, meta }) {
|
||||
throw new AssertionError(result.error);
|
||||
}
|
||||
const { id, title, block, dashedName } = challenge;
|
||||
assert.exists(
|
||||
dashedName,
|
||||
`Missing dashedName for challenge ${id} in ${block}.`
|
||||
);
|
||||
const pathAndTitle = `${block}/${dashedName}`;
|
||||
mongoIds.check(id, title);
|
||||
challengeTitles.check(title, pathAndTitle);
|
||||
const idVerificationMessage = mongoIds.check(id, title);
|
||||
assert.isNull(idVerificationMessage, idVerificationMessage);
|
||||
const dupeTitleCheck = challengeTitles.check(dashedName, block);
|
||||
assert.isTrue(
|
||||
dupeTitleCheck,
|
||||
`All challenges within a block must have a unique dashed name. ${dashedName} (at ${pathAndTitle}) is already assigned`
|
||||
);
|
||||
});
|
||||
|
||||
const { challengeType } = challenge;
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
class ChallengeTitles {
|
||||
constructor() {
|
||||
this.knownTitles = [];
|
||||
/**
|
||||
* Takes the shape of { [block]: title[]}
|
||||
*/
|
||||
this.knownTitles = {};
|
||||
}
|
||||
check(title, pathAndTitle) {
|
||||
check(title, block) {
|
||||
if (typeof title !== 'string') {
|
||||
throw new Error(
|
||||
`Expected a valid string for ${title}, but got a(n) ${typeof title}`
|
||||
@@ -12,19 +15,15 @@ class ChallengeTitles {
|
||||
if (titleToCheck.length === 0) {
|
||||
throw new Error('Expected a title length greater than 0');
|
||||
}
|
||||
// reassign titleToCheck if challenge is part of the project
|
||||
// based curriculum
|
||||
const isProjectCurriculumChallenge = title.match(/^Step\s*\d+$/);
|
||||
titleToCheck = isProjectCurriculumChallenge ? pathAndTitle : titleToCheck;
|
||||
const isKnown = this.knownTitles.includes(titleToCheck);
|
||||
// TODO: check for the exceptions or remove the warning.
|
||||
if (isKnown) {
|
||||
console.warn(`
|
||||
All current curriculum challenges must have a unique title.
|
||||
The title ${title} (at ${pathAndTitle}) is already assigned
|
||||
`);
|
||||
if (!this.knownTitles[block]) {
|
||||
this.knownTitles[block] = [];
|
||||
}
|
||||
this.knownTitles = [...this.knownTitles, titleToCheck];
|
||||
const isKnown = this.knownTitles[block].includes(title);
|
||||
if (isKnown) {
|
||||
return false;
|
||||
}
|
||||
this.knownTitles[block].push(title);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -24,16 +24,15 @@ class MongoIds {
|
||||
try {
|
||||
schema.validate(id);
|
||||
} catch {
|
||||
throw Error(`Expected a valid ObjectId for ${title}, but got ${id}`);
|
||||
return `Expected a valid ObjectId for ${title}, but got ${id}`;
|
||||
}
|
||||
|
||||
const idIndex = findIndex(this.knownIds, existing => id === existing);
|
||||
if (idIndex !== -1 && !duplicatedProjectIds.includes(id)) {
|
||||
throw Error(`The id for challenge ${title} appears more than once.
|
||||
With the exception of duplicatedProjectIds this should not happen.
|
||||
`);
|
||||
return `The id for challenge ${title} appears more than once. With the exception of duplicatedProjectIds this should not happen.`;
|
||||
}
|
||||
this.knownIds = [...this.knownIds, id];
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user