summaryrefslogtreecommitdiff
path: root/spec/workers/repository_check/single_repository_worker_spec.rb
blob: d2609d21546530f57088bf1e72582deb6ccc9b60 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
require 'spec_helper'
require 'fileutils'

describe RepositoryCheck::SingleRepositoryWorker do
  subject { described_class.new }

  it 'passes when the project has no push events' do
    project = create(:project_empty_repo, :wiki_disabled)
    project.events.destroy_all
    break_repo(project)

    subject.perform(project.id)

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

  it 'fails when the project has push events and a broken repository' do
    project = create(:project_empty_repo)
    create_push_event(project)
    break_repo(project)

    subject.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_empty_repo, :wiki_enabled)
    project.create_wiki

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

    break_wiki(project)
    subject.perform(project.id)

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

  it 'skips wikis when disabled' do
    project = create(:project_empty_repo, :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_empty_repo, :wiki_enabled)
    FileUtils.rm_rf(wiki_path(project))

    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_empty_repo)
    create_push_event(project)
    FileUtils.rm_rf(project.repository.path_to_repo)
    FileUtils.rm_rf(wiki_path(project))

    subject.perform(project.id)

    expect(File.exist?(wiki_path(project))).to eq(false)
  end

  def break_wiki(project)
    FileUtils.rm_rf(wiki_path(project) + '/objects')
  end

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

  def create_push_event(project)
    project.events.create(action: Event::PUSHED, author_id: create(:user).id)
  end

  def break_repo(project)
    FileUtils.rm_rf(File.join(project.repository.path_to_repo, 'objects'))
  end
end