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
112
|
# frozen_string_literal: true
require 'gitlab/dangerfiles/spec_helper'
require_relative '../../../../tooling/danger/specs'
require_relative '../../../../tooling/danger/project_helper'
RSpec.describe Tooling::Danger::Specs::ProjectFactorySuggestion, feature_category: :tooling do
include_context "with dangerfile"
let(:fake_danger) { DangerSpecHelper.fake_danger.include(Tooling::Danger::Specs) }
let(:fake_project_helper) { instance_double('Tooling::Danger::ProjectHelper') }
let(:filename) { 'spec/foo_spec.rb' }
let(:template) do
<<~MARKDOWN.chomp
```suggestion
%<suggested_line>s
```
Project creations are very slow. Using `let_it_be`, `build` or `build_stubbed` can improve test performance.
Warning: `let_it_be` may not be suitable if your test modifies data as this could result in state leaks!
In those cases, please use `let_it_be_with_reload` or `let_it_be_with_refind` instead.
If your are unsure which is the right method to use,
please refer to [testing best practices](https://docs.gitlab.com/ee/development/testing_guide/best_practices.html#optimize-factory-usage)
for background information and alternative options for optimizing factory usage.
Feel free to ignore this comment if you know `let` or `let!` are the better options and/or worry about causing state leaks.
MARKDOWN
end
let(:file_lines) do
[
" let(:project) { create(:project) }",
" let_it_be(:project) { create(:project, :repository)",
" let!(:project) { create(:project) }",
" let(:var) { create(:project) }",
" let(:merge_request) { create(:merge_request, project: project)",
" context 'when merge request exists' do",
" it { is_expected.to be_success }",
" end",
" let!(:var) { create(:project) }",
" let(:project) { create(:thing) }",
" let(:project) { build(:project) }",
" let(:project) do",
" create(:project)",
" end",
" let(:project) { create(:project, :repository) }",
" str = 'let(:project) { create(:project) }'",
" let(:project) { create(:project_empty_repo) }",
" let(:project) { create(:forked_project_with_submodules) }",
" let(:project) { create(:project_with_design) }",
" let(:authorization) { create(:project_authorization) }"
]
end
let(:matching_lines) do
[
"+ let(:should_not_error) { create(:project) }",
"+ let(:project) { create(:project) }",
"+ let!(:project) { create(:project) }",
"+ let(:var) { create(:project) }",
"+ let!(:var) { create(:project) }",
"+ let(:project) { create(:project, :repository) }",
"+ let(:project) { create(:project_empty_repo) }",
"+ let(:project) { create(:forked_project_with_submodules) }",
"+ let(:project) { create(:project_with_design) }"
]
end
let(:changed_lines) do
[
"+ line which doesn't exist in the file and should not cause an error",
"+ let_it_be(:project) { create(:project, :repository)",
"+ let(:project) { create(:thing) }",
"+ let(:project) do",
"+ create(:project)",
"+ end",
"+ str = 'let(:project) { create(:project) }'",
"+ let(:authorization) { create(:project_authorization) }"
] + matching_lines
end
subject(:specs) { fake_danger.new(helper: fake_helper) }
before do
allow(specs).to receive(:project_helper).and_return(fake_project_helper)
allow(specs.helper).to receive(:changed_lines).with(filename).and_return(changed_lines)
allow(specs.project_helper).to receive(:file_lines).and_return(file_lines)
end
it 'adds suggestions at the correct lines', :aggregate_failures do
[
{ suggested_line: " let_it_be(:project) { create(:project) }", number: 1 },
{ suggested_line: " let_it_be(:project) { create(:project) }", number: 3 },
{ suggested_line: " let_it_be(:var) { create(:project) }", number: 4 },
{ suggested_line: " let_it_be(:var) { create(:project) }", number: 9 },
{ suggested_line: " let_it_be(:project) { create(:project, :repository) }", number: 15 },
{ suggested_line: " let_it_be(:project) { create(:project_empty_repo) }", number: 17 },
{ suggested_line: " let_it_be(:project) { create(:forked_project_with_submodules) }", number: 18 },
{ suggested_line: " let_it_be(:project) { create(:project_with_design) }", number: 19 }
].each do |test_case|
comment = format(template, suggested_line: test_case[:suggested_line])
expect(specs).to receive(:markdown).with(comment, file: filename, line: test_case[:number])
end
specs.add_suggestions_for(filename)
end
end
|