From accd9b4634f1a2f220bc3e38cf5c4add04186fe6 Mon Sep 17 00:00:00 2001
From: Joshua Peek <josh@joshpeek.com>
Date: Fri, 25 Sep 2009 21:32:28 -0500
Subject: Restore "Start Rails::Application object

This reverts commit f14ad4145622f45e9bf7433b5fdef4ce427efe4b.
---
 railties/test/application/load_test.rb   |  56 ++++++++++++++++
 railties/test/isolation/abstract_unit.rb | 107 +++++++++++++++++++++++++++++++
 2 files changed, 163 insertions(+)
 create mode 100644 railties/test/application/load_test.rb
 create mode 100644 railties/test/isolation/abstract_unit.rb

(limited to 'railties/test')

diff --git a/railties/test/application/load_test.rb b/railties/test/application/load_test.rb
new file mode 100644
index 0000000000..6dda47bb8f
--- /dev/null
+++ b/railties/test/application/load_test.rb
@@ -0,0 +1,56 @@
+require "isolation/abstract_unit"
+require "rails"
+require "rack"
+
+module ApplicationTests
+  class LoadTest < Test::Unit::TestCase
+    include ActiveSupport::Testing::Isolation
+
+    def setup
+      build_app
+    end
+
+    test "rails app is present" do
+      assert File.exist?(app_path("config"))
+    end
+
+    test "running Rails::Application.load on the path returns a (vaguely) useful application" do
+      @app = Rails::Application.load app_path
+      assert_welcome get("/")
+    end
+
+    test "config.ru is used" do
+      app_file "config.ru", <<-CONFIG
+        class MyMiddleware
+          def initialize(app)
+            @app = app
+          end
+
+          def call(env)
+            status, headers, body = @app.call(env)
+            headers["Config-Ru-Test"] = "TESTING"
+            [status, headers, body]
+          end
+        end
+
+        use MyMiddleware
+        run proc {|env| [200, {"Content-Type" => "text/html"}, ["VICTORY"]] }
+      CONFIG
+
+      @app = Rails::Application.load app_path, :config => "config.ru"
+
+      assert_body    "VICTORY", get("/omg")
+      assert_header  "Config-Ru-Test", "TESTING", get("/omg")
+    end
+
+    test "arbitrary.rb can be used as a config" do
+      app_file "myapp.rb", <<-CONFIG
+        Myapp = proc {|env| [200, {"Content-Type" => "text/html"}, ["OMG ROBOTS"]] }
+      CONFIG
+
+      @app = Rails::Application.load app_path, :config => "myapp.rb"
+
+      assert_body "OMG ROBOTS", get("/omg")
+    end
+  end
+end
diff --git a/railties/test/isolation/abstract_unit.rb b/railties/test/isolation/abstract_unit.rb
new file mode 100644
index 0000000000..d3fc1f3395
--- /dev/null
+++ b/railties/test/isolation/abstract_unit.rb
@@ -0,0 +1,107 @@
+# 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.
+
+# 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 load paths usually get set inside of boot.rb
+$:.unshift "#{RAILS_FRAMEWORK_ROOT}/railties/lib"
+$:.unshift "#{RAILS_FRAMEWORK_ROOT}/actionpack/lib"
+
+# 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
+
+    def tmp_path(*args)
+      File.expand_path(File.join(File.dirname(__FILE__), *%w[.. .. tmp] + args))
+    end
+
+    def app_path(*args)
+      tmp_path(*%w[app] + args)
+    end
+  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
+    def build_app
+      FileUtils.cp_r(tmp_path('app_template'), app_path)
+    end
+
+    def app_file(path, contents)
+      File.open(app_path(path), 'w') do |f|
+        f.puts contents
+      end
+    end
+
+    def controller(name, contents)
+      app_file("app/controllers/#{name}_controller.rb", contents)
+    end
+  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
-- 
cgit v1.2.3