Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Guard, how to temporally track specific file?

Tags:

ruby

guard

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.

like image 911
megas Avatar asked Jan 23 '13 11:01

megas


3 Answers

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.

like image 69
David Hahn Avatar answered Nov 01 '22 09:11

David Hahn


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.

like image 41
exbinary Avatar answered Nov 01 '22 10:11

exbinary


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.

like image 25
Oto Brglez Avatar answered Nov 01 '22 10:11

Oto Brglez