-
-
Save cheshire137/c8446ce500268752ac3d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'erb' | |
# lib/missing_spec_generator.rb | |
class MissingSpecGenerator | |
RAILS_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..')) | |
def spec_file spec_path, file_name, spec_template, namespace | |
spec_name = file_name.gsub('.rb', '') + '_spec.rb' | |
if File.exist?("#{spec_path}/#{spec_name}") | |
puts "#{spec_path}/#{spec_name} exists" | |
else | |
puts "#{spec_path}/#{spec_name} missing" | |
puts "\n" | |
spec_file = ERB.new(spec_template) | |
class_name = "#{namespace}#{file_name.gsub('.rb', '').camelcase}" | |
spec = spec_file.result(binding) | |
puts spec | |
FileUtils.mkdir_p(spec_path) unless File.exist?(spec_path) | |
File.open("#{spec_path}/#{spec_name}", 'w') {|f| f.write(spec) } | |
puts "\n" | |
end | |
end | |
def traverse_specs dir_name, spec_template, namespace='' | |
source_path = RAILS_ROOT + '/app/' + dir_name | |
unless File.directory?(source_path) | |
puts "No #{dir_name}" | |
return | |
end | |
Dir.open(source_path).each do |file_name| | |
unless file_name.match(/^\./) # skip hidden folders (.svn) | |
if File.directory?(RAILS_ROOT + '/app/' + dir_name + '/' + file_name) | |
traverse_specs("#{dir_name}/#{file_name}", spec_template, | |
"#{namespace}#{file_name.camelcase}::") | |
else | |
spec_file("#{RAILS_ROOT}/spec/#{dir_name}", file_name, spec_template, | |
namespace) | |
end | |
end | |
end | |
end | |
def generate_missing_helper_specs | |
helper_template = %q{require 'rails_helper' | |
RSpec.describe <%= class_name %>, type: :helper do | |
pending "add some examples to (or delete) #{__FILE__}" | |
end | |
}.gsub(/^ /, '') | |
traverse_specs('helpers', helper_template) | |
end | |
def generate_missing_controller_specs | |
controller_template = %q{require 'rails_helper' | |
RSpec.describe <%= class_name %>, type: :controller do | |
pending "add some examples to (or delete) #{__FILE__}" | |
end | |
}.gsub(/^ /, '') | |
traverse_specs('controllers', controller_template) | |
end | |
def generate_missing_worker_specs | |
worker_template = %q{require 'rails_helper' | |
RSpec.describe <%= class_name %>, type: :worker do | |
pending "add some examples to (or delete) #{__FILE__}" | |
end | |
}.gsub(/^ /, '') | |
traverse_specs('workers', worker_template) | |
end | |
def generate_missing_model_specs | |
model_template = %q{require 'rails_helper' | |
RSpec.describe <%= class_name %>, type: :model do | |
pending "add some examples to (or delete) #{__FILE__}" | |
end | |
}.gsub(/^ /, '') | |
traverse_specs('models', model_template) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace :my_project do | |
desc "Generate the missing rspec files from base templates" | |
task :generate_missing_specs do | |
require 'missing_spec_generator' | |
msg = MissingSpecGenerator.new | |
msg.generate_missing_helper_specs | |
msg.generate_missing_controller_specs | |
msg.generate_missing_model_specs | |
msg.generate_missing_worker_specs | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Assuming this is rails lib, you could do like
RAILS_ROOT = Rails.root
or even replaceRAILS_ROOT
withRails.root