<commit_before>require 'spec_helper'
require 'ansible-ruby'

describe Ansible::Ruby::DslBuilders::FileLevel do
  let(:builder) { Ansible::Ruby::DslBuilders::FileLevel.new }

  def _evaluate
    builder._evaluate ruby
  end

  subject(:result) { _evaluate }

  before do
    klass = Class.new(Ansible::Ruby::Modules::Base) do
      attribute :src
      validates :src, presence: true
      attribute :dest
      validates :dest, presence: true
    end
    stub_const 'Ansible::Ruby::Modules::Copy', klass
  end

  context 'playbook' do
    context 'named' do
      let(:ruby) do
        <<-RUBY
        play 'the play name' do
          hosts 'host1'

          task 'Copy something' do
              copy do
                src '/file1.conf'
                dest '/file2.conf'
              end
          end
        end
        RUBY
      end

      it { is_expected.to be_a Ansible::Ruby::Models::Playbook }

      it do
        is_expected.to have_attributes plays: include(be_a(Ansible::Ruby::Models::Play))
      end
    end

    context 'unnamed' do
      let(:ruby) do
        <<-RUBY
        play do
          hosts 'host1'

          task 'Copy something' do
              copy do
                src '/file1.conf'
                dest '/file2.conf'
              end
          end
        end
        RUBY
      end

      it { is_expected.to be_a Ansible::Ruby::Models::Playbook }

      it do
        is_expected.to have_attributes plays: include(be_a(Ansible::Ruby::Models::Play))
      end
    end
  end

  context 'tasks' do
    let(:ruby) do
      <<-RUBY
      task 'Copy something' do
        copy do
          src '/file1.conf'
          dest '/file2.conf'
        end
      end

      task 'Copy something else' do
        copy do
          src '/file3.conf'
          dest '/file4.conf'
        end
      end
      RUBY
    end

    it { is_expected.to be_a Ansible::Ruby::Models::Tasks }
    it do
      is_expected.to have_attributes tasks: include(be_a(Ansible::Ruby::Models::Task),
                                                    be_a(Ansible::Ruby::Models::Task))
    end
  end

  context 'change from play to task' do
    subject { -> { _evaluate } }

    let(:ruby) do
      <<-RUBY
      play 'the play name' do
        hosts 'host1'
        roles %w(role1 role2)
      end

      task 'Copy something else' do
        copy do
          src '/file3.conf'
          dest '/file4.conf'
        end
      end
      RUBY
    end

    it { is_expected.to raise_error 'This is a playbook file due to a play coming before this task, cannot use task here!' }
  end

  context 'change from task to play' do
    subject { -> { _evaluate } }

    let(:ruby) do
      <<-RUBY
      task 'Copy something else' do
        copy do
          src '/file3.conf'
          dest '/file4.conf'<commit_msg>fix renames, add first cut of specs<commit_after>require 'spec_helper'
require 'ansible-ruby'

describe Ansible::Ruby::DslBuilders::FileLevel do
  let(:builder) { Ansible::Ruby::DslBuilders::FileLevel.new }

  def evaluate
    builder._evaluate ruby
  end

  subject(:result) { evaluate }

  before do
    klass = Class.new(Ansible::Ruby::Modules::Base) do
      attribute :src
      validates :src, presence: true
      attribute :dest
      validates :dest, presence: true
    end
    stub_const 'Ansible::Ruby::Modules::Copy', klass
  end

  context 'handlers' do
    context 'without handlers' do
      pending 'write this'
    end

    context 'with' do
      let(:ruby) do
        <<-RUBY
        handlers do
          handler 'copy_reboot' do
            copy do
              src '/file1.conf'
              dest '/file2.conf'
            end
          end
        end
        RUBY
      end

      it { is_expected.to be_a Ansible::Ruby::Models::Handlers }
      it { is_expected.to have_attributes handlers: include(be_a(Ansible::Ruby::Models::Handler)) }
    end
  end

  context 'playbook' do
    context 'named' do
      let(:ruby) do
        <<-RUBY
        play 'the play name' do
          hosts 'host1'

          task 'Copy something' do
              copy do
                src '/file1.conf'
                dest '/file2.conf'
              end
          end
        end
        RUBY
      end

      it { is_expected.to be_a Ansible::Ruby::Models::Playbook }

      it do
        is_expected.to have_attributes plays: include(be_a(Ansible::Ruby::Models::Play))
      end
    end

    context 'unnamed' do
      let(:ruby) do
        <<-RUBY
        play do
          hosts 'host1'

          task 'Copy something' do
              copy do
                src '/file1.conf'
                dest '/file2.conf'
              end
          end
        end
        RUBY
      end

      it { is_expected.to be_a Ansible::Ruby::Models::Playbook }

      it do
        is_expected.to have_attributes plays: include(be_a(Ansible::Ruby::Models::Play))
      end
    end
  end

  context 'tasks' do
    let(:ruby) do
      <<-RUBY
      task 'Copy something' do
        copy do
          src '/file1.conf'
          dest '/file2.conf'
        end
      end

      task 'Copy something else' do
        copy do
          src '/file3.conf'
          dest '/file4.conf'
        end
      end
      RUBY
    end

    it { is_expected.to be_a Ansible::Ruby::Models::Tasks }
    it do
      is_expected.to have_attributes tasks: include(be_a(Ansible::Ruby::Models::Task),
                                                    be_a(Ansible::Ruby::Models::Task))
    end
  end

  context 'change from play to task' do
    subject { -> { evaluate } }

    let(:ruby) do
      <<-RUBY
      play 'the play name' do
        hosts 'host1'
        roles %w(role1 role2)
      end

      task 'Copy something else' do
        copy do
          src '/file3.conf'
          dest '/file4.conf'
        end
      end
      RUBY
    end

    it { is_expected.to raise_error 'This is a playbook file due to a play coming before this task, cannot use task here!' }
  end

  context 'change from task to play' do
    subject { -> { evaluate } }

    let(:ruby) do
      <<-RUBY
      task 'Copy something else' do
        copy do
          src '/file3.conf'
          dest '/file4.conf'
{'fraction_non_alphanumeric': 0.09337641357027464, 'fraction_numerical': 0.005815831987075929, 'mean_word_length': 1.6199746085484554, 'pattern_counts': {'":': 0, '<': 25, '<?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': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3826228', 'n_tokens_mistral': 1989, 'n_tokens_neox': 1831, 'n_words': 545}