guard snippets

Autotest for Sinatra

Tagged watchr, autotest, sinatra, guard  Languages ruby

Guard

Add this to a file named Guardfile in your app directory:

# gem install guard guard-minitest
# https://github.com/guard/guard#readme

guard 'minitest' do
  watch(%r{^app/(.+)\.rb$})   { |m| "test/app/#{m[1]}_test.rb" }
  watch(%r{^lib/(.+)\.rb$})   { |m| "test/lib/#{m[1]}_test.rb" }
  watch(%r{^test/(.+)\.rb$})  { |m| "test/#{m[1]}.rb" }
end

Watchr

Put this in test/test.watchr (inspired by Padrino):

#
# From https://raw.github.com/padrino/padrino-recipes/master/files/watchrs/test.watchr
#
# install watchr
# $ gem install watchr
#
# Run With:
# $ watchr test.watchr
#

# --------------------------------------------------
# Helpers
# --------------------------------------------------

def run(cmd)
  exec = "bundle exec ruby #{cmd}"
  puts(exec)
  system(exec)
end
 
def run_all 
  system("bundle exec rake test")
end

# --------------------------------------------------
# Watchr Rules
# --------------------------------------------------
watch("^lib/(.*)\.rb")                     { |m| run("test/lib/#{m[1]}_test.rb") }

watch("^(.*)/controllers/(.*).rb")           { |m| run("test/#{m[1]}/controllers/#{m[2]}_controller_test.rb") }
watch("^test/(.*)/controllers/(.*)_test.rb") { |m| run("test/#{m[1]}/controllers/#{m[2]}_test.rb") }

watch("^(.*)/models/(.*).rb")                { |m| run("test/#{m[1]}/models/#{m[2]}_test.rb") }
watch("^test/(.*)/(.*)_test.rb")             { |m| run("test/#{m[1]}/models/#{m[2]}_test.rb") }

watch("test.*/test_helper\.rb")              { run_all }
watch("^test/(.*)_test\.rb")                 { |m| run("test/#{m[1]}_test.rb") }

# --------------------------------------------------
# Signal Handling
# --------------------------------------------------
# Ctrl-\ or Ctrl-ö (Swedish keyboard)
Signal.trap('QUIT') do
  puts "Quitting..."
  abort("\n") 
end

# Ctrl-C
Signal.trap('INT') do 
  puts " --- Running all tests ---\n\n"
  run_all
end

# Find your signals...
#Signal.list.each do |name, num|
#  Signal.trap(num) do 
#    puts "#{name}"
#  end rescue "xx"
#end

Put this in .bash_profile or .zsh/aliases:

alias autotest='watchr test/test.watchr'

Install watchr:

gem install watchr

Test your code:

exec # reload profile
autotest

You can also use Guard or autotest. Although improvements have been made during the years, they are all IMHO flawed pieces of software and badly documented.

Guard not picking up file changes?

Tagged guard, rb-inotify, rb-fsevent  Languages ruby

Add to Gemfile and uncomment relevant line:

# Linux
  #gem 'rb-inotify'
  # OSX
  #gem 'rb-fsevent'
  # Windows
  # gem 'migrate-to-linux'

Run guard.

Still not working? Files on NFS or dropbox? Force polling with *guard -p*…

Rails Console Update Problems?

Tagged rails, pry, guard, console, tty  Languages bash

Console not updating when typing? Pry/Ruby/Rails/something messing up console output? Check your stty settings:

stty -a

Is echo disabled. Do you see "-echo" in the output?

Try resetting the console:

reset
# or
stty

Check if you have incorrectly colorized output: http://stackoverflow.com/questions/8806643/colorized-output-breaks-linewrapping-with-readline/8916332#8916332

Are you using a gem or something else that colorizes console output? Try disabling them.

Examples: * turn * pry-theme

See: http://askubuntu.com/questions/171449/shell-does-not-show-typed-in-commands-reset-works-but-what-happened

How to run only one specific test with Rspec

Tagged rspec, focus, guard  Languages ruby

Focusing on one or more tests with Rspec

You can tell Rspec to run only tests tagged with "focus: true" by running Rspec with the --tag focus switch:

rspec ./spec/controllers/concerns/session_expiry_spec.rb --tag focus

This would run e.g. only this test which is marked with focus: true

it "resets session if session has expired", focus: true do
...
end

Integrating with Guard

Use this configuration to integrate this workflow with Guard:

guard 'rspec', cli: '--tag focus --format d --color', all_after_pass: false, all_on_start: false do
...
end

We also want to run all tests when there are no focused tests. We therefore add this to spec/spec_helper.rb:

RSpec.configure do |config|
  # This is dangerous with CI integration e.g. Jenkins
  # config.filter_run focus: true
  config.run_all_when_everything_filtered = true
end

Rspec configuration file

One alternative is to enable the focus option in the Rspec configuration file:

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

However, this would be dangerous when running tests with e.g. Jenkins and when someone forgets to remove "focus: true".