<commit_before>require 'fileutils'

require 'test/unit'
require 'rubygems'

# TODO: Remove setting this magic constant
RAILS_FRAMEWORK_ROOT = File.expand_path("#{File.dirname(__FILE__)}/../../..")

# These files do not require any others and are needed
# to run the tests
require "#{RAILS_FRAMEWORK_ROOT}/activesupport/lib/active_support/testing/isolation"
require "#{RAILS_FRAMEWORK_ROOT}/activesupport/lib/active_support/testing/declarative"
require "#{RAILS_FRAMEWORK_ROOT}/activesupport/lib/active_support/core_ext/kernel/reporting"
require "#{RAILS_FRAMEWORK_ROOT}/railties/lib/rails/generators/test_case"

module EngineTests
  class GeneratorTest < Rails::Generators::TestCase
    include ActiveSupport::Testing::Isolation

    TMP_PATH = File.expand_path(File.join(File.dirname(__FILE__), *%w[.. .. tmp]))
    self.destination_root = File.join(TMP_PATH, "foo_bar")

    def tmp_path(*args)
      File.join(TMP_PATH, *args)
    end

    def engine_path
      tmp_path('foo_bar')
    end

    def bundle_exec(cmd)
      `bundle exec rails #{cmd}`
    end

    def rails(cmd)
      environment = File.expand_path('../../../../load_paths', __FILE__)
      if File.exist?("#{environment}.rb")
        require_environment = "-r #{environment}"
      end
      `#{Gem.ruby} #{require_environment} #{RAILS_FRAMEWORK_ROOT}/bin/rails #{cmd}`
    end

    def build_engine
      FileUtils.mkdir_p(engine_path)
      FileUtils.rm_r(engine_path)

      rails("plugin new #{engine_path} --full --mountable")

      Dir.chdir(engine_path) do
        File.open("Gemfile", "w") do |f|
          f.write <<-GEMFILE.gsub(/^ {12}/, '')
            source "http://rubygems.org"

            gem 'rails', :path => '#{RAILS_FRAMEWORK_ROOT}'
            gem 'sqlite3'

            if RUBY_VERSION < '1.9'
              gem "ruby-debug", ">= 0.10.3"
            end
          GEMFILE
        end
      end
    end

    def setup
      build_engine
    end

    def test_controllers_are_correctly_namespaced
      Dir.chdir(engine_path) do
        bundle_exec("g controller topics")
        assert_file "app/controllers/foo_bar/topics_controller.rb", /FooBar::TopicsController/
      end
    end

    def test_models_are_correctly_namespaced
      Dir.chdir(engine_path) do
        bundle_exec("g model topic")
        assert_file "app/models/foo_bar/topic.rb", /FooBar::Topic/
      end
    end
  end
end<commit_msg>Add test for helper generator<commit_after>require 'fileutils'

require 'test/unit'
require 'rubygems'

# TODO: Remove setting this magic constant
RAILS_FRAMEWORK_ROOT = File.expand_path("#{File.dirname(__FILE__)}/../../..")

# These files do not require any others and are needed
# to run the tests
require "#{RAILS_FRAMEWORK_ROOT}/activesupport/lib/active_support/testing/isolation"
require "#{RAILS_FRAMEWORK_ROOT}/activesupport/lib/active_support/testing/declarative"
require "#{RAILS_FRAMEWORK_ROOT}/activesupport/lib/active_support/core_ext/kernel/reporting"
require "#{RAILS_FRAMEWORK_ROOT}/railties/lib/rails/generators/test_case"

module EngineTests
  class GeneratorTest < Rails::Generators::TestCase
    include ActiveSupport::Testing::Isolation

    TMP_PATH = File.expand_path(File.join(File.dirname(__FILE__), *%w[.. .. tmp]))
    self.destination_root = File.join(TMP_PATH, "foo_bar")

    def tmp_path(*args)
      File.join(TMP_PATH, *args)
    end

    def engine_path
      tmp_path('foo_bar')
    end

    def bundled_rails(cmd)
      `bundle exec rails #{cmd}`
    end

    def rails(cmd)
      environment = File.expand_path('../../../../load_paths', __FILE__)
      if File.exist?("#{environment}.rb")
        require_environment = "-r #{environment}"
      end
      `#{Gem.ruby} #{require_environment} #{RAILS_FRAMEWORK_ROOT}/bin/rails #{cmd}`
    end

    def build_engine
      FileUtils.mkdir_p(engine_path)
      FileUtils.rm_r(engine_path)

      rails("plugin new #{engine_path} --full --mountable")

      Dir.chdir(engine_path) do
        File.open("Gemfile", "w") do |f|
          f.write <<-GEMFILE.gsub(/^ {12}/, '')
            source "http://rubygems.org"

            gem 'rails', :path => '#{RAILS_FRAMEWORK_ROOT}'
            gem 'sqlite3'

            if RUBY_VERSION < '1.9'
              gem "ruby-debug", ">= 0.10.3"
            end
          GEMFILE
        end
      end
    end

    def setup
      build_engine
    end

    def test_controllers_are_correctly_namespaced
      Dir.chdir(engine_path) do
        bundled_rails("g controller topics")
        assert_file "app/controllers/foo_bar/topics_controller.rb", /FooBar::TopicsController/
      end
    end

    def test_models_are_correctly_namespaced
      Dir.chdir(engine_path) do
        bundled_rails("g model topic")
        assert_file "app/models/foo_bar/topic.rb", /FooBar::Topic/
      end
    end

    def test_helpers_are_correctly_namespaced
      Dir.chdir(engine_path) do
        bundled_rails("g helper topics")
        assert_file "app/helpers/foo_bar/topics_helper.rb", /FooBar::TopicsHelper/
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.12264338162333796, 'fraction_numerical': 0.0035721373288350863, 'mean_word_length': 2.9529411764705884, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4138669', 'n_tokens_mistral': 1697, 'n_tokens_neox': 1642, 'n_words': 313}