2020-04-29 18:44:27 +03:00
|
|
|
const should = require('should');
|
|
|
|
const sinon = require('sinon');
|
|
|
|
const fs = require('fs-extra');
|
|
|
|
const tmp = require('tmp');
|
|
|
|
const join = require('path').join;
|
2020-05-27 20:47:53 +03:00
|
|
|
const config = require('../../../../core/shared/config');
|
2021-04-27 15:28:04 +03:00
|
|
|
const loader = require('../../../../core/server/services/themes/loader');
|
|
|
|
const themeList = require('../../../../core/server/services/themes/list');
|
2017-03-22 09:52:58 +03:00
|
|
|
|
|
|
|
describe('Themes', function () {
|
|
|
|
afterEach(function () {
|
2019-01-21 19:53:44 +03:00
|
|
|
sinon.restore();
|
2017-03-22 09:52:58 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('Loader', function () {
|
2020-04-29 18:44:27 +03:00
|
|
|
let themePath;
|
2017-03-22 09:52:58 +03:00
|
|
|
|
|
|
|
beforeEach(function () {
|
|
|
|
themePath = tmp.dirSync({unsafeCleanup: true});
|
2019-01-21 19:53:44 +03:00
|
|
|
sinon.stub(config, 'getContentPath').withArgs('themes').returns(themePath.name);
|
2017-03-22 09:52:58 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(function () {
|
|
|
|
themePath.removeCallback();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('Load All', function () {
|
|
|
|
it('should load directory and include only folders', function (done) {
|
|
|
|
// create trash
|
2020-10-09 16:52:25 +03:00
|
|
|
fs.writeFileSync(join(themePath.name, 'casper.zip'), '');
|
|
|
|
fs.writeFileSync(join(themePath.name, '.DS_Store'), '');
|
2017-03-22 09:52:58 +03:00
|
|
|
|
|
|
|
// create actual theme
|
|
|
|
fs.mkdirSync(join(themePath.name, 'casper'));
|
|
|
|
fs.mkdirSync(join(themePath.name, 'casper', 'partials'));
|
2020-10-09 16:52:25 +03:00
|
|
|
fs.writeFileSync(join(themePath.name, 'casper', 'index.hbs'), '');
|
|
|
|
fs.writeFileSync(join(themePath.name, 'casper', 'partials', 'navigation.hbs'), '');
|
2017-03-22 09:52:58 +03:00
|
|
|
|
2019-07-09 17:35:18 +03:00
|
|
|
loader.loadAllThemes()
|
2017-03-22 09:52:58 +03:00
|
|
|
.then(function (result) {
|
2020-04-29 18:44:27 +03:00
|
|
|
const themeResult = themeList.getAll();
|
2017-03-22 09:52:58 +03:00
|
|
|
|
|
|
|
// Loader doesn't return anything
|
|
|
|
should.not.exist(result);
|
|
|
|
|
|
|
|
themeResult.should.eql({
|
|
|
|
casper: {
|
|
|
|
name: 'casper',
|
|
|
|
path: join(themePath.name, 'casper'),
|
|
|
|
'package.json': null
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
done();
|
|
|
|
})
|
|
|
|
.catch(done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should read directory and read package.json if present', function (done) {
|
|
|
|
// create trash
|
2020-10-09 16:52:25 +03:00
|
|
|
fs.writeFileSync(join(themePath.name, 'README.md'), '');
|
|
|
|
fs.writeFileSync(join(themePath.name, 'Thumbs.db'), '');
|
2017-03-22 09:52:58 +03:00
|
|
|
|
|
|
|
// create actual theme
|
|
|
|
fs.mkdirSync(join(themePath.name, 'casper'));
|
|
|
|
fs.mkdirSync(join(themePath.name, 'not-casper'));
|
|
|
|
fs.writeFileSync(
|
|
|
|
join(themePath.name, 'casper', 'package.json'),
|
|
|
|
JSON.stringify({name: 'casper', version: '0.1.2'})
|
|
|
|
);
|
|
|
|
|
2019-07-09 17:35:18 +03:00
|
|
|
loader.loadAllThemes()
|
2017-03-22 09:52:58 +03:00
|
|
|
.then(function (result) {
|
2020-04-29 18:44:27 +03:00
|
|
|
const themeResult = themeList.getAll();
|
2017-03-22 09:52:58 +03:00
|
|
|
|
|
|
|
// Loader doesn't return anything
|
|
|
|
should.not.exist(result);
|
|
|
|
|
|
|
|
themeResult.should.eql({
|
|
|
|
casper: {
|
|
|
|
name: 'casper',
|
|
|
|
path: join(themePath.name, 'casper'),
|
|
|
|
'package.json': {name: 'casper', version: '0.1.2'}
|
|
|
|
},
|
|
|
|
'not-casper': {
|
|
|
|
name: 'not-casper',
|
|
|
|
path: join(themePath.name, 'not-casper'),
|
|
|
|
'package.json': null
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
done();
|
|
|
|
})
|
|
|
|
.catch(done);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('Load One', function () {
|
|
|
|
it('should read directory and include only single requested theme', function (done) {
|
|
|
|
// create trash
|
2020-10-09 16:52:25 +03:00
|
|
|
fs.writeFileSync(join(themePath.name, 'casper.zip'), '');
|
|
|
|
fs.writeFileSync(join(themePath.name, '.DS_Store'), '');
|
2017-03-22 09:52:58 +03:00
|
|
|
|
|
|
|
// create actual theme
|
|
|
|
fs.mkdirSync(join(themePath.name, 'casper'));
|
2020-10-09 16:52:25 +03:00
|
|
|
fs.writeFileSync(join(themePath.name, 'casper', 'index.hbs'), '');
|
2017-03-22 09:52:58 +03:00
|
|
|
fs.writeFileSync(
|
|
|
|
join(themePath.name, 'casper', 'package.json'),
|
|
|
|
JSON.stringify({name: 'casper', version: '0.1.2'})
|
|
|
|
);
|
|
|
|
fs.mkdirSync(join(themePath.name, 'not-casper'));
|
2020-10-09 16:52:25 +03:00
|
|
|
fs.writeFileSync(join(themePath.name, 'not-casper', 'index.hbs'), '');
|
2017-03-22 09:52:58 +03:00
|
|
|
|
2019-07-09 17:35:18 +03:00
|
|
|
loader.loadOneTheme('casper')
|
2017-03-22 09:52:58 +03:00
|
|
|
.then(function (themeResult) {
|
|
|
|
themeResult.should.eql({
|
|
|
|
name: 'casper',
|
|
|
|
path: join(themePath.name, 'casper'),
|
|
|
|
'package.json': {name: 'casper', version: '0.1.2'}
|
|
|
|
});
|
|
|
|
|
|
|
|
done();
|
|
|
|
})
|
|
|
|
.catch(done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should throw an error if theme cannot be found', function (done) {
|
|
|
|
// create trash
|
2020-10-09 16:52:25 +03:00
|
|
|
fs.writeFileSync(join(themePath.name, 'casper.zip'), '');
|
|
|
|
fs.writeFileSync(join(themePath.name, '.DS_Store'), '');
|
2017-03-22 09:52:58 +03:00
|
|
|
|
2019-07-09 17:35:18 +03:00
|
|
|
loader.loadOneTheme('casper')
|
2017-03-22 09:52:58 +03:00
|
|
|
.then(function () {
|
|
|
|
done('Should have thrown an error');
|
|
|
|
})
|
|
|
|
.catch(function (err) {
|
|
|
|
err.message.should.eql('Package not found');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|