test_runner_test.rb 8.5 KB
Newer Older
1
require 'isolation/abstract_unit'
2
require 'active_support/core_ext/string/strip'
3 4 5 6 7 8 9

module ApplicationTests
  class TestRunnerTest < ActiveSupport::TestCase
    include ActiveSupport::Testing::Isolation

    def setup
      build_app
10
      ENV['RAILS_ENV'] = nil
11 12 13 14 15 16 17
      create_schema
    end

    def teardown
      teardown_app
    end

18 19 20 21 22 23 24 25 26 27 28
    def test_run_in_test_environment
      app_file 'test/unit/env_test.rb', <<-RUBY
        require 'test_helper'

        class EnvTest < ActiveSupport::TestCase
          def test_env
            puts "Current Environment: \#{Rails.env}"
          end
        end
      RUBY

29
      assert_match "Current Environment: test", run_test_command('test/unit/env_test.rb')
30 31
    end

32 33
    def test_run_single_file
      create_test_file :models, 'foo'
34
      create_test_file :models, 'bar'
35
      assert_match "1 tests, 1 assertions, 0 failures", run_test_command("test/models/foo_test.rb")
36 37 38 39 40
    end

    def test_run_multiple_files
      create_test_file :models,  'foo'
      create_test_file :models,  'bar'
41
      assert_match "2 tests, 2 assertions, 0 failures", run_test_command("test/models/foo_test.rb test/models/bar_test.rb")
42 43 44 45 46 47 48 49 50 51
    end

    def test_run_file_with_syntax_error
      app_file 'test/models/error_test.rb', <<-RUBY
        require 'test_helper'
        def; end
      RUBY

      error_stream = Tempfile.new('error')
      redirect_stderr(error_stream) { run_test_command('test/models/error_test.rb') }
52
      assert_match "syntax error", error_stream.read
53 54 55 56 57 58
    end

    def test_run_models
      create_test_file :models, 'foo'
      create_test_file :models, 'bar'
      create_test_file :controllers, 'foobar_controller'
59
      run_test_models_command.tap do |output|
60 61 62
        assert_match "FooTest", output
        assert_match "BarTest", output
        assert_match "2 tests, 2 assertions, 0 failures", output
63 64 65 66 67 68 69
      end
    end

    def test_run_helpers
      create_test_file :helpers, 'foo_helper'
      create_test_file :helpers, 'bar_helper'
      create_test_file :controllers, 'foobar_controller'
70
      run_test_helpers_command.tap do |output|
71 72 73
        assert_match "FooHelperTest", output
        assert_match "BarHelperTest", output
        assert_match "2 tests, 2 assertions, 0 failures", output
74 75 76 77 78 79 80 81
      end
    end

    def test_run_units
      create_test_file :models, 'foo'
      create_test_file :helpers, 'bar_helper'
      create_test_file :unit, 'baz_unit'
      create_test_file :controllers, 'foobar_controller'
82
      run_test_units_command.tap do |output|
83 84 85 86
        assert_match "FooTest", output
        assert_match "BarHelperTest", output
        assert_match "BazUnitTest", output
        assert_match "3 tests, 3 assertions, 0 failures", output
87 88 89 90 91 92 93
      end
    end

    def test_run_controllers
      create_test_file :controllers, 'foo_controller'
      create_test_file :controllers, 'bar_controller'
      create_test_file :models, 'foo'
94
      run_test_controllers_command.tap do |output|
95 96 97
        assert_match "FooControllerTest", output
        assert_match "BarControllerTest", output
        assert_match "2 tests, 2 assertions, 0 failures", output
98 99 100 101 102 103 104
      end
    end

    def test_run_mailers
      create_test_file :mailers, 'foo_mailer'
      create_test_file :mailers, 'bar_mailer'
      create_test_file :models, 'foo'
105
      run_test_mailers_command.tap do |output|
106 107 108
        assert_match "FooMailerTest", output
        assert_match "BarMailerTest", output
        assert_match "2 tests, 2 assertions, 0 failures", output
109 110 111 112 113 114 115 116
      end
    end

    def test_run_functionals
      create_test_file :mailers, 'foo_mailer'
      create_test_file :controllers, 'bar_controller'
      create_test_file :functional, 'baz_functional'
      create_test_file :models, 'foo'
117
      run_test_functionals_command.tap do |output|
118 119 120 121
        assert_match "FooMailerTest", output
        assert_match "BarControllerTest", output
        assert_match "BazFunctionalTest", output
        assert_match "3 tests, 3 assertions, 0 failures", output
122 123 124 125 126 127
      end
    end

    def test_run_integration
      create_test_file :integration, 'foo_integration'
      create_test_file :models, 'foo'
128
      run_test_integration_command.tap do |output|
129 130
        assert_match "FooIntegration", output
        assert_match "1 tests, 1 assertions, 0 failures", output
131 132 133
      end
    end

