Skip to content

Commit

Permalink
test: improve coverage (#5223)
Browse files Browse the repository at this point in the history
  • Loading branch information
D-Sketon committed Jul 9, 2023
1 parent 3eaafb4 commit 04bc704
Show file tree
Hide file tree
Showing 12 changed files with 213 additions and 7 deletions.
10 changes: 10 additions & 0 deletions test/scripts/box/box.js
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,16 @@ describe('Box', () => {
await rmdir(box.base);
});

it('process() - error ignore - 1', async () => {
const box = newBox('test', { ignore: [null] });
box.options.ignored.should.eql([]);
});

it('process() - error ignore - 2', async () => {
const box = newBox('test', { ignore: [111] });
box.options.ignored.should.eql([]);
});

it('process() - skip files if they match a glob epression in ignore', async () => {
const box = newBox('test', { ignore: '**/ignore_me' });
const data = {};
Expand Down
17 changes: 17 additions & 0 deletions test/scripts/console/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,23 @@ describe('generate', () => {

it('default', () => testGenerate());

it('public_dir is not a directory', async () => {
await Promise.all([
// Add some source files
writeFile(join(hexo.source_dir, 'test.txt'), 'test'),
// Add some files to public folder
writeFile(join(hexo.public_dir, 'foo.txt'), 'foo')
]);
const old = hexo.public_dir;
hexo.public_dir = join(hexo.public_dir, 'foo.txt');
try {
await generate();
} catch (e) {
e.message.split(' ').slice(1).join(' ').should.eql('is not a directory');
}
hexo.public_dir = old;
});

it('write file if not exist', async () => {
const src = join(hexo.source_dir, 'test.txt');
const dest = join(hexo.public_dir, 'test.txt');
Expand Down
19 changes: 18 additions & 1 deletion test/scripts/console/list.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

const { spy } = require('sinon');
const { spy, stub, assert: sinonAssert } = require('sinon');
const Promise = require('bluebird');

describe('Console list', () => {
const Hexo = require('../../../dist/hexo');
Expand All @@ -18,6 +19,22 @@ describe('Console list', () => {
hexo.call.args[0][1]._[0].should.eql('list');
});

it('has args', async () => {
const logStub = stub(console, 'log');

hexo.load = () => Promise.resolve();

const list = require('../../../dist/plugins/console/list').bind(hexo);

await list({ _: ['page'] });

sinonAssert.calledWithMatch(logStub, 'Date');
sinonAssert.calledWithMatch(logStub, 'Title');
sinonAssert.calledWithMatch(logStub, 'Path');
sinonAssert.calledWithMatch(logStub, 'No pages.');
logStub.restore();
});

it('list type not found', () => {
hexo.call = spy();

Expand Down
5 changes: 5 additions & 0 deletions test/scripts/extend/processor.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ describe('Processor', () => {

p.list()[1].should.exist;

// more than one arg
p.register((a, b) => {});

p.list()[1].should.exist;

// no fn
should.throw(() => p.register(), TypeError, 'fn must be a function');
});
Expand Down
12 changes: 7 additions & 5 deletions test/scripts/extend/tag.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
'use strict';

const { spy } = require('sinon');

describe('Tag', () => {
const Tag = require('../../../dist/extend/tag');
const tag = new Tag();
Expand Down Expand Up @@ -164,13 +162,17 @@ describe('Tag', () => {
it('render() - callback', () => {
const tag = new Tag();

const callback = spy();
// spy() is not a function
let spy = false;
const callback = () => {
spy = true;
};

tag.register('test', () => 'foo');

return tag.render('{% test %}', callback()).then(result => {
return tag.render('{% test %}', callback).then(result => {
result.should.eql('foo');
callback.calledOnce.should.be.true;
spy.should.eql(true);
});
});
});
26 changes: 26 additions & 0 deletions test/scripts/generators/asset.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const { join } = require('path');
const { mkdirs, rmdir, unlink, writeFile } = require('hexo-fs');
const testUtil = require('../../util');
const { spy } = require('sinon');

describe('asset', () => {
const Hexo = require('../../../dist/hexo');
Expand Down Expand Up @@ -44,6 +45,31 @@ describe('asset', () => {
]);
});

it('renderable - error', async () => {
const logSpy = spy();
hexo.log.error = logSpy;
const path = 'test.yml';
const source = join(hexo.base_dir, path);
const content = 'foo: :';

await Promise.all([
Asset.insert({_id: path, path}),
writeFile(source, content)
]);
const data = await generator(hexo.locals);
data[0].path.should.eql('test.json');
data[0].data.modified.should.be.true;
await data[0].data.data();
logSpy.called.should.be.true;

logSpy.args[0][1].should.contains('Asset render failed: %s');
logSpy.args[0][2].should.contains('test.json');
await Promise.all([
Asset.removeById(path),
unlink(source)
]);
});

it('not renderable', async () => {
const path = 'test.txt';
const source = join(hexo.base_dir, path);
Expand Down
8 changes: 8 additions & 0 deletions test/scripts/hexo/multi_config_path.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ describe('config flag handling', () => {
fs.writeFileSync(base + 'test2.yml', testYaml2);
fs.writeFileSync(base + 'test1.json', testJson1);
fs.writeFileSync(base + 'test2.json', testJson2);
// not supported type
fs.writeFileSync(base + 'test1.xml', '');
fs.writeFileSync('/tmp/test3.json', testJson3);
});

Expand All @@ -147,6 +149,12 @@ describe('config flag handling', () => {
hexo.log.reader[0].msg.should.eql('No config file entered.');
});

it('not supported type', () => {
mcp(base, 'test1.xml,test1.json').should.equal(base + '_multiconfig.yml');
hexo.log.reader[0].type.should.eql('warning');
hexo.log.reader[0].msg.should.eql('Config file test1.xml not supported type.');
});

it('1 file', () => {
mcp(base, 'test1.yml').should.eql(
pathFn.resolve(base + 'test1.yml'));
Expand Down
13 changes: 12 additions & 1 deletion test/scripts/hexo/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -903,7 +903,8 @@ describe('Post', () => {
].join('\n');

const data = await post.render(null, {
content
content,
engine: 'markdown'
});
data.content.trim().should.eql([
'<blockquote><p>test1</p>',
Expand All @@ -913,6 +914,16 @@ describe('Post', () => {
].join('\n'));
});

it('render() - swig comments', async () => {
const content = '{# blockquote #}';

const data = await post.render(null, {
content,
engine: 'markdown'
});
data.content.trim().should.eql('');
});

it('render() - shouln\'t break curly brackets', async () => {
hexo.config.syntax_highlighter = 'prismjs';

Expand Down
13 changes: 13 additions & 0 deletions test/scripts/hexo/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,15 @@ describe('Render', () => {
}
});

it('render() - null path and text', async () => {
try {
await hexo.render.render({text: null, engine: null});
should.fail('Return value must be rejected');
} catch (err) {
err.message.should.eql('No input file or string!');
}
});

it('render() - options', async () => {
const result = await hexo.render.render({
text: [
Expand Down Expand Up @@ -228,6 +237,10 @@ describe('Render', () => {
should.throw(() => hexo.render.renderSync(), 'No input file or string!');
});

it('renderSync() - null path and text', () => {
should.throw(() => hexo.render.renderSync({text: null, engine: null}), 'No input file or string!');
});

it('renderSync() - options', () => {
const result = hexo.render.renderSync({
text: [
Expand Down
41 changes: 41 additions & 0 deletions test/scripts/processors/asset.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const { dirname, join } = require('path');
const { mkdirs, rmdir, stat, unlink, writeFile } = require('hexo-fs');
const { spy } = require('sinon');

const dateFormat = 'YYYY-MM-DD HH:mm:ss';

Expand Down Expand Up @@ -235,6 +236,46 @@ describe('asset', () => {
]);
});

it('page - type: create - exist', async () => {
const logSpy = spy();
hexo.log.warn = logSpy;

const body = [
'title: "Hello world"',
'date: 2006-01-02 15:04:05',
'updated: 2014-12-13 01:02:03',
'---',
'The quick brown fox jumps over the lazy dog'
].join('\n');

const file = newFile({
path: 'hello.njk',
type: 'create',
renderable: true
});

await writeFile(file.source, body);
await process(file);
await process(file);

const page = Page.findOne({ source: file.path });
page.title.should.eql('Hello world');
page.date.format(dateFormat).should.eql('2006-01-02 15:04:05');
page.updated.format(dateFormat).should.eql('2014-12-13 01:02:03');
page._content.should.eql('The quick brown fox jumps over the lazy dog');
page.source.should.eql(file.path);
page.raw.should.eql(body);
page.path.should.eql('hello.html');
page.layout.should.eql('page');

logSpy.called.should.be.true;
logSpy.args[0][0].should.contains('Trying to "create" \x1B[35mhello.njk\x1B[39m, but the file already exists!');
await Promise.all([
page.remove(),
unlink(file.source)
]);
});

it('page - type: update', async () => {
const body = [
'title: "Hello world"',
Expand Down
44 changes: 44 additions & 0 deletions test/scripts/processors/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -1073,6 +1073,50 @@ describe('post', () => {
]);
});

it('post - delete existing draft assets if draft posts are hidden', async () => {
hexo.config.post_asset_folder = true;

const body = [
'title: "Hello world"',
'published: false',
'---'
].join('\n');

const file = newFile({
path: 'foo.html',
published: true,
type: 'create',
renderable: true
});

const assetId = 'source/_posts/foo/bar.jpg';
const assetPath = join(hexo.base_dir, assetId);

await Promise.all([
writeFile(file.source, body),
writeFile(assetPath, '')
]);

// drafts disabled - no draft assets should be generated
await process(file);
const post = Post.findOne({ source: file.path });
await PostAsset.insert({
_id: 'source/_posts/foo/bar.jpg',
slug: 'bar.jpg',
post: post._id
});
await process(file);

post.published.should.be.false;
should.not.exist(PostAsset.findById(assetId));

await Promise.all([
post.remove(),
unlink(file.source),
unlink(assetPath)
]);
});

it('post - post_asset_folder disabled', async () => {
hexo.config.post_asset_folder = false;

Expand Down
12 changes: 12 additions & 0 deletions test/scripts/theme_processors/source.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,16 @@ describe('source', () => {
await process(file);
should.not.exist(Asset.findById(id));
});

it('type: delete - not -exist', async () => {
const file = newFile({
path: 'style.css',
type: 'delete'
});

const id = 'themes/test/' + file.path;

await process(file);
should.not.exist(Asset.findById(id));
});
});

0 comments on commit 04bc704

Please sign in to comment.