Fixing brittle tests by running them again (shoulda)

Really fixing them would be nicer but that can be complicated, so just run them 3 times and if they fail every time fail, otherwise pass with warnings.

Code


  def self.brittle_should(test, &block)
    results = []
    tries = 3

    tries.times do |i|
      should test + " (try #{i+1})" do
        if results.empty? or results.none?
          begin
            instance_exec(&block)
            results << true
          rescue Object
            if results.size < tries
              warn "Brittle test #{test.inspect} failed, trying again..."
              results << false
            else
              raise
            end
          end
        end
      end
    end
  end

Rack::Utils.escape / unescape + CGI.escape/unescape/escapeHTML vs undefined method bytesize for nil

The official solution for this problem is to use e.g. CGI.escape thing.to_str,
my unofficial solution is to automate that ๐Ÿ™‚

Code

# https://grosser.it/2012/08/16/rackutils-escape-unescape-cgi-escapeunescapeescapehtml-vs-undefined-method-bytesize-for-nil/
AUTOMATED_TO_STR_FOR_SAFE_BUFFER = <<-RUBY
  def METHOD_with_html_safe(object)
    if object.is_a?(ActiveSupport::SafeBuffer)
      METHOD(object.to_str)
    else
      METHOD_without_html_safe(object)
    end
  end
  alias_method_chain :METHOD, :html_safe
RUBY

# can be removed if
# Rack::Utils.escape "a & & %24".html_safe
# Rack::Utils.unescape "a & & %24".html_safe
# does not raise an error, must work with strings and symbols
Rack::Utils.class_eval do
  [:escape, :unescape].each do |method|
    eval AUTOMATED_TO_STR_FOR_SAFE_BUFFER.gsub("METHOD", method.to_s)
    module_function :"#{method}_without_html_safe"
    module_function method
  end
end

# can be removed if
# CGI.escape "a & & %24".html_safe
# CGI.unescape "a & & %24".html_safe
# CGI.unescapeHTML "a & & %24".html_safe
# does not raise an error, must work with strings and symbols
# (escapeHTML always works)
CGI.class_eval do
  class << self
    [:escape, :unescape, :unescapeHTML].each do |method|
      eval AUTOMATED_TO_STR_FOR_SAFE_BUFFER.gsub("METHOD", method.to_s)
    end
  end
end

Object.send :remove_const, :AUTOMATED_TO_STR_FOR_SAFE_BUFFER

rewrite git history with git edit

I’m currently doing a lot of history rewrites to keep my branch clean (tons of changes that I rebase regularly)

So I built git-edit

Usage

git-edit abf1234
... do some work ...
git-edit --amend # amend changes to abf1234 and finish
# or if things go south ...
git-edit --abort  # get me out of here ๐Ÿ™‚

Install
Install ruby.

curl https://raw.github.com/grosser/dotfiles/master/bin/git-edit >\
 ~/bin/git-edit && chmod +x ~/bin/git-edit

Remember: Do not use unless you are the only one working on this branch!

return values from fork: fork_and_return

Very useful helper if you want to keep your environment clean, useful for testing or if you have to require something that has lots of side effects or memory leaks.

Usage

result = fork_and_return do
  require 'some_lib_with_side_effects'
  leak_some_memory
  get_stuff_done
end
puts result

Code

# https://grosser.it/2012/07/15/return-values-from-fork-fork_and_return/
def fork_and_return(&block)
  require 'parallel'
  Parallel.map([0], &block).first
end