feat(tools/scripts): test project create tool (#55247)

Co-authored-by: Oliver Eyton-Williams <ojeytonwilliams@gmail.com>
This commit is contained in:
Anna
2024-06-21 11:47:36 -04:00
committed by GitHub
parent 2847c7c77a
commit cd636a143a
3 changed files with 41 additions and 13 deletions

View File

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

View File

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

View File

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