From dc324b36b2aef337f17a3070b24ed33f92d40b96 Mon Sep 17 00:00:00 2001
From: Peter Jaszkowiak
Date: Wed, 19 Jul 2017 11:38:51 -0600
Subject: [PATCH] Add test for Plugins.showInstalled
---
src/plugins.js | 11 ++++++-----
.../@nodebb/another-thing/package.json | 1 +
.../@nodebb/another-thing/plugin.json | 1 +
.../@nodebb/nodebb-plugin-abc/package.json | 1 +
.../@nodebb/nodebb-plugin-abc/plugin.json | 1 +
.../nodebb-plugin-xyz/package.json | 1 +
.../nodebb-plugin-xyz/plugin.json | 1 +
.../plugin_modules/something-else/package.json | 1 +
.../plugin_modules/something-else/plugin.json | 1 +
test/plugins.js | 17 +++++++++++++++++
10 files changed, 31 insertions(+), 5 deletions(-)
create mode 100644 test/mocks/plugin_modules/@nodebb/another-thing/package.json
create mode 100644 test/mocks/plugin_modules/@nodebb/another-thing/plugin.json
create mode 100644 test/mocks/plugin_modules/@nodebb/nodebb-plugin-abc/package.json
create mode 100644 test/mocks/plugin_modules/@nodebb/nodebb-plugin-abc/plugin.json
create mode 100644 test/mocks/plugin_modules/nodebb-plugin-xyz/package.json
create mode 100644 test/mocks/plugin_modules/nodebb-plugin-xyz/plugin.json
create mode 100644 test/mocks/plugin_modules/something-else/package.json
create mode 100644 test/mocks/plugin_modules/something-else/plugin.json
diff --git a/src/plugins.js b/src/plugins.js
index de328887eb..41cd1da7b8 100644
--- a/src/plugins.js
+++ b/src/plugins.js
@@ -297,25 +297,26 @@ Plugins.normalise = function (apiReturn, callback) {
});
};
+Plugins.nodeModulesPath = path.join(__dirname, '../node_modules');
+
Plugins.showInstalled = function (callback) {
- var nodeModulesPath = path.join(__dirname, '../node_modules');
var pluginNamePattern = /^(@.*?\/)?nodebb-(theme|plugin|widget|rewards)-.*$/;
async.waterfall([
function (next) {
- fs.readdir(nodeModulesPath, next);
+ fs.readdir(Plugins.nodeModulesPath, next);
},
function (dirs, next) {
var pluginPaths = [];
async.each(dirs, function (dirname, next) {
- var dirPath = path.join(nodeModulesPath, dirname);
+ var dirPath = path.join(Plugins.nodeModulesPath, dirname);
async.waterfall([
function (cb) {
fs.stat(dirPath, function (err, stats) {
if (err && err.code !== 'ENOENT') {
- return next(err);
+ return cb(err);
}
if (err || !stats.isDirectory()) {
return next();
@@ -361,7 +362,7 @@ Plugins.showInstalled = function (callback) {
function (dirs, next) {
dirs = dirs.map(function (dir) {
- return path.join(nodeModulesPath, dir);
+ return path.join(Plugins.nodeModulesPath, dir);
});
var plugins = [];
diff --git a/test/mocks/plugin_modules/@nodebb/another-thing/package.json b/test/mocks/plugin_modules/@nodebb/another-thing/package.json
new file mode 100644
index 0000000000..9e26dfeeb6
--- /dev/null
+++ b/test/mocks/plugin_modules/@nodebb/another-thing/package.json
@@ -0,0 +1 @@
+{}
\ No newline at end of file
diff --git a/test/mocks/plugin_modules/@nodebb/another-thing/plugin.json b/test/mocks/plugin_modules/@nodebb/another-thing/plugin.json
new file mode 100644
index 0000000000..9e26dfeeb6
--- /dev/null
+++ b/test/mocks/plugin_modules/@nodebb/another-thing/plugin.json
@@ -0,0 +1 @@
+{}
\ No newline at end of file
diff --git a/test/mocks/plugin_modules/@nodebb/nodebb-plugin-abc/package.json b/test/mocks/plugin_modules/@nodebb/nodebb-plugin-abc/package.json
new file mode 100644
index 0000000000..9e26dfeeb6
--- /dev/null
+++ b/test/mocks/plugin_modules/@nodebb/nodebb-plugin-abc/package.json
@@ -0,0 +1 @@
+{}
\ No newline at end of file
diff --git a/test/mocks/plugin_modules/@nodebb/nodebb-plugin-abc/plugin.json b/test/mocks/plugin_modules/@nodebb/nodebb-plugin-abc/plugin.json
new file mode 100644
index 0000000000..9e26dfeeb6
--- /dev/null
+++ b/test/mocks/plugin_modules/@nodebb/nodebb-plugin-abc/plugin.json
@@ -0,0 +1 @@
+{}
\ No newline at end of file
diff --git a/test/mocks/plugin_modules/nodebb-plugin-xyz/package.json b/test/mocks/plugin_modules/nodebb-plugin-xyz/package.json
new file mode 100644
index 0000000000..9e26dfeeb6
--- /dev/null
+++ b/test/mocks/plugin_modules/nodebb-plugin-xyz/package.json
@@ -0,0 +1 @@
+{}
\ No newline at end of file
diff --git a/test/mocks/plugin_modules/nodebb-plugin-xyz/plugin.json b/test/mocks/plugin_modules/nodebb-plugin-xyz/plugin.json
new file mode 100644
index 0000000000..9e26dfeeb6
--- /dev/null
+++ b/test/mocks/plugin_modules/nodebb-plugin-xyz/plugin.json
@@ -0,0 +1 @@
+{}
\ No newline at end of file
diff --git a/test/mocks/plugin_modules/something-else/package.json b/test/mocks/plugin_modules/something-else/package.json
new file mode 100644
index 0000000000..9e26dfeeb6
--- /dev/null
+++ b/test/mocks/plugin_modules/something-else/package.json
@@ -0,0 +1 @@
+{}
\ No newline at end of file
diff --git a/test/mocks/plugin_modules/something-else/plugin.json b/test/mocks/plugin_modules/something-else/plugin.json
new file mode 100644
index 0000000000..9e26dfeeb6
--- /dev/null
+++ b/test/mocks/plugin_modules/something-else/plugin.json
@@ -0,0 +1 @@
+{}
\ No newline at end of file
diff --git a/test/plugins.js b/test/plugins.js
index caccbe2851..40c6c9c97f 100644
--- a/test/plugins.js
+++ b/test/plugins.js
@@ -94,6 +94,23 @@ describe('Plugins', function () {
});
});
+ it('should show installed plugins', function (done) {
+ var nodeModulesPath = plugins.nodeModulesPath;
+ plugins.nodeModulesPath = path.join(__dirname, './mocks/plugin_modules');
+
+ plugins.showInstalled(function (err, pluginsData) {
+ assert.ifError(err);
+ var paths = pluginsData.map(function (plugin) {
+ return path.relative(plugins.nodeModulesPath, plugin.path).replace(/\\/g, '/');
+ });
+ assert(paths.indexOf('nodebb-plugin-xyz') > -1);
+ assert(paths.indexOf('@nodebb/nodebb-plugin-abc') > -1);
+
+ plugins.nodeModulesPath = nodeModulesPath;
+ done();
+ });
+ });
+
describe('install/activate/uninstall', function () {
var latest;
var pluginName = 'nodebb-plugin-imgur';