Guard, how to temporally track specific file?
Asked Answered
P

4

7

I'm using Guard gem

At some time of development I need to track only a specific file or several files but not an entire project.

Is there some handy way to temporarily track a particular file?

I know it can be done by modifying guard file but I don't think it's a neat solution.

Perichondrium answered 23/1, 2013 at 11:19 Comment(2)
Please elaborate on why you're looking for this solution. Also, will it always be tracking a single file, or could you imagine wanting to toggle tracking on multiple files?Equitable
In case when I have a lot of broken tests the guard will bring me a mess of messages. That's why I want to get only messages from files I'm working on.Perichondrium
T
13

Actually you can just use focus: true in the it statement for instance:

In your Spec file.

it "needs to focus on this!", focus: true do
  #test written here.
end

Then in spec/spec_helper you need to add the config option.

RSpec.configure do |config| 
  config.treat_symbols_as_metadata_keys_with_true_values = true  
  config.filter_run :focus => true  
  config.run_all_when_everything_filtered = true 
end

Then Guard will automatically pick up test that is focused and run it only. Also the config.run_all_when_everything_filtered = true tells guard to run all of the tests when there is nothing filtered even though the name sounds misleading.

I find Ryan Bate's rails casts to be very helpful on Guard and Spork.

Trager answered 7/2, 2013 at 18:0 Comment(2)
Many thanks for this, that last config variable is horribly misleading. Suggested edit: Because config.treat_symbols_as_metadata_keys_with_true_values is on, the spec line it "needs to focus on this!", focus: => true do can be shortened to it "needs to focus on this!", :focus doRalline
For rspec 3 config.filter_run_when_matching :focus is enough, and "When nothing is tagged with :focus, all examples get run."Monicamonie
V
3

Perhaps groups will work for you?

# In your Guardfile
group :focus do
  guard :ruby do
    watch('file_to_focus_on.rb')
  end
end

# Run it with
guard -g focus

I know you mentioned you don't want to modify the Guardfile, but adding a group would just need to be done once, not every time you switch from watching the project to a focused set and back.

Of course if the set of files you need to focus on changes, you'll need to change the args to watch (and maybe add more watchers), but I figure you'll have to specify them somewhere and this seems as good a place as any.

Alternately, if you really don't want to list the files to focus on in the Guardfile, you could have the :focus group read in a list of files from a separate file or an environment variable as suggested by David above. The Guardfile is just plain ruby (with access to the guard DSL), so it's easy to read a file or ENV variable.

Visconti answered 3/2, 2013 at 14:48 Comment(3)
I like it, but it produce error - Could not load 'guard/ruby' or find class Guard::Ruby. I couldn't resolve this, any thoughts?Perichondrium
I changed guard :ruby to guard 'rspec', at the beginning guard runs the right file but then it runs all tests, so nothing changed very much.Perichondrium
The ruby guard plugin can be found here. With the guard-rspec plugin there are several ways to focus on a few files. @oto-brglez has described one way in a separate answer that makes use of rspec commandline options. The guard-rspec readme has a section on running a subset. Let me know if these pointers don't work for you and i can update the answer with more details.Visconti
W
1

If you use guard-rspec. You can do this.

Change your Guardfile rspec block so that is has something like this:

guard 'rspec', :cli => ENV['RSPEC_e'].nil? ? "": "-e #{ENV['RSPEC_e']}") do
  # ... regular rspec-rails stuff goes here ...
end

Start Guard. And set RSPEC_e before. Like so...

RSPEC_e=here guard

Then whenever you change something only specs that have text "here" (set by RSPEC_e) in their description will be re-run.

Wivina answered 6/2, 2013 at 12:18 Comment(2)
There's also the --tag option which might be simpler than the above. guard 'rspec', :cli => '-t focus' do ... and it will run only examples tagged with focus => true, e.g. it "is giving me trouble", :focus => true do ...Visconti
Yeah tags would also work nicely, but then u have to tag your specs. If u use -e you can just match text...Wivina
B
0

You could permanently modify the guard file to check an environment variable of your choosing and behave differently if it is present. For example, access the variable ENV['FILE']. Then you can prepend your command for running guard with FILE=foo.rb whenever you want.

Bootleg answered 1/2, 2013 at 16:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.