forked from rubocop/rubocop
-
Notifications
You must be signed in to change notification settings - Fork 2
/
gem_filename.rb
103 lines (88 loc) · 3.35 KB
/
gem_filename.rb
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
# frozen_string_literal: true
module RuboCop
module Cop
module Bundler
# Verifies that a project contains Gemfile or gems.rb file and correct
# associated lock file based on the configuration.
#
# @example EnforcedStyle: Gemfile (default)
# # bad
# Project contains gems.rb and gems.locked files
#
# # bad
# Project contains Gemfile and gems.locked file
#
# # good
# Project contains Gemfile and Gemfile.lock
#
# @example EnforcedStyle: gems.rb
# # bad
# Project contains Gemfile and Gemfile.lock files
#
# # bad
# Project contains gems.rb and Gemfile.lock file
#
# # good
# Project contains gems.rb and gems.locked files
class GemFilename < Base
include ConfigurableEnforcedStyle
include RangeHelp
MSG_GEMFILE_REQUIRED = '`gems.rb` file was found but `Gemfile` is required '\
'(file path: %<file_path>s).'
MSG_GEMS_RB_REQUIRED = '`Gemfile` was found but `gems.rb` file is required '\
'(file path: %<file_path>s).'
MSG_GEMFILE_MISMATCHED = 'Expected a `Gemfile.lock` with `Gemfile` but found '\
'`gems.locked` file (file path: %<file_path>s).'
MSG_GEMS_RB_MISMATCHED = 'Expected a `gems.locked` file with `gems.rb` but found '\
'`Gemfile.lock` (file path: %<file_path>s).'
GEMFILE_FILES = %w[Gemfile Gemfile.lock].freeze
GEMS_RB_FILES = %w[gems.rb gems.locked].freeze
def on_new_investigation
file_path = processed_source.file_path
basename = File.basename(file_path)
return if expected_gemfile?(basename)
register_offense(file_path, basename)
end
private
def register_offense(file_path, basename)
register_gemfile_offense(file_path, basename) if gemfile_offense?(basename)
register_gems_rb_offense(file_path, basename) if gems_rb_offense?(basename)
end
def register_gemfile_offense(file_path, basename)
message = case basename
when 'gems.rb'
MSG_GEMFILE_REQUIRED
when 'gems.locked'
MSG_GEMFILE_MISMATCHED
end
add_global_offense(format(message, file_path: file_path))
end
def register_gems_rb_offense(file_path, basename)
message = case basename
when 'Gemfile'
MSG_GEMS_RB_REQUIRED
when 'Gemfile.lock'
MSG_GEMS_RB_MISMATCHED
end
add_global_offense(format(message, file_path: file_path))
end
def gemfile_offense?(basename)
gemfile_required? && GEMS_RB_FILES.include?(basename)
end
def gems_rb_offense?(basename)
gems_rb_required? && GEMFILE_FILES.include?(basename)
end
def expected_gemfile?(basename)
(gemfile_required? && GEMFILE_FILES.include?(basename)) ||
(gems_rb_required? && GEMS_RB_FILES.include?(basename))
end
def gemfile_required?
style == :Gemfile
end
def gems_rb_required?
style == :'gems.rb'
end
end
end
end
end