<commit_before>require File.expand_path('../../test_helper', __FILE__)
require 'shell_test/shell_methods'

class ShellMethodsTest < Test::Unit::TestCase
  include ShellTest::ShellMethods

  #
  # set_env test
  #

  def test_set_env_sets_the_env_and_returns_the_current_env
    current_env = {}
    begin
      ENV.each_pair do |key, value|
        current_env[key] = value
      end

      assert_equal nil, ENV['NEW_ENV_VAR']
      assert_equal nil, current_env['NEW_ENV_VAR']

      assert_equal current_env, set_env('NEW_ENV_VAR' => 'value')
      assert_equal 'value', ENV['NEW_ENV_VAR']
    ensure
      ENV.clear
      current_env.each_pair do |key, value|
        ENV[key] = value
      end
    end
  end

  #
  # with_env test
  #

  def test_with_env_sets_variables_for_duration_of_block
    assert_equal nil, ENV['UNSET_VARIABLE']
    ENV['SET_VARIABLE'] = 'set'

    was_in_block = false
    with_env 'UNSET_VARIABLE' => 'unset' do
      was_in_block = true
      assert_equal 'set', ENV['SET_VARIABLE']
      assert_equal 'unset', ENV['UNSET_VARIABLE']
    end

    assert_equal true, was_in_block
    assert_equal 'set', ENV['SET_VARIABLE']
    assert_equal nil, ENV['UNSET_VARIABLE']
    assert_equal false, ENV.has_key?('UNSET_VARIABLE')
  end

  def test_with_env_resets_variables_even_on_error
    assert_equal nil, ENV['UNSET_VARIABLE']

    was_in_block = false
    err = assert_raises(RuntimeError) do
      with_env 'UNSET_VARIABLE' => 'unset' do
        was_in_block = true
        assert_equal 'unset', ENV['UNSET_VARIABLE']
        raise "error"
        flunk "should not have reached here"
      end
    end

    assert_equal 'error', err.message
    assert_equal true, was_in_block
    assert_equal nil, ENV['UNSET_VARIABLE']
  end

  def test_with_env_replaces_env_if_specified
    ENV['SET_VARIABLE'] = 'set'

    was_in_block = false
    with_env({}, true) do
      was_in_block = true
      assert_equal nil, ENV['SET_VARIABLE']
      assert_equal false, ENV.has_key?('SET_VARIABLE')
    end

    assert_equal true, was_in_block
    assert_equal 'set', ENV['SET_VARIABLE']
  end

  def test_with_env_returns_block_result
    assert_equal "result", with_env {"result"}
  end

  def test_with_env_allows_nil_env
    was_in_block = false
    with_env(nil) do
      was_in_block = true
    end

    assert_equal true, was_in_block
  end

  #
  # verbose test
  #

  def test_verbose_is_true_if_VERBOSE_is_truish
    with_env 'VERBOSE' => 'true' do
      assert_equal true, verbose?
    end

    with_env 'VERBOSE' => 'TruE' do
      assert_equal true, verbose?
    end

    with_env 'VERBOSE' => 'false' do
      assert_equal false, verbose?
    end

    with_env 'VERBOSE' => nil do
      assert_equal false, verbose?
    end
  end

  #
  # assert_script test
  #

  def test_assert_script_correctly_matches_no_output
    assert_script %Q{
printf ''
}

    assert_script %Q{printf ''}
  end

  def test_assert_script_correctly_matches_whitespace_output
    assert_script %Q{
printf "\\t\\n  "
\t
  }
    assert_script %Q{
echo

}
    assert_script %Q{echo

}
  end

  def test_assert_script_strips_indents
    assert_script %Q{
    echo goodnight
    goodnight
    }

    assert_script %Q{ \t   \r
    echo goodnight
    goodnight
    }

    assert_script %Q{
    printf "\\t\\n  "
    \t
      }

    assert_script %Q{
    echo

    }

    assert_script %Q{echo

}
  end

  def test_assert_script_for_multiple_commands
    assert_script %{
      $ echo one
      one
      $ echo two
      two
    }
  end

  def test_assert_script_for_multiline_commands
    assert_script %{
      $ for n in one two; do
      >   echo $n
      > done
      one
      two
    }
  end

  def test_assert_script_fails_on_mismatch
    assert_raises(TestUnitErrorClass) { assert_script %Q{printf ""\nflunk} }
    assert_raises(TestUnitErrorClass) { assert_script %Q{echo pass\nflunk} }
  end

  #
  # _assert_script test
  #

  def test__assert_script_does_not_strip_indents
    _assert_script %Q{
    printf "    \\t\\n      "
    \t
      }, :outdent => false
  end

  #
  # assert_script_match test
  #

  def test_assert_script_match_matches_regexps_to_output
    assert_script_match %Q{
      $ echo "goodnight
      > moon"
      goodnight
      m:.o+.:n
    }<commit_msg>added failing test for scripts with context<commit_after>require File.expand_path('../../test_helper', __FILE__)
require 'shell_test/shell_methods'

