<commit_before># references:
#   http://robots.thoughtbot.com/test-rake-tasks-like-a-boss
#   http://www.e-tobi.net/blog/2008/10/04/

require "rake"

shared_context "rake" do
  let(:task_name) {
    if defined?(environment)
      env_name = environment.name
    elsif defined?(environments)
      env_name = environments[0].name
    else
      env_name = deployment_config.environments[0].name
    end

    "#{env_name}:#{self.class.top_level_description}"
  }

  let(:task) { Rake::Task[task_name] }
  
  subject { task }
    
  before(:each) do
    Rake::Task.clear

    if defined?(deployment_config)
      @deployment_config = deployment_config
    else
      if defined?(environment)
        @environments = [environment]
      elsif defined?(environments)
        @environments = environments
      end
      
      @deployment_config = if @environments.nil?
        build(:deployment_config)
      else
        build(:deployment_config, environments: @environments)
      end
    end
    
    Heroploy::Tasks::DeployTaskLib.new(@deployment_config) 

    Rake::Task.tasks.each do |task|
      if task.name != task_name
        task.stub(:execute)
      end
    end

    stub_shell
  end  
end<commit_msg>#85 refactored rake shared context<commit_after># references:
#   http://robots.thoughtbot.com/test-rake-tasks-like-a-boss
#   http://www.e-tobi.net/blog/2008/10/04/

require "rake"

shared_context "rake" do
  let(:task_name) {
    if defined?(environment)
      env_name = environment.name
    elsif defined?(environments)
      env_name = environments[0].name
    else
      env_name = deployment_config.environments[0].name
    end

    "#{env_name}:#{self.class.top_level_description}"
  }

  let(:task) { Rake::Task[task_name] }
  
  subject { task }
    
  before(:each) do
    Rake::Task.clear

    Heroploy::Tasks::DeployTaskLib.new(build_deployment_config) 

    Rake::Task.tasks.each do |task|
      if task.name != task_name
        task.stub(:execute)
      end
    end

    stub_shell
  end  
  
  def build_environments
    if defined?(environment)
      [environment]
    elsif defined?(environments)
      environments
    end
  end
  
  def build_deployment_config
    if defined?(deployment_config)
      deployment_config
    else
      environments = build_environments
      
      if environments.nil?
        build(:deployment_config)
      else
        build(:deployment_config, environments: environments)
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.10575364667747164, 'fraction_numerical': 0.008914100486223663, 'mean_word_length': 2.735249621785174, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '6596296', 'n_tokens_mistral': 814, 'n_tokens_neox': 807, 'n_words': 141}