From 5ee3e6d0c51d95c5cd5515cc514b0ce886c4cb7f Mon Sep 17 00:00:00 2001 From: D-Sketon <2055272094@qq.com> Date: Sat, 3 Jun 2023 01:32:53 +0800 Subject: [PATCH] test: improve coverage (#5221) * test: improve coverage * test(processors): improve coverage * test(processors): improve coverage * test(helpers): improve coverage * test(console): improve coverage --- test/scripts/console/list_page.js | 14 +++++++ test/scripts/console/list_post.js | 11 ++++- test/scripts/console/list_route.js | 10 +++++ test/scripts/console/new.js | 31 ++++++++++++++ test/scripts/helpers/paginator.js | 18 ++++++++ test/scripts/processors/asset.js | 43 +++++++++++++++++++ test/scripts/processors/data.js | 27 ++++++++++++ test/scripts/processors/post.js | 67 ++++++++++++++++++++++++++++++ test/scripts/renderers/nunjucks.js | 8 ++++ 9 files changed, 228 insertions(+), 1 deletion(-) diff --git a/test/scripts/console/list_page.js b/test/scripts/console/list_page.js index 080a6993b6..dc8fd444ba 100644 --- a/test/scripts/console/list_page.js +++ b/test/scripts/console/list_page.js @@ -39,4 +39,18 @@ describe('Console list', () => { sinonAssert.calledWithMatch(logStub, 'Hello World'); sinonAssert.calledWithMatch(logStub, 'foo'); }); + + it('page with unicode', async () => { + await Page.insert({ + source: 'foo', + title: '\u0100', + path: 'bar' + }); + listPages(); + sinonAssert.calledWithMatch(logStub, 'Date'); + sinonAssert.calledWithMatch(logStub, 'Title'); + sinonAssert.calledWithMatch(logStub, 'Path'); + sinonAssert.calledWithMatch(logStub, '\u0100'); + sinonAssert.calledWithMatch(logStub, 'foo'); + }); }); diff --git a/test/scripts/console/list_post.js b/test/scripts/console/list_post.js index 23376a2bcc..64ab652d3b 100644 --- a/test/scripts/console/list_post.js +++ b/test/scripts/console/list_post.js @@ -1,5 +1,6 @@ 'use strict'; +const Promise = require('bluebird'); const { stub, assert: sinonAssert } = require('sinon'); describe('Console list', () => { @@ -34,8 +35,15 @@ describe('Console list', () => { {source: 'baz', slug: 'baz', title: 'Dude', date: 1e8 - 1} ]; + const tags = [ + ['foo'], + ['baz'], + ['baz'] + ]; + await hexo.init(); - await Post.insert(posts); + const output = await Post.insert(posts); + await Promise.each(tags, (tags, i) => output[i].setTags(tags)); await hexo.locals.invalidate(); listPosts(); @@ -48,6 +56,7 @@ describe('Console list', () => { sinonAssert.calledWithMatch(logStub, posts[i].source); sinonAssert.calledWithMatch(logStub, posts[i].slug); sinonAssert.calledWithMatch(logStub, posts[i].title); + sinonAssert.calledWithMatch(logStub, tags[i][0]); } }); }); diff --git a/test/scripts/console/list_route.js b/test/scripts/console/list_route.js index 0cbf86996f..3169544ad4 100644 --- a/test/scripts/console/list_route.js +++ b/test/scripts/console/list_route.js @@ -27,5 +27,15 @@ describe('Console list', () => { listRoutes(); sinonAssert.calledWithMatch(logStub, 'Total: 1'); + route.remove('test'); + }); + + it('route with nodes', async () => { + route.set('test0/test1', 'foo'); + + listRoutes(); + sinonAssert.calledWithMatch(logStub, 'Total: 1'); + sinonAssert.calledWithMatch(logStub, '└─┬ test0'); + sinonAssert.calledWithMatch(logStub, ' └── test1'); }); }); diff --git a/test/scripts/console/new.js b/test/scripts/console/new.js index 16e93f4b69..4fd68eaf62 100644 --- a/test/scripts/console/new.js +++ b/test/scripts/console/new.js @@ -5,6 +5,7 @@ const moment = require('moment'); const { join } = require('path'); const Promise = require('bluebird'); const { useFakeTimers } = require('sinon'); +const { spy } = require('sinon'); describe('new', () => { const Hexo = require('../../../dist/hexo'); @@ -39,6 +40,16 @@ describe('new', () => { return rmdir(hexo.base_dir); }); + it('no args', async () => { + hexo.call = spy(); + await n({ + _: [] + }); + hexo.call.calledOnce.should.be.true; + hexo.call.args[0][0].should.eql('help'); + hexo.call.args[0][1]._[0].should.eql('new'); + }); + it('title', async () => { const date = moment(now); const path = join(hexo.source_dir, '_posts', 'Hello-World.md'); @@ -157,6 +168,26 @@ describe('new', () => { await unlink(path); }); + it('without _', async () => { + const date = moment(now); + const path = join(hexo.source_dir, '_posts', 'bar.md'); + const body = [ + 'title: bar', + 'date: ' + date.format('YYYY-MM-DD HH:mm:ss'), + 'tags:', + '---' + ].join('\n') + '\n'; + + await n({ + _: [], + path: 'bar' + }); + const content = await readFile(path); + content.should.eql(body); + + await unlink(path); + }); + it('rename if target existed', async () => { const path = join(hexo.source_dir, '_posts', 'Hello-World-1.md'); diff --git a/test/scripts/helpers/paginator.js b/test/scripts/helpers/paginator.js index 04826e453b..fe46d685c8 100644 --- a/test/scripts/helpers/paginator.js +++ b/test/scripts/helpers/paginator.js @@ -341,4 +341,22 @@ describe('paginator', () => { '' ].join('')); }); + + it('force_prev_next - 2', () => { + const result = paginator({ + current: 1, + prev_next: false, + force_prev_next: true + }); + + result.should.eql([ + '', + '1', + '2', + '3', + '', + '10', + '' + ].join('')); + }); }); diff --git a/test/scripts/processors/asset.js b/test/scripts/processors/asset.js index 1b2c1a10b9..ed878e4084 100644 --- a/test/scripts/processors/asset.js +++ b/test/scripts/processors/asset.js @@ -188,6 +188,19 @@ describe('asset', () => { should.not.exist(Asset.findById(id)); }); + it('asset - type: delete - not exist', async () => { + const file = newFile({ + path: 'foo.jpg', + type: 'delete', + renderable: false + }); + + const id = 'source/' + file.path; + await process(file); + + should.not.exist(Asset.findById(id)); + }); + it('page - type: create', async () => { const body = [ 'title: "Hello world"', @@ -249,6 +262,25 @@ describe('asset', () => { ]); }); + it('page - type: skip', async () => { + const file = newFile({ + path: 'hello.njk', + type: 'skip', + renderable: true + }); + + await Page.insert({ + source: file.path, + path: 'hello.html' + }); + const page = Page.findOne({source: file.path}); + await process(file); + should.exist(page); + await Promise.all([ + page.remove() + ]); + }); + it('page - type: delete', async () => { const file = newFile({ path: 'hello.njk', @@ -264,6 +296,17 @@ describe('asset', () => { should.not.exist(Page.findOne({ source: file.path })); }); + it('page - type: delete - not exist', async () => { + const file = newFile({ + path: 'hello.njk', + type: 'delete', + renderable: true + }); + + await process(file); + should.not.exist(Page.findOne({ source: file.path })); + }); + it('page - use the status of the source file if date not set', async () => { const file = newFile({ path: 'hello.njk', diff --git a/test/scripts/processors/data.js b/test/scripts/processors/data.js index d2d1345dc1..ef5493e469 100644 --- a/test/scripts/processors/data.js +++ b/test/scripts/processors/data.js @@ -128,6 +128,22 @@ describe('data', () => { unlink(file.source); }); + it('type: skip', async () => { + const file = newFile({ + path: 'users.yml', + type: 'skip' + }); + + await Data.insert({ + _id: 'users', + data: {foo: 'bar'} + }); + const data = Data.findById('users'); + await process(file); + should.exist(data); + data.remove(); + }); + it('type: delete', async () => { const file = newFile({ path: 'users.yml', @@ -141,4 +157,15 @@ describe('data', () => { await process(file); should.not.exist(Data.findById('users')); }); + + it('type: delete - not exist', async () => { + const file = newFile({ + path: 'users.yml', + type: 'delete' + }); + + await process(file); + should.not.exist(Data.findById('users')); + }); + }); diff --git a/test/scripts/processors/post.js b/test/scripts/processors/post.js index 8d653f61ca..7530da34c4 100644 --- a/test/scripts/processors/post.js +++ b/test/scripts/processors/post.js @@ -250,6 +250,31 @@ describe('post', () => { Post.removeById(postId); }); + it('asset - type: delete - not exist', async () => { + hexo.config.post_asset_folder = true; + + const file = newFile({ + path: 'foo/bar.jpg', + published: true, + type: 'delete', + renderable: false + }); + + const id = 'source/' + file.path; + + const post = await Post.insert({ + source: '_posts/foo.html', + slug: 'foo' + }); + const postId = post._id; + + await process(file); + should.not.exist(PostAsset.findById(id)); + + Post.removeById(postId); + }); + + it('asset - skip if can\'t find a matching post', async () => { hexo.config.post_asset_folder = true; @@ -358,6 +383,36 @@ describe('post', () => { ]); }); + it('post - type: skip', async () => { + const file = newFile({ + path: 'foo.html', + published: true, + type: 'skip', + renderable: true + }); + + await Post.insert({ + source: file.path, + slug: 'foo' + }); + await process(file); + const post = Post.findOne({ source: file.path }); + should.exist(post); + post.remove(); + }); + + it('post - type: delete - not exist', async () => { + const file = newFile({ + path: 'foo.html', + published: true, + type: 'delete', + renderable: true + }); + + await process(file); + should.not.exist(Post.findOne({ source: file.path })); + }); + it('post - type: delete', async () => { const file = newFile({ path: 'foo.html', @@ -374,6 +429,18 @@ describe('post', () => { should.not.exist(Post.findOne({ source: file.path })); }); + it('post - type: delete - not exist', async () => { + const file = newFile({ + path: 'foo.html', + published: true, + type: 'delete', + renderable: true + }); + + await process(file); + should.not.exist(Post.findOne({ source: file.path })); + }); + it('post - parse file name', async () => { const body = [ 'title: "Hello world"', diff --git a/test/scripts/renderers/nunjucks.js b/test/scripts/renderers/nunjucks.js index a875211caf..4159f424b0 100644 --- a/test/scripts/renderers/nunjucks.js +++ b/test/scripts/renderers/nunjucks.js @@ -123,6 +123,14 @@ describe('nunjucks', () => { r({ text: forLoop }, data).should.eql('123'); }); + it('toarray other case', () => { + const data = { + arr: 1 + }; + + r({ text: forLoop }, data).should.eql(''); + }); + it('safedump undefined', () => { const text = [ '{{ items | safedump }}'