Skip to content

Commit

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

* refactor(test-console-render): destructure

* style: asyncArrow
  • Loading branch information
curbengh authored and SukkaW committed Jan 21, 2020
1 parent 3ed94e9 commit 667d9a0
Showing 1 changed file with 98 additions and 86 deletions.
184 changes: 98 additions & 86 deletions test/scripts/console/render.js
Original file line number Diff line number Diff line change
@@ -1,118 +1,130 @@
'use strict';

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

describe('render', () => {
const Hexo = require('../../../lib/hexo');
const hexo = new Hexo(pathFn.join(__dirname, 'render_test'), {silent: true});
const hexo = new Hexo(join(__dirname, 'render_test'), {silent: true});
const render = require('../../../lib/plugins/console/render').bind(hexo);

before(() => fs.mkdirs(hexo.base_dir).then(() => hexo.init()));
before(async () => {
await mkdirs(hexo.base_dir);
hexo.init();
});

after(() => fs.rmdir(hexo.base_dir));
after(() => rmdir(hexo.base_dir));

const body = [
'foo: 1',
'bar:',
' boo: 2'
].join('\n');

it('relative path', () => {
const src = pathFn.join(hexo.base_dir, 'test.yml');
const dest = pathFn.join(hexo.base_dir, 'result.json');

return fs.writeFile(src, body).then(() => render({_: ['test.yml'], output: 'result.json'})).then(() => fs.readFile(dest)).then(result => {
JSON.parse(result).should.eql({
foo: 1,
bar: {
boo: 2
}
});

return Promise.all([
fs.unlink(src),
fs.unlink(dest)
]);
it('relative path', async () => {
const src = join(hexo.base_dir, 'test.yml');
const dest = join(hexo.base_dir, 'result.json');

await writeFile(src, body);
await render({_: ['test.yml'], output: 'result.json'});
const result = await readFile(dest);
JSON.parse(result).should.eql({
foo: 1,
bar: {
boo: 2
}
});

await Promise.all([
unlink(src),
unlink(dest)
]);
});

it('absolute path', () => {
const src = pathFn.join(hexo.base_dir, 'test.yml');
const dest = pathFn.join(hexo.base_dir, 'result.json');

return fs.writeFile(src, body).then(() => render({_: [src], output: 'result.json'})).then(() => fs.readFile(dest)).then(result => {
JSON.parse(result).should.eql({
foo: 1,
bar: {
boo: 2
}
});

return Promise.all([
fs.unlink(src),
fs.unlink(dest)
]);
it('absolute path', async () => {
const src = join(hexo.base_dir, 'test.yml');
const dest = join(hexo.base_dir, 'result.json');

await writeFile(src, body);
await render({_: [src], output: 'result.json'});

const result = await readFile(dest);
JSON.parse(result).should.eql({
foo: 1,
bar: {
boo: 2
}
});

await Promise.all([
unlink(src),
unlink(dest)
]);
});

it('absolute output', () => {
const src = pathFn.join(hexo.base_dir, 'test.yml');
const dest = pathFn.join(hexo.base_dir, 'result.json');

return fs.writeFile(src, body).then(() => render({_: ['test.yml'], output: dest})).then(() => fs.readFile(dest)).then(result => {
JSON.parse(result).should.eql({
foo: 1,
bar: {
boo: 2
}
});

return Promise.all([
fs.unlink(src),
fs.unlink(dest)
]);
it('absolute output', async () => {
const src = join(hexo.base_dir, 'test.yml');
const dest = join(hexo.base_dir, 'result.json');

await writeFile(src, body);
await render({_: ['test.yml'], output: dest});

const result = await readFile(dest);
JSON.parse(result).should.eql({
foo: 1,
bar: {
boo: 2
}
});

await Promise.all([
unlink(src),
unlink(dest)
]);
});

// it('output'); missing-unit-test

it('engine', () => {
const src = pathFn.join(hexo.base_dir, 'test');
const dest = pathFn.join(hexo.base_dir, 'result.json');

return fs.writeFile(src, body).then(() => render({_: ['test'], output: 'result.json', engine: 'yaml'})).then(() => fs.readFile(dest)).then(result => {
JSON.parse(result).should.eql({
foo: 1,
bar: {
boo: 2
}
});

return Promise.all([
fs.unlink(src),
fs.unlink(dest)
]);
it('engine', async () => {
const src = join(hexo.base_dir, 'test');
const dest = join(hexo.base_dir, 'result.json');

await writeFile(src, body);
await render({_: ['test'], output: 'result.json', engine: 'yaml'});

const result = await readFile(dest);
JSON.parse(result).should.eql({
foo: 1,
bar: {
boo: 2
}
});

await Promise.all([
unlink(src),
unlink(dest)
]);
});

it('pretty', () => {
const src = pathFn.join(hexo.base_dir, 'test.yml');
const dest = pathFn.join(hexo.base_dir, 'result.json');

return fs.writeFile(src, body).then(() => render({_: ['test.yml'], output: 'result.json', pretty: true})).then(() => fs.readFile(dest)).then(result => {
result.should.eql(JSON.stringify({
foo: 1,
bar: {
boo: 2
}
}, null, ' '));

return Promise.all([
fs.unlink(src),
fs.unlink(dest)
]);
});
it('pretty', async () => {
const src = join(hexo.base_dir, 'test.yml');
const dest = join(hexo.base_dir, 'result.json');

await writeFile(src, body);
await render({_: ['test.yml'], output: 'result.json', pretty: true});

const result = await readFile(dest);
result.should.eql(JSON.stringify({
foo: 1,
bar: {
boo: 2
}
}, null, ' '));

await Promise.all([
unlink(src),
unlink(dest)
]);
});
});

0 comments on commit 667d9a0

Please sign in to comment.