test: stop relying on curriculum dir structure

Also refactors path resolution a little
This commit is contained in:
Oliver Eyton-Williams
2020-08-31 14:19:19 +02:00
parent 9089ddca5c
commit df532099bc
2 changed files with 13 additions and 18 deletions

View File

@ -131,6 +131,9 @@ async function parseTranslation(engPath, transPath, dict, lang) {
function createChallengeCreator(basePath, lang) { function createChallengeCreator(basePath, lang) {
const hasEnglishSource = hasEnglishSourceCreator(basePath); const hasEnglishSource = hasEnglishSourceCreator(basePath);
return async function createChallenge(filePath, maybeMeta) { return async function createChallenge(filePath, maybeMeta) {
function getFullPath(pathLang) {
return path.resolve(__dirname, basePath, pathLang, filePath);
}
let meta; let meta;
if (maybeMeta) { if (maybeMeta) {
meta = maybeMeta; meta = maybeMeta;
@ -149,17 +152,17 @@ function createChallengeCreator(basePath, lang) {
throw Error(`Missing English challenge for throw Error(`Missing English challenge for
${filePath} ${filePath}
It should be in It should be in
${path.resolve(basePath, 'english', filePath)} ${getFullPath('english')}
`); `);
// assumes superblock names are unique // assumes superblock names are unique
// while the auditing is ongoing, we default to English for un-audited certs // while the auditing is ongoing, we default to English for un-audited certs
// once that's complete, we can revert to using isEnglishChallenge(fullPath) // once that's complete, we can revert to using isEnglishChallenge(fullPath)
const useEnglish = lang === 'english' || !isAuditedCert(lang, superBlock); const useEnglish = lang === 'english' || !isAuditedCert(lang, superBlock);
const challenge = await (useEnglish const challenge = await (useEnglish
? parseMarkdown(path.resolve(basePath, 'english', filePath)) ? parseMarkdown(getFullPath('english'))
: parseTranslation( : parseTranslation(
path.resolve(basePath, 'english', filePath), getFullPath('english'),
path.resolve(basePath, lang, filePath), getFullPath(lang),
COMMENT_TRANSLATIONS, COMMENT_TRANSLATIONS,
lang lang
)); ));

View File

@ -1,19 +1,11 @@
/* global expect beforeAll */ /* global expect beforeAll */
const { const {
challengesDir,
createChallengeCreator, createChallengeCreator,
hasEnglishSourceCreator hasEnglishSourceCreator
} = require('./getChallenges'); } = require('./getChallenges');
/* eslint-disable max-len */
const REAL_PATH =
'01-responsive-web-design/applied-accessibility/add-a-text-alternative-to-images-for-visually-impaired-accessibility.english.md';
const REAL_MISSING_PATH =
'01-responsive-web-design/applied-accessibility/add-a-text-alternative-to-images-for-visually-impaired.md';
const EXISTING_CHALLENGE_PATH = 'challenge.md'; const EXISTING_CHALLENGE_PATH = 'challenge.md';
const MISSING_CHALLENGE_PATH = 'no/challenge.md'; const MISSING_CHALLENGE_PATH = 'no/challenge.md';
/* eslint-enable max-len */
let hasEnglishSource; let hasEnglishSource;
let createChallenge; let createChallenge;
@ -23,15 +15,15 @@ describe('create non-English challenge', () => {
describe('createChallenge', () => { describe('createChallenge', () => {
it('throws if lang is an invalid language', async () => { it('throws if lang is an invalid language', async () => {
createChallenge = createChallengeCreator(basePath, 'notlang'); createChallenge = createChallengeCreator(basePath, 'notlang');
await expect(createChallenge(REAL_PATH)).rejects.toThrow( await expect(
'notlang is not a accepted language' createChallenge(EXISTING_CHALLENGE_PATH, {})
); ).rejects.toThrow('notlang is not a accepted language');
}); });
it('throws an error if the source challenge is missing', async () => { it('throws an error if the source challenge is missing', async () => {
createChallenge = createChallengeCreator(challengesDir, 'chinese'); createChallenge = createChallengeCreator(basePath, 'chinese');
await expect(createChallenge(REAL_MISSING_PATH)).rejects.toThrow( await expect(createChallenge(MISSING_CHALLENGE_PATH, {})).rejects.toThrow(
`Missing English challenge for `Missing English challenge for
${REAL_MISSING_PATH} ${MISSING_CHALLENGE_PATH}
It should be in It should be in
` `
); );