From 810283ac4d6b8590da2145f1f6023df42d74e39a Mon Sep 17 00:00:00 2001 From: curbengh <43627182+curbengh@users.noreply.github.com> Date: Fri, 10 Jan 2020 12:37:07 +1030 Subject: [PATCH] refactor(test-console-new): async/await & destructure (#3978) * refactor(test-console-new): async/await * refactor(test-console-new): destructure * refactor(test-console-new): more async/await --- test/scripts/console/new.js | 199 ++++++++++++++++++++---------------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/test/scripts/console/new.js b/test/scripts/console/new.js index 48af7abfcf..d740569eb5 100644 --- a/test/scripts/console/new.js +++ b/test/scripts/console/new.js @@ -1,42 +1,47 @@ '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'), @@ -44,33 +49,35 @@ describe('new', () => { '---' ].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'), @@ -78,18 +85,19 @@ describe('new', () => { '---' ].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'), @@ -97,18 +105,19 @@ describe('new', () => { '---' ].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'), @@ -116,19 +125,20 @@ describe('new', () => { '---' ].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'), @@ -136,36 +146,38 @@ describe('new', () => { '---' ].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'), @@ -173,21 +185,25 @@ describe('new', () => { '---' ].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'), @@ -195,21 +211,25 @@ describe('new', () => { '---' ].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', @@ -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); }); });