DEV: Eliminate flakiness in specs that depend on plugins from fixtures (#21912)

This commit is contained in:
Osama Sayegh 2023-06-05 03:06:00 +03:00 committed by GitHub
parent 41836762c4
commit 6cf863080a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 31 additions and 30 deletions

View File

@ -243,8 +243,7 @@ RSpec.describe Plugin::Instance do
end
it "can activate plugins correctly" do
plugin = Plugin::Instance.new
plugin.path = "#{Rails.root}/spec/fixtures/plugins/my_plugin/plugin.rb"
plugin = plugin_from_fixtures("my_plugin")
junk_file = "#{plugin.auto_generated_path}/junk"
plugin.ensure_directory(junk_file)
@ -260,8 +259,7 @@ RSpec.describe Plugin::Instance do
end
it "registers auth providers correctly" do
plugin = Plugin::Instance.new
plugin.path = "#{Rails.root}/spec/fixtures/plugins/my_plugin/plugin.rb"
plugin = plugin_from_fixtures("my_plugin")
plugin.activate!
expect(DiscoursePluginRegistry.auth_providers.count).to eq(0)
plugin.notify_before_auth
@ -271,8 +269,7 @@ RSpec.describe Plugin::Instance do
end
it "finds all the custom assets" do
plugin = Plugin::Instance.new
plugin.path = "#{Rails.root}/spec/fixtures/plugins/my_plugin/plugin.rb"
plugin = plugin_from_fixtures("my_plugin")
plugin.register_asset("test.css")
plugin.register_asset("test2.scss")
@ -397,8 +394,8 @@ RSpec.describe Plugin::Instance do
end
describe "locales" do
let(:plugin_path) { "#{Rails.root}/spec/fixtures/plugins/custom_locales" }
let!(:plugin) { Plugin::Instance.new(nil, "#{plugin_path}/plugin.rb") }
let!(:plugin) { plugin_from_fixtures("custom_locales") }
let(:plugin_path) { File.dirname(plugin.path) }
let(:plural) do
{
keys: %i[one few other],

View File

@ -56,15 +56,13 @@ RSpec.describe Stylesheet::Compiler do
context "with a plugin" do
let :plugin1 do
plugin1 = Plugin::Instance.new
plugin1.path = "#{Rails.root}/spec/fixtures/plugins/my_plugin/plugin.rb"
plugin1 = plugin_from_fixtures("my_plugin")
plugin1.register_css "body { background: $primary }"
plugin1
end
let :plugin2 do
plugin2 = Plugin::Instance.new
plugin2.path = "#{Rails.root}/spec/fixtures/plugins/scss_plugin/plugin.rb"
plugin2 = plugin_from_fixtures("scss_plugin")
plugin2
end
@ -183,8 +181,7 @@ RSpec.describe Stylesheet::Compiler do
context "with a plugin" do
before do
plugin = Plugin::Instance.new
plugin.path = "#{Rails.root}/spec/fixtures/plugins/color_definition/plugin.rb"
plugin = plugin_from_fixtures("color_definition")
Discourse.plugins << plugin
plugin.activate!
end

View File

@ -1014,15 +1014,13 @@ RSpec.describe Stylesheet::Manager do
context "when there are enabled plugins" do
let(:plugin1) do
plugin1 = Plugin::Instance.new
plugin1.path = "#{Rails.root}/spec/fixtures/plugins/my_plugin/plugin.rb"
plugin1 = plugin_from_fixtures("my_plugin")
plugin1.register_css "body { padding: 1px 2px 3px 4px; }"
plugin1
end
let(:plugin2) do
plugin2 = Plugin::Instance.new
plugin2.path = "#{Rails.root}/spec/fixtures/plugins/scss_plugin/plugin.rb"
plugin2 = plugin_from_fixtures("scss_plugin")
plugin2
end

View File

@ -238,8 +238,7 @@ RSpec.describe SvgSprite do
context "with a plugin" do
let :plugin1 do
plugin1 = Plugin::Instance.new
plugin1.path = "#{Rails.root}/spec/fixtures/plugins/my_plugin/plugin.rb"
plugin1 = plugin_from_fixtures("my_plugin")
plugin1
end

View File

@ -346,7 +346,7 @@ RSpec.configure do |config|
end
config.after(:suite) do
FileUtils.remove_dir(file_from_fixtures_tmp_folder, true) if SpecSecureRandom.value
FileUtils.remove_dir(concurrency_safe_tmp_dir, true) if SpecSecureRandom.value
end
config.before :each, &TestSetup.method(:test_setup)
@ -559,15 +559,27 @@ def unfreeze_time
end
def file_from_fixtures(filename, directory = "images")
SpecSecureRandom.value ||= SecureRandom.hex
FileUtils.mkdir_p(file_from_fixtures_tmp_folder) unless Dir.exist?(file_from_fixtures_tmp_folder)
tmp_file_path = File.join(file_from_fixtures_tmp_folder, SecureRandom.hex << filename)
tmp_file_path = File.join(concurrency_safe_tmp_dir, SecureRandom.hex << filename)
FileUtils.cp("#{Rails.root}/spec/fixtures/#{directory}/#{filename}", tmp_file_path)
File.new(tmp_file_path)
end
def file_from_fixtures_tmp_folder
File.join(Dir.tmpdir, "rspec_#{Process.pid}_#{SpecSecureRandom.value}")
def plugin_from_fixtures(plugin_name)
tmp_plugins_dir = File.join(concurrency_safe_tmp_dir, "plugins")
FileUtils.mkdir(tmp_plugins_dir) if !Dir.exist?(tmp_plugins_dir)
FileUtils.cp_r("#{Rails.root}/spec/fixtures/plugins/#{plugin_name}", tmp_plugins_dir)
plugin = Plugin::Instance.new
plugin.path = File.join(tmp_plugins_dir, plugin_name, "plugin.rb")
plugin
end
def concurrency_safe_tmp_dir
SpecSecureRandom.value ||= SecureRandom.hex
dir_path = File.join(Dir.tmpdir, "rspec_#{Process.pid}_#{SpecSecureRandom.value}")
FileUtils.mkdir_p(dir_path) unless Dir.exist?(dir_path)
dir_path
end
def has_trigger?(trigger_name)

View File

@ -93,8 +93,7 @@ RSpec.describe BootstrapController do
context "with a plugin asset filter" do
let :plugin do
plugin = Plugin::Instance.new
plugin.path = "#{Rails.root}/spec/fixtures/plugins/my_plugin/plugin.rb"
plugin = plugin_from_fixtures("my_plugin")
plugin.register_asset_filter do |type, request|
next true if request.path == "/mypluginroute"
false

View File

@ -64,8 +64,7 @@ RSpec.describe StylesheetsController do
fab!(:user) { Fabricate(:user) }
let(:plugin) do
plugin = Plugin::Instance.new
plugin.path = "#{Rails.root}/spec/fixtures/plugins/my_plugin/plugin.rb"
plugin = plugin_from_fixtures("my_plugin")
plugin.register_css "body { padding: 1px 2px 3px 4px; }"
plugin
end