Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(test-console-config): async/await #3960

Merged
merged 3 commits into from
Jan 7, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
98 changes: 57 additions & 41 deletions test/scripts/console/config.js
Original file line number Diff line number Diff line change
@@ -1,45 +1,48 @@
'use strict';

const fs = require('hexo-fs');
const pathFn = require('path');
const yaml = require('js-yaml');
const { mkdirs, readFile, rmdir, unlink, writeFile } = require('hexo-fs');
const { join } = require('path');
const { load } = require('js-yaml');
const rewire = require('rewire');
const sinon = require('sinon');

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

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

beforeEach(() => fs.writeFile(hexo.config_path, ''));
beforeEach(() => writeFile(hexo.config_path, ''));

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

it('read all config', () => {
it('read all config', async () => {
const spy = sinon.spy();

return configModule.__with__({
await configModule.__with__({
console: {
log: spy
}
})(() => configModule.call(hexo, {_: []})).then(() => {
spy.args[0][0].should.eql(hexo.config);
});
})(() => configModule.call(hexo, {_: []}));

spy.args[0][0].should.eql(hexo.config);
});

it('read config', () => {
it('read config', async () => {
const spy = sinon.spy();

return configModule.__with__({
await configModule.__with__({
console: {
log: spy
}
})(() => configModule.call(hexo, {_: ['title']})).then(() => {
spy.args[0][0].should.eql(hexo.config.title);
});
})(() => configModule.call(hexo, {_: ['title']}));

spy.args[0][0].should.eql(hexo.config.title);
});

it('read nested config', () => {
Expand All @@ -60,51 +63,64 @@ describe('config', () => {
});
});

function writeConfig() {
async function writeConfig() {
const args = Array.from(arguments);

return config({_: args}).then(() => fs.readFile(hexo.config_path)).then(content => yaml.load(content));
await config({_: args});
const content = await readFile(hexo.config_path);
return load(content);
}

it('write config', () => writeConfig('title', 'My Blog').then(config => {
it('write config', async () => {
const config = await writeConfig('title', 'My Blog');
config.title.should.eql('My Blog');
}));
});

it('write config: number', () => writeConfig('server.port', '5000').then(config => {
it('write config: number', async () => {
const config = await writeConfig('server.port', '5000');
config.server.port.should.eql(5000);
}));
});

it('write config: false', () => writeConfig('post_asset_folder', 'false').then(config => {
config.post_asset_folder.should.be.false;
}));
it('write config: false', async () => {
const config = await writeConfig('post_asset_folder', 'false');
config.post_asset_folder.should.eql(false);
});

it('write config: true', () => writeConfig('post_asset_folder', 'true').then(config => {
config.post_asset_folder.should.be.true;
}));
it('write config: true', async () => {
const config = await writeConfig('post_asset_folder', 'true');
config.post_asset_folder.should.eql(true);
});

it('write config: null', () => writeConfig('language', 'null').then(config => {
it('write config: null', async () => {
const config = await writeConfig('language', 'null');
should.not.exist(config.language);
}));
});

it('write config: regex', () => writeConfig('include', /^pattern$/gim).then(config => {
it('write config: regex', async () => {
const config = await writeConfig('include', /^pattern$/gim);
config.include.should.eql(/^pattern$/gim);
}));
});

it('write config: json', async () => {
const configPath = join(hexo.base_dir, '_config.json');
hexo.config_path = join(hexo.base_dir, '_config.json');

it('write config: json', () => {
const configPath = pathFn.join(hexo.base_dir, '_config.json');
hexo.config_path = pathFn.join(hexo.base_dir, '_config.json');
await writeFile(configPath, '{}');
await config({_: ['title', 'My Blog']});

return fs.writeFile(configPath, '{}').then(() => config({_: ['title', 'My Blog']})).then(() => fs.readFile(configPath)).then(content => {
return readFile(configPath).then(content => {
const json = JSON.parse(content);

json.title.should.eql('My Blog');

hexo.config_path = pathFn.join(hexo.base_dir, '_config.yml');
return fs.unlink(configPath);
hexo.config_path = join(hexo.base_dir, '_config.yml');
return unlink(configPath);
});
});

it('create config if not exist', () => fs.unlink(hexo.config_path).then(() => writeConfig('subtitle', 'Hello world')).then(config => {
it('create config if not exist', async () => {
await unlink(hexo.config_path);
const config = await writeConfig('subtitle', 'Hello world');
config.subtitle.should.eql('Hello world');
}));
});
});