FEATURE: Allow theme tests to be run in production (#12815)

This commit allows site admins to run theme tests in production via a new `/theme-qunit` route. When you visit `/theme-qunit`, you'll see a list of the themes/components installed on your site that have tests, and from there you can select a theme or component that you run its tests.

We also have a new rake task `themes:install_and_test` that can be used to install a list of themes/components on a temporary database and run the tests of the themes/components that are installed. This rake task can be useful when upgrading/deploying a Discourse instance to make sure that the installed themes/components are compatible with the new Discourse version being deployed, and if the tests fail you can abort the build/deploy process so you don't end up with a broken site.
This commit is contained in:
Osama Sayegh
2021-04-26 12:56:45 +03:00
committed by GitHub
parent 7c3268e0c5
commit 7217dcb67a
27 changed files with 24460 additions and 208 deletions

View File

@ -329,26 +329,7 @@ task 'db:validate_indexes', [:arg] => ['db:ensure_post_migrations', 'environment
db = TemporaryDb.new
db.start
ActiveRecord::Base.establish_connection(
adapter: 'postgresql',
database: 'discourse',
port: db.pg_port,
host: 'localhost'
)
puts "Running migrations on blank database!"
old_stdout = $stdout.clone
old_stderr = $stderr.clone
$stdout.reopen(File.new('/dev/null', 'w'))
$stderr.reopen(File.new('/dev/null', 'w'))
SeedFu.quiet = true
Rake::Task["db:migrate"].invoke
$stdout.reopen(old_stdout)
$stderr.reopen(old_stderr)
db.migrate
ActiveRecord::Base.establish_connection(
adapter: 'postgresql',

View File

@ -185,6 +185,15 @@ def dependencies
source: 'route-recognizer/dist/route-recognizer.js.map',
public_root: true
},
{
source: 'qunit/qunit/qunit.js'
},
{
source: 'pretender/pretender.js'
},
{
source: 'sinon/pkg/sinon.js'
},
]
end

View File

@ -45,13 +45,14 @@ task "qunit:test", [:timeout, :qunit_path] do |_, args|
pid = Process.spawn(
{
"RAILS_ENV" => "test",
"RAILS_ENV" => ENV["QUNIT_RAILS_ENV"] || "test",
"SKIP_ENFORCE_HOSTNAME" => "1",
"UNICORN_PID_PATH" => "#{Rails.root}/tmp/pids/unicorn_test_#{port}.pid", # So this can run alongside development
"UNICORN_PORT" => port.to_s,
"UNICORN_SIDEKIQS" => "0"
},
"#{Rails.root}/bin/unicorn -c config/unicorn.conf.rb"
"#{Rails.root}/bin/unicorn -c config/unicorn.conf.rb",
pgroup: true
)
begin
@ -61,7 +62,7 @@ task "qunit:test", [:timeout, :qunit_path] do |_, args|
cmd = "node #{test_path}/run-qunit.js http://localhost:#{port}#{qunit_path}"
options = { seed: (ENV["QUNIT_SEED"] || Random.new.seed), hidepassed: 1 }
%w{module filter qunit_skip_core qunit_single_plugin theme_name theme_url}.each do |arg|
%w{module filter qunit_skip_core qunit_single_plugin theme_name theme_url theme_id}.each do |arg|
options[arg] = ENV[arg.upcase] if ENV[arg.upcase].present?
end
@ -108,7 +109,7 @@ task "qunit:test", [:timeout, :qunit_path] do |_, args|
ensure
# was having issues with HUP
Process.kill "KILL", pid
Process.kill "-KILL", pid
FileUtils.rm("#{Rails.root}/tmp/pids/unicorn_test_#{port}.pid")
end

View File

@ -98,22 +98,59 @@ task "themes:audit" => :environment do
end
desc "Run QUnit tests of a theme/component"
task "themes:qunit", :theme_name_or_url do |t, args|
name_or_url = args[:theme_name_or_url]
if name_or_url.blank?
raise "A theme name or URL must be provided."
end
if name_or_url =~ /^(url|name)=(.+)/
cmd = "THEME_#{Regexp.last_match(1).upcase}=#{Regexp.last_match(2)} "
cmd += `which rake`.strip + " qunit:test"
sh cmd
else
task "themes:qunit", :type, :value do |t, args|
type = args[:type]
value = args[:value]
if !%w(name url id).include?(type) || value.blank?
raise <<~MSG
Cannot parse passed argument #{name_or_url.inspect}.
Wrong arguments type:#{type.inspect}, value:#{value.inspect}"
Usage:
`bundle exec rake themes:unit[url=<theme_url>]`
`bundle exec rake themes:unit[url,<theme_url>]`
OR
`bundle exec rake themes:unit[name=<theme_name>]`
`bundle exec rake themes:unit[name,<theme_name>]`
OR
`bundle exec rake themes:unit[id,<theme_id>]`
MSG
end
ENV["THEME_#{type.upcase}"] = value.to_s
Rake::Task["qunit:test"].reenable
Rake::Task["qunit:test"].invoke(1200000, "/theme-qunit")
end
desc "Install a theme/component on a temporary DB and run QUnit tests"
task "themes:install_and_test" => :environment do |t, args|
db = TemporaryDb.new
db.start
db.migrate
ActiveRecord::Base.establish_connection(
adapter: 'postgresql',
database: 'discourse',
port: db.pg_port,
host: 'localhost'
)
seeded_themes = Theme.pluck(:id)
Rake::Task["themes:install"].invoke
themes = Theme.pluck(:name, :id)
ENV["PGPORT"] = db.pg_port.to_s
ENV["PGHOST"] = "localhost"
ENV["QUNIT_RAILS_ENV"] = "development"
ENV["DISCOURSE_DEV_DB"] = "discourse"
count = 0
themes.each do |(name, id)|
if seeded_themes.include?(id)
puts "Skipping seeded theme #{name} (id: #{id})"
next
end
puts "Running tests for theme #{name} (id: #{id})..."
Rake::Task["themes:qunit"].reenable
Rake::Task["themes:qunit"].invoke("id", id)
count += 1
end
raise "Error: No themes were installed" if count == 0
ensure
db&.stop
db&.remove
end