1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Merge pull request #27601 from y-yagi/make_work_all_commands_from_engine

Make all rails commands work in engine
This commit is contained in:
Kasper Timm Hansen 2017-01-15 20:15:45 +01:00 committed by GitHub
commit f8a3981beb
10 changed files with 124 additions and 54 deletions

View file

@ -1,3 +1,7 @@
* Make every Rails command work within engines.
*Sean Collins*, *Yuji Yaginuma*
* Don't generate HTML/ERB templates for scaffold controller with `--api` flag. * Don't generate HTML/ERB templates for scaffold controller with `--api` flag.
Fixes #27591. Fixes #27591.

View file

@ -11,6 +11,11 @@ module Rails
if defined?(ENGINE_PATH) if defined?(ENGINE_PATH)
def require_application_and_environment! def require_application_and_environment!
require ENGINE_PATH require ENGINE_PATH
if defined?(APP_PATH)
require APP_PATH
Rails.application.require_environment!
end
end end
def load_tasks def load_tasks

View file

@ -1,16 +1,3 @@
Usage: bin/rails COMMAND [args] [options]
<% if engine? %>
The common Rails commands available for engines are:
generate Generate new code (short-cut alias: "g")
destroy Undo code generated with "generate" (short-cut alias: "d")
test Run tests (short-cut alias: "t")
All commands can be run with -h for more information.
If you want to run any commands that need to be run in context
of the application, like `bin/rails server` or `bin/rails console`,
you should do it from the application's directory (typically test/dummy).
<% else %>
The most common rails commands are: The most common rails commands are:
generate Generate new code (short-cut alias: "g") generate Generate new code (short-cut alias: "g")
console Start the Rails console (short-cut alias: "c") console Start the Rails console (short-cut alias: "c")
@ -18,10 +5,11 @@ The most common rails commands are:
test Run tests (short-cut alias: "t") test Run tests (short-cut alias: "t")
dbconsole Start a console for the database specified in config/database.yml dbconsole Start a console for the database specified in config/database.yml
(short-cut alias: "db") (short-cut alias: "db")
<% unless engine? %>
new Create a new Rails application. "rails new my_app" creates a new Create a new Rails application. "rails new my_app" creates a
new application called MyApp in "./my_app" new application called MyApp in "./my_app"
<% end %>
All commands can be run with -h (or --help) for more information. All commands can be run with -h (or --help) for more information.
<% end %>
In addition to those commands, there are: In addition to those commands, there are:

View file

@ -95,7 +95,7 @@ module Rails
module Command module Command
class ServerCommand < Base # :nodoc: class ServerCommand < Base # :nodoc:
DEFAULT_PID_PATH = File.expand_path("tmp/pids/server.pid").freeze DEFAULT_PID_PATH = "tmp/pids/server.pid".freeze
class_option :port, aliases: "-p", type: :numeric, class_option :port, aliases: "-p", type: :numeric,
desc: "Runs Rails on the specified port.", banner: :port, default: 3000 desc: "Runs Rails on the specified port.", banner: :port, default: 3000
@ -141,7 +141,7 @@ module Rails
config: options[:config], config: options[:config],
environment: environment, environment: environment,
daemonize: options[:daemon], daemonize: options[:daemon],
pid: options[:pid], pid: pid,
caching: options["dev-caching"], caching: options["dev-caching"],
restart_cmd: restart_command restart_cmd: restart_command
} }
@ -165,6 +165,10 @@ module Rails
"bin/rails server #{@server} #{@original_options.join(" ")}" "bin/rails server #{@server} #{@original_options.join(" ")}"
end end
def pid
File.expand_path(options[:pid])
end
def self.banner(*) def self.banner(*)
"rails server [puma, thin etc] [options]" "rails server [puma, thin etc] [options]"
end end

View file

@ -1,12 +1,7 @@
require "rails/command" unless defined?(APP_PATH)
if File.exist?(File.expand_path("test/dummy/config/application.rb", ENGINE_ROOT))
APP_PATH = File.expand_path("test/dummy/config/application", ENGINE_ROOT)
end
end
aliases = { require "rails/commands"
"g" => "generate",
"d" => "destroy",
"t" => "test"
}
command = ARGV.shift
command = aliases[command] || command
Rails::Command.invoke command, ARGV

View file

@ -3,6 +3,7 @@
ENGINE_ROOT = File.expand_path('../..', __FILE__) ENGINE_ROOT = File.expand_path('../..', __FILE__)
ENGINE_PATH = File.expand_path('../../lib/<%= namespaced_name -%>/engine', __FILE__) ENGINE_PATH = File.expand_path('../../lib/<%= namespaced_name -%>/engine', __FILE__)
APP_PATH = File.expand_path('../../<%= dummy_path -%>/config/application', __FILE__)
# Set up gems listed in the Gemfile. # Set up gems listed in the Gemfile.
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__) ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)

