diff --git a/Gemfile b/Gemfile index c193614..123da1c 100644 --- a/Gemfile +++ b/Gemfile @@ -11,6 +11,7 @@ gem "stackprof" gem "standard" gem "ruby-prof" +gem "benchmark" gem "benchmark-ips" case ENV["PRISM_VERSION"]&.strip&.downcase diff --git a/spec/integration/syntax_suggest_spec.rb b/spec/integration/syntax_suggest_spec.rb index 015d088..79bfdc1 100644 --- a/spec/integration/syntax_suggest_spec.rb +++ b/spec/integration/syntax_suggest_spec.rb @@ -4,10 +4,6 @@ module SyntaxSuggest RSpec.describe "Integration tests that don't spawn a process (like using the cli)" do - before(:each) do - skip "Benchmark is not available" unless defined?(::Benchmark) - end - it "does not timeout on massive files" do next unless ENV["SYNTAX_SUGGEST_TIMEOUT"] @@ -17,7 +13,7 @@ module SyntaxSuggest io = StringIO.new - benchmark = Benchmark.measure do + benchmark_measure do debug_perf do SyntaxSuggest.call( io: io, @@ -28,7 +24,6 @@ module SyntaxSuggest end debug_display(io.string) - debug_display(benchmark) expect(io.string).to include(<<~EOM) 6 class SyntaxTree < Ripper @@ -46,7 +41,7 @@ module SyntaxSuggest io = StringIO.new debug_perf do - benchmark = Benchmark.measure do + benchmark_measure do SyntaxSuggest.call( io: io, source: file.read, @@ -54,7 +49,6 @@ module SyntaxSuggest ) end debug_display(io.string) - debug_display(benchmark) end expect(io.string).to_not include("def ruby_install_binstub_path") diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index b5d2924..c0aaf0f 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -85,6 +85,16 @@ def debug_perf end end +def benchmark_measure + raise "No block given" unless block_given? + + if defined?(::Benchmark) + debug_display(Benchmark.measure { yield }) + else + yield + end +end + def run!(cmd, raise_on_nonzero_exit: true) out = `#{cmd} 2>&1` raise "Command: #{cmd} failed: #{out}" if !$?.success? && raise_on_nonzero_exit