aboutsummaryrefslogtreecommitdiffstats
path: root/railties/test/railties/generators_test.rb
blob: 0abb2b7578b2586809c3d98373502dd462873a5d (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
RAILS_ISOLATED_ENGINE = true
require "isolation/abstract_unit"

require 'generators/generators_test_helper'
require "rails/generators/test_case"

module RailtiesTests
  class GeneratorTest < Rails::Generators::TestCase
    include ActiveSupport::Testing::Isolation

    def destination_root
      tmp_path 'foo_bar'
    end

    def tmp_path(*args)
      @tmp_path ||= File.realpath(Dir.mktmpdir)
      File.join(@tmp_path, *args)
    end

    def engine_path
      tmp_path('foo_bar')
    end

    def bundled_rails(cmd)
      `bundle exec rails #{cmd}`
    end

    def rails(cmd)
      environment = File.expand_path('../../../../load_paths', __FILE__)
      if File.exist?("#{environment}.rb")
        require_environment = "-r #{environment}"
      end
      `#{Gem.ruby} #{require_environment} #{RAILS_FRAMEWORK_ROOT}/railties/bin/rails #{cmd}`
    end

    def build_engine(is_mountable=false)
      FileUtils.rm_rf(engine_path)
      FileUtils.mkdir_p(engine_path)

      mountable = is_mountable ? "--mountable" : ""

      rails("plugin new #{engine_path} --full #{mountable}")

      Dir.chdir(engine_path) do
        File.open("Gemfile", "w") do |f|
          f.write <<-GEMFILE.gsub(/^ {12}/, '')
            source "http://rubygems.org"

            gem 'rails', path: '#{RAILS_FRAMEWORK_ROOT}'
            gem 'sqlite3'
          GEMFILE
        end
      end
    end

    def build_mountable_engine
      build_engine(true)
    end

    def test_controllers_are_correctly_namespaced_when_engine_is_mountable
      build_mountable_engine
      Dir.chdir(engine_path) do
        bundled_rails("g controller topics")
        assert_file "app/controllers/foo_bar/topics_controller.rb", /module FooBar\n  class TopicsController/
        assert_no_file "app/controllers/topics_controller.rb"
      end
    end

    def test_models_are_correctly_namespaced_when_engine_is_mountable
      build_mountable_engine
      Dir.chdir(engine_path) do
        bundled_rails("g model topic")
        assert_file "app/models/foo_bar/topic.rb", /module FooBar\n  class Topic/
        assert_no_file "app/models/topic.rb"
      end
    end

    def test_table_name_prefix_is_correctly_namespaced_when_engine_is_mountable
      build_mountable_engine
      Dir.chdir(engine_path) do
        bundled_rails("g model namespaced/topic")
        assert_file "app/models/foo_bar/namespaced.rb", /module FooBar\n  module Namespaced/ do |content|
          assert_class_method :table_name_prefix, content do |method_content|
            assert_match(/'foo_bar_namespaced_'/, method_content)
          end
        end
      end
    end

    def test_helpers_are_correctly_namespaced_when_engine_is_mountable
      build_mountable_engine
      Dir.chdir(engine_path) do
        bundled_rails("g helper topics")
        assert_file "app/helpers/foo_bar/topics_helper.rb", /module FooBar\n  module TopicsHelper/
        assert_no_file "app/helpers/topics_helper.rb"
      end
    end

    def test_controllers_are_not_namespaced_when_engine_is_not_mountable
      build_engine
      Dir.chdir(engine_path) do
        bundled_rails("g controller topics")
        assert_file "app/controllers/topics_controller.rb", /class TopicsController/
        assert_no_file "app/controllers/foo_bar/topics_controller.rb"
      end
    end

    def test_models_are_not_namespaced_when_engine_is_not_mountable
      build_engine
      Dir.chdir(engine_path) do
        bundled_rails("g model topic")
        assert_file "app/models/topic.rb", /class Topic/
        assert_no_file "app/models/foo_bar/topic.rb"
      end
    end

    def test_helpers_are_not_namespaced_when_engine_is_not_mountable
      build_engine
      Dir.chdir(engine_path) do
        bundled_rails("g helper topics")
        assert_file "app/helpers/topics_helper.rb", /module TopicsHelper/
        assert_no_file "app/helpers/foo_bar/topics_helper.rb"
      end
    end
  end
end