abstract_unit.rb 4.0 KB
Newer Older
1 2 3 4 5 6 7 8 9
# Note:
# It is important to keep this file as light as possible
# the goal for tests that require this is to test booting up
# rails from an empty state, so anything added here could
# hide potential failures
#
# It is also good to know what is the bare minimum to get
# Rails booted up.

J
Jeremy Kemper 已提交
10 11
require 'fileutils'

12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
# TODO: Remove rubygems when possible
require 'rubygems'
require 'test/unit'

# TODO: Remove setting this magic constant
RAILS_FRAMEWORK_ROOT = File.expand_path("#{File.dirname(__FILE__)}/../../..")

# These files do not require any others and are needed
# to run the tests
require "#{RAILS_FRAMEWORK_ROOT}/activesupport/lib/active_support/testing/isolation"
require "#{RAILS_FRAMEWORK_ROOT}/activesupport/lib/active_support/testing/declarative"

module TestHelpers
  module Paths
    module_function

28 29
    TMP_PATH = File.expand_path(File.join(File.dirname(__FILE__), *%w[.. .. tmp]))

30
    def tmp_path(*args)
31
      File.join(TMP_PATH, *args)
32 33 34 35 36
    end

    def app_path(*args)
      tmp_path(*%w[app] + args)
    end
J
Joshua Peek 已提交
37

38 39 40 41
    def framework_path
      RAILS_FRAMEWORK_ROOT
    end

J
Joshua Peek 已提交
42 43 44
    def rails_root
      app_path
    end
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
  end

  module Rack
    def extract_body(response)
      "".tap do |body|
        response[2].each {|chunk| body << chunk }
      end
    end

    def get(path)
      @app.call(::Rack::MockRequest.env_for(path))
    end

    def assert_welcome(resp)
      assert_equal 200, resp[0]
      assert resp[1]["Content-Type"] = "text/html"
      assert extract_body(resp).match(/Welcome aboard/)
    end

    def assert_success(resp)
      assert_equal 202, resp[0]
    end

    def assert_missing(resp)
      assert_equal 404, resp[0]
    end

    def assert_header(key, value, resp)
      assert_equal value, resp[1][key.to_s]
    end

    def assert_body(expected, resp)
      assert_equal expected, extract_body(resp)
    end
  end

  module Generation
82 83
    def build_app(options = {})
      FileUtils.rm_rf(app_path)
84
      FileUtils.cp_r(tmp_path('app_template'), app_path)
85 86 87 88 89 90 91 92

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

93 94 95 96
      add_to_config 'config.action_controller.session = { :key => "_myapp_session", :secret => "bac838a849c1d5c4de2e6a50af826079" }'
    end

    def add_to_config(str)
97
      environment = File.read("#{app_path}/config/application.rb")
98
      if environment =~ /(\n\s*end\s*)\Z/
99
        File.open("#{app_path}/config/application.rb", 'w') do |f|
100
          f.puts $` + "\n#{str}\n" + $1
101 102
        end
      end
103 104 105
    end

    def app_file(path, contents)
106 107
      FileUtils.mkdir_p File.dirname("#{app_path}/#{path}")
      File.open("#{app_path}/#{path}", 'w') do |f|
108 109 110 111 112 113 114
        f.puts contents
      end
    end

    def controller(name, contents)
      app_file("app/controllers/#{name}_controller.rb", contents)
    end
115 116

    def boot_rails
J
Jeremy Kemper 已提交
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
      bundled = "#{File.dirname(__FILE__)}/../../vendor/gems/environment"
      if File.exist?("#{bundled}.rb")
        require bundled
        %w(railties railties/lib).each do |path|
          $LOAD_PATH.unshift File.expand_path("../../../../#{path}", __FILE__)
        end
      else
        %w(
          actionmailer/lib
          actionpack/lib
          activemodel/lib
          activerecord/lib
          activeresource/lib
          activesupport/lib
          railties/lib
          railties
        ).reverse_each do |path|
          path = File.expand_path("../../../../#{path}", __FILE__)
          $:.unshift(path)
        end
137
      end
138
    end
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
  end
end

class Test::Unit::TestCase
  include TestHelpers::Paths
  include TestHelpers::Rack
  include TestHelpers::Generation
  extend  ActiveSupport::Testing::Declarative
end

# Create a scope and build a fixture rails app
Module.new do
  extend TestHelpers::Paths
  # Build a rails app
  if File.exist?(tmp_path)
    FileUtils.rm_rf(tmp_path)
  end

  FileUtils.mkdir(tmp_path)
  `#{Gem.ruby} #{RAILS_FRAMEWORK_ROOT}/railties/bin/rails #{tmp_path('app_template')}`
end