From e141208605652fd1ffa2ff9319e5cca001fd8fff Mon Sep 17 00:00:00 2001 From: David Taylor Date: Thu, 25 Aug 2022 09:43:13 +0100 Subject: [PATCH] DEV: Compile plugin tests using ember-cli (#18074) For now, `EMBER_CLI_PLUGIN_ASSETS` can be set to 0 to restore the old behavior. This option will be removed very soon. --- .../javascripts/discourse-plugins/index.js | 30 ++++++++++++++++++- .../discourse/lib/bootstrap-json/index.js | 14 ++++++++- 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/app/assets/javascripts/discourse-plugins/index.js b/app/assets/javascripts/discourse-plugins/index.js index 8e6742b87a1..d2bff2ae3e8 100644 --- a/app/assets/javascripts/discourse-plugins/index.js +++ b/app/assets/javascripts/discourse-plugins/index.js @@ -78,12 +78,20 @@ module.exports = { return pluginDirectories.map((directory) => { const name = directory.name; const jsDirectory = path.resolve(root, name, "assets/javascripts"); + const testDirectory = path.resolve(root, name, "test/javascripts"); const hasJs = fs.existsSync(jsDirectory); - return { name, jsDirectory, hasJs }; + const hasTests = fs.existsSync(testDirectory); + return { name, jsDirectory, testDirectory, hasJs, hasTests }; }); }, generatePluginsTree() { + const appTree = this._generatePluginAppTree(); + const testTree = this._generatePluginTestTree(); + return mergeTrees([appTree, testTree]); + }, + + _generatePluginAppTree() { const trees = this.pluginInfos() .filter((p) => p.hasJs) .map(({ name, jsDirectory }) => { @@ -101,6 +109,26 @@ module.exports = { return concat(mergeTrees([tree]), { inputFiles: ["**/*.js"], outputFile: `assets/plugins/${name}.js`, + allowNone: true, + }); + }); + return mergeTrees(trees); + }, + + _generatePluginTestTree() { + const trees = this.pluginInfos() + .filter((p) => p.hasTests) + .map(({ name, testDirectory }) => { + let tree = new WatchedDir(testDirectory); + + tree = fixLegacyExtensions(tree); + tree = namespaceModules(tree, name); + tree = this.processedAddonJsFiles(tree); + + return concat(mergeTrees([tree]), { + inputFiles: ["**/*.js"], + outputFile: `assets/plugins/test/${name}_tests.js`, + allowNone: true, }); }); return mergeTrees(trees); diff --git a/app/assets/javascripts/discourse/lib/bootstrap-json/index.js b/app/assets/javascripts/discourse/lib/bootstrap-json/index.js index 238cf1e734b..38d273fd5da 100644 --- a/app/assets/javascripts/discourse/lib/bootstrap-json/index.js +++ b/app/assets/javascripts/discourse/lib/bootstrap-json/index.js @@ -411,7 +411,19 @@ module.exports = { ) .join("\n"); } else if (shouldLoadPluginTestJs() && type === "test-plugin-tests-js") { - return ``; + if (process.env.EMBER_CLI_PLUGIN_ASSETS !== "0") { + return this.app.project + .findAddonByName("discourse-plugins") + .pluginInfos() + .filter(({ hasTests }) => hasTests) + .map( + ({ name }) => + `` + ) + .join("\n"); + } else { + return ``; + } } },