aboutsummaryrefslogtreecommitdiffstats
path: root/railties/test/commands/routes_test.rb
blob: 77ed2bda61a30d4e2222e6b085575e339ffbac9e (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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# frozen_string_literal: true

require "isolation/abstract_unit"
require "rails/command"
require "rails/commands/routes/routes_command"
require "io/console/size"

class Rails::Command::RoutesTest < ActiveSupport::TestCase
  setup :build_app
  teardown :teardown_app

  test "singular resource output in rails routes" do
    app_file "config/routes.rb", <<-RUBY
      Rails.application.routes.draw do
        resource :post
      end
    RUBY

    expected_output = ["   Prefix Verb   URI Pattern          Controller#Action",
                       " new_post GET    /post/new(.:format)  posts#new",
                       "edit_post GET    /post/edit(.:format) posts#edit",
                       "     post GET    /post(.:format)      posts#show",
                       "          PATCH  /post(.:format)      posts#update",
                       "          PUT    /post(.:format)      posts#update",
                       "          DELETE /post(.:format)      posts#destroy",
                       "          POST   /post(.:format)      posts#create\n"].join("\n")

    output = run_routes_command(["-c", "PostController"])
    assert_equal expected_output, output
  end

  test "rails routes with global search key" do
    app_file "config/routes.rb", <<-RUBY
      Rails.application.routes.draw do
        get '/cart', to: 'cart#show'
        post '/cart', to: 'cart#create'
        get '/basketballs', to: 'basketball#index'
      end
    RUBY

    output = run_routes_command(["-g", "show"])
    assert_equal <<~MESSAGE, output
                         Prefix Verb URI Pattern                                                                              Controller#Action
                           cart GET  /cart(.:format)                                                                          cart#show
             rails_service_blob GET  /rails/active_storage/blobs/:signed_id/*filename(.:format)                               active_storage/blobs#show
      rails_blob_representation GET  /rails/active_storage/representations/:signed_blob_id/:variation_key/*filename(.:format) active_storage/representations#show
             rails_disk_service GET  /rails/active_storage/disk/:encoded_key/*filename(.:format)                              active_storage/disk#show
    MESSAGE

    output = run_routes_command(["-g", "POST"])
    assert_equal <<~MESSAGE, output
                    Prefix Verb URI Pattern                                    Controller#Action
                           POST /cart(.:format)                                cart#create
      rails_direct_uploads POST /rails/active_storage/direct_uploads(.:format) active_storage/direct_uploads#create
    MESSAGE

    output = run_routes_command(["-g", "basketballs"])
    assert_equal "     Prefix Verb URI Pattern            Controller#Action\n" \
                 "basketballs GET  /basketballs(.:format) basketball#index\n", output
  end

  test "rails routes with controller search key" do
    app_file "config/routes.rb", <<-RUBY
      Rails.application.routes.draw do
        get '/cart', to: 'cart#show'
        get '/basketball', to: 'basketball#index'
      end
    RUBY

    output = run_routes_command(["-c", "cart"])
    assert_equal "Prefix Verb URI Pattern     Controller#Action\n  cart GET  /cart(.:format) cart#show\n", output

    output = run_routes_command(["-c", "Cart"])
    assert_equal "Prefix Verb URI Pattern     Controller#Action\n  cart GET  /cart(.:format) cart#show\n", output

    output = run_routes_command(["-c", "CartController"])
    assert_equal "Prefix Verb URI Pattern     Controller#Action\n  cart GET  /cart(.:format) cart#show\n", output
  end

  test "rails routes with namespaced controller search key" do
    app_file "config/routes.rb", <<-RUBY
      Rails.application.routes.draw do
        namespace :admin do
          resource :post
        end
      end
    RUBY

    expected_output = ["         Prefix Verb   URI Pattern                Controller#Action",
                       " new_admin_post GET    /admin/post/new(.:format)  admin/posts#new",
                       "edit_admin_post GET    /admin/post/edit(.:format) admin/posts#edit",
                       "     admin_post GET    /admin/post(.:format)      admin/posts#show",
                       "                PATCH  /admin/post(.:format)      admin/posts#update",
                       "                PUT    /admin/post(.:format)      admin/posts#update",
                       "                DELETE /admin/post(.:format)      admin/posts#destroy",
                       "                POST   /admin/post(.:format)      admin/posts#create\n"].join("\n")

    output = run_routes_command(["-c", "Admin::PostController"])
    assert_equal expected_output, output

    output = run_routes_command(["-c", "PostController"])
    assert_equal expected_output, output
  end

  test "rails routes displays message when no routes are defined" do
    app_file "config/routes.rb", <<-RUBY
      Rails.application.routes.draw do
      end
    RUBY

    assert_equal <<~MESSAGE, run_routes_command
                         Prefix Verb URI Pattern                                                                              Controller#Action
             rails_service_blob GET  /rails/active_storage/blobs/:signed_id/*filename(.:format)                               active_storage/blobs#show
      rails_blob_representation GET  /rails/active_storage/representations/:signed_blob_id/:variation_key/*filename(.:format) active_storage/representations#show
             rails_disk_service GET  /rails/active_storage/disk/:encoded_key/*filename(.:format)                              active_storage/disk#show
      update_rails_disk_service PUT  /rails/active_storage/disk/:encoded_token(.:format)                                      active_storage/disk#update
           rails_direct_uploads POST /rails/active_storage/direct_uploads(.:format)                                           active_storage/direct_uploads#create
    MESSAGE
  end

  test "rails routes with expanded option" do
    begin
      previous_console_winsize = IO.console.winsize
      IO.console.winsize = [0, 27]

      app_file "config/routes.rb", <<-RUBY
        Rails.application.routes.draw do
          get '/cart', to: 'cart#show'
        end
      RUBY

      output = run_routes_command(["--expanded"])

      assert_equal <<~MESSAGE, output
        --[ Route 1 ]--------------
        Prefix            | cart
        Verb              | GET
        URI               | /cart(.:format)
        Controller#Action | cart#show
        --[ Route 2 ]--------------
        Prefix            | rails_service_blob
        Verb              | GET
        URI               | /rails/active_storage/blobs/:signed_id/*filename(.:format)
        Controller#Action | active_storage/blobs#show
        --[ Route 3 ]--------------
        Prefix            | rails_blob_representation
        Verb              | GET
        URI               | /rails/active_storage/representations/:signed_blob_id/:variation_key/*filename(.:format)
        Controller#Action | active_storage/representations#show
        --[ Route 4 ]--------------
        Prefix            | rails_disk_service
        Verb              | GET
        URI               | /rails/active_storage/disk/:encoded_key/*filename(.:format)
        Controller#Action | active_storage/disk#show
        --[ Route 5 ]--------------
        Prefix            | update_rails_disk_service
        Verb              | PUT
        URI               | /rails/active_storage/disk/:encoded_token(.:format)
        Controller#Action | active_storage/disk#update
        --[ Route 6 ]--------------
        Prefix            | rails_direct_uploads
        Verb              | POST
        URI               | /rails/active_storage/direct_uploads(.:format)
        Controller#Action | active_storage/direct_uploads#create
      MESSAGE
    ensure
      IO.console.winsize = previous_console_winsize
    end
  end

  private
    def run_routes_command(args = [])
      rails "routes", args
    end
end