Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
it('write file if not exist', () => {
const src = pathFn.join(hexo.source_dir, 'test.txt');
const dest = pathFn.join(hexo.public_dir, 'test.txt');
const content = 'test';
// Add some source files
return fs.writeFile(src, content).then(() => // First generation
generate()).then(() => // Delete generated files
fs.unlink(dest)).then(() => // Second generation
generate()).then(() => fs.readFile(dest)).then(result => {
result.should.eql(content);
// Remove source files and generated files
return Promise.all([
fs.unlink(src),
fs.unlink(dest)
]);
});
});
generate()).then(() => // Change the generated file
fs.writeFile(dest, newContent)).then(() => // Second generation
generate()).then(() => // Read the generated file
it('process() - skip files if they match any of the glob expressions in ignore', () => {
const box = newBox('test', { ignore: ['**/ignore_me', '**/ignore_me_too.txt'] });
const data = {};
box.addProcessor(file => {
data[file.path] = file;
});
return Promise.all([
fs.writeFile(pathFn.join(box.base, 'foo.txt'), 'foo'),
fs.writeFile(pathFn.join(box.base, 'ignore_me', 'bar.txt'), 'ignore_me'),
fs.writeFile(pathFn.join(box.base, 'ignore_me_too.txt'), 'ignore_me_too')
]).then(() => box.process()).then(() => {
const keys = Object.keys(data);
keys.length.should.eql(1);
keys[0].should.eql('foo.txt');
}).finally(() => fs.rmdir(box.base));
});
before(() => Promise.all([
fs.mkdirs(themeDir),
fs.writeFile(hexo.config_path, 'theme: test')
]).then(() => hexo.init()));
it('process() - always skip node_modules of theme', () => {
const box = newBox('test', { ignore: '**/ignore_me' });
const data = {};
box.addProcessor(file => {
data[file.path] = file;
});
return Promise.all([
fs.writeFile(pathFn.join(box.base, 'foo.txt'), 'foo'),
fs.writeFile(pathFn.join(box.base, 'ignore_me', 'bar.txt'), 'ignore_me'),
fs.writeFile(pathFn.join(box.base, 'themes', 'bar', 'node_modules', 'bar_library', 'bar.js'), 'themes')
]).then(() => box.process()).then(() => {
const keys = Object.keys(data);
keys.length.should.eql(1);
keys[0].should.eql('foo.txt');
}).finally(() => fs.rmdir(box.base));
});
it('custom source_dir', () => fs.writeFile(hexo.config_path, 'source_dir: bar').then(() => loadConfig(hexo)).then(() => {
hexo.source_dir.should.eql(pathFn.resolve(hexo.base_dir, 'bar') + pathFn.sep);
}).finally(() => fs.unlink(hexo.config_path)));
it('delete database', () => {
var dbPath = hexo.database.options.path;
return fs.writeFile(dbPath, '').then(() => clean()).then(() => fs.exists(dbPath)).then(exist => {
exist.should.be.false;
});
});
it('process() - update (mtime changed and hash changed)', () => {
const box = newBox('test');
const name = 'a.txt';
const path = pathFn.join(box.base, name);
const cacheId = 'test/' + name;
const processor = sinon.spy();
box.addProcessor(processor);
return Promise.all([
fs.writeFile(path, 'a'),
box.Cache.insert({
_id: cacheId,
modified: 0,
hash: util.hash('b').toString('hex')
})
]).then(() => box.process()).then(() => {
const file = processor.args[0][0];
file.type.should.eql('update');
file.path.should.eql(name);
}).finally(() => fs.rmdir(box.base));
});
function createPackageFile(...args) {
var pkg = {
name: 'hexo-site',
version: '0.0.0',
private: true,
dependencies: {}
};
for (var i = 0, len = args.length; i < len; i++) {
pkg.dependencies[args[i]] = '*';
}
return fs.writeFile(pathFn.join(hexo.base_dir, 'package.json'), JSON.stringify(pkg, null, ' '));
}
const body = [
'title: "Hello world"',
'---'
].join('\n');
const file = newFile({
path: 'hello.swig',
type: 'update',
renderable: true
});
let id;
return Promise.all([
Page.insert({source: file.path, path: 'hello.html'}),
fs.writeFile(file.source, body)
]).spread(doc => {
id = doc._id;
return process(file);
}).then(() => {
const page = Page.findOne({source: file.path});
page._id.should.eql(id);
page.title.should.eql('Hello world');
return Promise.all([
page.remove(),
fs.unlink(file.source)
]);
});
});