[rubygems/rubygems] Use a constant empty tar header to avoid extra allocations
[ruby.git] / spec / bundler / quality_spec.rb
blob7cdb9930172bfde9e4ef13e27a828632d0461097
1 # frozen_string_literal: true
3 require "set"
5 RSpec.describe "The library itself" do
6   def check_for_git_merge_conflicts(filename)
7     merge_conflicts_regex = /
8       <<<<<<<|
9       =======|
10       >>>>>>>
11     /x
13     failing_lines = []
14     each_line(filename) do |line, number|
15       failing_lines << number + 1 if line&.match?(merge_conflicts_regex)
16     end
18     return if failing_lines.empty?
19     "#{filename} has unresolved git merge conflicts on lines #{failing_lines.join(", ")}"
20   end
22   def check_for_tab_characters(filename)
23     failing_lines = []
24     each_line(filename) do |line, number|
25       failing_lines << number + 1 if line.include?("\t")
26     end
28     return if failing_lines.empty?
29     "#{filename} has tab characters on lines #{failing_lines.join(", ")}"
30   end
32   def check_for_extra_spaces(filename)
33     failing_lines = []
34     each_line(filename) do |line, number|
35       next if /^\s+#.*\s+\n$/.match?(line)
36       failing_lines << number + 1 if /\s+\n$/.match?(line)
37     end
39     return if failing_lines.empty?
40     "#{filename} has spaces on the EOL on lines #{failing_lines.join(", ")}"
41   end
43   def check_for_extraneous_quotes(filename)
44     failing_lines = []
45     each_line(filename) do |line, number|
46       failing_lines << number + 1 if /\u{2019}/.match?(line)
47     end
49     return if failing_lines.empty?
50     "#{filename} has an extraneous quote on lines #{failing_lines.join(", ")}"
51   end
53   def check_for_expendable_words(filename)
54     failing_line_message = []
55     useless_words = %w[
56       actually
57       basically
58       clearly
59       just
60       obviously
61       really
62       simply
63     ]
64     pattern = /\b#{Regexp.union(useless_words)}\b/i
66     each_line(filename) do |line, number|
67       next unless word_found = pattern.match(line)
68       failing_line_message << "#{filename}:#{number.succ} has '#{word_found}'. Avoid using these kinds of weak modifiers."
69     end
71     failing_line_message unless failing_line_message.empty?
72   end
74   def check_for_specific_pronouns(filename)
75     failing_line_message = []
76     specific_pronouns = /\b(he|she|his|hers|him|her|himself|herself)\b/i
78     each_line(filename) do |line, number|
79       next unless word_found = specific_pronouns.match(line)
80       failing_line_message << "#{filename}:#{number.succ} has '#{word_found}'. Use more generic pronouns in documentation."
81     end
83     failing_line_message unless failing_line_message.empty?
84   end
86   it "has no malformed whitespace" do
87     exempt = /\.gitmodules|fixtures|vendor|LICENSE|vcr_cassettes|rbreadline\.diff|index\.txt$/
88     error_messages = []
89     tracked_files.each do |filename|
90       next if filename&.match?(exempt)
91       error_messages << check_for_tab_characters(filename)
92       error_messages << check_for_extra_spaces(filename)
93     end
94     expect(error_messages.compact).to be_well_formed
95   end
97   it "has no extraneous quotes" do
98     exempt = /vendor|vcr_cassettes|LICENSE|rbreadline\.diff/
99     error_messages = []
100     tracked_files.each do |filename|
101       next if filename&.match?(exempt)
102       error_messages << check_for_extraneous_quotes(filename)
103     end
104     expect(error_messages.compact).to be_well_formed
105   end
107   it "does not include any unresolved merge conflicts" do
108     error_messages = []
109     exempt = %r{lock/lockfile_spec|quality_spec|vcr_cassettes|\.ronn|lockfile_parser\.rb}
110     tracked_files.each do |filename|
111       next if filename&.match?(exempt)
112       error_messages << check_for_git_merge_conflicts(filename)
113     end
114     expect(error_messages.compact).to be_well_formed
115   end
117   it "maintains language quality of the documentation" do
118     included = /ronn/
119     error_messages = []
120     man_tracked_files.each do |filename|
121       next unless filename&.match?(included)
122       error_messages << check_for_expendable_words(filename)
123       error_messages << check_for_specific_pronouns(filename)
124     end
125     expect(error_messages.compact).to be_well_formed
126   end
128   it "maintains language quality of sentences used in source code" do
129     error_messages = []
130     exempt = /vendor|vcr_cassettes|CODE_OF_CONDUCT/
131     lib_tracked_files.each do |filename|
132       next if filename&.match?(exempt)
133       error_messages << check_for_expendable_words(filename)
134       error_messages << check_for_specific_pronouns(filename)
135     end
136     expect(error_messages.compact).to be_well_formed
137   end
139   it "documents all used settings" do
140     exemptions = %w[
141       forget_cli_options
142       gem.changelog
143       gem.ci
144       gem.coc
145       gem.linter
146       gem.mit
147       gem.rubocop
148       gem.test
149       git.allow_insecure
150       inline
151       trust-policy
152     ]
154     all_settings = Hash.new {|h, k| h[k] = [] }
155     documented_settings = []
157     Bundler::Settings::BOOL_KEYS.each {|k| all_settings[k] << "in Bundler::Settings::BOOL_KEYS" }
158     Bundler::Settings::NUMBER_KEYS.each {|k| all_settings[k] << "in Bundler::Settings::NUMBER_KEYS" }
159     Bundler::Settings::ARRAY_KEYS.each {|k| all_settings[k] << "in Bundler::Settings::ARRAY_KEYS" }
160     Bundler::Settings::STRING_KEYS.each {|k| all_settings[k] << "in Bundler::Settings::STRING_KEYS" }
162     key_pattern = /([a-z\._-]+)/i
163     lib_tracked_files.each do |filename|
164       each_line(filename) do |line, number|
165         line.scan(/Bundler\.settings\[:#{key_pattern}\]/).flatten.each {|s| all_settings[s] << "referenced at `#{filename}:#{number.succ}`" }
166       end
167     end
168     documented_settings = File.read("lib/bundler/man/bundle-config.1.ronn")[/LIST OF AVAILABLE KEYS.*/m].scan(/^\* `#{key_pattern}`/).flatten
170     documented_settings.each do |s|
171       all_settings.delete(s)
172       expect(exemptions.delete(s)).to be_nil, "setting #{s} was exempted but was actually documented"
173     end
175     exemptions.each do |s|
176       expect(all_settings.delete(s)).to be_truthy, "setting #{s} was exempted but unused"
177     end
178     error_messages = all_settings.map do |setting, refs|
179       "The `#{setting}` setting is undocumented\n\t- #{refs.join("\n\t- ")}\n"
180     end
182     expect(error_messages.sort).to be_well_formed
184     expect(documented_settings).to be_sorted
185   end
187   it "can still be built" do
188     with_built_bundler do |_gem_path|
189       expect(err).to be_empty, "bundler should build as a gem without warnings, but\n#{err}"
190     end
191   end
193   it "ships the correct set of files" do
194     git_list = tracked_files.reject {|f| f.start_with?("spec/") }
196     gem_list = loaded_gemspec.files
197     gem_list.map! {|f| f.sub(%r{\Aexe/}, "libexec/") } if ruby_core?
199     expect(git_list).to match_array(gem_list)
200   end
202   it "does not contain any warnings" do
203     exclusions = %w[
204       lib/bundler/capistrano.rb
205       lib/bundler/deployment.rb
206       lib/bundler/gem_tasks.rb
207       lib/bundler/vlad.rb
208     ]
209     files_to_require = lib_tracked_files.grep(/\.rb$/) - exclusions
210     files_to_require.reject! {|f| f.start_with?("lib/bundler/vendor") }
211     files_to_require.map! {|f| File.expand_path(f, source_root) }
212     files_to_require.sort!
213     sys_exec("ruby -w") do |input, _, _|
214       files_to_require.each do |f|
215         input.puts "require '#{f}'"
216       end
217     end
219     warnings = last_command.stdboth.split("\n")
220     # ignore warnings around deprecated Object#=~ method in RubyGems
221     warnings.reject! {|w| w =~ %r{rubygems\/version.rb.*deprecated\ Object#=~} }
223     expect(warnings).to be_well_formed
224   end
226   it "does not use require internally, but require_relative" do
227     exempt = %r{templates/|\.5|\.1|vendor/}
228     all_bad_requires = []
229     lib_tracked_files.each do |filename|
230       next if filename&.match?(exempt)
231       each_line(filename) do |line, number|
232         line.scan(/^ *require "bundler/).each { all_bad_requires << "#{filename}:#{number.succ}" }
233       end
234     end
236     expect(all_bad_requires).to be_empty, "#{all_bad_requires.size} internal requires that should use `require_relative`: #{all_bad_requires}"
237   end
239   private
241   def each_line(filename, &block)
242     File.readlines(filename, encoding: "UTF-8").each_with_index(&block)
243   end