Remove unnecessary nocolorize flag

Setting `TERM=dumb` is enough to disable colorization in those tests.

Mocked app should also load console helpers

Move console method extension to IRBConsole's constructor

Test IRB autocompletion control without mutating ENV

Control IRB autocompletion without mutating ENV

Correct local variable name
This commit is contained in:
Stan Lo 2024-01-19 21:37:57 +00:00
parent 30cb7a6221
commit 4d6714fcd3
No known key found for this signature in database
GPG Key ID: 76D63D5E36F02747
3 changed files with 40 additions and 60 deletions

View File

@ -16,12 +16,9 @@ module Rails
def initialize
require "irb"
require "irb/completion"
IRB::WorkSpace.prepend(BacktraceCleaner)
if !Rails.env.local?
# Use env var here so users can override them with env var too
ENV["IRB_USE_AUTOCOMPLETE"] ||= "false"
end
IRB::WorkSpace.prepend(BacktraceCleaner)
IRB::ExtendCommandBundle.include(Rails::ConsoleMethods)
end
def name
@ -31,6 +28,10 @@ module Rails
def start
IRB.setup(nil)
if !Rails.env.local? && !ENV.key?("IRB_USE_AUTOCOMPLETE")
IRB.conf[:USE_AUTOCOMPLETE] = false
end
env = colorized_env
IRB.conf[:PROMPT][:RAILS_PROMPT] = {
@ -108,9 +109,6 @@ module Rails
puts "Loading #{Rails.env} environment (Rails #{Rails.version})"
end
if defined?(console::ExtendCommandBundle)
console::ExtendCommandBundle.include(Rails::ConsoleMethods)
end
console.start
end
end

View File

@ -140,7 +140,7 @@ class FullStackConsoleTest < ActiveSupport::TestCase
end
def test_sandbox
options = "--sandbox -- --nocolorize"
options = "--sandbox"
spawn_console(options)
write_prompt "Post.count", "=> 0"
@ -172,7 +172,7 @@ class FullStackConsoleTest < ActiveSupport::TestCase
config.sandbox_by_default = true
RUBY
options = "-e production -- --verbose --nocolorize"
options = "-e production -- --verbose"
spawn_console(options)
write_prompt "puts Rails.application.sandbox", "puts Rails.application.sandbox\r\ntrue"
@ -184,7 +184,7 @@ class FullStackConsoleTest < ActiveSupport::TestCase
config.sandbox_by_default = true
RUBY
options = "-e production --no-sandbox -- --verbose --nocolorize"
options = "-e production --no-sandbox -- --verbose"
spawn_console(options)
write_prompt "puts Rails.application.sandbox", "puts Rails.application.sandbox\r\nfalse"
@ -196,7 +196,7 @@ class FullStackConsoleTest < ActiveSupport::TestCase
config.sandbox_by_default = true
RUBY
options = "-- --verbose --nocolorize"
options = "-- --verbose"
spawn_console(options)
write_prompt "puts Rails.application.sandbox", "puts Rails.application.sandbox\r\nfalse"
@ -204,7 +204,7 @@ class FullStackConsoleTest < ActiveSupport::TestCase
end
def test_environment_option_and_irb_option
options = "-e test -- --verbose --nocolorize"
options = "-e test -- --verbose"
spawn_console(options)
write_prompt "a = 1", "a = 1"
@ -213,21 +213,21 @@ class FullStackConsoleTest < ActiveSupport::TestCase
end
def test_production_console_prompt
options = "-e production -- --nocolorize"
options = "-e production"
spawn_console(options)
write_prompt "123", "prod:001> 123"
end
def test_development_console_prompt
options = "-e development -- --nocolorize"
options = "-e development"
spawn_console(options)
write_prompt "123", "dev:001> 123"
end
def test_test_console_prompt
options = "-e test -- --nocolorize"
options = "-e test"
spawn_console(options)
write_prompt "123", "test:001> 123"
@ -240,11 +240,32 @@ class FullStackConsoleTest < ActiveSupport::TestCase
RUBY
irbrc.close
options = "-e test -- --nocolorize"
options = "-e test"
spawn_console(options, env: { "IRBRC" => irbrc.path })
write_prompt "123", ">> 123"
ensure
File.unlink(irbrc)
end
def test_console_disables_IRB_auto_completion_in_non_local
options = "-e production -- --verbose"
spawn_console(options)
write_prompt "IRB.conf[:USE_AUTOCOMPLETE]", "IRB.conf[:USE_AUTOCOMPLETE]\r\n=> false"
end
def test_console_accepts_override_on_IRB_auto_completion_flag
options = "-e production -- --verbose"
spawn_console(options, env: { "IRB_USE_AUTOCOMPLETE" => "true" })
write_prompt "IRB.conf[:USE_AUTOCOMPLETE]", "IRB.conf[:USE_AUTOCOMPLETE]\r\n=> true"
end
def test_console_doesnt_disable_IRB_auto_completion_in_local
options = "-e development -- --verbose"
spawn_console(options)
write_prompt "IRB.conf[:USE_AUTOCOMPLETE]", "IRB.conf[:USE_AUTOCOMPLETE]\r\n=> true"
end
end

View File

@ -58,47 +58,6 @@ class Rails::ConsoleTest < ActiveSupport::TestCase
assert_equal "IRB", Rails::Console.new(app).console.name
end
def test_console_disables_IRB_auto_completion_in_non_local
original_use_autocomplete = ENV["IRB_USE_AUTOCOMPLETE"]
ENV["IRB_USE_AUTOCOMPLETE"] = nil
with_rack_env "production" do
app = build_app(nil)
assert_not_predicate Rails.env, :local?
assert_equal "IRB", Rails::Console.new(app).console.name
assert_equal "false", ENV["IRB_USE_AUTOCOMPLETE"]
end
ensure
ENV["IRB_USE_AUTOCOMPLETE"] = original_use_autocomplete
end
def test_console_accepts_override_on_IRB_auto_completion_flag
original_use_autocomplete = ENV["IRB_USE_AUTOCOMPLETE"]
ENV["IRB_USE_AUTOCOMPLETE"] = "true"
with_rack_env "production" do
app = build_app(nil)
assert_equal "IRB", Rails::Console.new(app).console.name
assert_equal "true", ENV["IRB_USE_AUTOCOMPLETE"]
end
ensure
ENV["IRB_USE_AUTOCOMPLETE"] = original_use_autocomplete
end
def test_console_doesnt_disable_IRB_auto_completion_in_local
original_use_autocomplete = ENV["IRB_USE_AUTOCOMPLETE"]
ENV["IRB_USE_AUTOCOMPLETE"] = nil
with_rails_env nil do
app = build_app(nil)
assert_predicate Rails.env, :local?
assert_equal "IRB", Rails::Console.new(app).console.name
assert_nil ENV["IRB_USE_AUTOCOMPLETE"]
end
ensure
ENV["IRB_USE_AUTOCOMPLETE"] = original_use_autocomplete
end
def test_prompt_env_colorization
irb_console = Rails::Console::IRBConsole.new
red = "\e[31m"
@ -186,7 +145,7 @@ class Rails::ConsoleTest < ActiveSupport::TestCase
end
def build_app(console)
mocked_console = Class.new do
mocked_app = Class.new do
attr_accessor :sandbox
attr_reader :console, :disable_sandbox, :sandbox_by_default
@ -199,9 +158,11 @@ class Rails::ConsoleTest < ActiveSupport::TestCase
end
def load_console
require "rails/console/app"
require "rails/console/helpers"
end
end
mocked_console.new(console)
mocked_app.new(console)
end
def parse_arguments(args)