<commit_before>require File.join(File.dirname(__FILE__), 'test_helper')
require 'rubigen'

# Some generator-related assertions:
#   assert_generated_file(name, &block) # block passed the file contents
#   assert_directory_exists(name)
#   assert_generated_class(name, &block)
#   assert_generated_module(name, &block)
#   assert_generated_test_for(name, &block)
#
# The assert_generated_(class|module|test_for) &block is passed the 
# body of the class/module within the file
#
#   assert_has_method(body, *methods) # check that the body has a 
#   list of methods (methods with parentheses not supported yet)
#
# Other helper methods are:
#   app_root_files - put this in teardown to show files generated 
#   by the test method (e.g. p app_root_files)
#
#   bare_setup - place this in setup method to create the app_root 
#   folder for each test
#
#   bare_teardown - place this in teardown method to destroy the 
#   tmp_root folder after each test

module Sprout::GeneratorTestHelper

  attr_accessor :tmp_root
  attr_accessor :app_root

  def setup
    super
    @tmp_root = File.join 'test', 'fixtures', 'generators', 'tmp'
    @app_root = File.join tmp_root, 'myproject'
    bare_setup
  end

  def teardown
    super
    bare_teardown
  end

  protected

  def app_sources
    source = 'app_generators'
    [RubiGen::PathSource.new(:test, source)]
  end

  # Runs the create command (like the command line does)
  def run_generator(name, params, sources, options = {})
    # TODO: Create/Configure to use tmp_dir
    generator = build_generator(name, params, sources, options)
    silence_generator do
      generator.command(:create).invoke!
    end
    generator
  end

  # Instatiates the Generator
  def build_generator(name, params, sources, options)
    @stdout ||= StringIO.new
    options.merge!(:collision => :force)  # so no questions are prompted
    options.merge!(:stdout => @stdout)  # so stdout is piped to a StringIO
    if sources.is_a?(Symbol)
      if sources == :app
        RubiGen::Base.use_application_sources!
      else
        RubiGen::Base.use_component_sources!
      end
    else
      RubiGen::Base.reset_sources
      RubiGen::Base.prepend_sources(*sources) unless sources.blank?
    end<commit_msg>Cleaned up app_sources in generator test helper<commit_after>require File.join(File.dirname(__FILE__), 'test_helper')
require 'rubigen'

# Some generator-related assertions:
#   assert_generated_file(name, &block) # block passed the file contents
#   assert_directory_exists(name)
#   assert_generated_class(name, &block)
#   assert_generated_module(name, &block)
#   assert_generated_test_for(name, &block)
#
# The assert_generated_(class|module|test_for) &block is passed the 
# body of the class/module within the file
#
#   assert_has_method(body, *methods) # check that the body has a 
#   list of methods (methods with parentheses not supported yet)
#
# Other helper methods are:
#   app_root_files - put this in teardown to show files generated 
#   by the test method (e.g. p app_root_files)
#
#   bare_setup - place this in setup method to create the app_root 
#   folder for each test
#
#   bare_teardown - place this in teardown method to destroy the 
#   tmp_root folder after each test

module Sprout::GeneratorTestHelper

  attr_accessor :tmp_root
  attr_accessor :app_root

  def setup
    super
    @tmp_root = File.join 'test', 'fixtures', 'generators', 'tmp'
    @app_root = File.join tmp_root, 'myproject'
    bare_setup
  end

  def teardown
    super
    bare_teardown
  end

  protected

  def app_sources
    [RubiGen::PathSource.new(:test, 'app_generators')]
  end

  # Runs the create command (like the command line does)
  def run_generator(name, params, sources, options = {})
    # TODO: Create/Configure to use tmp_dir
    generator = build_generator(name, params, sources, options)
    silence_generator do
      generator.command(:create).invoke!
    end
    generator
  end

  # Instatiates the Generator
  def build_generator(name, params, sources, options)
    @stdout ||= StringIO.new
    options.merge!(:collision => :force)  # so no questions are prompted
    options.merge!(:stdout => @stdout)  # so stdout is piped to a StringIO
    if sources.is_a?(Symbol)
      if sources == :app
        RubiGen::Base.use_application_sources!
      else
        RubiGen::Base.use_component_sources!
      end
    else
      RubiGen::Base.reset_sources
      RubiGen::Base.prepend_sources(*sources) unless sources.blank?
    end
{'fraction_non_alphanumeric': 0.08659839715048975, 'fraction_numerical': 0.0, 'mean_word_length': 3.800213675213675, 'pattern_counts': {'":': 0, '<': 3, '<?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': '3379512', 'n_tokens_mistral': 1411, 'n_tokens_neox': 1392, 'n_words': 413}