Skip to content

Commit

Permalink
Merge pull request #27 from Shopify/add-binary-name
Browse files Browse the repository at this point in the history
Add binary_name back
  • Loading branch information
peterzhu2118 authored Sep 18, 2023
2 parents 234a4e7 + 4f6354d commit 65b9f06
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions lib/ruby_memcheck/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class Configuration
/\Arb_yield/,
].freeze

attr_reader :binary_name
attr_reader :ruby
attr_reader :valgrind
attr_reader :valgrind_options
Expand All @@ -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
Expand Down
14 changes: 12 additions & 2 deletions lib/ruby_memcheck/test_task_reporter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 28 additions & 0 deletions test/ruby_memcheck/shared_test_task_reporter_tests.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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: {})
Expand Down

0 comments on commit 65b9f06

Please sign in to comment.