<commit_before>require_relative '../spec_helper'

describe Dapp::GitArtifact do
  include SpecHelper::Common
  include SpecHelper::Git
  include SpecHelper::Application

  before :each do
    init
    git_init!
    stub_stages
    stub_application
  end

  def init
    FileUtils.mkdir 'project'
    @where_to_add = File.expand_path('where-to-add')
    Dir.chdir File.expand_path('project')
  end

  def config
    @config ||= default_config.merge(_home_path: '')
  end

  def stubbed_stage
    instance_double(Dapp::Build::Stage::Base).tap do |instance|
      allow(instance).to receive(:prev_stage=)
    end
  end

  def stub_stages
    @stage_commit = {}
    [Dapp::Build::Stage::GAArchive, Dapp::Build::Stage::GALatestPatch].each do |stage|
      allow_any_instance_of(stage).to receive(:layer_commit) do
        @stage_commit[stage.name] ||= {}
        @stage_commit[stage.name][@branch] ||= git_latest_commit(branch: @branch)
      end
    end
    allow_any_instance_of(Dapp::Build::Stage::GALatestPatch).to receive(:prev_g_a_stage) { g_a_archive_stage }
  end

  def project
    super do
      allow_any_instance_of(Dapp::Project).to receive(:git_path) { 'git' }
      allow_any_instance_of(Dapp::Project).to receive(:tar_path) { 'tar' }
      allow_any_instance_of(Dapp::Project).to receive(:sudo_path) { 'sudo' }
      allow_any_instance_of(Dapp::Project).to receive(:install_path) { 'install' }
    end
  end

  def g_a_archive_stage
    @g_a_archive_stage ||= Dapp::Build::Stage::GAArchive.new(empty_application, stubbed_stage)
  end

  def g_a_latest_patch_stage
    @g_a_latest_patch_stage ||= Dapp::Build::Stage::GALatestPatch.new(empty_application, stubbed_stage)
  end

  def git_artifact
    Dapp::GitArtifact.new(stubbed_repo, **git_artifact_local_options)
  end

  def stubbed_repo
    instance_double(Dapp::GitRepo::Own).tap do |instance|
      allow(instance).to receive(:container_path) { '.git' }
      allow(instance).to receive(:application) { application }
    end
  end

  def git_artifact_local_options
    {
      cwd: (@cwd ||= ''),
      paths: (@paths ||= []),
      exclude_paths: (@exclude_paths ||= []),
      branch: (@branch ||= 'master'),
      where_to_add: @where_to_add,
      group: (@group ||= 'root'),
      owner: (@owner ||= 'root')
    }
  end

  [:patch, :archive].each do |type|
    define_method "#{type}_apply" do |add_files: ['data.txt'], added_files: add_files, not_added_files: [], **kwargs, &blk|
      @branch = kwargs[:branch] unless kwargs[:branch].nil?
      command_apply(archive_command) if type == :patch && !kwargs[:ignore_archive_apply]

      [:cwd, :paths, :exclude_paths, :where_to_add, :group, :owner].each do |opt|
        instance_variable_set(:"@#{opt}", kwargs[opt]) unless kwargs[opt].nil?
      end
      add_files.each { |file_path| git_change_and_commit!(file_path, branch: @branch) }

      command_apply(send("#{type}_command"))

      expect(File.exist?(@where_to_add)).to be_truthy
      added_files.each { |file_path| expect(File.exist?(File.join(@where_to_add, file_path))).to be_truthy }<commit_msg>modified spec<commit_after>require_relative '../spec_helper'

describe Dapp::GitArtifact do
  include SpecHelper::Common
  include SpecHelper::Git
  include SpecHelper::Application

  before :each do
    init
    git_init!
    stub_stages
    stub_application
  end

  def init
    FileUtils.mkdir 'project'
    @where_to_add = File.expand_path('where-to-add')
    Dir.chdir File.expand_path('project')
  end

  def config
    @config ||= default_config.merge(_home_path: '')
  end

  def stubbed_stage
    instance_double(Dapp::Build::Stage::Base).tap do |instance|
      allow(instance).to receive(:prev_stage=)
    end
  end

  def stub_stages
    @stage_commit = {}
    [Dapp::Build::Stage::GAArchive, Dapp::Build::Stage::GALatestPatch].each do |stage|
      allow_any_instance_of(stage).to receive(:layer_commit) do
        @stage_commit[stage.name] ||= {}
        @stage_commit[stage.name][@branch] ||= git_latest_commit(branch: @branch)
      end
    end
    allow_any_instance_of(Dapp::Build::Stage::GALatestPatch).to receive(:prev_g_a_stage) { g_a_archive_stage }
  end

  def project
    super do
      allow_any_instance_of(Dapp::Project).to receive(:git_path) { 'git' }
      allow_any_instance_of(Dapp::Project).to receive(:tar_path) { 'tar' }
      allow_any_instance_of(Dapp::Project).to receive(:sudo_path) { 'sudo' }
      allow_any_instance_of(Dapp::Project).to receive(:install_path) { 'install' }
    end
  end

  def g_a_archive_stage
    @g_a_archive_stage ||= Dapp::Build::Stage::GAArchive.new(empty_application, stubbed_stage)
  end

  def g_a_latest_patch_stage
    @g_a_latest_patch_stage ||= Dapp::Build::Stage::GALatestPatch.new(empty_application, stubbed_stage)
  end

  def git_artifact
    Dapp::GitArtifact.new(stubbed_repo, **git_artifact_local_options)
  end

  def stubbed_repo
    instance_double(Dapp::GitRepo::Own).tap do |instance|
      allow(instance).to receive(:container_path) { '.git' }
      allow(instance).to receive(:application) { application }
      allow(instance).to receive(:git_bare) { |command| git(command) }
    end
  end

  def git_artifact_local_options
    {
      cwd: (@cwd ||= ''),
      paths: (@paths ||= []),
      exclude_paths: (@exclude_paths ||= []),
      branch: (@branch ||= 'master'),
      where_to_add: @where_to_add,
      group: (@group ||= 'root'),
      owner: (@owner ||= 'root')
    }
  end

  [:patch, :archive].each do |type|
    define_method "#{type}_apply" do |add_files: ['data.txt'], added_files: add_files, not_added_files: [], **kwargs, &blk|
      @branch = kwargs[:branch] unless kwargs[:branch].nil?
      command_apply(archive_command) if type == :patch && !kwargs[:ignore_archive_apply]

      [:cwd, :paths, :exclude_paths, :where_to_add, :group, :owner].each do |opt|
        instance_variable_set(:"@#{opt}", kwargs[opt]) unless kwargs[opt].nil?
      end
      add_files.each { |file_path| git_change_and_commit!(file_path, branch: @branch) }

      command_apply(send("#{type}_command"))

      expect(File.exist?(@where_to_add)).to be_truthy
      added_files.each { |file_path| expect(File.exist?(File.join(@where_to_add, file_path))).to be_truthy }
{'fraction_non_alphanumeric': 0.14145950280673616, 'fraction_numerical': 0.0, 'mean_word_length': 4.192339716902581, 'pattern_counts': {'":': 0, '<': 3, '<?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': '1982604', 'n_tokens_mistral': 2175, 'n_tokens_neox': 2123, 'n_words': 363}