Skip to content

Commit

Permalink
DEV: Eliminate flakiness in specs that depend on plugins from fixtures (
Browse files Browse the repository at this point in the history
  • Loading branch information
OsamaSayegh committed Jun 5, 2023
1 parent 4183676 commit 6cf8630
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 30 deletions.
13 changes: 5 additions & 8 deletions spec/lib/plugin/instance_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,7 @@ def enabled?
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)
Expand All @@ -260,8 +259,7 @@ def enabled?
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
Expand All @@ -271,8 +269,7 @@ def enabled?
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")
Expand Down Expand Up @@ -397,8 +394,8 @@ def enabled?
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],
Expand Down
9 changes: 3 additions & 6 deletions spec/lib/stylesheet/compiler_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,13 @@

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

Expand Down Expand Up @@ -183,8 +181,7 @@

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
Expand Down
6 changes: 2 additions & 4 deletions spec/lib/stylesheet/manager_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1014,15 +1014,13 @@ def manager(theme_id = nil)

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

Expand Down
3 changes: 1 addition & 2 deletions spec/lib/svg_sprite/svg_sprite_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,7 @@

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

Expand Down
24 changes: 18 additions & 6 deletions spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ class << example.exception
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)
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 1 addition & 2 deletions spec/requests/bootstrap_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,7 @@

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
Expand Down
3 changes: 1 addition & 2 deletions spec/requests/stylesheets_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,7 @@
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
Expand Down

0 comments on commit 6cf8630

Please sign in to comment.