Over the course of our project with multiple developers and styles, we have some tests and code that are commented out. Is there an easy way to find all code that is commented out?
The tool should be smart enough to find out that it is ruby code that is being commented not, real comments.
At this point I can only think of grep like grep #, but it should be something smarter and less manual.
This may be ugly but I think you can inspect each ".rb" file with a rake task, line per line, matching it with a regexp (something like /#.*\n/), and run an eval("matching_string") on each match. If the comment isn't ruby code it will simply fail.
It seems you'll need a looks_like_ruby method so you can do this:
puts line if line =~ /^\s+#/ && looks_like_ruby(line)
A quick and dirty implementation (off the top of my head)
def looks_like_ruby(text)
text =~ Regexp.union((Kernel.methods + Object.methods).uniq.map(&:to_s))
end
Not perfect, but better than grepping for #
Related
I'm not sure what is causing it, but the word "should" is being replaced in my code with the word "does". I'm writing a spec in ruby on rails, and I'm trying to follow a BDD approach to my unit tests. One of their recommendations is writing each unit test as a whole sentence, starting with the word, "should", so I wrote the following test:
it 'should not return if there are no existing activities on the project' do
end
The code is being replaced by the following:
it 'does not return if there are no existing activities on the project' do
end
Notice that the word "should" has been replaced. Is this RuboCop? If so, what is the rule I disable to prevent this from happening?
If it's not RuboCop, what could it be?
Well it is actually something Rubocop does for you through RSpec/ExampleWording. Rubocop has an Rspec Style guide, you can check the part about "Should" in Example Docstrings
From the docs:
Do not write 'should' or 'should not' in the beginning of your example
docstrings. The descriptions represent actual functionality, not what
might be happening. Use the third person in the present tense.
So disabling it is actually a bad practice and you should stick to the docs and write your docstrings accordingly.
You can disable RSpec/ExampleWording
When working with ruby on rails code, what is the conventional way (or most elegant way) to let rails do nothing?
For example when debugging and commenting out a line where there has to be a statement:
begin
#some_commented_out_function
rescue => e
[...]
end
Is there a dummy function I quickly can put in instead of the commented out line? What is the convention for this?
Ruby doesn't have a pass or no-op statement. In your example, that code would work as written, with or without a comment.
If you'd like to make it explicit, a line with just nil or a comment explaining the method would probably be considered conventional.
I am writing Rails tests using the standard Test::Unit/TestCase.
Is there any way to somehow filter what gets printed to the log, so that you only print the stack for specific test cases.
I have a functional test file with many test cases in it, and I'm really only interested in debugging one test case. Printing my own log statements still requires searching through a few thousand lines of generated log. Or something similar to the RSpec 'pending' functionality.
Run from a command line ruby test/unit/my_model.rb to run one test suite. You can also use a debugger, such as (wrapped by) RubyMine or pry, to stop on a specific test case and look at the log.
But if a sledge-hammer does not solve the problem, you can use tweezers: config.logger.level = Logger::WARN in your test.rb, from Set logging levels in Ruby on Rails
It is probably better if instead of strangling the output to log/test.log, you become familiar with a command such as grep. Grep allows you to run very advanced search queries through files or directories, as long as your running on some flavor of *nix. The simplest use would be
grep search_term file_name
The reason I say you shouldn't constrict the log output is because someday that could bit you in the **s. Hope this helps.
In cucucmber i want to run a step after all the scenarios in a feature are run, can I have an after hook for the entire feature, I currently have after hooks for each scenario.
I know its been a long time, but i havent been a user here for long but,
There is an exit hook that is used like this:
at_exit do
# Add code here
end
This should be placed in your env.rb file or the features/support directory
Here's a great link
It's a bit of a workaround, but you could just have scenarios at the beginning and the end of the feature for setup/teardown. Scenarios are run in the order that they are specified so as long as you have the setup scenario at the top and the teardown at the bottom then it works fine.
I also name the Scenario 'Scenario: feature setup' and 'Scenario: feature teardown' to make it more obvious when outputting the results to a formatter.
You can use a custom formatter, and use the after_feature method.
(I used to have a link with more information, but #katta just pointed out that its no longer available)
Sure, just tag your feature.
After('#mytag') do
#Do your magic here
end
This documentation might help: http://cukes.info/cucumber/api/ruby/latest/Cucumber/RbSupport/RbDsl.html#AfterStep-instance_method
We have a fairly large rails application and I have started this output in our unicorn.log:
#:0xc644248>#:0xc644248>#:0xc4f06e4>#:0xc4f06e4>#:0xca481b4>#:0xca481b4>#:0xc53f604>#:0xc53f604>#:0xcd7a60c>#:0xcd7a60c>#:0xc5df2f8>#:0xc5df2f8>#:0xc69fd00>#:0xc69fd00>#:0xc560ae8>#:0xc560ae8>
It seems to me like there probably is a stray Kernel.puts method call somewhere, but I've been searching for hours and can't find it.
Anyone have tips for tracking something like this down?
You could monkey patch puts, and raise an exception when it's called. You could even fine tune that with a regexp match on your output string (which looks like a recursive object dump).
module Kernel
def puts (s)
raise "puts called, check the backtrace for the source" if s =~ /#:[a-z0-9]>*/
end
end
It could also be that it's not a call to puts, but rather #inspect.
Have you checked for display? That's another method that prints stuff out.
You could go over all the files and search for any calls to Kernel.puts, like so:
find -iname "*.rb" | xargs grep -iR 'Kernel.puts'
However, in terms of neatness (and effectiveness), I would probably go for the solution provided by Jeff Paquette.
This is what I use, it's similar to Banang's answer but maybe even simpler. Do a grep from the directory like so:
grep -rn 'puts' .
Sure it searches everything but you can run it in whatever directory you want to limit that. That should give you the file and line number you need. You can fine tune the search criteria as you wish.