<commit_before>'use strict';

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

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

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

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

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

  it('read all config', async () => {
    const _stub = stub(console, 'log');

    try {
      await config({_: []});
    } finally {
      _stub.restore();
    }

    _stub.calledWith(hexo.config).should.be.true;
  });

  it('read config', async () => {
    const _stub = stub(console, 'log');

    try {
      await config({_: ['title']});
    } finally {
      _stub.restore();
    }

    _stub.calledWith(hexo.config.title).should.be.true;
  });

  it('read nested config', async () => {
    const _stub = stub(console, 'log');

    try {
      hexo.config.server = {
        port: 12345
      };

      await config({_: ['server.port']});
      _stub.calledWith(hexo.config.server.port).should.be.true;
    } finally {
      delete hexo.config.server;
      _stub.restore();
    }
  });

  async function writeConfig(...args) {
    await config({_: args});
    const content = await readFile(hexo.config_path);
    return load(content);
  }

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

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

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

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

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

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

  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');

    await writeFile(configPath, '{}');
    await config({_: ['title', 'My Blog']});

    const content = await readFile(configPath);

    const json = JSON.parse(content);

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

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

  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');
  });
});<commit_msg>fix(lint): fix lint error "require-atomic-updates"<commit_after>'use strict';

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

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

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

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

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

  it('read all config', async () => {
    const _stub = stub(console, 'log');

    try {
      await config({_: []});
    } finally {
      _stub.restore();
    }

    _stub.calledWith(hexo.config).should.be.true;
  });

  it('read config', async () => {
    const _stub = stub(console, 'log');

    try {
      await config({_: ['title']});
    } finally {
      _stub.restore();
    }

    _stub.calledWith(hexo.config.title).should.be.true;
  });

  it('read nested config', async () => {
    const _stub = stub(console, 'log');

    try {
      hexo.config.server = {
        port: 12345
      };

      await config({_: ['server.port']});
      _stub.calledWith(hexo.config.server.port).should.be.true;
    } finally {
      delete hexo.config.server;
      _stub.restore();
    }
  });

  async function writeConfig(...args) {
    await config({_: args});
    const content = await readFile(hexo.config_path);
    return load(content);
  }

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

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

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

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

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

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

  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');

    await writeFile(configPath, '{}');
    await config({_: ['title', 'My Blog']});

    return readFile(configPath).then(content => {
      const json = JSON.parse(content);

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

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

  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');
  });
});
{'fraction_non_alphanumeric': 0.19049746797736075, 'fraction_numerical': 0.0038725052129877867, 'mean_word_length': 3.279796048438496, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 36, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4715081', 'n_tokens_mistral': 2242, 'n_tokens_neox': 2180, 'n_words': 440}