Skip to content

Instantly share code, notes, and snippets.

@niklas
Created April 3, 2013 19:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save niklas/5304603 to your computer and use it in GitHub Desktop.
Save niklas/5304603 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby-1.9.3-p392@markup_snapshot
# Our pixel hero wants to know when we change the HTML layout. This script runs
# selected cucumber features in given branch/sha1 and makes snapshots in HTML
# and HAML, original and stripped.
#
# Prerequisites:
# you
# * have rails project
# * use cucumber with capybara
# * use git
# * use rvm & bundler
# * have bash in your path
# Setup:
# rvm use ruby-1.9.3-p392@markup_snapshot --create
require 'pathname'
require 'fileutils'
class MarkupSnapshot
Root = Pathname.pwd.join('tmp/markup_snapshots').expand_path
Cukes = []
StepsPath = Root.join('snapshot_steps.rb')
class Cuke < Struct.new(:name, :content)
def initialize(*a)
super
name.gsub!(/\W/i,'_')
unless name =~ /\.feature$/
self.name += '.feature'
end
end
end
def self.cuke(name, content)
Cukes << Cuke.new(name,content)
end
def self.run(*a)
new(*a).run
end
attr_reader :commit
def initialize(commit=nil)
@commit = commit
end
def run
#checkout
#bundle
#migrate
#run_cukes
#install_gems
dump_hamls
end
private
def install_gems
html2haml_version = '1.0.1'
sys %Q~gem list | grep html2haml | grep #{html2haml_version} || gem install html2haml --no-ri --no-rdoc -v #{html2haml_version}~
nokogiri_version = '1.5.9'
sys %Q~gem list | grep nokogiri | grep #{nokogiri_version} || gem install nokogiri --no-ri --no-rdoc -v #{nokogiri_version}~
end
def checkout
if commit
sys %Q~git checkout #{commit}~
end
end
def bundle
sys_project %Q~bundle check || bundle install~
end
def migrate
sys_project %Q~bundle exec rake db:drop db:create db:migrate RAILS_ENV=test~
end
def root
Root.join commit ? commit : 'current'
end
def run_cukes
FileUtils.mkdir_p root
write_steps
write_cukes
cukes = Cukes.map { |cuke| Root.join(cuke.name) }
sys_project %Q~bundle exec cucumber -r features/ -r #{StepsPath} #{cukes.join(' ')}~
end
# TODO run wget mirror to save all assets
def write_steps
File.open StepsPath, 'w' do |f|
f.puts <<-EOSTEPS
When /^I take a snapshot$/ do
require 'capybara/util/save_and_open_page'
@snapshot_index ||= 0
@snapshot_index += 1
Capybara.save_and_open_page_path = #{root.to_s.inspect}
Capybara.save_page(body, @snapshot_index.to_s + '.html')
end
EOSTEPS
end
end
def write_cukes
Cukes.each do |cuke|
File.open Root.join(cuke.name), 'w' do |f|
f.write cuke.content
end
end
end
def htmls
@htmls ||= [].tap do |files|
root.find do |html|
if html.fnmatch('*.html')
files << html
end
end
end
end
def dump_hamls
require 'nokogiri'
htmls.each do |html|
dump_haml(html)
stripped = html.sub(/.html$/, '.stripped.html')
File.open(stripped, 'w') { |f| f.print strip_text_from_html(File.read(html)) }
dump_haml(stripped)
end
end
def dump_haml(html)
haml = html.sub(/.html$/, '.haml')
sys 'html2haml', '--trace', html.to_s, haml.to_s
end
def strip_text_from_html(html)
Nokogiri::HTML.parse(html).tap do |doc|
doc.css('*').each do |node|
node[:alt] = nil if node[:alt]
if node.text?
node.remove
else
node.children.each do |child|
child.remove if child.text?
end
end
end
end.to_s
end
# runs the given command and tells the user about it
def sys(*a)
line = a.join(' ')
STDERR.puts " >>> #{line}"
system line
end
# runs the given command in the original rvm environment
def sys_project(*a)
line = a.join(' ')
STDERR.puts " >>> #{line}"
line = 'source .rvmrc && ' + line
system 'bash', '-l', '-c', line
end
end
MarkupSnapshot.cuke 'homepage', <<EOCUKE
Feature: Homepage
Scenario: empty
Given I am on the home page
Then I take a snapshot
EOCUKE
# Add more cukes here
MarkupSnapshot.run(ARGV.first)
# vim:filetype=ruby
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment