This repository has been archived by the owner on Jul 9, 2020. It is now read-only.
forked from WebPurple/external-courses
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-utils.js
99 lines (84 loc) · 3.49 KB
/
test-utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
const validateHTML = require('html-validator');
const validateCSS = require('css-validator');
const {readFileSync} = require('fs');
module.exports = (exerciseName, read = false, taskExt = 'js') => {
const dirWithTask = `${__dirname}/src/${exerciseName}`;
return {
task: (number, cb) => {
const oneParam = typeof number === 'function';
const taskCb = oneParam ? number : cb;
const taskNo = oneParam ? '' : number;
const pathToTask = `${dirWithTask}/task${taskNo ? `-${taskNo}` : taskNo}.${taskExt}`;
const moduleExport = read ? readCode(pathToTask) : softRequire(pathToTask);
const conditionalDescribe = moduleExport ? describe : xdescribe;
conditionalDescribe(`Task ${taskNo}`, () => taskCb(moduleExport));
},
html: markup => done => {
validateHTML({data: markup, format: 'json'})
.then(({messages}) => {
const errors = messages.filter(m => m.type === 'error');
if (errors.length) {
const report = `\n${errors
.map((e, i) => `${i + 1}. ${e.message}`)
.join('\n')}`;
return done.fail(report);
}
return done();
})
.catch(({message}) => {
console.warn(`Internet connection error: ${message}`);
return done();
});
},
css: markup => done => {
const compressedHTML = markup.replace(/\s/g, ' ');
const styleTag = findStringBetween(compressedHTML, '<style>', '</style>')[0] || '';
const hrefs = findStringBetween(compressedHTML, '<link', '>').reduce((hrefs, currentHref) => {
if (~currentHref.indexOf('.css') && !~currentHref.indexOf('http')) {
const validLink = findStringBetween(currentHref, 'href=("|\')', '.css')[0];
if (validLink) {
hrefs.push(`${validLink}.css`);
}
}
return hrefs;
}, []);
const bundle = [
styleTag,
...hrefs.map(href => readFileSync(`${dirWithTask}/${href}`, 'utf-8')),
].join('');
validateCSS({text: bundle, profile: 'css3svg'}, (error, res) => {
if (error) {
console.warn(`Internet connection error: ${error.message}`);
return done();
}
const {errors = []} = res;
if (errors.length) {
const report = `\n${errors
.map((e, i) => `${i + 1}. ${e.message} in \n ${e.context}`)
.join('')
.replace(/\s+/g, ' ')}`;
return done.fail(report);
}
return done();
});
},
};
};
const findStringBetween = (str, first, last) => {
const matched = str.match(new RegExp(first + '(.*?)' + last, 'gm')) || [];
return matched.map(s => s.replace(new RegExp(first), '').replace(new RegExp(last), ''));
};
const readCode = absolutePath => {
try {
return readFileSync(absolutePath).toString();
} catch (err) {
return null;
}
};
const softRequire = modulePath => {
try {
return require(modulePath);
} catch (err) {
return null;
}
};