134 135 136
    def test_run_all_suites
      suites = [:models, :helpers, :unit, :controllers, :mailers, :functional, :integration]
      suites.each { |suite| create_test_file suite, "foo_#{suite}" }
137
      run_test_command('') .tap do |output|
138 139
        suites.each { |suite| assert_match "Foo#{suite.to_s.camelize}Test", output }
        assert_match "7 tests, 7 assertions, 0 failures", output
140 141 142
      end
    end

143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
    def test_run_named_test
      app_file 'test/unit/chu_2_koi_test.rb', <<-RUBY
        require 'test_helper'

        class Chu2KoiTest < ActiveSupport::TestCase
          def test_rikka
            puts 'Rikka'
          end

          def test_sanae
            puts 'Sanae'
          end
        end
      RUBY

158
      run_test_command('test/unit/chu_2_koi_test.rb TESTOPTS="-n test_rikka"').tap do |output|
159 160
        assert_match "Rikka", output
        assert_no_match "Sanae", output
161 162 163
      end
    end

164 165
    def test_load_fixtures_when_running_test_suites
      create_model_with_fixture
166
      suites = [:models, :helpers, [:units, :unit], :controllers, :mailers,
167 168
        [:functionals, :functional], :integration]

169 170
      suites.each do |suite, directory|
        directory ||= suite
171
        create_fixture_test directory
172
        assert_match "3 users", run_task(["test:#{suite}"])
173 174 175 176
        Dir.chdir(app_path) { FileUtils.rm_f "test/#{directory}" }
      end
    end

177 178 179 180 181 182 183 184 185 186 187
    def test_run_different_environment_using_env_var
      app_file 'test/unit/env_test.rb', <<-RUBY
        require 'test_helper'

        class EnvTest < ActiveSupport::TestCase
          def test_env
            puts Rails.env
          end
        end
      RUBY

188
      ENV['RAILS_ENV'] = 'development'
189
      assert_match "development", run_test_command('test/unit/env_test.rb')
190 191 192
    end

    def test_run_different_environment_using_e_tag
193
      env = "development"
194 195 196 197 198 199 200 201 202 203
      app_file 'test/unit/env_test.rb', <<-RUBY
        require 'test_helper'

        class EnvTest < ActiveSupport::TestCase
          def test_env
            puts Rails.env
          end
        end
      RUBY

204
      assert_match env, run_test_command("test/unit/env_test.rb RAILS_ENV=#{env}")
205 206 207 208
    end

    def test_generated_scaffold_works_with_rails_test
      create_scaffold
209
      assert_match "0 failures, 0 errors, 0 skips", run_test_command('')
210 211
    end

212
    private
213 214 215 216
      def run_task(tasks)
        Dir.chdir(app_path) { `bundle exec rake #{tasks.join ' '}` }
      end

217
      def run_test_command(arguments = 'test/unit/test_test.rb')
218 219 220 221 222 223
        run_task ['test', arguments]
      end
      %w{ mailers models helpers units controllers functionals integration }.each do |type|
        define_method("run_test_#{type}_command") do
          run_task ["test:#{type}"]
        end
224 225
      end

226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
      def create_model_with_fixture
        script 'generate model user name:string'

        app_file 'test/fixtures/users.yml', <<-YAML.strip_heredoc
          vampire:
            id: 1
            name: Koyomi Araragi
          crab:
            id: 2
            name: Senjougahara Hitagi
          cat:
            id: 3
            name: Tsubasa Hanekawa
        YAML

241
        run_migration
242 243 244 245 246 247 248 249 250 251 252 253 254 255
      end

      def create_fixture_test(path = :unit, name = 'test')
        app_file "test/#{path}/#{name}_test.rb", <<-RUBY
          require 'test_helper'

          class #{name.camelize}Test < ActiveSupport::TestCase
            def test_fixture
              puts "\#{User.count} users (\#{__FILE__})"
            end
          end
        RUBY
      end

256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
      def create_schema
        app_file 'db/schema.rb', ''
      end

      def redirect_stderr(target_stream)
        previous_stderr = STDERR.dup
        $stderr.reopen(target_stream)
        yield
        target_stream.rewind
      ensure
        $stderr = previous_stderr
      end

      def create_test_file(path = :unit, name = 'test')
        app_file "test/#{path}/#{name}_test.rb", <<-RUBY
          require 'test_helper'

          class #{name.camelize}Test < ActiveSupport::TestCase
            def test_truth
              puts "#{name.camelize}Test"
              assert true
            end
          end
        RUBY
      end
281 282 283 284 285 286 287 288 289 290

      def create_scaffold
        script 'generate scaffold user name:string'
        Dir.chdir(app_path) { File.exist?('app/models/user.rb') }
        run_migration
      end

      def run_migration
        Dir.chdir(app_path) { `bundle exec rake db:migrate` }
      end
291 292
  end
end