Skip to content

Commit

Permalink
refactor(test-console-new): async/await & destructure (hexojs#3978)
Browse files Browse the repository at this point in the history
* refactor(test-console-new): async/await

* refactor(test-console-new): destructure

* refactor(test-console-new): more async/await
  • Loading branch information
curbengh authored and SukkaW committed Jan 10, 2020
1 parent 814f843 commit 810283a
Showing 1 changed file with 110 additions and 89 deletions.
199 changes: 110 additions & 89 deletions test/scripts/console/new.js
Original file line number Diff line number Diff line change
@@ -1,215 +1,235 @@
'use strict';

const fs = require('hexo-fs');
const { exists, mkdirs, readFile, rmdir, unlink } = require('hexo-fs');
const moment = require('moment');
const pathFn = require('path');
const { join } = require('path');
const Promise = require('bluebird');
const sinon = require('sinon');
const { useFakeTimers } = require('sinon');

describe('new', () => {
const Hexo = require('../../../lib/hexo');
const hexo = new Hexo(pathFn.join(__dirname, 'new_test'), {silent: true});
const hexo = new Hexo(join(__dirname, 'new_test'), {silent: true});
const n = require('../../../lib/plugins/console/new').bind(hexo);
const post = hexo.post;
const now = Date.now();
let clock;

before(() => {
clock = sinon.useFakeTimers(now);

return fs.mkdirs(hexo.base_dir).then(() => hexo.init()).then(() => hexo.scaffold.set('post', [
'title: {{ title }}',
'date: {{ date }}',
'tags:',
'---'
].join('\n'))).then(() => hexo.scaffold.set('draft', [
'title: {{ title }}',
'tags:',
'---'
].join('\n')));
before(async () => {
clock = useFakeTimers(now);

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

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

it('title', () => {
it('title', async () => {
const date = moment(now);
const path = pathFn.join(hexo.source_dir, '_posts', 'Hello-World.md');
const path = join(hexo.source_dir, '_posts', 'Hello-World.md');
const body = [
'title: Hello World',
'date: ' + date.format('YYYY-MM-DD HH:mm:ss'),
'tags:',
'---'
].join('\n') + '\n';

return n({
await n({
_: ['Hello World']
}).then(() => fs.readFile(path)).then(content => {
content.should.eql(body);
return fs.unlink(path);
});
const content = await readFile(path);
content.should.eql(body);

await unlink(path);
});

it('layout', () => {
const path = pathFn.join(hexo.source_dir, '_drafts', 'Hello-World.md');
it('layout', async () => {
const path = join(hexo.source_dir, '_drafts', 'Hello-World.md');
const body = [
'title: Hello World',
'tags:',
'---'
].join('\n') + '\n';

return n({
await n({
_: ['draft', 'Hello World']
}).then(() => fs.readFile(path)).then(content => {
content.should.eql(body);
return fs.unlink(path);
});
const content = await readFile(path);
content.should.eql(body);

await unlink(path);
});

it('slug', () => {
it('slug', async () => {
const date = moment(now);
const path = pathFn.join(hexo.source_dir, '_posts', 'foo.md');
const path = join(hexo.source_dir, '_posts', 'foo.md');
const body = [
'title: Hello World',
'date: ' + date.format('YYYY-MM-DD HH:mm:ss'),
'tags:',
'---'
].join('\n') + '\n';

return n({
await n({
_: ['Hello World'],
slug: 'foo'
}).then(() => fs.readFile(path)).then(content => {
content.should.eql(body);
return fs.unlink(path);
});
const content = await readFile(path);
content.should.eql(body);

await unlink(path);
});

it('slug - s', () => {
it('slug - s', async () => {
const date = moment(now);
const path = pathFn.join(hexo.source_dir, '_posts', 'foo.md');
const path = join(hexo.source_dir, '_posts', 'foo.md');
const body = [
'title: Hello World',
'date: ' + date.format('YYYY-MM-DD HH:mm:ss'),
'tags:',
'---'
].join('\n') + '\n';

return n({
await n({
_: ['Hello World'],
s: 'foo'
}).then(() => fs.readFile(path)).then(content => {
content.should.eql(body);
return fs.unlink(path);
});
const content = await readFile(path);
content.should.eql(body);

await unlink(path);
});

it('path', () => {
it('path', async () => {
const date = moment(now);
const path = pathFn.join(hexo.source_dir, '_posts', 'bar.md');
const path = join(hexo.source_dir, '_posts', 'bar.md');
const body = [
'title: Hello World',
'date: ' + date.format('YYYY-MM-DD HH:mm:ss'),
'tags:',
'---'
].join('\n') + '\n';

return n({
await n({
_: ['Hello World'],
slug: 'foo',
path: 'bar'
}).then(() => fs.readFile(path)).then(content => {
content.should.eql(body);
return fs.unlink(path);
});
const content = await readFile(path);
content.should.eql(body);

await unlink(path);
});

it('path - p', () => {
it('path - p', async () => {
const date = moment(now);
const path = pathFn.join(hexo.source_dir, '_posts', 'bar.md');
const path = join(hexo.source_dir, '_posts', 'bar.md');
const body = [
'title: Hello World',
'date: ' + date.format('YYYY-MM-DD HH:mm:ss'),
'tags:',
'---'
].join('\n') + '\n';

return n({
await n({
_: ['Hello World'],
slug: 'foo',
p: 'bar'
}).then(() => fs.readFile(path)).then(content => {
content.should.eql(body);
return fs.unlink(path);
});
const content = await readFile(path);
content.should.eql(body);

await unlink(path);
});

it('rename if target existed', () => {
const path = pathFn.join(hexo.source_dir, '_posts', 'Hello-World-1.md');
it('rename if target existed', async () => {
const path = join(hexo.source_dir, '_posts', 'Hello-World-1.md');

return post.create({
await post.create({
title: 'Hello World'
}).then(() => n({
});
await n({
_: ['Hello World']
})).then(() => fs.exists(path)).then(exist => {
exist.should.be.true;

return Promise.all([
fs.unlink(path),
fs.unlink(pathFn.join(hexo.source_dir, '_posts', 'Hello-World.md'))
]);
});
const exist = await exists(path);
exist.should.eql(true);

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

it('replace existing files', () => {
it('replace existing files', async () => {
const date = moment(now);
const path = pathFn.join(hexo.source_dir, '_posts', 'Hello-World.md');
const path = join(hexo.source_dir, '_posts', 'Hello-World.md');
const body = [
'title: Hello World',
'date: ' + date.format('YYYY-MM-DD HH:mm:ss'),
'tags:',
'---'
].join('\n') + '\n';

return post.create({
await post.create({
title: 'Hello World'
}).then(() => n({
});
await n({
_: ['Hello World'],
replace: true
})).then(() => fs.exists(pathFn.join(hexo.source_dir, '_posts', 'Hello-World-1.md'))).then(exist => {
exist.should.be.false;
}).then(() => fs.readFile(path)).then(content => {
content.should.eql(body);
}).finally(() => fs.unlink(path));
});
const exist = await exists(join(hexo.source_dir, '_posts', 'Hello-World-1.md'));
exist.should.eql(false);

const content = await readFile(path);
content.should.eql(body);

await unlink(path);
});

it('replace existing files - r', () => {
it('replace existing files - r', async () => {
const date = moment(now);
const path = pathFn.join(hexo.source_dir, '_posts', 'Hello-World.md');
const path = join(hexo.source_dir, '_posts', 'Hello-World.md');
const body = [
'title: Hello World',
'date: ' + date.format('YYYY-MM-DD HH:mm:ss'),
'tags:',
'---'
].join('\n') + '\n';

return post.create({
await post.create({
title: 'Hello World'
}).then(() => n({
});
await n({
_: ['Hello World'],
r: true
})).then(() => fs.exists(pathFn.join(hexo.source_dir, '_posts', 'Hello-World-1.md'))).then(exist => {
exist.should.be.false;
}).then(() => fs.readFile(path)).then(content => {
content.should.eql(body);
}).finally(() => fs.unlink(path));
});
const exist = await exists(join(hexo.source_dir, '_posts', 'Hello-World-1.md'));
exist.should.eql(false);

const content = await readFile(path);
content.should.eql(body);

await unlink(path);
});

it('extra data', () => {
it('extra data', async () => {
const date = moment(now);
const path = pathFn.join(hexo.source_dir, '_posts', 'Hello-World.md');
const path = join(hexo.source_dir, '_posts', 'Hello-World.md');
const body = [
'title: Hello World',
'foo: bar',
Expand All @@ -218,12 +238,13 @@ describe('new', () => {
'---'
].join('\n') + '\n';

return n({
await n({
_: ['Hello World'],
foo: 'bar'
}).then(() => fs.readFile(path)).then(content => {
content.should.eql(body);
return fs.unlink(path);
});
const content = await readFile(path);
content.should.eql(body);

await unlink(path);
});
});

0 comments on commit 810283a

Please sign in to comment.