class ShellMethodsTest < Test::Unit::TestCase
  include ShellTest::ShellMethods

  #
  # set_env test
  #

  def test_set_env_sets_the_env_and_returns_the_current_env
    current_env = {}
    begin
      ENV.each_pair do |key, value|
        current_env[key] = value
      end

      assert_equal nil, ENV['NEW_ENV_VAR']
      assert_equal nil, current_env['NEW_ENV_VAR']

      assert_equal current_env, set_env('NEW_ENV_VAR' => 'value')
      assert_equal 'value', ENV['NEW_ENV_VAR']
    ensure
      ENV.clear
      current_env.each_pair do |key, value|
        ENV[key] = value
      end
    end
  end

  #
  # with_env test
  #

  def test_with_env_sets_variables_for_duration_of_block
    assert_equal nil, ENV['UNSET_VARIABLE']
    ENV['SET_VARIABLE'] = 'set'

    was_in_block = false
    with_env 'UNSET_VARIABLE' => 'unset' do
      was_in_block = true
      assert_equal 'set', ENV['SET_VARIABLE']
      assert_equal 'unset', ENV['UNSET_VARIABLE']
    end

    assert_equal true, was_in_block
    assert_equal 'set', ENV['SET_VARIABLE']
    assert_equal nil, ENV['UNSET_VARIABLE']
    assert_equal false, ENV.has_key?('UNSET_VARIABLE')
  end

  def test_with_env_resets_variables_even_on_error
    assert_equal nil, ENV['UNSET_VARIABLE']

    was_in_block = false
    err = assert_raises(RuntimeError) do
      with_env 'UNSET_VARIABLE' => 'unset' do
        was_in_block = true
        assert_equal 'unset', ENV['UNSET_VARIABLE']
        raise "error"
        flunk "should not have reached here"
      end
    end

    assert_equal 'error', err.message
    assert_equal true, was_in_block
    assert_equal nil, ENV['UNSET_VARIABLE']
  end

  def test_with_env_replaces_env_if_specified
    ENV['SET_VARIABLE'] = 'set'

    was_in_block = false
    with_env({}, true) do
      was_in_block = true
      assert_equal nil, ENV['SET_VARIABLE']
      assert_equal false, ENV.has_key?('SET_VARIABLE')
    end

    assert_equal true, was_in_block
    assert_equal 'set', ENV['SET_VARIABLE']
  end

  def test_with_env_returns_block_result
    assert_equal "result", with_env {"result"}
  end

  def test_with_env_allows_nil_env
    was_in_block = false
    with_env(nil) do
      was_in_block = true
    end

    assert_equal true, was_in_block
  end

  #
  # verbose test
  #

  def test_verbose_is_true_if_VERBOSE_is_truish
    with_env 'VERBOSE' => 'true' do
      assert_equal true, verbose?
    end

    with_env 'VERBOSE' => 'TruE' do
      assert_equal true, verbose?
    end

    with_env 'VERBOSE' => 'false' do
      assert_equal false, verbose?
    end

    with_env 'VERBOSE' => nil do
      assert_equal false, verbose?
    end
  end

  #
  # assert_script test
  #

  def test_assert_script_correctly_matches_no_output
    assert_script %Q{
printf ''
}

    assert_script %Q{printf ''}
  end

  def test_assert_script_correctly_matches_whitespace_output
    assert_script %Q{
printf "\\t\\n  "
\t
  }
    assert_script %Q{
echo

}
    assert_script %Q{echo

}
  end

  def test_assert_script_strips_indents
    assert_script %Q{
    echo goodnight
    goodnight
    }

    assert_script %Q{ \t   \r
    echo goodnight
    goodnight
    }

    assert_script %Q{
    printf "\\t\\n  "
    \t
      }

    assert_script %Q{
    echo

    }

    assert_script %Q{echo

}
  end

  def test_assert_script_for_multiple_commands
    assert_script %{
      $ echo one
      one
      $ echo two
      two
    }
  end

  def test_assert_script_for_multiline_commands
    assert_script %{
      $ for n in one two; do
      >   echo $n
      > done
      one
      two
    }
  end

  def test_assert_script_for_multiple_commands_with_context
    assert_script %{
      $ VALUE=one
      $ echo $VALUE
      one
    }
  end

  def test_assert_script_fails_on_mismatch
    assert_raises(TestUnitErrorClass) { assert_script %Q{printf ""\nflunk} }
    assert_raises(TestUnitErrorClass) { assert_script %Q{echo pass\nflunk} }
  end

  #
  # _assert_script test
  #

  def test__assert_script_does_not_strip_indents
    _assert_script %Q{
    printf "    \\t\\n      "
    \t
      }, :outdent => false
  end

  #
  # assert_script_match test
  #

  def test_assert_script_match_matches_regexps_to_output
    assert_script_match %Q{
      $ echo "goodnight
      > moon"
      goodnight
      m:.o+.:n
    }
{'fraction_non_alphanumeric': 0.08335238966384632, 'fraction_numerical': 0.0, 'mean_word_length': 2.8030434782608697, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 25, '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': '1442184', 'n_tokens_mistral': 3237, 'n_tokens_neox': 3115, 'n_words': 638}