<commit_before>require "minitest/autorun"

require_relative "../lib/cocoaseeds"

class CoreTest < Minitest::Test

  def setup
    pwd = File.expand_path(File.dirname(__FILE__))

    @project_dirname = File.join(pwd, "TestProj")
    @seeds_dirname = File.join(@project_dirname, "Seeds")

    # clean
    FileUtils.rm_rf(@project_dirname)

    # create a new project
    @project_filename = File.join(@project_dirname, "TestProj.xcodeproj")
    project = Xcodeproj::Project.new(@project_filename)
    project.new_target(:application, "TestProj", :ios)
    project.new_target(:test, "TestProjTests", :ios)
    project.save

    # cocoaseeds
    @seed = Seeds::Core.new(@project_dirname)
    @seed.mute = true
  end

  def teardown
    FileUtils.rm_rf(@project_dirname)
  end

  def project
    Xcodeproj::Project.open(@project_filename)
  end

  def phase(target_name)
    self.project.target_named(target_name).sources_build_phase
  end

  def seedfile(content)
    path = File.join(@project_dirname, "Seedfile")
    content = content ? content.strip.sub(/^\s+/, '') : ''
    File.write(path, content)
  end

  def test_raise_no_project
    FileUtils.rm_rf(@project_dirname)
    assert_raises Seeds::Exception do @seed.install end
  end

  def test_raise_no_seedfile
    assert_raises Seeds::Exception do @seed.install end
  end

  def test_raise_invalid_target
    seedfile %{
      target :InvalidTargetName do
        github "devxoul/JLToast", "1.2.2"
      end
    }
    assert_raises Seeds::Exception do @seed.install end
  end

  def test_install
    seedfile %{
      github "devxoul/JLToast", "1.2.2", :files => "JLToast/*.{h,swift}"
    }
    @seed.install

    assert\
      File.exists?(File.join(@seeds_dirname, "JLToast")),
      "Directory Seeds/JLToast not exists."

    refute_nil\
      self.project["Seeds"]["JLToast"],
      "Group 'Seeds/JLToast' not exists in the project."

    self.project["Seeds"]["JLToast"].files.each do |file|
      assert_match /.*\.(h|swift)/, file.name
    end

    self.project.targets.each do |target|
      phase = target.sources_build_phase
      assert phase.files.length > 0
      phase.file_display_names.each do |filename|
        assert_match /.*\.(h|swift)/, filename
      end
    end
  end

  def test_install_target
    seedfile %{
      target :TestProjTests do
        github "devxoul/JLToast", "1.2.2", :files => "JLToast/*.{h,swift}"
      end
    }
    @seed.install

    refute self.phase(:TestProj).include_filename?(/.*\.(h|swift)/)
    assert self.phase(:TestProjTests).include_filename?(/.*\.(h|swift)/)
  end

  def test_install_separated_target
    seedfile %{
      target :TestProj do
        github "devxoul/JLToast", "1.2.2", :files => "JLToast/*.{h,swift}"
      end

      target :TestProjTests do
        github "devxoul/SwipeBack", "1.0.4", :files => "SwipeBack/*.{h,m}"
      end
    }
    @seed.install

    assert self.phase(:TestProj).include_filename?(/JLToast.*\.(h|swift)/)
    refute self.phase(:TestProj).include_filename?(/.*SwipeBack\.(h|m)/)
    assert self.phase(:TestProjTests).include_filename?(/.*SwipeBack\.(h|m)/)
    refute self.phase(:TestProjTests).include_filename?(/JLToast.*\.(h|swift)/)
  end

  def test_remove
    seedfile %{
      github "devxoul/JLToast", "1.2.2", :files => "JLToast/*.{h,swift}"
    }
    @seed.install

    seedfile nil<commit_msg>Add tests for common seeds with separated targets.<commit_after>require "minitest/autorun"

require_relative "../lib/cocoaseeds"

