<commit_before>require 'spec_helper'
require 'tempfile'

describe HiveTests::Configuration do
  RSpec.shared_examples('config') do
    its(:host) do
      is_expected.to eq(expected_host)
    end

    its(:port) do
      is_expected.to eq(expected_port)
    end

    its(:host_shared_directory_path) do
      is_expected.to match(expected_host_shared_directory_path)
    end

    its(:docker_shared_directory_path) do
      is_expected.to eq(expected_docker_shared_directory_path)
    end

    its(:hive_version) do
      is_expected.to eq(expected_hive_version)
    end

    its(:connection_timeout) do
      is_expected.to eq(expected_timeout)
    end

    its(:hive_options) do
      is_expected.to eq(expected_hive_options)
    end
  end

  let(:expected_host_shared_directory_path) do
    '/Users/Shared/tmp/spec-tmp-files'
  end
  let(:expected_docker_shared_directory_path) { '/tmp/spec-tmp-files' }
  let(:expected_hive_version) { 10 }
  let(:expected_timeout) { 1800 }
  let(:expected_partition_flag) { 'true' }
  let(:expected_partition_mode) { 'nonstrict' }
  let(:expected_partiton_pernodexi) { '100000' }
  let(:expected_partitions) { '100000' }
  let(:expected_java_opts) { '-Xmx2048m' }
  let(:expected_hive_options) do
    {'hive.exec.dynamic.partition' => expected_partition_flag,
     'hive.exec.dynamic.partition.mode' => expected_partition_mode,
     'hive.exec.max.dynamic.partitions.pernodexi' => expected_partiton_pernodexi,
     'hive.exec.max.dynamic.partitions' => expected_partitions,
     'mapred.child.java.opts' => expected_java_opts}
  end

  context 'when no configuration file is provided' do
    let(:expected_port) { 10000 }

    before { allow(Dir).to receive(:mktmpdir) { mock_tmpdir } }

    subject { described_class.new }

    context 'when on Mac' do
      let(:mock_tmpdir) { '/Users/Shared/test/' }
      let(:expected_host) { '192.168.99.100' }
      let(:expected_host_shared_directory_path) do
        %r{/Users/Shared/.*/spec-tmp-files}
      end

      before do
        allow_any_instance_of(described_class).to receive(:mac?) { true }
      end

      include_examples('config')
    end

    context 'when on Linux' do
      let(:mock_tmpdir) { '/tmp/test/' }
      let(:expected_host) { '127.0.0.1' }
      let(:expected_host_shared_directory_path) do
        %r{/tmp/.*/spec-tmp-files}
      end

      before do
        allow_any_instance_of(described_class).to receive(:mac?) { false }
      end

      include_examples('config')
    end
  end

  context 'when there is a configuration file' do
    let(:path_to_config_file) do
      Tempfile.open(%w(config .yml)) do |f|
        f.write yaml_hash.to_yaml
        f.path
      end
    end
    let(:expected_host) { '127.0.0.2' }
    let(:expected_port) { 10001 }

    context 'where all parameters are present' do
      let(:yaml_hash) do
        {
          'hive' =>
            {<commit_msg>Getting rid of regexps in tests.<commit_after>require 'spec_helper'
require 'tempfile'

describe HiveTests::Configuration do
  RSpec.shared_examples('config') do
    its(:host) do
      is_expected.to eq(expected_host)
    end

    its(:port) do
      is_expected.to eq(expected_port)
    end

    its(:host_shared_directory_path) do
      is_expected.to match(expected_host_shared_directory_path)
    end

    its(:docker_shared_directory_path) do
      is_expected.to eq(expected_docker_shared_directory_path)
    end

    its(:hive_version) do
      is_expected.to eq(expected_hive_version)
    end

    its(:connection_timeout) do
      is_expected.to eq(expected_timeout)
    end

    its(:hive_options) do
      is_expected.to eq(expected_hive_options)
    end
  end

  let(:expected_host_shared_directory_path) do
    '/Users/Shared/tmp/spec-tmp-files'
  end
  let(:expected_docker_shared_directory_path) { '/tmp/spec-tmp-files' }
  let(:expected_hive_version) { 10 }
  let(:expected_timeout) { 1800 }
  let(:expected_partition_flag) { 'true' }
  let(:expected_partition_mode) { 'nonstrict' }
  let(:expected_partiton_pernodexi) { '100000' }
  let(:expected_partitions) { '100000' }
  let(:expected_java_opts) { '-Xmx2048m' }
  let(:expected_hive_options) do
    {'hive.exec.dynamic.partition' => expected_partition_flag,
     'hive.exec.dynamic.partition.mode' => expected_partition_mode,
     'hive.exec.max.dynamic.partitions.pernodexi' => expected_partiton_pernodexi,
     'hive.exec.max.dynamic.partitions' => expected_partitions,
     'mapred.child.java.opts' => expected_java_opts}
  end

  context 'when no configuration file is provided' do
    let(:expected_port) { 10000 }

    before { allow(Dir).to receive(:mktmpdir) { mock_tmpdir } }

    subject { described_class.new }

    context 'when on Mac' do
      let(:mock_tmpdir) { '/Users/Shared/test/' }
      let(:expected_host) { '192.168.99.100' }
      let(:expected_host_shared_directory_path) { '/Users/Shared/test/spec-tmp-files' }

      before do
        allow_any_instance_of(described_class).to receive(:mac?) { true }
      end

      include_examples('config')
    end

    context 'when on Linux' do
      let(:mock_tmpdir) { '/tmp/test/' }
      let(:expected_host) { '127.0.0.1' }
      let(:expected_host_shared_directory_path) { '/tmp/test/spec-tmp-files' }

      before do
        allow_any_instance_of(described_class).to receive(:mac?) { false }
      end

      include_examples('config')
    end
  end

  context 'when there is a configuration file' do
    let(:path_to_config_file) do
      Tempfile.open(%w(config .yml)) do |f|
        f.write yaml_hash.to_yaml
        f.path
      end
    end
    let(:expected_host) { '127.0.0.2' }
    let(:expected_port) { 10001 }

    context 'where all parameters are present' do
      let(:yaml_hash) do
        {
          'hive' =>
            {
{'fraction_non_alphanumeric': 0.11503200138384363, 'fraction_numerical': 0.019027849852966613, 'mean_word_length': 3.1989832970225125, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 6, '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': '4880186', 'n_tokens_mistral': 2050, 'n_tokens_neox': 1912, 'n_words': 341}