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

35
    test "don't allow running plugin_new generator as a generator" do
P
Piotr Sarnacki 已提交
36 37 38 39
      FileUtils.cd rails_root do
        output =  `ruby 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 41
    end

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

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

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

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

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

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

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

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

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

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

    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
135
  end
136
end