<commit_before>require 'spec_helper'
require 'tmpdir'
require 'pathname'
require 'fileutils'
require 'json'

require 'mamiya/script'

require 'mamiya/package'
require 'mamiya/steps/prepare'

describe Mamiya::Steps::Prepare do
  let!(:tmpdir) { Dir.mktmpdir("mamiya-steps-prepare-spec") }
  after { FileUtils.remove_entry_secure tmpdir }

  let(:target_dir) { Pathname.new(tmpdir).join('release').tap(&:mkdir) }

  let(:target_meta) do
    {
      script: 'script.rb',
    }
  end

  let(:script) do
    double('script',
      before_prepare: proc {},
      prepare: proc {},
      after_prepare: proc {},
    )
  end

  let(:config) do
    double('config')
  end

  let(:options) do
    {target: target_dir}
  end

  subject(:step) { described_class.new(script: nil, config: config, **options) }

  before do
    File.write target_dir.join('.mamiya.meta.json'), target_meta.to_json
    allow(Mamiya::Script).to receive(:new).and_return(script)
    allow(script).to receive(:load!).with(target_dir.realpath.join('.mamiya.script', 'script.rb')).and_return(script)
    allow(script).to receive(:set).and_return(script)
  end

  describe "#run!" do
    it "calls hooks with proper order" do
      hooks = %i(before_prepare prepare after_prepare)

      flags = []
      hooks.each do |sym|
        allow(script).to receive(sym).and_return(proc { flags << sym })
      end

      expect { step.run! }.
        to change { flags }.
        from([]).
        to(hooks)
    end

    it "calls after hook even if exception occured" do
      e = Exception.new("Good bye, the cruel world")
      allow(script).to receive(:prepare).and_return(proc { raise e })

      received = nil
      allow(script).to receive(:after_prepare).and_return(proc { |_| received = _ })

      expect {
        begin
          step.run!
        rescue Exception; end
      }.
        to change { received }.
        from(nil).to(e)
    end

    it "calls hook in :target_dir (pwd)" do
      pwd = nil
      script.stub(prepare: proc { pwd = Dir.pwd })

      expect {
        step.run!
      }.not_to change { Dir.pwd }

      expect(File.realpath(pwd)).to eq target_dir.realpath.to_s
    end

    it "sets release_path" do
      expect(script).to receive(:set).with(:release_path, target_dir.realpath)
      expect(script).to receive(:set).with(:logger, step.logger)
      step.run!
    end
  end
end<commit_msg>Spec for 1439e286acbbbd9465f4e17816ebc36bd98016e2<commit_after>require 'spec_helper'
require 'tmpdir'
require 'pathname'
require 'fileutils'
require 'json'

require 'mamiya/script'

require 'mamiya/package'
require 'mamiya/steps/prepare'

describe Mamiya::Steps::Prepare do
  let!(:tmpdir) { Dir.mktmpdir("mamiya-steps-prepare-spec") }
  after { FileUtils.remove_entry_secure tmpdir }

  let(:target_dir) { Pathname.new(tmpdir).join('release').tap(&:mkdir) }

  let(:target_meta) do
    {
      script: 'script.rb',
    }
  end

  let(:script) do
    double('script',
      before_prepare: proc {},
      prepare: proc {},
      after_prepare: proc {},
    )
  end

  let(:config) do
    double('config')
  end

  let(:options) do
    {target: target_dir, labels: [:foo, :bar]}
  end

  subject(:step) { described_class.new(script: nil, config: config, **options) }

  before do
    File.write target_dir.join('.mamiya.meta.json'), target_meta.to_json
    allow(Mamiya::Script).to receive(:new).and_return(script)
    allow(script).to receive(:load!).with(target_dir.realpath.join('.mamiya.script', 'script.rb')).and_return(script)
    allow(script).to receive(:set).and_return(script)
  end

  describe "#run!" do
    it "calls hooks with proper order" do
      hooks = %i(before_prepare prepare after_prepare)

      flags = []
      hooks.each do |sym|
        allow(script).to receive(sym).and_return(proc { flags << sym })
      end

      expect { step.run! }.
        to change { flags }.
        from([]).
        to(hooks)
    end

    it "calls after hook even if exception occured" do
      e = Exception.new("Good bye, the cruel world")
      allow(script).to receive(:prepare).and_return(proc { raise e })

      received = nil
      allow(script).to receive(:after_prepare).and_return(proc { |_| received = _ })

      expect {
        begin
          step.run!
        rescue Exception; end
      }.
        to change { received }.
        from(nil).to(e)
    end

    it "calls hook in :target_dir (pwd)" do
      pwd = nil
      script.stub(prepare: proc { pwd = Dir.pwd })

      expect {
        step.run!
      }.not_to change { Dir.pwd }

      expect(File.realpath(pwd)).to eq target_dir.realpath.to_s
    end

    it "sets release_path" do
      expect(script).to receive(:set).with(:release_path, target_dir.realpath)
      expect(script).to receive(:set).with(:logger, step.logger)
      step.run!
    end

    it "calls hooks using labels" do
      allow(script).to receive(:before_prepare).with(%i(foo bar)).and_return(proc {})
      allow(script).to receive(:prepare).with(%i(foo bar)).and_return(proc {})
      allow(script).to receive(:after_prepare).with(%i(foo bar)).and_return(proc {})

      step.run!
    end
  end
end
{'fraction_non_alphanumeric': 0.13865628042843234, 'fraction_numerical': 0.004868549172346641, 'mean_word_length': 2.9086757990867578, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, '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': '1330734', 'n_tokens_mistral': 1734, 'n_tokens_neox': 1691, 'n_words': 363}