generators_test.rb 4.2 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 29 30 31 32 33
    test "allow running plugin new generator inside Rails app directory" do
      FileUtils.cd rails_root
      `./script/rails plugin new vendor/plugins/bukkits`
      assert File.exist?(File.join(rails_root, "vendor/plugins/bukkits/test/dummy/config/application.rb"))
    end

34 35 36 37 38 39
    test "don't allow running plugin_new generator as a generator" do
      FileUtils.cd rails_root
      output =  `./script/rails g plugin_new vendor/plugins/bukkits`
      assert_match /This generator should not be used directly as a generator. You should use `rails plugin new` command instead/, output
    end

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

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

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

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

76
      # Initialize the application
77
      require "#{app_path}/config/environment"
78
      require "rails/generators"
79
      Rails::Generators.configure!
80 81 82

      assert_equal :rspec, Rails::Generators.options[:rails][:test_framework]
      assert_equal "-w", Rails::Generators.aliases[:rails][:test_framework]
83
      assert_equal Hash[:shoulda => :test_unit], Rails::Generators.fallbacks
84
      assert_equal ["some/where"], Rails::Generators.templates_path
85 86 87
    end

    test "generators no color on initialization" do
88 89 90 91
      add_to_config <<-RUBY
        config.generators.colorize_logging = false
      RUBY

92
      # Initialize the application
93 94
      require "#{app_path}/config/environment"
      require "rails/generators"
95
      Rails::Generators.configure!
96 97 98 99 100

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

    test "generators with hashes for options and aliases" do
C
Carlhuda 已提交
101
      with_bare_config do |c|
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
        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
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132

    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
133
  end
134
end