1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
|
require 'rails/generators'
require 'active_support/core_ext'
require 'tmpdir'
require 'fileutils'
module Ammeter
module RSpec
module Rails
# Delegates to Rails::Generators::TestCase to work with RSpec.
module GeneratorExampleGroup
extend ActiveSupport::Concern
include ::RSpec::Rails::RailsExampleGroup
DELEGATED_METHODS = [:destination_root, :current_path, :generator_class]
module ClassMethods
mattr_accessor :test_unit_test_case_delegate
delegate :default_arguments, :to => :'self.test_unit_test_case_delegate'
DELEGATED_METHODS.each do |method|
delegate method, :to => :'self.test_unit_test_case_delegate'
end
delegate :destination, :arguments, :to => ::Rails::Generators::TestCase
def prepare_destination
self.test_unit_test_case_delegate.send :prepare_destination
end
def ensure_current_path
self.test_unit_test_case_delegate.send :ensure_current_path
end
def initialize_delegate
self.test_unit_test_case_delegate = ::Rails::Generators::TestCase.new 'pending'
self.test_unit_test_case_delegate.class.tests(described_class)
@generator = nil
end
def generator(given_args=self.default_arguments, config={})
@generator ||= begin
args, opts = Thor::Options.split(given_args)
self.test_unit_test_case_delegate.generator(args, opts, config)
end
end
def run_generator(given_args=self.default_arguments, config={})
OutputCapturer.capture(:stdout) { generator(given_args, config).invoke_all }
end
end
def invoke_task name
OutputCapturer.capture(:stdout) { generator.invoke_task(generator_class.all_tasks[name.to_s]) }
end
included do
delegate :generator, :run_generator, :destination, :arguments, :ensure_current_path, :to => :'self.class'
DELEGATED_METHODS.each do |method|
delegate method, :to => :'self.class'
end
def prepare_destination
self.class.send :prepare_destination
end
::Rails::Generators::TestCase.destination File.expand_path('ammeter', Dir.tmpdir)
initialize_delegate
before do
self.class.initialize_delegate
prepare_destination
end
after do
# ensure_current_path
end
metadata[:type] = :generator
end
def file relative
File.expand_path(relative, destination_root)
end
def migration_file relative
file_path = file(relative)
migration_file = Dir.glob("#{File.dirname(file_path)}/[0-9]*_#{File.basename(file_path)}").first
migration_file = "#{File.dirname(file_path)}/TIMESTAMP_#{File.basename(file_path)}" if migration_file.nil?
migration_file
end
def subject
generator
end
end
end
end
end
|