Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Try to automated replace create for build_stubbed in specs
require 'rubygems'
require 'fast'
def nice_experiment_with(file)
parts = file.split('/')
dir = parts[0..-2]
filename = "experiment_#{parts[-1]}"
File.join(*dir, filename)
end
ignored =
if File.exist?('failed.txt')
File.readlines('failed.txt').map(&:chomp)
else
[]
end
def experiment(file, search, replacement)
ast = Fast.ast_from_file(file)
results = Fast.search(ast, search)
unless results.empty?
new_content = Fast.replace_file(file, search, replacement)
new_spec = nice_experiment_with(file)
return if File.exist?(new_spec)
File.open(new_spec, 'w+') { |f| f.puts new_content }
if system("bin/spring rspec #{new_spec}")
system "mv #{new_spec} #{file}"
puts "โœ… #{file}"
else
puts "๐Ÿ”ด #{file}"
end
end
rescue
puts "๐Ÿ”ด๐Ÿ”ด ๐Ÿ”ด #{file}: #{$ERROR_INFO}"
end
`grep ' create' spec/**/*_spec.rb | awk -s: '{ print $1}'`.lines.map { |e| e[0..-3] }.uniq.each do |file|
next if ignored.include?(file)
next if file =~ /chewy/
puts file
experiment(file, '(send nil create)', ->(node) { replace(node.location.selector, 'build_stubbed') })
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment