A platform for community discussion. Free, open, simple.
DEV: Fix running qunit on test directories (#44689)
Jarek Radoszd6554b2f6f80
diff --git a/bin/qunit b/bin/qunitindex 7e56e64e2ca..56e7205de02 100755--- a/bin/qunit+++ b/bin/qunit@@ -13,9 +13,13 @@ require_relative "../lib/chrome_installed_checker"class QunitRunnerDEFAULT_BROWSER_START_TIMEOUT = 45DEFAULT_BROWSER_INACTIVITY_TIMEOUT = 30+ # the filter ends up in the test page URL; the dev server rejects query strings over 10KB+ MAX_FILE_PATH_FILTER_LENGTH = 8 * 1024+ TEST_FILE_GLOB = "*-test.{js,gjs,ts,gts}"def initialize(args)- @file_paths = []+ @paths = []+ @test_files = []@verbose = ENV["ACTIONS_STEP_DEBUG"] == "true"@dry_run = false@rails_port = ENV["UNICORN_PORT"]&.to_i || 3000@@ -173,7 +177,7 @@ class QunitRunnerensure_supported_target!- @file_paths = args+ @paths = argsdetect_plugin_targets!expand_directory_paths!@@ -203,7 +207,7 @@ class QunitRunner# Check if plugin has teststest_dirs = %w[test/javascripts test].map { |subdir| File.join(plugin_path, subdir) }test_dirs.any? do |test_dir|- Dir.exist?(test_dir) && Dir.glob(File.join(test_dir, "**", "*-test.{js,gjs}")).any?+ Dir.exist?(test_dir) && Dir.glob(File.join(test_dir, "**", TEST_FILE_GLOB)).any?endend.map { |plugin_dir_name| resolve_plugin_namespace(plugin_dir_name) }@@ -212,9 +216,13 @@ class QunitRunnerdef expand_directory_paths!expanded = []- @file_paths.each do |path|+ @paths.each do |path|if File.directory?(path)- test_files = Dir.glob(File.join(path, "**", "*-test.{js,gjs}"))+ if !test_directory?(path)+ abort "Error: #{path} is not a test directory. Pass frontend/discourse/tests, one of its subdirectories, or a plugin directory."+ end++ test_files = Dir.glob(File.join(path, "**", TEST_FILE_GLOB))if test_files.empty?abort "Error: No test files found in directory: #{path}"else@@ -227,21 +235,25 @@ class QunitRunnerendend- @file_paths = expanded.uniq+ @test_files = expanded.uniqend- def run_tests- ensure_file_paths_exist!+ # Directories become path-prefix filters, so they must map onto test module paths+ def test_directory?(path)+ expanded = File.expand_path(path)+ "#{expanded}/".start_with?("#{frontend_dir}/tests/") || plugin_path?(expanded)+ end- if @file_paths && @verbose- puts "Files to test: #{@file_paths.length}"- puts " #{@file_paths.join("\n ")}"+ def run_tests+ if @verbose+ puts "Files to test: #{@test_files.length}"+ puts " #{@test_files.join("\n ")}"endtarget = resolved_targetputs "Target: #{target}" if @verbose- file_path_filter = build_file_path_filter(@file_paths)+ file_path_filter = build_file_path_filter(@paths)run_ember_exam(file_path_filter: file_path_filter, filter: @filter)end@@ -345,15 +357,6 @@ class QunitRunnerfile_pathend- def ensure_file_paths_exist!- @file_paths.each do |file_path|- next if File.exist?(file_path)-- puts "Error: File not found: #{file_path}"- exit 1- end- end-def check_dev_environment!if @dry_runputs "[dry-run] skipping dev environment check"@@ -548,11 +551,29 @@ class QunitRunnerpages.shuffle.join(",")end+ # Directories are passed as prefixes rather than expanded, to keep the filter within+ # MAX_FILE_PATH_FILTER_LENGTHdef build_file_path_filter(paths)- return nil if paths.empty?+ return nil if paths.empty? || paths.any? { |path| core_tests_root?(path) }++ filter = paths.map { |path| file_path_filter_entry(path) }.uniq.join(",")+ return filter if filter.length <= MAX_FILE_PATH_FILTER_LENGTH++ abort <<~MESSAGE+ Error: Too many test files to filter by path. The filter is #{(filter.length / 1024.0).ceil}KB, but the test page URL allows #{MAX_FILE_PATH_FILTER_LENGTH / 1024}KB.+ Pass directories instead, e.g. bin/qunit frontend/discourse/tests/unit+ MESSAGE+ end++ def core_tests_root?(path)+ File.expand_path(path) == "#{frontend_dir}/tests"+ end++ def file_path_filter_entry(path)+ relative = convert_to_ember_relative_path(path)+ return relative if !File.directory?(path)- relative_paths = paths.map { |path| convert_to_ember_relative_path(path) }- relative_paths.join(",")+ plugin_path?(path) ? "#{relative}/" : "./#{relative}/"enddef build_query@@ -594,7 +615,7 @@ class QunitRunnerdef resolved_target@resolved_target ||=begin- implicit_target = (target_from_paths(@file_paths) if @file_paths.any?)+ implicit_target = (target_from_paths(@paths) if @paths.any?)if @target && implicit_target && @target != implicit_targetputs "Files are for '#{implicit_target}', but target was configured as '#{@target}'"@@ -693,7 +714,7 @@ class QunitRunnerenddef normalize_plugin_test_path(path)- path.sub(%r{\Atests?/javascripts/}, "")+ path.to_s.sub(%r{\Atests?/javascripts(?:/|\z)|\Atests?\z}, "")enddef plugin_names_from_filter(filter)diff --git a/frontend/discourse/testem.js b/frontend/discourse/testem.jsindex 3d8b33a5daa..7b55df1718c 100644--- a/frontend/discourse/testem.js+++ b/frontend/discourse/testem.js@@ -286,7 +286,7 @@ module.exports = {"--disable-software-rasterizer","--disable-search-engine-choice-screen","--mute-audio",- `--remote-debugging-port=${process.env.CI ? 0 : 3001}`,+ "--remote-debugging-port=0","--window-size=1440,900","--enable-precise-memory-info","--js-flags=--max_old_space_size=4096",@@ -300,7 +300,7 @@ module.exports = {"--disable-software-rasterizer","--disable-search-engine-choice-screen","--mute-audio",- `--remote-debugging-port=${process.env.CI ? 0 : 3001}`,+ "--remote-debugging-port=0","--window-size=1440,900","--enable-precise-memory-info","--js-flags=--max_old_space_size=4096",diff --git a/spec/tasks/qunit_cli_spec.rb b/spec/tasks/qunit_cli_spec.rbindex c0c20eb1ae0..6dce57c6b76 100644--- a/spec/tasks/qunit_cli_spec.rb+++ b/spec/tasks/qunit_cli_spec.rb@@ -25,6 +25,10 @@ describe "bin/qunit" dolet(:chat_test_file) { Dir.glob("#{Rails.root.join("plugins/chat/test/**/*-test.js")}").first }+ def core_test_files_in(dir)+ Dir.glob(Rails.root.join("frontend/discourse/tests/#{dir}/**/*-test.{js,gjs,ts,gts}")).sort+ end+let(:default_watchdog_env) do{"QUNIT_BROWSER_WATCHDOG" => "1",@@ -184,6 +188,36 @@ describe "bin/qunit" doexpect(result.env).to include("LOAD_PLUGINS" => "1")end+ it "passes core directories as a path prefix" do+ result = run("frontend/discourse/tests/integration/components")+ expect(result.status).to eq(0)+ expect(result.args).to include("--file-path", "./integration/components/")+ end++ it "skips the path filter for the core tests root" do+ result = run("frontend/discourse/tests")+ expect(result.status).to eq(0)+ expect(result.args).not_to include("--file-path")+ end++ it "passes plugin directories as a path prefix" do+ result = run("plugins/chat/test/javascripts")+ expect(result.status).to eq(0)+ expect(result.args).to include("--file-path", "discourse/plugins/chat/")+ end++ it "rejects directories outside the test directories" do+ result = run("frontend/discourse")+ expect(result.status).to eq(1)+ expect(result.err).to include("frontend/discourse is not a test directory")+ end++ it "aborts when a file list is too long for the test page URL" do+ result = run(*core_test_files_in("acceptance"))+ expect(result.status).to eq(1)+ expect(result.err).to include("Too many test files", "Pass directories instead")+ end+it "prevents running files from multiple targets" doresult = run(core_test_file, chat_test_file)expect(result.status).to eq(1)