module TestHelpers::Generation

Public Instance Methods

add_to_config(str) click to toggle source
# File railties/test/isolation/abstract_unit.rb, line 254
def add_to_config(str)
  environment = File.read("#{app_path}/config/application.rb")
  if environment =~ /(\n\s*end\s*end\s*)\z/
    File.open("#{app_path}/config/application.rb", "w") do |f|
      f.puts $` + "\n#{str}\n" + $1
    end
  end
end
add_to_env_config(env, str) click to toggle source
# File railties/test/isolation/abstract_unit.rb, line 263
def add_to_env_config(env, str)
  environment = File.read("#{app_path}/config/environments/#{env}.rb")
  if environment =~ /(\n\s*end\s*)\z/
    File.open("#{app_path}/config/environments/#{env}.rb", "w") do |f|
      f.puts $` + "\n#{str}\n" + $1
    end
  end
end
add_to_top_of_config(str) click to toggle source
# File railties/test/isolation/abstract_unit.rb, line 245
def add_to_top_of_config(str)
  environment = File.read("#{app_path}/config/application.rb")
  if environment =~ /(Quails::Application\s*)/
    File.open("#{app_path}/config/application.rb", "w") do |f|
      f.puts $` + $1 + "\n#{str}\n" + $'
    end
  end
end
app_file(path, contents, mode = "w") click to toggle source
# File railties/test/isolation/abstract_unit.rb, line 286
def app_file(path, contents, mode = "w")
  FileUtils.mkdir_p File.dirname("#{app_path}/#{path}")
  File.open("#{app_path}/#{path}", mode) do |f|
    f.puts contents
  end
end
build_app(options = {}) click to toggle source

Build an application by invoking the generator and going through the whole stack.

# File railties/test/isolation/abstract_unit.rb, line 108
    def build_app(options = {})
      @prev_quails_env = ENV["RAILS_ENV"]
      ENV["RAILS_ENV"] = "development"
      ENV["SECRET_KEY_BASE"] ||= SecureRandom.hex(16)

      FileUtils.rm_rf(app_path)
      FileUtils.cp_r(app_template_path, app_path)

      # Delete the initializers unless requested
      unless options[:initializers]
        Dir["#{app_path}/config/initializers/**/*.rb"].each do |initializer|
          File.delete(initializer)
        end
      end

      routes = File.read("#{app_path}/config/routes.rb")
      if routes =~ /(\n\s*end\s*)\z/
        File.open("#{app_path}/config/routes.rb", "w") do |f|
          f.puts $` + "\nActiveSupport::Deprecation.silence { match ':controller(/:action(/:id))(.:format)', via: :all }\n" + $1
        end
      end

      File.open("#{app_path}/config/database.yml", "w") do |f|
        f.puts <<-YAML
        default: &default
          adapter: sqlite3
          pool: 5
          timeout: 5000
        development:
          <<: *default
          database: db/development.sqlite3
        test:
          <<: *default
          database: db/test.sqlite3
        production:
          <<: *default
          database: db/production.sqlite3
        YAML
      end

      add_to_config <<-RUBY
        config.eager_load = false
        config.session_store :cookie_store, key: "_myapp_session"
        config.active_support.deprecation = :log
        config.active_support.test_order = :random
        config.action_controller.allow_forgery_protection = false
        config.log_level = :info
      RUBY
    end
controller(name, contents) click to toggle source
# File railties/test/isolation/abstract_unit.rb, line 297
def controller(name, contents)
  app_file("app/controllers/#{name}_controller.rb", contents)
end
engine(name) { |bukkit| ... } click to toggle source
# File railties/test/isolation/abstract_unit.rb, line 222
def engine(name)
  dir = "#{app_path}/random/#{name}"
  FileUtils.mkdir_p(dir)

  app = File.readlines("#{app_path}/config/application.rb")
  app.insert(4, "$:.unshift(\"#{dir}/lib\")")
  app.insert(5, "require #{name.inspect}")

  File.open("#{app_path}/config/application.rb", "r+") do |f|
    f.puts app
  end

  Bukkit.new(dir).tap do |bukkit|
    yield bukkit if block_given?
  end
end
make_basic_app() { |app| ... } click to toggle source

Make a very basic app, without creating the whole directory structure. This is faster and simpler than the method above.

# File railties/test/isolation/abstract_unit.rb, line 164
def make_basic_app
  require "quails"
  require "action_controller/railtie"
  require "action_view/railtie"

  @app = Class.new(Quails::Application)
  @app.config.eager_load = false
  @app.secrets.secret_key_base = "3b7cd727ee24e8444053437c36cc66c4"
  @app.config.session_store :cookie_store, key: "_myapp_session"
  @app.config.active_support.deprecation = :log
  @app.config.active_support.test_order = :random
  @app.config.log_level = :info

  yield @app if block_given?
  @app.initialize!

  @app.routes.draw do
    get "/" => "omg#index"
  end

  require "rack/test"
  extend ::Rack::Test::Methods
end
remove_file(path) click to toggle source
# File railties/test/isolation/abstract_unit.rb, line 293
def remove_file(path)
  FileUtils.rm_rf "#{app_path}/#{path}"
end
remove_from_config(str) click to toggle source
# File railties/test/isolation/abstract_unit.rb, line 272
def remove_from_config(str)
  remove_from_file("#{app_path}/config/application.rb", str)
end
remove_from_env_config(env, str) click to toggle source
# File railties/test/isolation/abstract_unit.rb, line 276
def remove_from_env_config(env, str)
  remove_from_file("#{app_path}/config/environments/#{env}.rb", str)
end
remove_from_file(file, str) click to toggle source
# File railties/test/isolation/abstract_unit.rb, line 280
def remove_from_file(file, str)
  contents = File.read(file)
  contents.sub!(/#{str}/, "")
  File.write(file, contents)
end
script(script) click to toggle source
# File railties/test/isolation/abstract_unit.rb, line 239
def script(script)
  Dir.chdir(app_path) do
    `#{Gem.ruby} #{app_path}/bin/quails #{script}`
  end
end
simple_controller() click to toggle source
# File railties/test/isolation/abstract_unit.rb, line 188
    def simple_controller
      controller :foo, <<-RUBY
        class FooController < ApplicationController
          def index
            render plain: "foo"
          end
        end
      RUBY

      app_file "config/routes.rb", <<-RUBY
        Quails.application.routes.draw do
          get ':controller(/:action)'
        end
      RUBY
    end
teardown_app() click to toggle source
# File railties/test/isolation/abstract_unit.rb, line 158
def teardown_app
  ENV["RAILS_ENV"] = @prev_quails_env if @prev_quails_env
end
use_frameworks(arr) click to toggle source
# File railties/test/isolation/abstract_unit.rb, line 301
def use_frameworks(arr)
  to_remove = [:actionmailer, :activerecord] - arr

  if to_remove.include?(:activerecord)
    remove_from_config "config.active_record.*"
  end

  $:.reject! { |path| path =~ %r'/(#{to_remove.join('|')})/' }
end