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
99
100
101
102
103
104
105
106
107
108
109
110
111
|
# frozen_string_literal: true
require 'gitlab-dangerfiles'
require 'danger'
require 'danger/plugins/internal/helper'
require 'gitlab/dangerfiles/spec_helper'
require_relative '../../../tooling/danger/model_validations'
require_relative '../../../tooling/danger/project_helper'
RSpec.describe Tooling::Danger::ModelValidations, feature_category: :tooling do
include_context "with dangerfile"
let(:fake_danger) { DangerSpecHelper.fake_danger.include(described_class) }
let(:fake_project_helper) { instance_double(Tooling::Danger::ProjectHelper) }
subject(:model_validations) { fake_danger.new(helper: fake_helper) }
before do
allow(model_validations).to receive(:project_helper).and_return(fake_project_helper)
end
describe '#add_comment_for_added_validations' do
let(:file_lines) { file_diff.map { |line| line.delete_prefix('+').delete_prefix('-') } }
let(:filename) { 'app/models/user.rb' }
let(:added_filename) { 'app/models/user.rb' }
before do
allow(model_validations.project_helper).to receive(:file_lines).and_return(file_lines)
allow(model_validations.helper).to receive(:added_files).and_return([added_filename])
allow(model_validations.helper).to receive(:modified_files).and_return([filename])
allow(model_validations.helper).to receive(:changed_lines).with(filename).and_return(file_diff)
end
context 'when model has a newly added validation' do
let(:file_diff) do
[
"+ scope :admins, -> { where(admin: true) }",
"+ validates :name, presence: true, length: { maximum: 255 }",
"+ validates_with UserValidator",
"+ validate :check_password_weakness",
"+ validates_each :restricted_visibility_levels do |record, attr, value|",
"+ validates_associated :members",
"+ with_options if: :is_admin? do |admin|",
"+ admin.validates :password, length: { minimum: 10 }",
"+ admin.validates :email, presence: true",
"+ end",
"+ with_options if: :is_admin? { |admin| admin.validates :email, presence: true }",
"- validates :first_name, length: { maximum: 127 }"
]
end
it 'adds suggestions at the correct line' do
suggested_line = "\n#{described_class::SUGGEST_MR_COMMENT.chomp}"
matching_line_numbers = [*2..6, 8, 9, 11]
matching_line_numbers.each do |line_number|
expect(model_validations).to receive(:markdown).with(suggested_line, file: filename, line: line_number)
end
model_validations.add_comment_for_added_validations
end
end
context 'when model does not have a newly added validation' do
let(:file_diff) do
[
"+ scope :admins, -> { where(admin: true) }",
"- validates :first_name, length: { maximum: 127 }"
]
end
it 'does not add suggestion' do
expect(model_validations).not_to receive(:markdown)
model_validations.add_comment_for_added_validations
end
end
end
describe '#changed_model_files' do
let(:expected_files) do
%w[
app/models/user.rb
app/models/users/user_follow_user.rb
ee/app/models/ee/user.rb
ee/app/models/sca/license_policy.rb
app/models/concerns/presentable.rb
]
end
before do
added_files = %w[app/models/user_preferences.rb app/models/concerns/presentable.rb]
modified_files = %w[
app/models/user.rb
app/models/users/user_follow_user.rb
ee/app/models/ee/user.rb
ee/app/models/sca/license_policy.rb
config/metrics/count_7d/new_metric.yml
app/assets/index.js
]
allow(model_validations.helper).to receive(:added_files).and_return(added_files)
allow(model_validations.helper).to receive(:modified_files).and_return(modified_files)
end
it 'returns added and modified files' do
expect(model_validations.changed_model_files).to match_array(expected_files)
end
end
end
|