-
Notifications
You must be signed in to change notification settings - Fork 553
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
Fails to cover file loaded multiple times #389
Comments
You need to give us more information on how to reproduce this issue, otherwise there is nothing we can do. Please read CONTRIBUTING.md file for more information about creating bug reports. Thanks! Sample code would be great. |
File.rb: def method
puts 'Hello'
end require 'simplecov'
SimpleCov.start
puts "SimpleCov v#{SimpleCov::VERSION}"
load File.expand_path('../file.rb', __FILE__)
method
load File.expand_path('../file.rb', __FILE__) Result:
If I comment the second
|
I don't quite follow where you're running the SimpleCov.start from, which file has the method defined, and which file is loading it. maybe try this #328 (comment): require 'json'
SimpleCov.at_exit do
filename = File.join(SimpleCov.root, "coverage/#{$$}.json")
File.write filename, JSON.pretty_generate(Coverage.result.sort)
end
pwd = File.expand_path('..', __FILE__)
File.write("file_that_defines_method_in_main.rb", <<-EOF)
puts "File #{__FILE__} loaded by #{caller[0]}"
def method_in_main
puts "Hello, I am called from #{__FILE__} #{__LINE__} by #{caller[0]}"
end
EOF
SimpleCov.start do
command_name "test run"
end
load File.join(pwd, "file_that_defines_method_in_main.rb")
method_in_main
load File.join(pwd, "file_that_defines_method_in_main.rb") |
Oh sorry, I was running [
[
"/tmp/test22731/file_that_defines_method_in_main.rb",
[
1,
1,
0,
null
]
]
] [
[
"/tmp/test22731/file_that_defines_method_in_main.rb",
[
1,
1,
1,
null
]
]
] The result is the same with ruby 2.0.0 and ruby 2.2.0 |
Actually, I can get a correct result by patching Object#load and changing the command_name def load(file, wrap = false)
SimpleCov.result
SimpleCov.start do
command_name "#{command_name}1"
end
Kernel.load(file, wrap)
end
SimpleCov.at_exit do
SimpleCov.result.format!
end I think the problem comes from ruby's Coverage module, not SimpleCov, but we can work around. Ruby 2.3 introduce Coverage.peek_result which may be used to simplify the workaround |
These config files are singletons that contain state, which means that they need to be reloaded between each test run. This is not currently supported by the ruby coverage.so module which is used by SimpleCov simplecov-ruby/simplecov#389
On Check Api test, the classes are reloaded and this was messing up with the code coverage. Each time the file was reloaded, it reset the coverage. This patch merges the results. Found on simplecov-ruby/simplecov#389
On Check Api test, the classes are reloaded and this was messing up with the code coverage. Each time the file was reloaded, it reset the coverage. This patch merges the results. Found on simplecov-ruby/simplecov#389
On Check Api test, the classes are reloaded and this was messing up with the code coverage. Each time the file was reloaded, it reset the coverage. This patch merges the results. Found on simplecov-ruby/simplecov#389
On Check Api test, the classes are reloaded and this was messing up with the code coverage. Each time the file was reloaded, it reset the coverage. This patch merges the results. Found on simplecov-ruby/simplecov#389
Yes probably a coverage bug or rather loading a file multiples times isn't supported as per the coverage module and its intended usage. Might be worth adding as a limitation to the README. |
simplecov is incompatible with code reloads (simplecov-ruby/simplecov#389), so trying to add `cache_classes = false` to our regular test runs results in broken coverage reports.
@Aethelflaed How/where did you patch Object#load in your previous comment? I'm thinking of implementing your solution because I believe I'm having the same issue. Sorry, I know it's been awhile since you last had to think about this. |
@trisys3 I just |
I've a bot which defines each statement in a separate file to simplify testing.
To test the files, I remove all statements and then just load (not require, as it won't work) the ones I need for the specific test.
The coverage seems to work fine until I reach a test which loads all the statements.
If I had already tested e.g. ping.rb, and then this test runs, it will reload ping.rb anyway and reset all covering information about that file.
The coverage does not merge multiple loads of the same file but instead keeps only the last occurence.
I'm not sure but this may be due to ruby's Coverage and not Simplecov.
ruby 2.0.0p598 (2014-11-13 revision 48408) [x86_64-linux]
also tested with ruby 2.2.0p0 (2014-12-25 revision 49005) [x86_64-linux]
The text was updated successfully, but these errors were encountered: