mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-04-27 10:00:40 -04:00
feat(tools/scripts): test project create tool (#55247)
Co-authored-by: Oliver Eyton-Williams <ojeytonwilliams@gmail.com>
This commit is contained in:
@@ -6,7 +6,7 @@ import { format } from 'prettier';
|
||||
import ObjectID from 'bson-objectid';
|
||||
|
||||
import { SuperBlocks } from '../../shared/config/superblocks';
|
||||
import { createStepFile } from './utils';
|
||||
import { createStepFile, validateBlockName } from './utils';
|
||||
import { getSuperBlockSubPath } from './fs-utils';
|
||||
import { Meta } from './helpers/project-metadata';
|
||||
|
||||
@@ -198,17 +198,9 @@ void prompt([
|
||||
{
|
||||
name: 'block',
|
||||
message: 'What is the dashed name (in kebab-case) for this project?',
|
||||
validate: (block: string) => {
|
||||
if (!block.length) {
|
||||
return 'please enter a dashed name';
|
||||
}
|
||||
if (/[^a-z0-9-]/.test(block)) {
|
||||
return 'please use alphanumerical characters and kebab case';
|
||||
}
|
||||
return true;
|
||||
},
|
||||
validate: validateBlockName,
|
||||
filter: (block: string) => {
|
||||
return block.toLowerCase();
|
||||
return block.toLowerCase().trim();
|
||||
}
|
||||
},
|
||||
{
|
||||
|
||||
@@ -21,7 +21,8 @@ import {
|
||||
createChallengeFile,
|
||||
createStepFile,
|
||||
insertStepIntoMeta,
|
||||
updateStepTitles
|
||||
updateStepTitles,
|
||||
validateBlockName
|
||||
} from './utils';
|
||||
|
||||
const basePath = join(
|
||||
@@ -78,6 +79,30 @@ describe('Challenge utils helper scripts', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('createProject util', () => {
|
||||
it('should allow alphanumerical names with trailing whitespace', () => {
|
||||
expect(
|
||||
validateBlockName('learn-callbacks-by-creating-a-bookshelf ')
|
||||
).toBe(true);
|
||||
});
|
||||
it('should allow alphanumerical names with no trailing whitespace', () => {
|
||||
expect(validateBlockName('learn-callbacks-by-creating-a-bookshelf')).toBe(
|
||||
true
|
||||
);
|
||||
});
|
||||
it('should not allow non-kebab case names', () => {
|
||||
expect(validateBlockName('learnCallbacksBetter')).toBe(
|
||||
'please use alphanumerical characters and kebab case'
|
||||
);
|
||||
});
|
||||
it('should not allow white space names', () => {
|
||||
expect(validateBlockName(' ')).toBe('please enter a dashed name');
|
||||
});
|
||||
it('should not allow empty names', () => {
|
||||
expect(validateBlockName('')).toBe('please enter a dashed name');
|
||||
});
|
||||
});
|
||||
|
||||
describe('createChallengeFile util', () => {
|
||||
it('should create the challenge', () => {
|
||||
fs.writeFileSync(
|
||||
|
||||
@@ -201,6 +201,16 @@ const getChallengeSeeds = (
|
||||
return parseMDSync(challengeFilePath).challengeFiles;
|
||||
};
|
||||
|
||||
const validateBlockName = (block: string): boolean | string => {
|
||||
if (!block.trim().length) {
|
||||
return 'please enter a dashed name';
|
||||
}
|
||||
if (/[^a-z0-9-]/.test(block.trim())) {
|
||||
return 'please use alphanumerical characters and kebab case';
|
||||
}
|
||||
return true;
|
||||
};
|
||||
|
||||
export {
|
||||
createStepFile,
|
||||
createChallengeFile,
|
||||
@@ -211,5 +221,6 @@ export {
|
||||
insertChallengeIntoMeta,
|
||||
insertStepIntoMeta,
|
||||
deleteChallengeFromMeta,
|
||||
deleteStepFromMeta
|
||||
deleteStepFromMeta,
|
||||
validateBlockName
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user