generators_test.rb 3.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
require "isolation/abstract_unit"

module ApplicationTests
  class GeneratorsTest < Test::Unit::TestCase
    include ActiveSupport::Testing::Isolation

    def setup
      build_app
      boot_rails
    end

C
Carlhuda 已提交
12 13 14 15 16
    def app_const
      @app_const ||= Class.new(Rails::Application)
    end

    def with_config
17
      require "rails/all"
18
      require "rails/generators"
C
Carlhuda 已提交
19 20 21
      yield app_const.config
    end

C
Carlhuda 已提交
22 23 24 25 26 27
    def with_bare_config
      require "rails"
      require "rails/generators"
      yield app_const.config
    end

28
    test "generators default values" do
C
Carlhuda 已提交
29
      with_bare_config do |c|
30 31 32
        assert_equal(true, c.generators.colorize_logging)
        assert_equal({}, c.generators.aliases)
        assert_equal({}, c.generators.options)
J
José Valim 已提交
33
        assert_equal({}, c.generators.fallbacks)
34 35 36 37
      end
    end

    test "generators set rails options" do
38
      with_bare_config do |c|
39 40
        c.generators.orm            = :datamapper
        c.generators.test_framework = :rspec
41 42
        c.generators.helper         = false
        expected = { :rails => { :orm => :datamapper, :test_framework => :rspec, :helper => false } }
43 44 45 46 47
        assert_equal(expected, c.generators.options)
      end
    end

    test "generators set rails aliases" do
C
Carlhuda 已提交
48
      with_config do |c|
49 50 51 52 53 54
        c.generators.aliases = { :rails => { :test_framework => "-w" } }
        expected = { :rails => { :test_framework => "-w" } }
        assert_equal expected, c.generators.aliases
      end
    end

55
    test "generators aliases, options, templates and fallbacks on initialization" do
56 57 58 59
      add_to_config <<-RUBY
        config.generators.rails :aliases => { :test_framework => "-w" }
        config.generators.orm :datamapper
        config.generators.test_framework :rspec
J
José Valim 已提交
60
        config.generators.fallbacks[:shoulda] = :test_unit
61
        config.generators.templates << "some/where"
62 63
      RUBY

64
      # Initialize the application
65
      require "#{app_path}/config/environment"
66
      require "rails/generators"
67
      Rails::Generators.configure!
68 69 70

      assert_equal :rspec, Rails::Generators.options[:rails][:test_framework]
      assert_equal "-w", Rails::Generators.aliases[:rails][:test_framework]
71 72
      assert_equal Hash[:shoulda => :test_unit], Rails::Generators.fallbacks
      assert_equal ["#{app_path}/lib/templates", "some/where"], Rails::Generators.templates_path
73 74 75
    end

    test "generators no color on initialization" do
76 77 78 79
      add_to_config <<-RUBY
        config.generators.colorize_logging = false
      RUBY

80
      # Initialize the application
81 82
      require "#{app_path}/config/environment"
      require "rails/generators"
83
      Rails::Generators.configure!
84 85 86 87 88

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

    test "generators with hashes for options and aliases" do
C
Carlhuda 已提交
89
      with_bare_config do |c|
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
        c.generators do |g|
          g.orm    :datamapper, :migration => false
          g.plugin :aliases => { :generator => "-g" },
                   :generator => true
        end

        expected = {
          :rails => { :orm => :datamapper },
          :plugin => { :generator => true },
          :datamapper => { :migration => false }
        }

        assert_equal expected, c.generators.options
        assert_equal({ :plugin => { :generator => "-g" } }, c.generators.aliases)
      end
    end
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120

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

        expected = {
          :rails => { :orm => :datamapper },
          :datamapper => { :migration => false }
        }

        assert_equal expected, c.generators.options
      end
    end
121
  end
122
end