Skip to content

A platform for community discussion. Free, open, simple.

DEV: Only load specific plugin bundles during qunit test (#33678)

David Taylor5ba28911cafc
diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml
index d333e3c5bd9..25269b0450f 100644
--- a/.github/workflows/tests.yml
+++ b/.github/workflows/tests.yml
@@ -279,7 +279,7 @@ jobs:
- name: Plugin QUnit
if: matrix.build_type == 'frontend' && matrix.target == 'plugins'
- run: QUNIT_WRITE_EXECUTION_FILE=1 bin/rake plugin:qunit['*']
+ run: bin/rake plugin:qunit['*']
- name: Theme QUnit
if: matrix.build_type == 'frontend' && matrix.target == 'themes'
diff --git a/app/assets/javascripts/discourse-plugins/index.js b/app/assets/javascripts/discourse-plugins/index.js
index 624a9ab0c8c..dc5a3aefb3a 100644
--- a/app/assets/javascripts/discourse-plugins/index.js
+++ b/app/assets/javascripts/discourse-plugins/index.js
@@ -78,6 +78,20 @@ function parsePluginName(pluginRbPath) {
);
}
+function getTestRequiredPlugins(aboutJsonPath) {
+ if (fs.existsSync(aboutJsonPath)) {
+ const aboutJson = JSON.parse(fs.readFileSync(aboutJsonPath, "utf8"));
+ const requiredPlugins = aboutJson.tests?.requiredPlugins || [];
+ return requiredPlugins.map((plugin) =>
+ plugin
+ .split("/")
+ .at(-1)
+ .replace(/\.git$/, "")
+ );
+ }
+ return [];
+}
+
module.exports = {
name: require("./package").name,
@@ -125,6 +139,9 @@ module.exports = {
const hasAdminJs = fs.existsSync(adminJsDirectory);
const hasTests = fs.existsSync(testDirectory);
const hasConfig = fs.existsSync(configDirectory);
+ const testRequiredPlugins = getTestRequiredPlugins(
+ path.resolve(root, directoryName, "about.json")
+ );
return {
pluginName,
directoryName,
@@ -136,6 +153,7 @@ module.exports = {
hasAdminJs,
hasTests,
hasConfig,
+ testRequiredPlugins,
};
});
},
@@ -279,12 +297,21 @@ module.exports = {
}
}
- return scripts
+ const scriptTags = scripts
.map(
({ src, name }) =>
`<script src="${config.rootURL}assets/${src}" data-discourse-plugin="${name}"></script>`
)
.join("\n");
+
+ const requiredPluginInfos = {};
+ for (const { pluginName, testRequiredPlugins } of pluginInfos) {
+ requiredPluginInfos[pluginName] = testRequiredPlugins;
+ }
+
+ const requiredPluginInfoTag = `<script type="application/json" id="discourse-required-plugin-info">${JSON.stringify(requiredPluginInfos)}</script>`;
+
+ return `${scriptTags}\n${requiredPluginInfoTag}`;
},
pluginTestScriptTags(config) {
diff --git a/app/assets/javascripts/discourse/public/assets/scripts/discourse-test-load-dynamic-js.js b/app/assets/javascripts/discourse/public/assets/scripts/discourse-test-load-dynamic-js.js
index 5f461030810..b98165b9c2e 100644
--- a/app/assets/javascripts/discourse/public/assets/scripts/discourse-test-load-dynamic-js.js
+++ b/app/assets/javascripts/discourse/public/assets/scripts/discourse-test-load-dynamic-js.js
@@ -1,12 +1,33 @@
const dynamicJsTemplate = document.querySelector("#dynamic-test-js");
const params = new URLSearchParams(document.location.search);
-const target = params.get("target");
-const skipPlugins = !target || target === "core";
+const target = params.get("target") || "core";
+
+// Same list maintained in qunit_controller.rb
+const alwaysRequiredPlugins = ["discourse-local-dates"];
+
+const requiredPluginInfo = JSON.parse(
+ dynamicJsTemplate.content.querySelector("#discourse-required-plugin-info")
+ ?.innerHTML || "{}"
+);
(async function setup() {
for (const element of dynamicJsTemplate.content.childNodes) {
- if (skipPlugins && element.dataset?.discoursePlugin) {
+ const pluginName = element.dataset?.discoursePlugin;
+
+ if (pluginName && target === "core") {
+ continue;
+ }
+
+ const shouldLoad =
+ !pluginName ||
+ ["all", "plugins"].includes(target) ||
+ pluginName === "_all" ||
+ target === pluginName ||
+ alwaysRequiredPlugins.includes(pluginName) ||
+ requiredPluginInfo[target]?.includes(pluginName);
+
+ if (!shouldLoad) {
continue;
}
diff --git a/app/assets/javascripts/discourse/testem.js b/app/assets/javascripts/discourse/testem.js
index da2dfaa5175..699f781dab2 100644
--- a/app/assets/javascripts/discourse/testem.js
+++ b/app/assets/javascripts/discourse/testem.js
@@ -136,7 +136,7 @@ module.exports = {
disable_watching: true,
launch_in_ci: [process.env.TESTEM_DEFAULT_BROWSER || "Chrome"],
tap_failed_tests_only: false,
- parallel: -1,
+ parallel: parseInt(process.env.QUNIT_PARALLEL || 1, 10),
browser_start_timeout: 120,
browser_args: {
Chromium: [
@@ -186,6 +186,13 @@ fetch(`${target}/about.json`).catch(() => {
);
});
+const pluginTestPages = process.env.PLUGIN_TARGETS;
+if (pluginTestPages) {
+ module.exports.test_page = pluginTestPages.split(",").map((plugin) => {
+ return `tests/index.html?hidepassed&target=${plugin}`;
+ });
+}
+
const themeTestPages = process.env.THEME_TEST_PAGES;
if (themeTestPages) {
diff --git a/app/assets/javascripts/discourse/tests/setup-tests.js b/app/assets/javascripts/discourse/tests/setup-tests.js
index 3a9d181b2c4..efee311e40f 100644
--- a/app/assets/javascripts/discourse/tests/setup-tests.js
+++ b/app/assets/javascripts/discourse/tests/setup-tests.js
@@ -155,6 +155,8 @@ function setupToolbar() {
select.value ||= "core";
select.querySelector("option:not([value])").remove();
select.querySelector("option[value=-----]").disabled = true;
+ select.querySelector("option[value=plugins]").innerText =
+ "all plugins (not recommended)";
select.querySelector("option[value=all]").innerText =
"all (not recommended)";
});
@@ -213,6 +215,16 @@ export default function setupTests(config) {
QUnit.config.hidepassed = true;
QUnit.config.testTimeout = 60_000;
+ window.onunhandledrejection = (event) => {
+ // reports test boot failures to testem, so the browser doesn't hang forever
+ window.Testem?.emit(
+ "top-level-error",
+ event.reason,
+ window.location.href,
+ "0"
+ );
+ };
+
sinon.config = {
injectIntoThis: false,
injectInto: null,
diff --git a/app/controllers/qunit_controller.rb b/app/controllers/qunit_controller.rb
index 305ed4a0a7c..170e5bc3b68 100644
--- a/app/controllers/qunit_controller.rb
+++ b/app/controllers/qunit_controller.rb
@@ -1,6 +1,9 @@
# frozen_string_literal: true
class QunitController < ApplicationController
+ # Same list maintained in discourse-test-load-dynamic-js.js
+ ALWAYS_LOADED_PLUGINS = %w[discourse-local-dates]
+
skip_before_action *%i[
check_xhr
preload_json
@@ -45,6 +48,13 @@ class QunitController < ApplicationController
return
end
+ about_json =
+ JSON.parse(theme.theme_fields.find_by(target_id: Theme.targets[:about])&.value || "{}")
+ @required_plugins =
+ about_json
+ .dig("tests", "requiredPlugins")
+ &.map { |p| p.split("/").last.delete_suffix(".git") } || []
+
request.env[:resolved_theme_id] = theme.id
request.env[:skip_theme_ids_transformation] = true
end
diff --git a/app/models/theme.rb b/app/models/theme.rb
index 7a9b01751f0..fecbd592eb8 100644
--- a/app/models/theme.rb
+++ b/app/models/theme.rb
@@ -473,6 +473,7 @@ class Theme < ActiveRecord::Base
extra_js: 6,
tests_js: 7,
migrations: 8,
+ about: 9,
)
end
diff --git a/app/models/theme_field.rb b/app/models/theme_field.rb
index 69355ab1b1e..33f04939739 100644
--- a/app/models/theme_field.rb
+++ b/app/models/theme_field.rb
@@ -96,6 +96,7 @@ class ThemeField < ActiveRecord::Base
yaml: 5,
js: 6,
theme_screenshot_upload_var: 7,
+ json: 8,
)
end
@@ -697,6 +698,13 @@ class ThemeField < ActiveRecord::Base
types: :js,
canonical: ->(h) { "test/#{h[:name]}" },
),
+ ThemeFileMatcher.new(
+ regex: /\Aabout\.json\z/,
+ names: "about",
+ types: :json,
+ targets: :about,
+ canonical: ->(h) { "about.json" },
+ ),
ThemeFileMatcher.new(
regex: /\Asettings\.ya?ml\z/,
names: "yaml",
diff --git a/app/views/qunit/theme.html.erb b/app/views/qunit/theme.html.erb
index aa1207739d6..c6feabd5d8b 100644
--- a/app/views/qunit/theme.html.erb
+++ b/app/views/qunit/theme.html.erb
@@ -13,7 +13,7 @@
<%= preload_script_url ExtraLocalesController.url("main") %>
<%= preload_script_url ExtraLocalesController.url("mf") %>
<%= preload_script "admin" %>
- <%- Discourse.find_plugin_js_assets(include_disabled: true, include_admin_asset: true).each do |file| %>
+ <%- Discourse.find_plugin_js_assets(include_disabled: true, include_admin_asset: true, only: @required_plugins).each do |file| %>
<%= preload_script file %>
<%- end %>
<%= preload_script "test-site-settings" %>
@@ -33,6 +33,10 @@
<%= discourse_stylesheet_link_tag(:common, theme_id: nil) %>
<%= discourse_stylesheet_link_tag(:desktop, theme_id: nil) %>
+
+ <%- Discourse.find_plugin_css_assets(include_disabled: true, only: @required_plugins, mobile_view: false, desktop_view: true, request: request).each do |file| %>
+ <%= discourse_stylesheet_link_tag(file) %>
+ <%- end %>
<%- else %>
<style>
html {
diff --git a/lib/discourse.rb b/lib/discourse.rb
index 8775afa14e5..0bb31da4856 100644
--- a/lib/discourse.rb
+++ b/lib/discourse.rb
@@ -397,6 +397,7 @@ module Discourse
next if args[:include_official] == false && plugin.metadata.official?
next if args[:include_unofficial] == false && !plugin.metadata.official?
next if !args[:include_disabled] && !plugin.enabled?
+ next if args[:only] && !args[:only].include?(plugin.directory_name)
true
end
diff --git a/lib/tasks/plugin.rake b/lib/tasks/plugin.rake
index dc92bcf4f3e..ff28a31f0bf 100644
--- a/lib/tasks/plugin.rake
+++ b/lib/tasks/plugin.rake
@@ -232,16 +232,15 @@ task "plugin:qunit", :plugin do |t, args|
cmd = "LOAD_PLUGINS=1 "
- target =
- if args[:plugin] == "*"
- puts "Running qunit tests for all plugins"
- "plugins"
- else
- puts "Running qunit tests for #{args[:plugin]}"
- args[:plugin]
- end
+ if args[:plugin] == "*"
+ plugin_names = Dir.glob("plugins/*/test/**/*-test.js").map { |file| file.split("/")[1] }.uniq
+ puts "Running qunit tests for all plugins: #{plugin_names.join(", ")}"
+ cmd += "PLUGIN_TARGETS='#{plugin_names.join(",")}' "
+ else
+ puts "Running qunit tests for #{args[:plugin]}"
+ cmd += "TARGET='#{args[:plugin]}' "
+ end
- cmd += "TARGET='#{target}' "
cmd += "#{rake} qunit:test"
system cmd
diff --git a/lib/tasks/qunit.rake b/lib/tasks/qunit.rake
index 77a4a195e63..f7e935fb3e5 100644
--- a/lib/tasks/qunit.rake
+++ b/lib/tasks/qunit.rake
@@ -129,7 +129,7 @@ task "qunit:test", %i[qunit_path filter] do |_, args|
cmd += ["--parallel", parallel] if parallel
else
cmd += ["pnpm", "ember", "exam", "--query", query]
- cmd += ["--load-balance", "--parallel", parallel] if parallel
+ cmd += ["--load-balance", "--parallel", parallel] if parallel && !ENV["PLUGIN_TARGETS"]
cmd += ["--filter", filter] if filter
cmd << "--write-execution-file" if ENV["QUNIT_WRITE_EXECUTION_FILE"]
end
diff --git a/plugins/chat/about.json b/plugins/chat/about.json
new file mode 100644
index 00000000000..15309d5504d
--- /dev/null
+++ b/plugins/chat/about.json
@@ -0,0 +1,8 @@
+{
+ "tests": {
+ "requiredPlugins": [
+ "discourse-lazy-videos",
+ "spoiler-alert"
+ ]
+ }
+}
\ No newline at end of file
diff --git a/spec/models/remote_theme_spec.rb b/spec/models/remote_theme_spec.rb
index 9b807c08c08..c3a455b66fe 100644
--- a/spec/models/remote_theme_spec.rb
+++ b/spec/models/remote_theme_spec.rb
@@ -206,7 +206,7 @@ RSpec.describe RemoteTheme do
expect(theme.theme_modifier_set.serialize_topic_excerpts).to eq(true)
expect(theme.theme_modifier_set.custom_homepage).to eq(true)
- expect(theme.theme_fields.length).to eq(14)
+ expect(theme.theme_fields.length).to eq(15)
mapped = Hash[*theme.theme_fields.map { |f| ["#{f.target_id}-#{f.name}", f.value] }.flatten]
@@ -227,7 +227,7 @@ RSpec.describe RemoteTheme do
"export default function migrate(settings) {\n return settings;\n}\n",
)
- expect(mapped.length).to eq(14)
+ expect(mapped.length).to eq(15)
expect(theme.settings.length).to eq(2)
expect(theme.settings[:boolean_setting].value).to eq(true)
@@ -381,7 +381,7 @@ RSpec.describe RemoteTheme do
RemoteTheme::ImportError,
I18n.t(
"themes.import_error.asset_too_big",
- filename: "common/color_definitions.scss",
+ filename: "about.json",
limit: ActiveSupport::NumberHelper.number_to_human_size(1),
),
)
@@ -759,7 +759,7 @@ RSpec.describe RemoteTheme do
theme = RemoteTheme.import_theme_from_directory(theme_dir)
expect(theme.name).to eq("Header Icons")
- expect(theme.theme_fields.count).to eq(6)
+ expect(theme.theme_fields.count).to eq(7)
end
end
end
diff --git a/spec/requests/admin/themes_controller_spec.rb b/spec/requests/admin/themes_controller_spec.rb
index 723f32ea14d..6d49c16efc5 100644
--- a/spec/requests/admin/themes_controller_spec.rb
+++ b/spec/requests/admin/themes_controller_spec.rb
@@ -138,7 +138,7 @@ RSpec.describe Admin::ThemesController do
json = response.parsed_body
expect(json["theme"]["name"]).to eq("Awesome Theme")
- expect(json["theme"]["theme_fields"].length).to eq(3)
+ expect(json["theme"]["theme_fields"].length).to eq(4)
end
end
@@ -337,7 +337,7 @@ RSpec.describe Admin::ThemesController do
json = response.parsed_body
expect(json["theme"]["name"]).to eq("Header Icons")
- expect(json["theme"]["theme_fields"].length).to eq(6)
+ expect(json["theme"]["theme_fields"].length).to eq(7)
expect(json["theme"]["auto_update"]).to eq(false)
expect(UserHistory.where(action: UserHistory.actions[:change_theme]).count).to eq(1)
end
@@ -369,7 +369,7 @@ RSpec.describe Admin::ThemesController do
expect(json["theme"]["name"]).to eq("Some other name")
expect(json["theme"]["id"]).to eq(other_existing_theme.id)
- expect(json["theme"]["theme_fields"].length).to eq(6)
+ expect(json["theme"]["theme_fields"].length).to eq(7)
expect(UserHistory.where(action: UserHistory.actions[:change_theme]).count).to eq(1)
end
@@ -399,7 +399,7 @@ RSpec.describe Admin::ThemesController do
expect(json["theme"]["name"]).to eq("Header Icons")
expect(json["theme"]["id"]).not_to eq(existing_theme.id)
- expect(json["theme"]["theme_fields"].length).to eq(6)
+ expect(json["theme"]["theme_fields"].length).to eq(7)
expect(json["theme"]["auto_update"]).to eq(false)
expect(UserHistory.where(action: UserHistory.actions[:change_theme]).count).to eq(1)
end
diff --git a/spec/tasks/themes_spec.rb b/spec/tasks/themes_spec.rb
index d6a63c4fddb..09756731303 100644
--- a/spec/tasks/themes_spec.rb
+++ b/spec/tasks/themes_spec.rb
@@ -80,7 +80,7 @@ RSpec.describe "tasks/themes" do
theme.reload
- expect(theme.theme_fields.count).to eq(2)
+ expect(theme.theme_fields.count).to eq(3)
expect(theme.theme_fields.where(name: "header").first.value).to eq("I AM A HEADER")
expect(theme.theme_settings_migrations.count).to eq(0)
expect(theme.remote_theme.commits_behind).to eq(0)
@@ -110,7 +110,7 @@ RSpec.describe "tasks/themes" do
theme.reload
- expect(theme.theme_fields.count).to eq(2)
+ expect(theme.theme_fields.count).to eq(3)
expect(theme.theme_fields.where(name: "header").first.value).to eq("I AM A HEADER")
expect(theme.theme_settings_migrations.count).to eq(0)
expect(theme.remote_theme.commits_behind).to eq(0)