BigW Consortium Gitlab

commit_spec.rb 2.89 KB
Newer Older
1 2 3
require 'spec_helper'

describe Commit do
4
  let(:project) { create :project }
5
  let(:commit) { project.repository.commit }
6

7 8
  describe '#title' do
    it "returns no_commit_message when safe_message is blank" do
9 10
      allow(commit).to receive(:safe_message).and_return('')
      expect(commit.title).to eq("--no commit message")
11
    end
12

13
    it "truncates a message without a newline at 80 characters" do
Dmitriy Zaporozhets committed
14
      message = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec sodales id felis id blandit. Vivamus egestas lacinia lacus, sed rutrum mauris.'
15

16 17
      allow(commit).to receive(:safe_message).and_return(message)
      expect(commit.title).to eq("#{message[0..79]}…")
18
    end
19

20 21
    it "truncates a message with a newline before 80 characters at the newline" do
      message = commit.safe_message.split(" ").first
22

23 24
      allow(commit).to receive(:safe_message).and_return(message + "\n" + message)
      expect(commit.title).to eq(message)
25
    end
26

Dmitriy Zaporozhets committed
27 28 29 30 31
    it "does not truncates a message with a newline after 80 but less 100 characters" do
      message =<<eos
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec sodales id felis id blandit.
Vivamus egestas lacinia lacus, sed rutrum mauris.
eos
32

33 34
      allow(commit).to receive(:safe_message).and_return(message)
      expect(commit.title).to eq(message.split("\n").first)
35 36
    end
  end
37 38 39 40

  describe "delegation" do
    subject { commit }

41 42 43 44 45 46 47 48 49 50 51
    it { is_expected.to respond_to(:message) }
    it { is_expected.to respond_to(:authored_date) }
    it { is_expected.to respond_to(:committed_date) }
    it { is_expected.to respond_to(:committer_email) }
    it { is_expected.to respond_to(:author_email) }
    it { is_expected.to respond_to(:parents) }
    it { is_expected.to respond_to(:date) }
    it { is_expected.to respond_to(:diffs) }
    it { is_expected.to respond_to(:tree) }
    it { is_expected.to respond_to(:id) }
    it { is_expected.to respond_to(:to_patch) }
52
  end
53 54 55

  describe '#closes_issues' do
    let(:issue) { create :issue, project: project }
56 57
    let(:other_project) { create :project, :public }
    let(:other_issue) { create :issue, project: other_project }
58 59

    it 'detects issues that this commit is marked as closing' do
60
      commit.stub(safe_message: "Fixes ##{issue.iid}")
61
      expect(commit.closes_issues(project)).to eq([issue])
62
    end
63 64 65 66

    it 'does not detect issues from other projects' do
      ext_ref = "#{other_project.path_with_namespace}##{other_issue.iid}"
      commit.stub(safe_message: "Fixes #{ext_ref}")
67
      expect(commit.closes_issues(project)).to be_empty
68
    end
69 70 71 72 73
  end

  it_behaves_like 'a mentionable' do
    let(:subject) { commit }
    let(:mauthor) { create :user, email: commit.author_email }
74
    let(:backref_text) { "commit #{subject.id}" }
75 76 77 78 79
    let(:set_mentionable_text) { ->(txt){ subject.stub(safe_message: txt) } }

    # Include the subject in the repository stub.
    let(:extra_commits) { [subject] }
  end
80
end