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);
|
|
|
|
});
|
2022-04-01 16:07:04 +03:00
|
|
|
|
|
|
|
it('should read directory and include symlinked directories', function (done) {
|
|
|
|
let packagePath;
|
|
|
|
let pkgJson;
|
|
|
|
|
|
|
|
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);
|
|
|
|
fs.writeFileSync(join(packagePath.name, 'testtheme', 'index.hbs'), '');
|
|
|
|
|
|
|
|
// Symlink one theme to the other so we should have 2 themes
|
|
|
|
fs.symlinkSync(join(packagePath.name, 'testtheme'), join(packagePath.name, 'testtheme2'));
|
|
|
|
|
|
|
|
packageJSON.readPackages(packagePath.name)
|
|
|
|
.then(function (pkgs) {
|
|
|
|
pkgs.should.eql({
|
|
|
|
testtheme: {
|
|
|
|
name: 'testtheme',
|
|
|
|
path: join(packagePath.name, 'testtheme'),
|
|
|
|
'package.json': null
|
|
|
|
},
|
|
|
|
testtheme2: {
|
|
|
|
name: 'testtheme2',
|
|
|
|
path: join(packagePath.name, 'testtheme2'),
|
|
|
|
'package.json': null
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
done();
|
|
|
|
})
|
|
|
|
.catch(done)
|
|
|
|
.finally(packagePath.removeCallback);
|
|
|
|
});
|
2023-09-26 13:51:11 +03:00
|
|
|
|
|
|
|
it('should read directory and ignore invalid symlinks', async function () {
|
|
|
|
const packagePath = tmp.dirSync({unsafeCleanup: true});
|
|
|
|
const pkgJson = JSON.stringify({
|
|
|
|
name: 'test'
|
|
|
|
});
|
|
|
|
|
|
|
|
// create example theme
|
|
|
|
fs.mkdirSync(join(packagePath.name, 'testtheme'));
|
|
|
|
fs.writeFileSync(join(packagePath.name, 'testtheme', 'package.json'), pkgJson);
|
|
|
|
fs.writeFileSync(join(packagePath.name, 'testtheme', 'index.hbs'), '');
|
|
|
|
|
|
|
|
// Create a symlink that has a missing source dir
|
|
|
|
fs.symlinkSync(join(packagePath.name, 'missing-dir'), join(packagePath.name, 'source'));
|
|
|
|
|
|
|
|
try {
|
|
|
|
const pkgs = await packageJSON.readPackages(packagePath.name);
|
|
|
|
pkgs.should.eql({
|
|
|
|
testtheme: {
|
|
|
|
name: 'testtheme',
|
|
|
|
path: join(packagePath.name, 'testtheme'),
|
|
|
|
'package.json': null
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} finally {
|
|
|
|
await packagePath.removeCallback();
|
|
|
|
}
|
|
|
|
});
|
2020-03-30 18:26:47 +03:00
|
|
|
});
|
|
|
|
|
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);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|