2021-05-06 16:24:04 +03:00
|
|
|
require('./utils');
|
|
|
|
|
2020-04-29 18:44:27 +03:00
|
|
|
const tmp = require('tmp');
|
|
|
|
const join = require('path').join;
|
|
|
|
const fs = require('fs-extra');
|
2021-06-09 16:11:23 +03:00
|
|
|
const packageJSON = require('../');
|
2020-12-10 13:37:43 +03:00
|
|
|
|
2021-06-09 16:11:23 +03:00
|
|
|
describe('package-json read', function () {
|
|
|
|
describe('readPackages', function () {
|
2020-03-30 18:26:47 +03:00
|
|
|
it('should read directory and ignore unneeded items', function (done) {
|
2020-04-29 18:44:27 +03:00
|
|
|
const packagePath = tmp.dirSync({unsafeCleanup: true});
|
2020-03-30 18:26:47 +03:00
|
|
|
|
|
|
|
// create example theme
|
|
|
|
fs.mkdirSync(join(packagePath.name, 'casper'));
|
2020-10-09 16:52:25 +03:00
|
|
|
fs.writeFileSync(join(packagePath.name, 'casper', 'index.hbs'), '');
|
2020-03-30 18:26:47 +03:00
|
|
|
|
|
|
|
// create some trash
|
|
|
|
fs.mkdirSync(join(packagePath.name, 'node_modules'));
|
|
|
|
fs.mkdirSync(join(packagePath.name, 'bower_components'));
|
|
|
|
fs.mkdirSync(join(packagePath.name, '.git'));
|
2020-10-09 16:52:25 +03:00
|
|
|
fs.writeFileSync(join(packagePath.name, '.DS_Store'), '');
|
2020-03-30 18:26:47 +03:00
|
|
|
|
2020-11-24 00:48:32 +03:00
|
|
|
packageJSON.readPackages(packagePath.name)
|
2020-03-30 18:26:47 +03:00
|
|
|
.then(function (pkgs) {
|
|
|
|
pkgs.should.eql({
|
|
|
|
casper: {
|
|
|
|
name: 'casper',
|
|
|
|
path: join(packagePath.name, 'casper'),
|
|
|
|
'package.json': null
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
done();
|
|
|
|
})
|
|
|
|
.catch(done)
|
|
|
|
.finally(packagePath.removeCallback);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should read directory and parse package.json files', function (done) {
|
2020-04-29 18:44:27 +03:00
|
|
|
let packagePath;
|
|
|
|
let pkgJson;
|
2020-03-30 18:26:47 +03:00
|
|
|
|
|
|
|
packagePath = tmp.dirSync({unsafeCleanup: true});
|
|
|
|
pkgJson = JSON.stringify({
|
|
|
|
name: 'test',
|
|
|
|
version: '0.0.0'
|
|
|
|
});
|
|
|
|
|
|
|
|
// create example theme
|
|
|
|
fs.mkdirSync(join(packagePath.name, 'testtheme'));
|
|
|
|
fs.writeFileSync(join(packagePath.name, 'testtheme', 'package.json'), pkgJson);
|
2020-10-09 16:52:25 +03:00
|
|
|
fs.writeFileSync(join(packagePath.name, 'testtheme', 'index.hbs'), '');
|
2020-03-30 18:26:47 +03:00
|
|
|
|
2020-11-24 00:48:32 +03:00
|
|
|
packageJSON.readPackages(packagePath.name)
|
2020-03-30 18:26:47 +03:00
|
|
|
.then(function (pkgs) {
|
|
|
|
pkgs.should.eql({
|
|
|
|
testtheme: {
|
|
|
|
name: 'testtheme',
|
|
|
|
path: join(packagePath.name, 'testtheme'),
|
|
|
|
'package.json': {
|
|
|
|
name: 'test',
|
|
|
|
version: '0.0.0'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
done();
|
|
|
|
})
|
|
|
|
.catch(done)
|
|
|
|
.finally(packagePath.removeCallback);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should read directory and ignore invalid package.json files', function (done) {
|
2020-04-29 18:44:27 +03:00
|
|
|
let packagePath;
|
|
|
|
let pkgJson;
|
2020-03-30 18:26:47 +03:00
|
|
|
|
|
|
|
packagePath = tmp.dirSync({unsafeCleanup: true});
|
|
|
|
pkgJson = JSON.stringify({
|
|
|
|
name: 'test'
|
|
|
|
});
|
|
|
|
|
|
|
|
// create example theme
|
|
|
|
fs.mkdirSync(join(packagePath.name, 'testtheme'));
|
|
|
|
fs.writeFileSync(join(packagePath.name, 'testtheme', 'package.json'), pkgJson);
|
2020-10-09 16:52:25 +03:00
|
|
|
fs.writeFileSync(join(packagePath.name, 'testtheme', 'index.hbs'), '');
|
2020-03-30 18:26:47 +03:00
|
|
|
|
2020-11-24 00:48:32 +03:00
|
|
|
packageJSON.readPackages(packagePath.name)
|
2020-03-30 18:26:47 +03:00
|
|
|
.then(function (pkgs) {
|
|
|
|
pkgs.should.eql({
|
|
|
|
testtheme: {
|
|
|
|
name: 'testtheme',
|
|
|
|
path: join(packagePath.name, 'testtheme'),
|
|
|
|
'package.json': null
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
done();
|
|
|
|
})
|
|
|
|
.catch(done)
|
|
|
|
.finally(packagePath.removeCallback);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-06-09 16:11:23 +03:00
|
|
|
describe('readPackage', function () {
|
2020-03-30 18:26:47 +03:00
|
|
|
it('should read directory and ignore unneeded items', function (done) {
|
2020-04-29 18:44:27 +03:00
|
|
|
const packagePath = tmp.dirSync({unsafeCleanup: true});
|
2020-03-30 18:26:47 +03:00
|
|
|
|
|
|
|
// create example theme
|
|
|
|
fs.mkdirSync(join(packagePath.name, 'casper'));
|
2020-10-09 16:52:25 +03:00
|
|
|
fs.writeFileSync(join(packagePath.name, 'casper', 'index.hbs'), '');
|
2020-03-30 18:26:47 +03:00
|
|
|
|
|
|
|
// create some trash
|
|
|
|
fs.mkdirSync(join(packagePath.name, 'node_modules'));
|
|
|
|
fs.mkdirSync(join(packagePath.name, 'bower_components'));
|
|
|
|
fs.mkdirSync(join(packagePath.name, '.git'));
|
2020-10-09 16:52:25 +03:00
|
|
|
fs.writeFileSync(join(packagePath.name, '.DS_Store'), '');
|
2020-03-30 18:26:47 +03:00
|
|
|
|
2020-11-24 00:48:32 +03:00
|
|
|
packageJSON.readPackage(packagePath.name, 'casper')
|
2020-03-30 18:26:47 +03:00
|
|
|
.then(function (pkgs) {
|
|
|
|
pkgs.should.eql({
|
|
|
|
casper: {
|
|
|
|
name: 'casper',
|
|
|
|
path: join(packagePath.name, 'casper'),
|
|
|
|
'package.json': null
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
done();
|
|
|
|
})
|
|
|
|
.catch(done)
|
|
|
|
.finally(packagePath.removeCallback);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should read directory and parse package.json files', function (done) {
|
2020-04-29 18:44:27 +03:00
|
|
|
let packagePath;
|
|
|
|
let pkgJson;
|
2020-03-30 18:26:47 +03:00
|
|
|
|
|
|
|
packagePath = tmp.dirSync({unsafeCleanup: true});
|
|
|
|
pkgJson = JSON.stringify({
|
|
|
|
name: 'test',
|
|
|
|
version: '0.0.0'
|
|
|
|
});
|
|
|
|
|
|
|
|
// create example theme
|
|
|
|
fs.mkdirSync(join(packagePath.name, 'testtheme'));
|
|
|
|
fs.writeFileSync(join(packagePath.name, 'testtheme', 'package.json'), pkgJson);
|
2020-10-09 16:52:25 +03:00
|
|
|
fs.writeFileSync(join(packagePath.name, 'testtheme', 'index.hbs'), '');
|
2020-03-30 18:26:47 +03:00
|
|
|
|
2020-11-24 00:48:32 +03:00
|
|
|
packageJSON.readPackage(packagePath.name, 'testtheme')
|
2020-03-30 18:26:47 +03:00
|
|
|
.then(function (pkgs) {
|
|
|
|
pkgs.should.eql({
|
|
|
|
testtheme: {
|
|
|
|
name: 'testtheme',
|
|
|
|
path: join(packagePath.name, 'testtheme'),
|
|
|
|
'package.json': {
|
|
|
|
name: 'test',
|
|
|
|
version: '0.0.0'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
done();
|
|
|
|
})
|
|
|
|
.catch(done)
|
|
|
|
.finally(packagePath.removeCallback);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should read directory and ignore invalid package.json files', function (done) {
|
2020-04-29 18:44:27 +03:00
|
|
|
let packagePath;
|
|
|
|
let pkgJson;
|
2020-03-30 18:26:47 +03:00
|
|
|
|
|
|
|
packagePath = tmp.dirSync({unsafeCleanup: true});
|
|
|
|
pkgJson = JSON.stringify({
|
|
|
|
name: 'test'
|
|
|
|
});
|
|
|
|
|
|
|
|
// create example theme
|
|
|
|
fs.mkdirSync(join(packagePath.name, 'testtheme'));
|
|
|
|
fs.writeFileSync(join(packagePath.name, 'testtheme', 'package.json'), pkgJson);
|
2020-10-09 16:52:25 +03:00
|
|
|
fs.writeFileSync(join(packagePath.name, 'testtheme', 'index.hbs'), '');
|
2020-03-30 18:26:47 +03:00
|
|
|
|
2020-11-24 00:48:32 +03:00
|
|
|
packageJSON.readPackage(packagePath.name, 'testtheme')
|
2020-03-30 18:26:47 +03:00
|
|
|
.then(function (pkgs) {
|
|
|
|
pkgs.should.eql({
|
|
|
|
testtheme: {
|
|
|
|
name: 'testtheme',
|
|
|
|
path: join(packagePath.name, 'testtheme'),
|
|
|
|
'package.json': null
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
done();
|
|
|
|
})
|
|
|
|
.catch(done)
|
|
|
|
.finally(packagePath.removeCallback);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should read directory and include only single requested package', function (done) {
|
2020-04-29 18:44:27 +03:00
|
|
|
const packagePath = tmp.dirSync({unsafeCleanup: true});
|
2020-03-30 18:26:47 +03:00
|
|
|
|
|
|
|
// create trash
|
2020-10-09 16:52:25 +03:00
|
|
|
fs.writeFileSync(join(packagePath.name, 'casper.zip'), '');
|
|
|
|
fs.writeFileSync(join(packagePath.name, '.DS_Store'), '');
|
2020-03-30 18:26:47 +03:00
|
|
|
|
|
|
|
// create actual theme
|
|
|
|
fs.mkdirSync(join(packagePath.name, 'casper'));
|
|
|
|
fs.mkdirSync(join(packagePath.name, 'casper', 'partials'));
|
2020-10-09 16:52:25 +03:00
|
|
|
fs.writeFileSync(join(packagePath.name, 'casper', 'index.hbs'), '');
|
|
|
|
fs.writeFileSync(join(packagePath.name, 'casper', 'partials', 'navigation.hbs'), '');
|
2020-03-30 18:26:47 +03:00
|
|
|
fs.mkdirSync(join(packagePath.name, 'not-casper'));
|
2020-10-09 16:52:25 +03:00
|
|
|
fs.writeFileSync(join(packagePath.name, 'not-casper', 'index.hbs'), '');
|
2020-03-30 18:26:47 +03:00
|
|
|
|
2020-11-24 00:48:32 +03:00
|
|
|
packageJSON.readPackage(packagePath.name, 'casper')
|
2020-03-30 18:26:47 +03:00
|
|
|
.then(function (pkgs) {
|
|
|
|
pkgs.should.eql({
|
|
|
|
casper: {
|
|
|
|
name: 'casper',
|
|
|
|
path: join(packagePath.name, 'casper'),
|
|
|
|
'package.json': null
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
done();
|
|
|
|
})
|
|
|
|
.catch(done)
|
|
|
|
.finally(packagePath.removeCallback);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return an error if package cannot be found', function (done) {
|
2020-04-29 18:44:27 +03:00
|
|
|
const packagePath = tmp.dirSync({unsafeCleanup: true});
|
2020-03-30 18:26:47 +03:00
|
|
|
|
|
|
|
// create trash
|
2020-10-09 16:52:25 +03:00
|
|
|
fs.writeFileSync(join(packagePath.name, 'casper.zip'), '');
|
|
|
|
fs.writeFileSync(join(packagePath.name, '.DS_Store'), '');
|
2020-03-30 18:26:47 +03:00
|
|
|
|
2020-11-24 00:48:32 +03:00
|
|
|
packageJSON.readPackage(packagePath.name, 'casper')
|
2020-03-30 18:26:47 +03:00
|
|
|
.then(function () {
|
|
|
|
done('Should have thrown an error');
|
|
|
|
})
|
|
|
|
.catch(function (err) {
|
|
|
|
err.message.should.eql('Package not found');
|
|
|
|
done();
|
|
|
|
})
|
|
|
|
.finally(packagePath.removeCallback);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return empty object if package is not a directory', function (done) {
|
2020-04-29 18:44:27 +03:00
|
|
|
const packagePath = tmp.dirSync({unsafeCleanup: true});
|
2020-03-30 18:26:47 +03:00
|
|
|
|
|
|
|
// create trash
|
2020-10-09 16:52:25 +03:00
|
|
|
fs.writeFileSync(join(packagePath.name, 'casper.zip'), '');
|
|
|
|
fs.writeFileSync(join(packagePath.name, '.DS_Store'), '');
|
2020-03-30 18:26:47 +03:00
|
|
|
|
2020-11-24 00:48:32 +03:00
|
|
|
packageJSON.readPackage(packagePath.name, 'casper.zip')
|
2020-03-30 18:26:47 +03:00
|
|
|
.then(function (pkg) {
|
|
|
|
pkg.should.eql({});
|
|
|
|
|
|
|
|
done();
|
|
|
|
})
|
|
|
|
.catch(done)
|
|
|
|
.finally(packagePath.removeCallback);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|