-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow enabling/disabling IronTrail in rspec #9
Changes from all commits
72cfe27
e533b67
9316d1a
dd27096
ffa27dd
56b2e9f
12bd878
a3d9cc6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,66 @@ | ||
# frozen_string_literal: true | ||
|
||
if ENV['RAILS_ENV'] == 'production' | ||
raise 'This file should not be required in production. ' \ | ||
'Change the RAILS_ENV env var temporarily to override this.' | ||
end | ||
|
||
require 'iron_trail' | ||
|
||
module IronTrail | ||
module Testing | ||
class << self | ||
attr_accessor :enabled | ||
|
||
def enable! | ||
DbFunctions.new(ActiveRecord::Base.connection).install_functions | ||
@enabled = true | ||
end | ||
|
||
def disable! | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's unlikely to happen since you need to require this, but one might do it in a rails console, I'd like iron_trail to protect me from doing this in production There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. agreed. I'll add a protection if |
||
# We "disable" it by replacing the trigger function by a no-op one. | ||
# This should be faster than adding/removing triggers from several | ||
# tables every time. | ||
sql = <<~SQL | ||
CREATE OR REPLACE FUNCTION irontrail_log_row() | ||
RETURNS TRIGGER AS $$ | ||
BEGIN | ||
RETURN NULL; | ||
END; | ||
$$ LANGUAGE plpgsql; | ||
SQL | ||
|
||
ActiveRecord::Base.connection.execute(sql) | ||
@enabled = false | ||
end | ||
|
||
def with_iron_trail(want_enabled:, &block) | ||
was_enabled = IronTrail::Testing.enabled | ||
|
||
if want_enabled | ||
::IronTrail::Testing.enable! unless was_enabled | ||
else | ||
::IronTrail::Testing.disable! if was_enabled | ||
end | ||
|
||
block.call | ||
ensure | ||
if want_enabled && !was_enabled | ||
::IronTrail::Testing.disable! | ||
elsif !want_enabled && was_enabled | ||
::IronTrail::Testing.enable! | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|
||
RSpec.configure do |config| | ||
config.around(:each, iron_trail: true) do |example| | ||
IronTrail::Testing.with_iron_trail(want_enabled: true) { example.run } | ||
end | ||
config.around(:each, iron_trail: false) do |example| | ||
raise "Using iron_trail: false does not do what you might think it does. To disable iron_trail, " \ | ||
"use IronTrail::Testing.with_iron_trail(want_enabled: false) { ... } instead." | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'iron_trail/testing/rspec' | ||
|
||
IronTrail::Testing.disable! | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: also add a test where it is enabled by default, to ensure that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @peterbrendel done in dd27096 |
||
|
||
RSpec.describe 'lib/iron_trail/testing/rspec.rb' do | ||
let(:person) { Person.create!(first_name: 'Arthur', last_name: 'Schopenhauer') } | ||
|
||
subject(:do_some_changes!) do | ||
person.update!(first_name: 'Jim') | ||
person.update!(first_name: 'Jane') | ||
end | ||
|
||
describe 'IronTrail::Testing#with_iron_trail' do | ||
context 'when IronTrail is disabled but we enable it for a while' do | ||
it 'tracks only while enabled' do | ||
person.update!(first_name: 'Jim') | ||
|
||
expect(person.reload.iron_trails.length).to be(0) | ||
|
||
IronTrail::Testing.with_iron_trail(want_enabled: true) do | ||
person.update!(first_name: 'Jane') | ||
end | ||
|
||
expect(person.reload.iron_trails.length).to be(1) | ||
|
||
person.update!(first_name: 'Joe') | ||
|
||
expect(person.reload.iron_trails.length).to be(1) | ||
end | ||
end | ||
end | ||
|
||
describe 'rspec helpers' do | ||
context 'with IronTrail disabled' do | ||
it 'does not track anything' do | ||
do_some_changes! | ||
|
||
expect(person.reload.iron_trails.length).to be(0) | ||
end | ||
end | ||
|
||
context 'with IronTrail enabled through the helper', iron_trail: true do | ||
it 'does not track anything' do | ||
do_some_changes! | ||
|
||
expect(person.reload.iron_trails.length).to be(3) | ||
end | ||
end | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would raising here stop the application from booting?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yep 👍