<commit_before>require 'spec_helper'
require 'fileutils'

describe RepositoryCheck::SingleRepositoryWorker do
  subject(:worker) { described_class.new }

  it 'skips when the project repo is empty' do
    project = create(:project, :wiki_disabled)

    expect(worker).not_to receive(:git_fsck)

    worker.perform(project.id)

    expect(project.reload.last_repository_check_failed).to eq(false)
  end

  it 'succeeds when the project repo is valid' do
    project = create(:project, :repository, :wiki_disabled)

    expect(worker).to receive(:git_fsck).and_call_original

    expect do
      worker.perform(project.id)
    end.to change { project.reload.last_repository_check_at }

    expect(project.reload.last_repository_check_failed).to eq(false)
  end

  it 'fails when the project is not empty and a broken repository' do
    project = create(:project, :repository)
    break_project(project)

    worker.perform(project.id)

    expect(project.reload.last_repository_check_failed).to eq(true)
  end

  it 'fails if the wiki repository is broken' do
    project = create(:project, :wiki_enabled)
    project.create_wiki

    # Test sanity: everything should be fine before the wiki repo is broken
    worker.perform(project.id)
    expect(project.reload.last_repository_check_failed).to eq(false)

    break_wiki(project)
    worker.perform(project.id)

    expect(project.reload.last_repository_check_failed).to eq(true)
  end

  it 'skips wikis when disabled' do
    project = create(:project, :wiki_disabled)
    # Make sure the test would fail if the wiki repo was checked
    break_wiki(project)

    subject.perform(project.id)

    expect(project.reload.last_repository_check_failed).to eq(false)
  end

  it 'creates missing wikis' do
    project = create(:project, :wiki_enabled)
    Gitlab::Shell.new.rm_directory(project.repository_storage_path, project.wiki.path)

    subject.perform(project.id)

    expect(project.reload.last_repository_check_failed).to eq(false)
  end

  it 'does not create a wiki if the main repo does not exist at all' do
    project = create(:project, :repository)
    Gitlab::Shell.new.rm_directory(project.repository_storage_path, project.path)
    Gitlab::Shell.new.rm_directory(project.repository_storage_path, project.wiki.path)

    subject.perform(project.id)

    expect(Gitlab::Shell.new.exists?(project.repository_storage_path, project.wiki.path)).to eq(false)
  end

  def break_wiki(project)
    break_repo(wiki_path(project))
  end

  def wiki_path(project)
    project.wiki.repository.path_to_repo
  end

  def break_project(project)
    break_repo(project.repository.path_to_repo)
  end

  def break_repo(repo)
    # Create or replace blob ffffffffffffffffffffffffffffffffffffffff with an empty file
    # This will make the repo invalid, _and_ 'git init' cannot fix it.<commit_msg>Project#repository_storage_path no longer exists<commit_after>require 'spec_helper'
require 'fileutils'

describe RepositoryCheck::SingleRepositoryWorker do
  subject(:worker) { described_class.new }

  it 'skips when the project repo is empty' do
    project = create(:project, :wiki_disabled)

    expect(worker).not_to receive(:git_fsck)

    worker.perform(project.id)

    expect(project.reload.last_repository_check_failed).to eq(false)
  end

  it 'succeeds when the project repo is valid' do
    project = create(:project, :repository, :wiki_disabled)

    expect(worker).to receive(:git_fsck).and_call_original

    expect do
      worker.perform(project.id)
    end.to change { project.reload.last_repository_check_at }

    expect(project.reload.last_repository_check_failed).to eq(false)
  end

  it 'fails when the project is not empty and a broken repository' do
    project = create(:project, :repository)
    break_project(project)

    worker.perform(project.id)

    expect(project.reload.last_repository_check_failed).to eq(true)
  end

  it 'fails if the wiki repository is broken' do
    project = create(:project, :wiki_enabled)
    project.create_wiki

    # Test sanity: everything should be fine before the wiki repo is broken
    worker.perform(project.id)
    expect(project.reload.last_repository_check_failed).to eq(false)

    break_wiki(project)
    worker.perform(project.id)

    expect(project.reload.last_repository_check_failed).to eq(true)
  end

  it 'skips wikis when disabled' do
    project = create(:project, :wiki_disabled)
    # Make sure the test would fail if the wiki repo was checked
    break_wiki(project)

    subject.perform(project.id)

    expect(project.reload.last_repository_check_failed).to eq(false)
  end

  it 'creates missing wikis' do
    project = create(:project, :wiki_enabled)
    Gitlab::Shell.new.rm_directory(project.repository_storage, project.wiki.path)

    subject.perform(project.id)

    expect(project.reload.last_repository_check_failed).to eq(false)
  end

  it 'does not create a wiki if the main repo does not exist at all' do
    project = create(:project, :repository)
    Gitlab::Shell.new.rm_directory(project.repository_storage, project.path)
    Gitlab::Shell.new.rm_directory(project.repository_storage, project.wiki.path)

    subject.perform(project.id)

    expect(Gitlab::Shell.new.exists?(project.repository_storage, project.wiki.path)).to eq(false)
  end

  def break_wiki(project)
    break_repo(wiki_path(project))
  end

  def wiki_path(project)
    project.wiki.repository.path_to_repo
  end

  def break_project(project)
    break_repo(project.repository.path_to_repo)
  end

  def break_repo(repo)
    # Create or replace blob ffffffffffffffffffffffffffffffffffffffff with an empty file
    # This will make the repo invalid, _and_ 'git init' cannot fix it.
{'fraction_non_alphanumeric': 0.08861203719950869, 'fraction_numerical': 0.0, 'mean_word_length': 4.337078651685394, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, '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': '5526286', 'n_tokens_mistral': 1771, 'n_tokens_neox': 1690, 'n_words': 420}