Skip to content

Commit

Permalink
Merge pull request #811 from flippercloud/ssshhh
Browse files Browse the repository at this point in the history
Quiet test output to make it easier to find errors
  • Loading branch information
bkeepers authored Jan 8, 2024
2 parents aee29ac + c68f5d4 commit 217331b
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 21 deletions.
4 changes: 2 additions & 2 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ end

require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new(:spec) do |t|
t.rspec_opts = %w(--color --format documentation)
t.rspec_opts = %w(--color)
t.verbose = false
end

namespace :spec do
Expand All @@ -41,7 +42,6 @@ end
Rake::TestTask.new do |t|
t.libs = %w(lib test)
t.pattern = 'test/**/*_test.rb'
t.options = '--documentation'
t.warning = false
end

Expand Down
15 changes: 12 additions & 3 deletions lib/flipper/instrumentation/log_subscriber.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'securerandom'
require 'active_support/gem_version'
require 'active_support/notifications'
require 'active_support/log_subscriber'

Expand Down Expand Up @@ -71,11 +72,19 @@ def logger
self.class.logger
end

def self.attach
attach_to InstrumentationNamespace
end

def self.detach
# Rails 5.2 doesn't support this, that's fine
detach_from InstrumentationNamespace if respond_to?(:detach_from)
end

private

# Rails 7.1 changed the signature of this function.
# Checking if > 7.0.99 rather than >= 7.1 so that 7.1 pre-release versions are included.
COLOR_OPTIONS = if Rails.gem_version > Gem::Version.new('7.0.99')
COLOR_OPTIONS = if Gem::Requirement.new(">=7.1").satisfied_by?(ActiveSupport.gem_version)
{ bold: true }.freeze
else
true
Expand All @@ -88,5 +97,5 @@ def color_name(name)
end
end

Instrumentation::LogSubscriber.attach_to InstrumentationNamespace
Instrumentation::LogSubscriber.attach
end
16 changes: 10 additions & 6 deletions spec/flipper/adapters/active_record_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require 'flipper/adapters/active_record'
SpecHelpers.silence { require 'flipper/adapters/active_record' }
require 'active_support/core_ext/kernel'

# Turn off migration logging for specs
Expand Down Expand Up @@ -43,7 +43,7 @@

context "with #{config[:adapter]}" do
before(:all) do
ActiveRecord::Tasks::DatabaseTasks.create(config)
silence { ActiveRecord::Tasks::DatabaseTasks.create(config) }
end

before(:each) do
Expand All @@ -59,7 +59,7 @@
end

after(:all) do
ActiveRecord::Tasks::DatabaseTasks.drop(config)
silence { ActiveRecord::Tasks::DatabaseTasks.drop(config) }
end

it_should_behave_like 'a flipper adapter'
Expand All @@ -70,8 +70,12 @@
Flipper.configuration = nil
Flipper.instance = nil

silence_warnings { load 'flipper/adapters/active_record.rb' }
expect { Flipper::Adapters::ActiveRecord.new }.not_to raise_error
expect {
silence do
load 'flipper/adapters/active_record.rb'
Flipper::Adapters::ActiveRecord.new
end
}.not_to raise_error
end

it "should load actor ids fine" do
Expand All @@ -92,7 +96,7 @@
Flipper.configuration = nil
Flipper.instance = nil

silence_warnings { load 'flipper/adapters/active_record.rb' }
silence { load 'flipper/adapters/active_record.rb' }
end

it 'configures itself' do
Expand Down
2 changes: 1 addition & 1 deletion spec/flipper/adapters/sequel_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
Flipper.configuration = nil
Flipper.instance = nil

load 'flipper/adapters/sequel.rb'
silence { load 'flipper/adapters/sequel.rb' }
end

it 'configures itself' do
Expand Down
11 changes: 9 additions & 2 deletions spec/flipper/instrumentation/log_subscriber_spec.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
require 'logger'
require 'flipper/adapters/instrumented'
require 'flipper/instrumentation/log_subscriber'
require 'flipper/adapters/instrumented'

begin
require 'active_support/isolated_execution_state'
rescue LoadError
# ActiveSupport::IsolatedExecutionState is only available in Rails 5.2+
end

# Don't log in other tests, we'll manually re-attach when this one starts
Flipper::Instrumentation::LogSubscriber.detach

RSpec.describe Flipper::Instrumentation::LogSubscriber do
let(:adapter) do
memory = Flipper::Adapters::Memory.new
Expand All @@ -32,8 +35,12 @@
described_class.logger = nil
end

before(:all) do
described_class.attach
end

after(:all) do
ActiveSupport::Notifications.unsubscribe("flipper")
described_class.detach
end

let(:log) { @io.string }
Expand Down
8 changes: 5 additions & 3 deletions spec/support/spec_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
require 'rack/test'

module SpecHelpers
extend self

def self.included(base)
base.let(:flipper) { build_flipper }
base.let(:app) { build_app(flipper) }
Expand Down Expand Up @@ -80,11 +82,11 @@ def silence

yield

$stderr = original_stderr
$stdout = original_stdout

# Return output
output.string
ensure
$stderr = original_stderr
$stdout = original_stdout
end
end

Expand Down
6 changes: 2 additions & 4 deletions test/adapters/active_record_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ def test_models_honor_table_name_prefixes_and_suffixes
ActiveRecord::Base.table_name_prefix = :foo_
ActiveRecord::Base.table_name_suffix = :_bar

Flipper::Adapters::ActiveRecord.send(:remove_const, :Feature)
Flipper::Adapters::ActiveRecord.send(:remove_const, :Gate)
Flipper::Adapters.send(:remove_const, :ActiveRecord)
load("flipper/adapters/active_record.rb")

assert_equal "foo_flipper_features_bar", Flipper::Adapters::ActiveRecord::Feature.table_name
Expand All @@ -58,8 +57,7 @@ def test_models_honor_table_name_prefixes_and_suffixes
ActiveRecord::Base.table_name_prefix = ""
ActiveRecord::Base.table_name_suffix = ""

Flipper::Adapters::ActiveRecord.send(:remove_const, :Feature)
Flipper::Adapters::ActiveRecord.send(:remove_const, :Gate)
Flipper::Adapters.send(:remove_const, :ActiveRecord)
load("flipper/adapters/active_record.rb")
end
end

0 comments on commit 217331b

Please sign in to comment.