Welcome to mirror list, hosted at ThFree Co, Russian Federation.

divergence_counts_spec.rb « forks « projects « models « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: fd69cc0f3e77e9cb2317f2c6918c5e78f6bff6e6 (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
87
88
89
90
91
92
93
94
95
96
97
98
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Projects::Forks::DivergenceCounts, feature_category: :source_code_management do
  include ProjectForksHelper

  let_it_be(:user) { create(:user) }

  describe '#counts', :use_clean_rails_redis_caching do
    let(:source_repo) { create(:project, :repository, :public).repository }
    let(:fork_repo) { fork_project(source_repo.project, user, { repository: true }).repository }
    let(:fork_branch) { 'fork-branch' }
    let(:cache_key) { ['project_forks', fork_repo.project.id, fork_branch, 'divergence_counts'] }

    def expect_cached_counts(value)
      counts = described_class.new(fork_repo.project, fork_branch).counts

      ahead, behind = value
      expect(counts).to eq({ ahead: ahead, behind: behind })

      cached_value = [source_repo.commit.sha, fork_repo.commit(fork_branch).sha, value]
      expect(Rails.cache.read(cache_key)).to eq(cached_value)
    end

    it 'shows how far behind/ahead a fork is from the upstream' do
      fork_repo.create_branch(fork_branch)

      expect_cached_counts([0, 0])

      fork_repo.commit_files(
        user,
        branch_name: fork_branch, message: 'Committing something',
        actions: [{ action: :create, file_path: 'encoding/CHANGELOG', content: 'New file' }]
      )

      expect_cached_counts([1, 0])

      fork_repo.commit_files(
        user,
        branch_name: fork_branch, message: 'Committing something else',
        actions: [{ action: :create, file_path: 'encoding/ONE-MORE-CHANGELOG', content: 'One more new file' }]
      )

      expect_cached_counts([2, 0])

      source_repo.commit_files(
        user,
        branch_name: source_repo.root_ref, message: 'Commit to root ref',
        actions: [{ action: :create, file_path: 'encoding/CHANGELOG', content: 'One more' }]
      )

      expect_cached_counts([2, 1])

      source_repo.commit_files(
        user,
        branch_name: source_repo.root_ref, message: 'Another commit to root ref',
        actions: [{ action: :create, file_path: 'encoding/NEW-CHANGELOG', content: 'One more time' }]
      )

      expect_cached_counts([2, 2])

      # When the fork is too far ahead
      stub_const("#{described_class}::LATEST_COMMITS_COUNT", 1)
      fork_repo.commit_files(
        user,
        branch_name: fork_branch, message: 'Another commit to fork',
        actions: [{ action: :create, file_path: 'encoding/TOO-NEW-CHANGELOG', content: 'New file' }]
      )

      expect_cached_counts(nil)
    end

    context 'when counts calculated from a branch that exists upstream' do
      let(:fork_branch) { 'feature' }

      it 'compares the fork branch to upstream default branch' do
        # The branch itself diverges from the upstream default branch
        expect_cached_counts([1, 29])

        source_repo.commit_files(
          user,
          branch_name: source_repo.root_ref, message: 'Commit to root ref',
          actions: [{ action: :create, file_path: 'encoding/CHANGELOG', content: 'New file' }]
        )

        fork_repo.commit_files(
          user,
          branch_name: fork_branch, message: 'Committing to feature branch',
          actions: [{ action: :create, file_path: 'encoding/FEATURE-BRANCH', content: 'New file' }]
        )

        # It takes into account diverged commits from upstream AND from fork
        expect_cached_counts([2, 30])
      end
    end
  end
end