From 4f6354df902a45cbc94fceec777cccf4bda802fd Mon Sep 17 00:00:00 2001 From: Peter Zhu Date: Mon, 18 Sep 2023 11:17:03 -0400 Subject: [PATCH] Add binary_name back When set, it is the only binary that errors will be reported for. --- README.md | 1 + lib/ruby_memcheck/configuration.rb | 4 +-- lib/ruby_memcheck/test_task_reporter.rb | 14 ++++++++-- .../shared_test_task_reporter_tests.rb | 28 +++++++++++++++++++ 4 files changed, 43 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index cb53b09..fe71d0e 100644 --- a/README.md +++ b/README.md @@ -153,6 +153,7 @@ If you want to override any of the default configurations you can call `RubyMemc `RubyMemcheck::Configuration` accepts a variety of keyword arguments. Here are all the arguments: +- `binary_name`: Optional. The name of the only binary to report errors for. Use this if there is too much noise caused by other binaries. - `ruby`: Optional. The command to run to invoke Ruby. Defaults to the Ruby that is currently being used. - `valgrind`: Optional. The command to run to invoke Valgrind. Defaults to the string `"valgrind"`. - `valgrind_options`: Optional. Array of options to pass into Valgrind. This is only present as an escape hatch, so avoid using it. This may be deprecated or removed in future versions. diff --git a/lib/ruby_memcheck/configuration.rb b/lib/ruby_memcheck/configuration.rb index 0e31ac1..a373b3e 100644 --- a/lib/ruby_memcheck/configuration.rb +++ b/lib/ruby_memcheck/configuration.rb @@ -31,6 +31,7 @@ class Configuration /\Arb_yield/, ].freeze + attr_reader :binary_name attr_reader :ruby attr_reader :valgrind attr_reader :valgrind_options @@ -57,8 +58,7 @@ def initialize( output_io: $stderr, filter_all_errors: false ) - warn("ruby_memcheck: binary_name is no longer required for configuration") if binary_name - + @binary_name = binary_name @ruby = ruby @valgrind = valgrind @valgrind_options = valgrind_options diff --git a/lib/ruby_memcheck/test_task_reporter.rb b/lib/ruby_memcheck/test_task_reporter.rb index a9c21b9..0803277 100644 --- a/lib/ruby_memcheck/test_task_reporter.rb +++ b/lib/ruby_memcheck/test_task_reporter.rb @@ -42,9 +42,19 @@ def loaded_binaries @loaded_binaries = loaded_features.keep_if do |feat| # Keep only binaries (ignore Ruby files). File.extname(feat) == ".so" - end.freeze + end + + if configuration.binary_name + @loaded_binaries.keep_if do |feat| + File.basename(feat, ".*") == configuration.binary_name + end + + if @loaded_binaries.empty? + raise "The Ruby program executed never loaded a binary called `#{configuration.binary_name}`" + end + end - @loaded_binaries + @loaded_binaries.freeze end def valgrind_xml_files diff --git a/test/ruby_memcheck/shared_test_task_reporter_tests.rb b/test/ruby_memcheck/shared_test_task_reporter_tests.rb index 94eb914..b81e654 100644 --- a/test/ruby_memcheck/shared_test_task_reporter_tests.rb +++ b/test/ruby_memcheck/shared_test_task_reporter_tests.rb @@ -235,6 +235,34 @@ def test_envionment_variable_RUBY_MEMCHECK_RUNNING end end + def test_configration_binary_name + build_configuration(binary_name: "ruby_memcheck_c_test_one") + error = assert_raises do + run_with_memcheck(<<~RUBY) + RubyMemcheck::CTestOne.new.memory_leak + RubyMemcheck::CTestTwo.new.memory_leak + RUBY + end + assert_equal(RubyMemcheck::TestTaskReporter::VALGRIND_REPORT_MSG, error.message) + + assert_equal(1, @test_task.reporter.errors.length) + + output = @output_io.string + refute_empty(output) + assert_match(/^100 bytes in 1 blocks are definitely lost in loss record/, output) + assert_match(/^ \*c_test_one_memory_leak \(ruby_memcheck_c_test_one\.c:\d+\)$/, output) + end + + def test_configration_invalid_binary_name + build_configuration(binary_name: "invalid_binary_name") + error = assert_raises do + run_with_memcheck(<<~RUBY) + RubyMemcheck::CTestOne.new.memory_leak + RUBY + end + assert_includes(error.message, "`invalid_binary_name`") + end + private def run_with_memcheck(code, raise_on_failure: true, spawn_opts: {})