generators_test.rb 4.4 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
33
      FileUtils.cd(rails_root){ `ruby bin/rails plugin new vendor/plugins/bukkits` }
J
José Valim 已提交
34
      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
        c.generators.aliases = { rails: { test_framework: "-w" } }
        expected = { rails: { test_framework: "-w" } }
60 61 62 63
        assert_equal expected, c.generators.aliases
      end
    end

64
    test "generators aliases, options, templates and fallbacks on initialization" do
65
      add_to_config <<-RUBY
66
        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 99 100
          g.orm    :data_mapper, migration: false
          g.plugin aliases: { generator: "-g" },
                   generator: true
101 102 103
        end

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

        assert_equal expected, c.generators.options
110
        assert_equal({ plugin: { generator: "-g" } }, c.generators.aliases)
111 112
      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

129
    test "api only generators hide assets, helper, js and css namespaces and set api option" do
130 131 132 133 134 135 136 137 138 139 140 141
      add_to_config <<-RUBY
        config.generators.api_only = true
      RUBY

      # Initialize the application
      require "#{app_path}/config/environment"
      Rails.application.load_generators

      assert Rails::Generators.hidden_namespaces.include?("assets")
      assert Rails::Generators.hidden_namespaces.include?("helper")
      assert Rails::Generators.hidden_namespaces.include?("js")
      assert Rails::Generators.hidden_namespaces.include?("css")
142
      assert Rails::Generators.options[:rails][:api]
143
    end
144
  end
145
end