generators_test.rb 3.9 KB
Newer Older
1 2 3
require "isolation/abstract_unit"

module ApplicationTests
4
  class GeneratorsTest < ActiveSupport::TestCase
5 6 7 8 9 10 11
    include ActiveSupport::Testing::Isolation

    def setup
      build_app
      boot_rails
    end

12 13 14 15
    def teardown
      teardown_app
    end

C
Carlhuda 已提交
16 17 18 19 20
    def app_const
      @app_const ||= Class.new(Rails::Application)
    end

    def with_config
21
      require "rails/all"
22
      require "rails/generators"
C
Carlhuda 已提交
23 24 25
      yield app_const.config
    end

C
Carlhuda 已提交
26 27 28 29 30 31
    def with_bare_config
      require "rails"
      require "rails/generators"
      yield app_const.config
    end

32
    test "allow running plugin new generator inside Rails app directory" do
J
José Valim 已提交
33 34
      FileUtils.cd(rails_root){ `ruby script/rails plugin new vendor/plugins/bukkits` }
      assert File.exist?(File.join(rails_root, "vendor/plugins/bukkits/test/dummy/config/application.rb"))
35 36
    end

37
    test "generators default values" do
C
Carlhuda 已提交
38
      with_bare_config do |c|
39 40 41
        assert_equal(true, c.generators.colorize_logging)
        assert_equal({}, c.generators.aliases)
        assert_equal({}, c.generators.options)
J
José Valim 已提交
42
        assert_equal({}, c.generators.fallbacks)
43 44 45 46
      end
    end

    test "generators set rails options" do
47
      with_bare_config do |c|
48
        c.generators.orm            = :data_mapper
49
        c.generators.test_framework = :rspec
50
        c.generators.helper         = false
51
        expected = { :rails => { :orm => :data_mapper, :test_framework => :rspec, :helper => false } }
52 53 54 55 56
        assert_equal(expected, c.generators.options)
      end
    end

    test "generators set rails aliases" do
C
Carlhuda 已提交
57
      with_config do |c|
58 59 60 61 62 63
        c.generators.aliases = { :rails => { :test_framework => "-w" } }
        expected = { :rails => { :test_framework => "-w" } }
        assert_equal expected, c.generators.aliases
      end
    end

64
    test "generators aliases, options, templates and fallbacks on initialization" do
65 66
      add_to_config <<-RUBY
        config.generators.rails :aliases => { :test_framework => "-w" }
67
        config.generators.orm :data_mapper
68
        config.generators.test_framework :rspec
J
José Valim 已提交
69
        config.generators.fallbacks[:shoulda] = :test_unit
70
        config.generators.templates << "some/where"
71 72
      RUBY

73
      # Initialize the application
74
      require "#{app_path}/config/environment"
J
José Valim 已提交
75
      Rails.application.load_generators
76 77 78

      assert_equal :rspec, Rails::Generators.options[:rails][:test_framework]
      assert_equal "-w", Rails::Generators.aliases[:rails][:test_framework]
79
      assert_equal Hash[:shoulda => :test_unit], Rails::Generators.fallbacks
80
      assert_equal ["some/where"], Rails::Generators.templates_path
81 82 83
    end

    test "generators no color on initialization" do
84 85 86 87
      add_to_config <<-RUBY
        config.generators.colorize_logging = false
      RUBY

88
      # Initialize the application
89
      require "#{app_path}/config/environment"
J
José Valim 已提交
90
      Rails.application.load_generators
91 92 93 94 95

      assert_equal Thor::Base.shell, Thor::Shell::Basic
    end

    test "generators with hashes for options and aliases" do
C
Carlhuda 已提交
96
      with_bare_config do |c|
97
        c.generators do |g|
98
          g.orm    :data_mapper, :migration => false
99 100 101 102 103
          g.plugin :aliases => { :generator => "-g" },
                   :generator => true
        end

        expected = {
104
          :rails => { :orm => :data_mapper },
105
          :plugin => { :generator => true },
106
          :data_mapper => { :migration => false }
107 108 109 110 111 112
        }

        assert_equal expected, c.generators.options
        assert_equal({ :plugin => { :generator => "-g" } }, c.generators.aliases)
      end
    end
113 114 115 116

    test "generators with string and hash for options should generate symbol keys" do
      with_bare_config do |c|
        c.generators do |g|
117
          g.orm    'data_mapper', :migration => false
118 119 120
        end

        expected = {
121 122
          :rails => { :orm => :data_mapper },
          :data_mapper => { :migration => false }
123 124 125 126 127
        }

        assert_equal expected, c.generators.options
      end
    end
128
  end
129
end