View file

@ -122,11 +122,11 @@ class Rails::ServerTest < ActiveSupport::TestCase
end end
def test_default_options def test_default_options
old_default_options = parse_arguments server = Rails::Server.new
old_default_options = server.default_options
Dir.chdir("..") do Dir.chdir("..") do
default_options = parse_arguments assert_equal old_default_options, server.default_options
assert_equal old_default_options, default_options
end end
end end

View file

@ -1,24 +0,0 @@
require "abstract_unit"
class Rails::Engine::CommandsTasksTest < ActiveSupport::TestCase
def setup
@destination_root = Dir.mktmpdir("bukkits")
Dir.chdir(@destination_root) { `bundle exec rails plugin new bukkits --mountable` }
end
def teardown
FileUtils.rm_rf(@destination_root)
end
def test_help_command_work_inside_engine
output = capture(:stderr) do
Dir.chdir(plugin_path) { `bin/rails --help` }
end
assert_no_match "NameError", output
end
private
def plugin_path
"#{@destination_root}/bukkits"
end
end

View file

@ -0,0 +1,96 @@
require "abstract_unit"
begin
require "pty"
rescue LoadError
end
class Rails::Engine::CommandsTest < ActiveSupport::TestCase
def setup
@destination_root = Dir.mktmpdir("bukkits")
Dir.chdir(@destination_root) { `bundle exec rails plugin new bukkits --mountable` }
end
def teardown
FileUtils.rm_rf(@destination_root)
end
def test_help_command_work_inside_engine
output = capture(:stderr) do
Dir.chdir(plugin_path) { `bin/rails --help` }
end
assert_no_match "NameError", output
end
def test_runner_command_work_inside_engine
output = capture(:stdout) do
Dir.chdir(plugin_path) { system("bin/rails runner 'puts Rails.env'") }
end
assert_equal "test", output.strip
end
def test_console_command_work_inside_engine
skip "PTY unavailable" unless available_pty?
master, slave = PTY.open
spawn_command("console", slave)
assert_output(">", master)
ensure
master.puts "quit"
end
def test_dbconsole_command_work_inside_engine
skip "PTY unavailable" unless available_pty?
master, slave = PTY.open
spawn_command("dbconsole", slave)
assert_output("sqlite>", master)
ensure
master.puts ".exit"
end
def test_server_command_work_inside_engine
skip "PTY unavailable" unless available_pty?
master, slave = PTY.open
pid = spawn_command("server", slave)
assert_output("Listening on", master)
ensure
kill(pid)
end
private
def plugin_path
"#{@destination_root}/bukkits"
end
def assert_output(expected, io, timeout = 10)
timeout = Time.now + timeout
output = ""
until output.include?(expected) || Time.now > timeout
if IO.select([io], [], [], 0.1)
output << io.read(1)
end
end
assert_includes output, expected, "#{expected.inspect} expected, but got:\n\n#{output}"
end
def spawn_command(command, fd)
Process.spawn(
"#{plugin_path}/bin/rails #{command}",
in: fd, out: fd, err: fd
)
end
def available_pty?
defined?(PTY) && PTY.respond_to?(:open)
end
def kill(pid)
Process.kill("TERM", pid)
Process.wait(pid)
rescue Errno::ESRCH
end
end

View file

@ -421,6 +421,7 @@ class PluginGeneratorTest < Rails::Generators::TestCase
run_generator [destination_root, "--full"] run_generator [destination_root, "--full"]
assert_file "bin/rails", /ENGINE_PATH = File.expand_path\('..\/..\/lib\/bukkits\/engine', __FILE__\)/ assert_file "bin/rails", /ENGINE_PATH = File.expand_path\('..\/..\/lib\/bukkits\/engine', __FILE__\)/
assert_file "bin/rails", /ENGINE_ROOT = File.expand_path\('..\/..', __FILE__\)/ assert_file "bin/rails", /ENGINE_ROOT = File.expand_path\('..\/..', __FILE__\)/
assert_file "bin/rails", %r|APP_PATH = File.expand_path\('../../test/dummy/config/application', __FILE__\)|
assert_file "bin/rails", /require 'rails\/all'/ assert_file "bin/rails", /require 'rails\/all'/
assert_file "bin/rails", /require 'rails\/engine\/commands'/ assert_file "bin/rails", /require 'rails\/engine\/commands'/
end end