aboutsummaryrefslogtreecommitdiffstats
path: root/railties/test/commands/console_test.rb
blob: 01847ae58ca1520d0348cd79695f822962a08d58 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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
82
83
84
85
86
87
require 'abstract_unit'
require 'rails/commands/console'

class Rails::ConsoleTest < ActiveSupport::TestCase
  class FakeConsole
  end

  def setup
  end

  def test_sandbox_option
    console = Rails::Console.new(app, ["--sandbox"])
    assert console.sandbox?
  end

  def test_short_version_of_sandbox_option
    console = Rails::Console.new(app, ["-s"])
    assert console.sandbox?
  end

  def test_debugger_option
    console = Rails::Console.new(app, ["--debugger"])
    assert console.debugger?
  end

  def test_no_options
    console = Rails::Console.new(app, [])
    assert !console.debugger?
    assert !console.sandbox?
  end

  def test_start
    app.expects(:sandbox=).with(nil)
    FakeConsole.expects(:start)

    start

    assert_match /Loading \w+ environment \(Rails/, output
  end

  def test_start_with_debugger
    app.expects(:sandbox=).with(nil)
    rails_console.expects(:require_debugger).returns(nil)
    FakeConsole.expects(:start)

    start ["--debugger"]
  end

  def test_start_with_sandbox
    app.expects(:sandbox=).with(true)
    FakeConsole.expects(:start)

    start ["--sandbox"]

    assert_match /Loading \w+ environment in sandbox \(Rails/, output
  end

  def test_console_defaults_to_IRB
    config = mock("config", :console => nil)
    app = mock("app", :config => config)
    app.expects(:load_console).returns(nil)

    assert_equal IRB, Rails::Console.new(app).console
  end

  private

  attr_reader :output

  def rails_console
    @rails_console ||= Rails::Console.new(app)
  end

  def start(argv = [])
    rails_console.stubs(:arguments => argv)
    @output = output = capture(:stdout) { rails_console.start }
  end

  def app
    @app ||= begin
      config = mock("config", :console => FakeConsole)
      app = mock("app", :config => config)
      app.expects(:load_console)
      app
    end
  end
end