Skip to content

Commit db78445

Browse files
committed
test: improve coverage
1 parent fbb69c3 commit db78445

File tree

5 files changed

+124
-3
lines changed

5 files changed

+124
-3
lines changed

test/scripts/helpers/gravatar.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ describe('gravatar', () => {
66
return crypto.createHash('md5').update(str).digest('hex');
77
}
88

9-
const gravatar = gravatarHelper as any;
9+
const gravatar = gravatarHelper;
1010

1111
const email = 'abc@abc.com';
1212
const hash = md5(email);

test/scripts/hexo/hexo.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,45 @@ describe('Hexo', () => {
153153

154154
it('load() - theme', async () => await testLoad(join(hexo.theme_dir, 'source')));
155155

156+
157+
it('load() - load database', async () => {
158+
hexo._dbLoaded = false;
159+
const dbPath = hexo.database.options.path;
160+
161+
const fixture = {
162+
meta: {
163+
version: 1,
164+
warehouse: require('warehouse').version
165+
},
166+
models: {
167+
PostTag: [
168+
{ _id: 'cuid111111111111111111113', post_id: 'cuid111111111111111111111', tag_id: 'cuid111111111111111111112' }
169+
],
170+
Tag: [
171+
{ _id: 'cuid111111111111111111112', name: 'foo' }
172+
],
173+
Post: [
174+
{ _id: 'cuid111111111111111111111', source: 'test', slug: 'test' }
175+
]
176+
}
177+
};
178+
await writeFile(dbPath, JSON.stringify(fixture));
179+
await hexo.load();
180+
// check Model
181+
hexo.model('PostTag').toArray({lean: true}).length.should.eql(fixture.models.PostTag.length);
182+
hexo.model('Tag').toArray({lean: true}).length.should.eql(fixture.models.Tag.length);
183+
hexo.model('Post').toArray({lean: true}).length.should.eql(fixture.models.Post.length);
184+
hexo._binaryRelationIndex.post_tag.keyIndex.size.should.eql(1);
185+
hexo._binaryRelationIndex.post_tag.valueIndex.size.should.eql(1);
186+
await unlink(dbPath);
187+
// clean up
188+
await hexo.model('PostTag').removeById('cuid111111111111111111113');
189+
await hexo.model('Tag').removeById('cuid111111111111111111112');
190+
await hexo.model('Post').removeById('cuid111111111111111111111');
191+
hexo._binaryRelationIndex.post_tag.keyIndex.clear();
192+
hexo._binaryRelationIndex.post_tag.valueIndex.clear();
193+
});
194+
156195
// Issue #3964
157196
it('load() - merge theme config - deep clone', async () => {
158197
const hexo = new Hexo(__dirname, { silent: true });

test/scripts/hexo/load_plugins.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import Hexo from '../../../lib/hexo';
44
import loadPlugins from '../../../lib/hexo/load_plugins';
55
import BluebirdPromise from 'bluebird';
66
import chai from 'chai';
7+
import { spy } from 'sinon';
78
const should = chai.should();
89

910
describe('Load plugins', () => {
@@ -93,6 +94,20 @@ describe('Load plugins', () => {
9394
});
9495
});
9596

97+
it('fail to load plugins', () => {
98+
const logSpy = spy();
99+
hexo.log.error = logSpy;
100+
const name = 'hexo-plugin-test';
101+
const path = join(hexo.plugin_dir, name, 'index.js');
102+
return BluebirdPromise.all([
103+
createPackageFile(name),
104+
writeFile(path, 'throw new Error("test")')
105+
]).then(() => loadPlugins(hexo)).then(() => {
106+
logSpy.args[0][1].should.contains('Plugin load failed: %s');
107+
logSpy.args[0][2].should.contains('hexo-plugin-test');
108+
});
109+
});
110+
96111
it('load async plugins', () => {
97112
const name = 'hexo-async-plugin-test';
98113
const path = join(hexo.plugin_dir, name, 'index.js');
@@ -217,6 +232,20 @@ describe('Load plugins', () => {
217232
return unlink(path);
218233
});
219234

235+
it('fail to load scripts', async () => {
236+
const logSpy = spy();
237+
hexo.log.error = logSpy;
238+
const path = join(hexo.script_dir, 'test.js');
239+
240+
writeFile(path, 'throw new Error("test")');
241+
await loadPlugins(hexo);
242+
243+
logSpy.args[0][1].should.contains('Script load failed: %s');
244+
logSpy.args[0][2].should.contains('test.js');
245+
return unlink(path);
246+
});
247+
248+
220249
it('load theme scripts', () => {
221250
const path = join(hexo.theme_script_dir, 'test.js');
222251

test/scripts/hexo/post.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -666,7 +666,7 @@ describe('Post', () => {
666666
].join('\n'));
667667

668668
const path = join(hexo.source_dir, '_posts', 'fooo.md');
669-
const data = await post.create({
669+
await post.create({
670670
title: 'fooo',
671671
layout: 'draft',
672672
tags: customTags,
@@ -1486,6 +1486,16 @@ describe('Post', () => {
14861486
data.content.should.contains('1');
14871487
data.content.should.contains('22222');
14881488

1489+
content = '{{ 1 }} \n `{% custom %}` 22222 `{% endcustom }`';
1490+
data = await post.render('', {
1491+
content,
1492+
engine: 'markdown'
1493+
});
1494+
data.content.should.contains('1');
1495+
data.content.should.contains('{% custom %}');
1496+
data.content.should.contains('22222');
1497+
data.content.should.contains('{% endcustom }');
1498+
14891499
// lost two characters
14901500
content = '{{ 1 }} \n `{#` \n 22222';
14911501
data = await post.render('', {

test/scripts/processors/post.ts

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
import { join } from 'path';
3-
import { mkdirs, rmdir, unlink, writeFile } from 'hexo-fs';
3+
import { exists, mkdirs, rmdir, unlink, writeFile } from 'hexo-fs';
44
import BluebirdPromise from 'bluebird';
55
import defaultConfig from '../../../lib/hexo/default_config';
66
import Hexo from '../../../lib/hexo';
@@ -358,6 +358,49 @@ describe('post', () => {
358358
]);
359359
});
360360

361+
it('post - type: create - post_asset_folder enabled without asset', async () => {
362+
hexo.config.post_asset_folder = true;
363+
364+
const fooPath = join(hexo.source_dir, '_posts', 'foo');
365+
if (await exists(fooPath)) {
366+
await rmdir(fooPath);
367+
}
368+
369+
const body = [
370+
'title: "Hello world"',
371+
'date: 2006-01-02 15:04:05',
372+
'updated: 2014-12-13 01:02:03',
373+
'---',
374+
'The quick brown fox jumps over the lazy dog'
375+
].join('\n');
376+
377+
const file = newFile({
378+
path: 'foo.html',
379+
published: true,
380+
type: 'create',
381+
renderable: true
382+
});
383+
384+
await writeFile(file.source, body);
385+
await process(file);
386+
const post = Post.findOne({ source: file.path });
387+
388+
post.title.should.eql('Hello world');
389+
post.date.format(dateFormat).should.eql('2006-01-02 15:04:05');
390+
post.updated.format(dateFormat).should.eql('2014-12-13 01:02:03');
391+
post._content.should.eql('The quick brown fox jumps over the lazy dog');
392+
post.source.should.eql(file.path);
393+
post.raw.should.eql(body);
394+
post.slug.should.eql('foo');
395+
post.published.should.be.true;
396+
397+
hexo.config.post_asset_folder = false;
398+
return BluebirdPromise.all([
399+
post.remove(),
400+
unlink(file.source)
401+
]);
402+
});
403+
361404
it('post - type: update', async () => {
362405
const body = [
363406
'title: "New world"',

0 commit comments

Comments
 (0)