mirror of
https://github.com/discourse/discourse.git
synced 2025-01-18 15:43:16 +08:00
DEV: Simplify qunit target selection (#22591)
Previously we had three query parameters to control which tests would be run. The default was to run all core/plugin tests together, which would almost always lead to errors and does not match the way we run tests in CI. This commit removes the three old parameters (skip_core, skip_plugins and single_plugin), and introduces a new 'target' parameter. This can have a value of 'core', 'plugins', 'all', or a specific plugin name. The default is 'core'. Attempting to use the old parameters will raise an error.
This commit is contained in:
parent
dfe94ba118
commit
9c915345ea
|
@ -1,7 +1,8 @@
|
|||
const dynamicJsTemplate = document.querySelector("#dynamic-test-js");
|
||||
|
||||
const params = new URLSearchParams(document.location.search);
|
||||
const skipPlugins = params.get("qunit_skip_plugins");
|
||||
const target = params.get("target");
|
||||
const skipPlugins = !target || target === "core";
|
||||
|
||||
(async function setup() {
|
||||
for (const element of dynamicJsTemplate.content.childNodes) {
|
||||
|
|
|
@ -125,31 +125,39 @@ function setupToolbar() {
|
|||
)
|
||||
);
|
||||
|
||||
QUnit.config.urlConfig.push({
|
||||
id: "qunit_skip_core",
|
||||
label: "Skip Core",
|
||||
value: "1",
|
||||
});
|
||||
|
||||
QUnit.config.urlConfig.push({
|
||||
id: "qunit_skip_plugins",
|
||||
label: "Skip Plugins",
|
||||
value: "1",
|
||||
});
|
||||
|
||||
const pluginNames = new Set();
|
||||
|
||||
Object.keys(requirejs.entries).forEach((moduleName) => {
|
||||
const found = moduleName.match(/\/plugins\/([\w-]+)\//);
|
||||
if (found && moduleName.match(/\-test/)) {
|
||||
pluginNames.add(found[1]);
|
||||
}
|
||||
});
|
||||
document
|
||||
.querySelector("#dynamic-test-js")
|
||||
?.content.querySelectorAll("script[data-discourse-plugin]")
|
||||
.forEach((script) => pluginNames.add(script.dataset.discoursePlugin));
|
||||
|
||||
QUnit.config.urlConfig.push({
|
||||
id: "qunit_single_plugin",
|
||||
label: "Plugin",
|
||||
value: Array.from(pluginNames),
|
||||
id: "target",
|
||||
label: "Target",
|
||||
value: ["core", "plugins", "all", "-----", ...Array.from(pluginNames)],
|
||||
});
|
||||
|
||||
QUnit.begin(() => {
|
||||
const select = document.querySelector(
|
||||
`#qunit-testrunner-toolbar [name=target]`
|
||||
);
|
||||
|
||||
const testingThemeId = parseInt(
|
||||
document.querySelector("script[data-theme-id]")?.dataset.themeId,
|
||||
10
|
||||
);
|
||||
if (testingThemeId) {
|
||||
select.innerHTML = `<option selected>theme id ${testingThemeId}</option>`;
|
||||
select.disabled = true;
|
||||
return;
|
||||
}
|
||||
|
||||
select.value ||= "core";
|
||||
select.querySelector("option:not([value])").remove();
|
||||
select.querySelector("option[value=-----]").disabled = true;
|
||||
select.querySelector("option[value=all]").innerText =
|
||||
"all (not recommended)";
|
||||
});
|
||||
|
||||
// Abort tests when the qunit controls are clicked
|
||||
|
@ -346,20 +354,9 @@ export default function setupTests(config) {
|
|||
QUnit.config.autostart = false;
|
||||
}
|
||||
|
||||
let skipCore =
|
||||
getUrlParameter("qunit_single_plugin") ||
|
||||
getUrlParameter("qunit_skip_core") === "1";
|
||||
handleLegacyParameters();
|
||||
|
||||
let singlePlugin = getUrlParameter("qunit_single_plugin");
|
||||
let skipPlugins = !singlePlugin && getUrlParameter("qunit_skip_plugins");
|
||||
|
||||
if (skipCore && !getUrlParameter("qunit_skip_core")) {
|
||||
replaceUrlParameter("qunit_skip_core", "1");
|
||||
}
|
||||
|
||||
if (!skipPlugins && getUrlParameter("qunit_skip_plugins")) {
|
||||
replaceUrlParameter("qunit_skip_plugins", null);
|
||||
}
|
||||
const target = getUrlParameter("target") || "core";
|
||||
|
||||
const shouldLoadModule = (name) => {
|
||||
if (!/\-test/.test(name)) {
|
||||
|
@ -370,13 +367,15 @@ export default function setupTests(config) {
|
|||
const isCore = !isPlugin;
|
||||
const pluginName = name.match(/\/plugins\/([\w-]+)\//)?.[1];
|
||||
|
||||
if (skipCore && isCore) {
|
||||
const loadCore = target === "core" || target === "all";
|
||||
const loadAllPlugins = target === "plugins" || target === "all";
|
||||
|
||||
if (isCore && !loadCore) {
|
||||
return false;
|
||||
} else if (skipPlugins && isPlugin) {
|
||||
return false;
|
||||
} else if (singlePlugin && singlePlugin !== pluginName) {
|
||||
} else if (isPlugin && !(loadAllPlugins || pluginName === target)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
};
|
||||
|
||||
|
@ -402,28 +401,6 @@ function getUrlParameter(name) {
|
|||
return queryParams.get(name);
|
||||
}
|
||||
|
||||
function replaceUrlParameter(name, value) {
|
||||
const queryParams = new URLSearchParams(window.location.search);
|
||||
if (value === null) {
|
||||
queryParams.delete(name);
|
||||
} else {
|
||||
queryParams.set(name, value);
|
||||
}
|
||||
history.replaceState(null, null, "?" + queryParams.toString());
|
||||
|
||||
QUnit.begin(() => {
|
||||
QUnit.config[name] = value;
|
||||
const formElement = document.querySelector(
|
||||
`#qunit-testrunner-toolbar [name=${name}]`
|
||||
);
|
||||
if (formElement?.type === "checkbox") {
|
||||
formElement.checked = !!value;
|
||||
} else if (formElement) {
|
||||
formElement.value = value;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function patchFailedAssertion() {
|
||||
const oldPushResult = QUnit.assert.pushResult;
|
||||
|
||||
|
@ -439,3 +416,19 @@ function patchFailedAssertion() {
|
|||
oldPushResult.call(this, resultInfo);
|
||||
};
|
||||
}
|
||||
|
||||
function handleLegacyParameters() {
|
||||
for (const param of [
|
||||
"qunit_single_plugin",
|
||||
"qunit_skip_core",
|
||||
"qunit_skip_plugins",
|
||||
]) {
|
||||
if (getUrlParameter(param)) {
|
||||
QUnit.begin(() => {
|
||||
throw new Error(
|
||||
`${param} is no longer supported. Use the 'target' parameter instead`
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -25,7 +25,8 @@ document.addEventListener("discourse-booted", () => {
|
|||
}
|
||||
|
||||
const params = new URLSearchParams(window.location.search);
|
||||
const skipCore = params.get("qunit_skip_core") === "1";
|
||||
const target = params.get("target");
|
||||
const testingCore = !target || target === "core";
|
||||
const disableAutoStart = params.get("qunit_disable_auto_start") === "1";
|
||||
|
||||
Ember.ENV.LOG_STACKTRACE_ON_DEPRECATION = false;
|
||||
|
@ -60,7 +61,7 @@ document.addEventListener("discourse-booted", () => {
|
|||
setupTestContainer: false,
|
||||
loadTests: false,
|
||||
startTests: !disableAutoStart,
|
||||
setupEmberOnerrorValidation: !skipCore,
|
||||
setupEmberOnerrorValidation: testingCore,
|
||||
setupTestIsolationValidation: true,
|
||||
});
|
||||
});
|
||||
|
|
|
@ -209,14 +209,17 @@ task "plugin:qunit", %i[plugin timeout] do |t, args|
|
|||
rake = "#{Rails.root}/bin/rake"
|
||||
|
||||
cmd = "LOAD_PLUGINS=1 "
|
||||
cmd += "QUNIT_SKIP_CORE=1 "
|
||||
|
||||
if args[:plugin] == "*"
|
||||
puts "Running qunit tests for all plugins"
|
||||
else
|
||||
puts "Running qunit tests for #{args[:plugin]}"
|
||||
cmd += "QUNIT_SINGLE_PLUGIN='#{args[:plugin]}' "
|
||||
end
|
||||
target =
|
||||
if args[:plugin] == "*"
|
||||
puts "Running qunit tests for all plugins"
|
||||
"plugins"
|
||||
else
|
||||
puts "Running qunit tests for #{args[:plugin]}"
|
||||
args[:plugin]
|
||||
end
|
||||
|
||||
cmd += "TARGET='#{target}' "
|
||||
|
||||
cmd += "#{rake} qunit:test"
|
||||
cmd += "[#{args[:timeout]}]" if args[:timeout]
|
||||
|
|
|
@ -69,6 +69,7 @@ task "qunit:test", %i[timeout qunit_path filter] do |_, args|
|
|||
theme_name
|
||||
theme_url
|
||||
theme_id
|
||||
target
|
||||
].each { |arg| options[arg] = ENV[arg.upcase] if ENV[arg.upcase].present? }
|
||||
|
||||
options["report_requests"] = "1" if report_requests
|
||||
|
|
Loading…
Reference in New Issue
Block a user