Skip to content

Commit

Permalink
Updated unit tests (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
andersevenrud committed Apr 11, 2020
1 parent 1416357 commit a63d2bf
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 3 deletions.
13 changes: 13 additions & 0 deletions __mocks__/bent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const fs = require('fs-extra');
const tar = require('tar');
const path = require('path');

module.exports = () => async () => {
const root = path.resolve(__dirname, 'user-installable-package');
const files = await fs.readdir(root);

return tar.c({
gzip: true,
cwd: root
}, files);
};
3 changes: 3 additions & 0 deletions __mocks__/user-installable-package/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "UserInstallablePackage"
}
18 changes: 18 additions & 0 deletions __tests__/packages.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ const osjs = require('osjs');
const path = require('path');
const Packages = require('../src/packages.js');

jest.mock('bent');

describe('Packages', () => {
let core;
let packages;
Expand All @@ -25,6 +27,22 @@ describe('Packages', () => {
.toBe(true);
});

test('#installPackage', async () => {
await expect(packages.installPackage('jest:/UserInstallablePackage.tgz?redacted', {
root: 'home:/.packages'
}, {username: 'packages'})).resolves.toEqual({
reload: true
});
});

test('#uninstallPackage', async () => {
await expect(packages.uninstallPackage('UserInstallablePackage', {
root: 'home:/.packages'
}, {username: 'packages'})).resolves.toEqual({
reload: true
});
});

test('#handleMessage', () => {
const params = [{
pid: 1,
Expand Down
10 changes: 7 additions & 3 deletions src/packages.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,18 +159,22 @@ class Packages {
const name = archiveName(url);
const target = await realpath(`${options.root}/${name}`, user);

if (await fs.exists(target)) {
if (path.resolve(target) === path.resolve(options.root)) {
throw new Error('Invalid package source');
} else if (await fs.exists(target)) {
throw new Error('Target already exists');
} else if (options.system) {
throw new Error('System packages not yet implemented');
}

const stream = await fetchSteam(url, options);
await fs.mkdir(target);

await fs.mkdirp(target);
await extract(stream, target);

// FIXME: npm packages have a 'package' subdirectory
if (!await fs.exists(path.resolve(target, 'metadata.json'))) {
const exists = await fs.exists(path.resolve(target, 'metadata.json'));
if (!exists) {
await fs.remove(target);

throw new Error('Invalid package');
Expand Down

0 comments on commit a63d2bf

Please sign in to comment.