2021-05-06 16:24:04 +03:00
|
|
|
require('./utils');
|
|
|
|
|
2020-04-29 18:44:27 +03:00
|
|
|
const tmp = require('tmp');
|
|
|
|
const fs = require('fs-extra');
|
2021-06-09 16:11:23 +03:00
|
|
|
const parse = require('../lib/parse');
|
2020-12-10 13:37:43 +03:00
|
|
|
|
2021-06-09 16:11:23 +03:00
|
|
|
describe('package-json parse', function () {
|
2020-03-30 18:26:47 +03:00
|
|
|
it('should parse valid package.json', function (done) {
|
2020-04-29 18:44:27 +03:00
|
|
|
let pkgJson;
|
|
|
|
let tmpFile;
|
2020-03-30 18:26:47 +03:00
|
|
|
|
|
|
|
tmpFile = tmp.fileSync();
|
|
|
|
pkgJson = JSON.stringify({
|
|
|
|
name: 'test',
|
|
|
|
version: '0.0.0'
|
|
|
|
});
|
|
|
|
|
|
|
|
fs.writeSync(tmpFile.fd, pkgJson);
|
|
|
|
|
2021-06-09 16:11:23 +03:00
|
|
|
parse(tmpFile.name)
|
2020-03-30 18:26:47 +03:00
|
|
|
.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) {
|
2020-04-29 18:44:27 +03:00
|
|
|
let pkgJson;
|
|
|
|
let tmpFile;
|
2020-03-30 18:26:47 +03:00
|
|
|
|
|
|
|
tmpFile = tmp.fileSync();
|
|
|
|
pkgJson = JSON.stringify({
|
|
|
|
version: '0.0.0'
|
|
|
|
});
|
|
|
|
|
|
|
|
fs.writeSync(tmpFile.fd, pkgJson);
|
|
|
|
|
2021-06-09 16:11:23 +03:00
|
|
|
parse(tmpFile.name)
|
2020-03-30 18:26:47 +03:00
|
|
|
.then(function () {
|
|
|
|
done(new Error('packageJSON.parse succeeded, but should\'ve failed'));
|
|
|
|
})
|
|
|
|
.catch(function (err) {
|
2021-06-09 16:05:36 +03:00
|
|
|
err.message.should.equal('"name" or "version" is missing from theme package.json file.');
|
2020-03-30 18:26:47 +03:00
|
|
|
err.context.should.equal(tmpFile.name);
|
2021-06-09 16:05:36 +03:00
|
|
|
err.help.should.equal('This will be required in future. Please see https://ghost.org/docs/themes/');
|
2020-03-30 18:26:47 +03:00
|
|
|
|
|
|
|
done();
|
|
|
|
})
|
|
|
|
.catch(done)
|
|
|
|
.finally(tmpFile.removeCallback);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should fail when version is missing', function (done) {
|
2020-04-29 18:44:27 +03:00
|
|
|
let pkgJson;
|
|
|
|
let tmpFile;
|
2020-03-30 18:26:47 +03:00
|
|
|
|
|
|
|
tmpFile = tmp.fileSync();
|
|
|
|
pkgJson = JSON.stringify({
|
|
|
|
name: 'test'
|
|
|
|
});
|
|
|
|
|
|
|
|
fs.writeSync(tmpFile.fd, pkgJson);
|
|
|
|
|
2021-06-09 16:11:23 +03:00
|
|
|
parse(tmpFile.name)
|
2020-03-30 18:26:47 +03:00
|
|
|
.then(function () {
|
|
|
|
done(new Error('packageJSON.parse succeeded, but should\'ve failed'));
|
|
|
|
})
|
|
|
|
.catch(function (err) {
|
2021-06-09 16:05:36 +03:00
|
|
|
err.message.should.equal('"name" or "version" is missing from theme package.json file.');
|
2020-03-30 18:26:47 +03:00
|
|
|
err.context.should.equal(tmpFile.name);
|
2021-06-09 16:05:36 +03:00
|
|
|
err.help.should.equal('This will be required in future. Please see https://ghost.org/docs/themes/');
|
2020-03-30 18:26:47 +03:00
|
|
|
|
|
|
|
done();
|
|
|
|
})
|
|
|
|
.catch(done)
|
|
|
|
.finally(tmpFile.removeCallback);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should fail when JSON is invalid', function (done) {
|
2020-04-29 18:44:27 +03:00
|
|
|
let pkgJson;
|
|
|
|
let tmpFile;
|
2020-03-30 18:26:47 +03:00
|
|
|
|
|
|
|
tmpFile = tmp.fileSync();
|
|
|
|
pkgJson = '{name:"test"}';
|
|
|
|
|
|
|
|
fs.writeSync(tmpFile.fd, pkgJson);
|
|
|
|
|
2021-06-09 16:11:23 +03:00
|
|
|
parse(tmpFile.name)
|
2020-03-30 18:26:47 +03:00
|
|
|
.then(function () {
|
|
|
|
done(new Error('packageJSON.parse succeeded, but should\'ve failed'));
|
|
|
|
})
|
|
|
|
.catch(function (err) {
|
2021-06-09 16:05:36 +03:00
|
|
|
err.message.should.equal('Theme package.json file is malformed');
|
2020-03-30 18:26:47 +03:00
|
|
|
err.context.should.equal(tmpFile.name);
|
2021-06-09 16:05:36 +03:00
|
|
|
err.help.should.equal('This will be required in future. Please see https://ghost.org/docs/themes/');
|
2020-03-30 18:26:47 +03:00
|
|
|
|
|
|
|
done();
|
|
|
|
})
|
|
|
|
.catch(done)
|
|
|
|
.finally(tmpFile.removeCallback);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should fail when file is missing', function (done) {
|
2020-04-29 18:44:27 +03:00
|
|
|
const tmpFile = tmp.fileSync();
|
2020-03-30 18:26:47 +03:00
|
|
|
|
|
|
|
tmpFile.removeCallback();
|
2021-06-09 16:11:23 +03:00
|
|
|
parse(tmpFile.name)
|
2020-03-30 18:26:47 +03:00
|
|
|
.then(function () {
|
|
|
|
done(new Error('packageJSON.parse succeeded, but should\'ve failed'));
|
|
|
|
})
|
|
|
|
.catch(function (err) {
|
2021-06-09 16:05:36 +03:00
|
|
|
err.message.should.equal('Could not read package.json file');
|
2020-03-30 18:26:47 +03:00
|
|
|
err.context.should.equal(tmpFile.name);
|
|
|
|
|
|
|
|
done();
|
|
|
|
})
|
|
|
|
.catch(done);
|
|
|
|
});
|
|
|
|
});
|