Skip to content

Commit

Permalink
fixed failing test suite
Browse files Browse the repository at this point in the history
  • Loading branch information
awongCM committed Apr 29, 2024
1 parent df29205 commit f635055
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 43 deletions.
2 changes: 1 addition & 1 deletion lib/plugins/console/unpublish.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function unPublishConsole(args) {

return this.post.unpublish({
slug: args._.pop(),
layout: args._.length ? args._[0] : this.config.default_layout
layout: 'draft' // assumes only published posts to be reverteds
}, args.r || args.replace).then(post => {
this.log.info('Unpublished: %s', magenta(tildify(post.path)));
});
Expand Down
97 changes: 55 additions & 42 deletions test/scripts/console/unpublish.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ const { useFakeTimers, spy } = require('sinon');

describe('unpublish', () => {
const Hexo = require('../../../lib/hexo');
const hexo = new Hexo(join(__dirname, 'unpublish_test'), {silent: true});
const unpublish = require('../../../lib/plugins/console/unpublish').bind(hexo);
const hexo = new Hexo(join(__dirname, 'unpublish_test'), { silent: true });
const unpublish = require('../../../lib/plugins/console/unpublish').bind(
hexo
);
const post = hexo.post;
const now = Date.now();
let clock;
Expand All @@ -19,42 +21,37 @@ describe('unpublish', () => {

await mkdirs(hexo.base_dir);
await hexo.init();
await hexo.scaffold.set('post', [
'---',
'title: {{ title }}',
'date: {{ date }}',
'tags:',
'---'
].join('\n'));
await hexo.scaffold.set('draft', [
'---',
'title: {{ title }}',
'tags:',
'---'
].join('\n'));
await hexo.scaffold.set(
'post',
['---', 'title: {{ title }}', 'date: {{ date }}', 'tags:', '---'].join(
'\n'
)
);
await hexo.scaffold.set(
'draft',
['---', 'title: {{ title }}', 'tags:', '---'].join('\n')
);
});

after(() => {
clock.restore();
return rmdir(hexo.base_dir);
});

beforeEach(() => post.create({
title: 'Hello World',
layout: 'post',
date: moment(now).format('YYYY-MM-DD HH:mm:ss')
}));
beforeEach(() =>
post.create({
title: 'Hello World',
layout: 'post',
date: moment(now).format('YYYY-MM-DD HH:mm:ss')
})
);

it('slug', async () => {
const postPath = join(hexo.source_dir, '_posts', 'Hello-World.md');
const path = join(hexo.source_dir, '_drafts', 'Hello-World.md');

const content = [
'---',
'title: Hello World',
'tags:',
'---'
].join('\n') + '\n';
const content
= ['---', 'title: Hello World', 'tags:', '---'].join('\n') + '\n';

await unpublish({
_: ['Hello-World']
Expand All @@ -70,11 +67,13 @@ describe('unpublish', () => {
});

it('no args', async () => {
const hexo = new Hexo(join(__dirname, 'unpublish_test'), {silent: true});
const hexo = new Hexo(join(__dirname, 'unpublish_test'), { silent: true });
hexo.call = spy();
const unpublish = require('../../../lib/plugins/console/unpublish').bind(hexo);
const unpublish = require('../../../lib/plugins/console/unpublish').bind(
hexo
);

await unpublish({_: []});
await unpublish({ _: [] });

hexo.call.calledOnce.should.be.true;
hexo.call.args[0][0].should.eql('help');
Expand All @@ -85,13 +84,14 @@ describe('unpublish', () => {
const path = join(hexo.source_dir, '_posts', 'Hello-World.md');
const date = moment(now);

const content = [
'---',
'title: Hello World',
'date: ' + date.format('YYYY-MM-DD HH:mm:ss'),
'tags:',
'---'
].join('\n') + '\n';
const content
= [
'---',
'title: Hello World',
'date: ' + date.format('YYYY-MM-DD HH:mm:ss'),
'tags:',
'---'
].join('\n') + '\n';

await unpublish({
_: ['photo', 'Hello-World']
Expand All @@ -103,10 +103,15 @@ describe('unpublish', () => {
});

it('rename if target existed', async () => {
const path = join(hexo.source_dir, '_posts', 'Hello-World.md');
const path = join(hexo.source_dir, '_drafts', 'Hello-World-1.md');
const publish = require('../../../lib/plugins/console/publish').bind(hexo);

await post.create({
title: 'Hello World'
title: 'Hello World',
layout: 'draft'
});
await publish({
_: ['Hello-World']
});
await unpublish({
_: ['Hello-World']
Expand All @@ -117,21 +122,29 @@ describe('unpublish', () => {

await Promise.all([
unlink(path),
unlink(join(hexo.source_dir, '_posts', 'Hello-World.md'))
unlink(join(hexo.source_dir, '_drafts', 'Hello-World.md'))
]);
});

it('replace existing target', async () => {
const path = join(hexo.source_dir, '_posts', 'Hello-World.md');
const path = join(hexo.source_dir, '_drafts', 'Hello-World.md');
const publish = require('../../../lib/plugins/console/publish').bind(hexo);

await post.create({
title: 'Hello World'
title: 'Hello World',
layout: 'draft'
});
await publish({
_: ['Hello-World']
});
await unpublish({
_: ['Hello-World'],
replace: true
});
const exist = await exists(join(hexo.source_dir, '_drafts', 'Hello-World-1.md'));

const exist = await exists(
join(hexo.source_dir, '_drafts', 'Hello-World-1.md')
);
exist.should.be.false;

await unlink(path);
Expand Down

0 comments on commit f635055

Please sign in to comment.