class CoreTest < Minitest::Test

  def setup
    pwd = File.expand_path(File.dirname(__FILE__))

    @project_dirname = File.join(pwd, "TestProj")
    @seeds_dirname = File.join(@project_dirname, "Seeds")

    # clean
    FileUtils.rm_rf(@project_dirname)

    # create a new project
    @project_filename = File.join(@project_dirname, "TestProj.xcodeproj")
    project = Xcodeproj::Project.new(@project_filename)
    project.new_target(:application, "TestProj", :ios)
    project.new_target(:test, "TestProjTests", :ios)
    project.save

    # cocoaseeds
    @seed = Seeds::Core.new(@project_dirname)
    @seed.mute = true
  end

  def teardown
    FileUtils.rm_rf(@project_dirname)
  end

  def project
    Xcodeproj::Project.open(@project_filename)
  end

  def phase(target_name)
    self.project.target_named(target_name).sources_build_phase
  end

  def seedfile(content)
    path = File.join(@project_dirname, "Seedfile")
    content = content ? content.strip.sub(/^\s+/, '') : ''
    File.write(path, content)
  end

  def test_raise_no_project
    FileUtils.rm_rf(@project_dirname)
    assert_raises Seeds::Exception do @seed.install end
  end

  def test_raise_no_seedfile
    assert_raises Seeds::Exception do @seed.install end
  end

  def test_raise_invalid_target
    seedfile %{
      target :InvalidTargetName do
        github "devxoul/JLToast", "1.2.2"
      end
    }
    assert_raises Seeds::Exception do @seed.install end
  end

  def test_install
    seedfile %{
      github "devxoul/JLToast", "1.2.2", :files => "JLToast/*.{h,swift}"
    }
    @seed.install

    assert\
      File.exists?(File.join(@seeds_dirname, "JLToast")),
      "Directory Seeds/JLToast not exists."

    refute_nil\
      self.project["Seeds"]["JLToast"],
      "Group 'Seeds/JLToast' not exists in the project."

    self.project["Seeds"]["JLToast"].files.each do |file|
      assert_match /.*\.(h|swift)/, file.name
    end

    self.project.targets.each do |target|
      phase = target.sources_build_phase
      assert phase.files.length > 0
      phase.file_display_names.each do |filename|
        assert_match /.*\.(h|swift)/, filename
      end
    end
  end

  def test_install_target
    seedfile %{
      target :TestProjTests do
        github "devxoul/JLToast", "1.2.2", :files => "JLToast/*.{h,swift}"
      end
    }
    @seed.install

    refute self.phase(:TestProj).include_filename?(/.*\.(h|swift)/)
    assert self.phase(:TestProjTests).include_filename?(/.*\.(h|swift)/)
  end

  def test_install_separated_target
    seedfile %{
      target :TestProj do
        github "devxoul/JLToast", "1.2.2", :files => "JLToast/*.{h,swift}"
      end

      target :TestProjTests do
        github "devxoul/SwipeBack", "1.0.4", :files => "SwipeBack/*.{h,m}"
      end
    }
    @seed.install

    assert self.phase(:TestProj).include_filename?(/JLToast.*\.(h|swift)/)
    refute self.phase(:TestProj).include_filename?(/.*SwipeBack\.(h|m)/)
    assert self.phase(:TestProjTests).include_filename?(/.*SwipeBack\.(h|m)/)
    refute self.phase(:TestProjTests).include_filename?(/JLToast.*\.(h|swift)/)
  end

  def test_install_common_before_separated_target
    seedfile %{
      github "devxoul/JLToast", "1.2.2", :files => "JLToast/*.{h,swift}"

      target :TestProjTests do
        github "devxoul/SwipeBack", "1.0.4", :files => "SwipeBack/*.{h,m}"
      end
    }
    @seed.install

    assert\
      self.phase(:TestProj).include_filename?(/JLToast.*\.(h|swift)/),
      "TestProj should have JLToast files."
    assert\
      self.phase(:TestProjTests).include_filename?(/.*SwipeBack\.(h|m)/),
      "TestProjTests should have SwipeBack files."
    assert\
      self.phase(:TestProjTests).include_filename?(/JLToast.*\.(h|swift)/),
      "TestProjTests should have JLToast files."
  end

  def test_install_common_after_separated_target
    seedfile %{
      target :TestProjTests do
        github "devxoul/SwipeBack", "1.0.4", :files => "SwipeBack/*.{h,m}"
      end

      github "devxoul/JLToast", "1.2.2", :files => "JLToast/*.{h,swift}"
    }
    @seed.install

    assert\
      self.phase(:TestProj).include_filename?(/JLToast.*\.(h|swift)/),
      "TestProj should have JLToast files."
    assert\
      self.phase(:TestProjTests).include_filename?(/.*SwipeBack\.(h|m)/),
      "TestProjTests should have SwipeBack files."
    assert\
      self.phase(:TestProjTests).include_filename?(/JLToast.*\.(h|swift)/),
      "TestProjTests should have JLToast files."
  end

  def test_remove
    seedfile %{
      github "devxoul/JLToast", "1.2.2", :files => "JLToast/*.{h,swift}"
    }
    @seed.install

    seedfile nil
{'fraction_non_alphanumeric': 0.14480807086614172, 'fraction_numerical': 0.006151574803149607, 'mean_word_length': 3.1159493670886076, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 19, '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': '5350387', 'n_tokens_mistral': 2980, 'n_tokens_neox': 2881, 'n_words': 498}