-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathcode.ts
More file actions
110 lines (94 loc) · 3.19 KB
/
code.ts
File metadata and controls
110 lines (94 loc) · 3.19 KB
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
100
101
102
103
104
105
106
107
108
109
110
import { join } from 'path';
import { mkdirs, rmdir, unlink, writeFile } from 'hexo-fs';
import Hexo from '../../../lib/hexo';
import codeGenerator from '../../../lib/plugins/generator/code';
import defaults from '../../../lib/hexo/default_config';
import chai from 'chai';
const should = chai.should();
type CodeParams = Parameters<typeof codeGenerator>
type CodeReturn = ReturnType<typeof codeGenerator>
describe('code', () => {
const hexo = new Hexo(join(__dirname, 'code_test'), {silent: true});
const generator: (...args: CodeParams) => CodeReturn = codeGenerator.bind(hexo);
const Code = hexo.model('Code');
const codeDir = defaults.code_dir;
before(async () => {
await mkdirs(hexo.base_dir);
await hexo.init();
});
after(() => rmdir(hexo.base_dir));
it('renderable', async () => {
const path = 'test.j2';
const source = join(hexo.base_dir, defaults.source_dir, defaults.code_dir, path);
const content = '{{ 1 }}';
await Promise.all([
Code.insert({
_id: `${defaults.source_dir}/${codeDir}/${path}`,
slug: `${codeDir}/${path}`,
path: `${codeDir}/${path}`,
content
}),
writeFile(source, content)
]);
const data = await generator();
data[0].path.should.eql(`${codeDir}/${path}`);
data[0].data.modified.should.be.true;
const result = await data[0].data.data;
result.should.eql(content);
await Promise.all([
Code.removeById(`${defaults.source_dir}/${codeDir}/${path}`),
unlink(source)
]);
});
it('not renderable', async () => {
const path = 'test.txt';
const source = join(hexo.base_dir, defaults.source_dir, defaults.code_dir, path);
const content = 'test content';
await Promise.all([
Code.insert({
_id: `${defaults.source_dir}/${codeDir}/${path}`,
slug: `${codeDir}/${path}`,
path: `${codeDir}/${path}`,
content
}),
writeFile(source, content)
]);
const data = await generator();
data[0].path.should.eql(`${codeDir}/${path}`);
data[0].data.modified.should.be.true;
const result = await data[0].data.data;
result.should.eql(content);
await Promise.all([
Code.removeById(`${defaults.source_dir}/${codeDir}/${path}`),
unlink(source)
]);
});
it('remove codes which does not exist', async () => {
const path = 'test.js';
await Code.insert({
_id: `${defaults.source_dir}/${codeDir}/${path}`,
slug: `${codeDir}/${path}`,
path: `${codeDir}/${path}`
});
await generator();
should.not.exist(Code.findById(`${defaults.source_dir}/${codeDir}/${path}`));
});
it('don\'t remove extension name', async () => {
const path = 'test.min.js';
const source = join(hexo.base_dir, defaults.source_dir, defaults.code_dir, path);
await Promise.all([
Code.insert({
_id: `${defaults.source_dir}/${codeDir}/${path}`,
slug: `${codeDir}/${path}`,
path: `${codeDir}/${path}`
}),
writeFile(source, '')
]);
const data = await generator();
data[0].path.should.eql(`${codeDir}/${path}`);
await Promise.all([
Code.removeById(`${defaults.source_dir}/${codeDir}/${path}`),
unlink(source)
]);
});
});