2018-06-02 21:48:23 +02:00
|
|
|
var should = require('should'),
|
2017-12-14 22:07:53 +01:00
|
|
|
tmp = require('tmp'),
|
|
|
|
fs = require('fs-extra'),
|
2020-03-30 16:26:47 +01:00
|
|
|
packageJSON = require('../../../../../core/server/lib/fs/package-json');
|
2017-12-14 22:07:53 +01:00
|
|
|
|
|
|
|
describe('lib/fs/package-json: parse', function () {
|
|
|
|
it('should parse valid package.json', function (done) {
|
|
|
|
var pkgJson, tmpFile;
|
|
|
|
|
|
|
|
tmpFile = tmp.fileSync();
|
|
|
|
pkgJson = JSON.stringify({
|
|
|
|
name: 'test',
|
|
|
|
version: '0.0.0'
|
|
|
|
});
|
|
|
|
|
|
|
|
fs.writeSync(tmpFile.fd, pkgJson);
|
|
|
|
|
|
|
|
packageJSON.parse(tmpFile.name)
|
|
|
|
.then(function (pkg) {
|
|
|
|
pkg.should.eql({
|
|
|
|
name: 'test',
|
|
|
|
version: '0.0.0'
|
|
|
|
});
|
|
|
|
|
|
|
|
done();
|
|
|
|
})
|
|
|
|
.catch(done)
|
|
|
|
.finally(tmpFile.removeCallback);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should fail when name is missing', function (done) {
|
|
|
|
var pkgJson, tmpFile;
|
|
|
|
|
|
|
|
tmpFile = tmp.fileSync();
|
|
|
|
pkgJson = JSON.stringify({
|
|
|
|
version: '0.0.0'
|
|
|
|
});
|
|
|
|
|
|
|
|
fs.writeSync(tmpFile.fd, pkgJson);
|
|
|
|
|
|
|
|
packageJSON.parse(tmpFile.name)
|
|
|
|
.then(function () {
|
|
|
|
done(new Error('packageJSON.parse succeeded, but should\'ve failed'));
|
|
|
|
})
|
|
|
|
.catch(function (err) {
|
|
|
|
err.message.should.equal('"name" or "version" is missing from theme package.json file.');
|
|
|
|
err.context.should.equal(tmpFile.name);
|
2019-07-25 15:17:23 +08:00
|
|
|
err.help.should.equal('This will be required in future. Please see https://ghost.org/docs/api/handlebars-themes/');
|
2017-12-14 22:07:53 +01:00
|
|
|
|
|
|
|
done();
|
|
|
|
})
|
|
|
|
.catch(done)
|
|
|
|
.finally(tmpFile.removeCallback);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should fail when version is missing', function (done) {
|
|
|
|
var pkgJson, tmpFile;
|
|
|
|
|
|
|
|
tmpFile = tmp.fileSync();
|
|
|
|
pkgJson = JSON.stringify({
|
|
|
|
name: 'test'
|
|
|
|
});
|
|
|
|
|
|
|
|
fs.writeSync(tmpFile.fd, pkgJson);
|
|
|
|
|
|
|
|
packageJSON.parse(tmpFile.name)
|
|
|
|
.then(function () {
|
|
|
|
done(new Error('packageJSON.parse succeeded, but should\'ve failed'));
|
|
|
|
})
|
|
|
|
.catch(function (err) {
|
|
|
|
err.message.should.equal('"name" or "version" is missing from theme package.json file.');
|
|
|
|
err.context.should.equal(tmpFile.name);
|
2019-07-25 15:17:23 +08:00
|
|
|
err.help.should.equal('This will be required in future. Please see https://ghost.org/docs/api/handlebars-themes/');
|
2017-12-14 22:07:53 +01:00
|
|
|
|
|
|
|
done();
|
|
|
|
})
|
|
|
|
.catch(done)
|
|
|
|
.finally(tmpFile.removeCallback);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should fail when JSON is invalid', function (done) {
|
|
|
|
var pkgJson, tmpFile;
|
|
|
|
|
|
|
|
tmpFile = tmp.fileSync();
|
|
|
|
pkgJson = '{name:"test"}';
|
|
|
|
|
|
|
|
fs.writeSync(tmpFile.fd, pkgJson);
|
|
|
|
|
|
|
|
packageJSON.parse(tmpFile.name)
|
|
|
|
.then(function () {
|
|
|
|
done(new Error('packageJSON.parse succeeded, but should\'ve failed'));
|
|
|
|
})
|
|
|
|
.catch(function (err) {
|
|
|
|
err.message.should.equal('Theme package.json file is malformed');
|
|
|
|
err.context.should.equal(tmpFile.name);
|
2019-07-25 15:17:23 +08:00
|
|
|
err.help.should.equal('This will be required in future. Please see https://ghost.org/docs/api/handlebars-themes/');
|
2017-12-14 22:07:53 +01:00
|
|
|
|
|
|
|
done();
|
|
|
|
})
|
|
|
|
.catch(done)
|
|
|
|
.finally(tmpFile.removeCallback);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should fail when file is missing', function (done) {
|
|
|
|
var tmpFile = tmp.fileSync();
|
|
|
|
|
|
|
|
tmpFile.removeCallback();
|
|
|
|
packageJSON.parse(tmpFile.name)
|
|
|
|
.then(function () {
|
|
|
|
done(new Error('packageJSON.parse succeeded, but should\'ve failed'));
|
|
|
|
})
|
|
|
|
.catch(function (err) {
|
|
|
|
err.message.should.equal('Could not read package.json file');
|
|
|
|
err.context.should.equal(tmpFile.name);
|
|
|
|
|
|
|
|
done();
|
|
|
|
})
|
|
|
|
.catch(done);
|
|
|
|
});
|
|
|
|
});
|