ruby/spec/bundler/quality_spec.rb
Go Sueyoshi 4423facbff [ruby/rubygems] Add --ext=go to bundle gem
(https://github.com/ruby/rubygems/pull/8183)

* Add new gem templates

* Add `--ext=go` in `bundle gem`

* Add setup-go to .github/workflows/main.yml

* Embed go version in go.mod

* Use go in bundler CI

* Add example method to template

* Install Go in .circleci/config.yml

* Install Go in .gitlab-ci.yml

* Allow hard tabs in go template

* Run `rake update_manifest`

* Fix test

* Move go_gem to gemspec

Respect to 9b0ec80

* nits: 

* includes valid module name in go.mod

* generate header file

* Run `go mod tidy` to create `go.sum`

* Check if `go.sum` is generated only when Go is installed

To avoid test failure in environments where Go is not installed

* Run CI

* Workaround for hung up

c.f. https://github.com/rubygems/rubygems/actions/runs/11639408044/job/32415545422

* Write man for --ext=go

* Re-generate man with `./bin/rake man:build`

* pinning 📌

* Update with `./bin/rake man:build`

* nits: Extract to method

* nits: Use `sys_exec` instead of `system`

* Clean go module cache after test

Workaround following error

```
1) bundle gem gem naming with underscore --ext parameter set with go includes go_gem extension in extconf.rb
   Failure/Error: FileUtils.rm_r(dir)

   Errno::EACCES:
     Permission denied @ apply2files - /home/runner/work/rubygems/rubygems/bundler/tmp/2.2/home/go/pkg/mod/gopkg.in/yaml.v3@v3.0.1/decode_test.go
   # ./spec/support/helpers.rb:37:in `block in reset!'
   # ./spec/support/helpers.rb:21:in `each'
   # ./spec/support/helpers.rb:21:in `reset!'
   # ./spec/spec_helper.rb:130:in `block (2 levels) in <top (required)>'
   # /home/runner/work/rubygems/rubygems/lib/rubygems.rb:303:in `load'
   # /home/runner/work/rubygems/rubygems/lib/rubygems.rb:303:in `activate_and_load_bin_path'
```

Files installed with `go get` have permissions set to 444
ref. https://github.com/golang/go/issues/35615

```
$ ls -l /home/runner/work/rubygems/rubygems/bundler/tmp/2.2/home/go/pkg/mod/gopkg.in/yaml.v3@v3.0.1/decode_test.go
-r--r--r-- 1 runner runner 42320 Nov 15 06:38 /home/runner/work/rubygems/rubygems/bundler/tmp/2.2/home/go/pkg/mod/gopkg.in/yaml.v3@v3.0.1/decode_test.go
```

So they cannot be deleted by `FileUtils.rm_r`.
Therefore, this is necessary to execute `go clean -modcache` separately from `FileUtils.rm_r` to circumvent it.

* Remove needless changes

ref. https://github.com/ruby/rubygems/pull/8183#discussion_r2532902051

* ci: setup-go is needless

* Don't run go command in `bundle gem`

ref. https://github.com/ruby/rubygems/pull/8183#discussion_r2532765470

* Revert unrelated date changes

---------

https://github.com/ruby/rubygems/commit/260d7d60b3

Co-authored-by: Hiroshi SHIBATA <hsbt@ruby-lang.org>
2025-11-19 00:47:07 +00:00

262 lines
8.5 KiB
Ruby

# frozen_string_literal: true
require "set"
RSpec.describe "The library itself" do
def check_for_git_merge_conflicts(filename)
merge_conflicts_regex = /
<<<<<<<|
=======|
>>>>>>>
/x
failing_lines = []
each_line(filename) do |line, number|
failing_lines << number + 1 if line&.match?(merge_conflicts_regex)
end
return if failing_lines.empty?
"#{filename} has unresolved git merge conflicts on lines #{failing_lines.join(", ")}"
end
def check_for_tab_characters(filename)
# Because Go uses hard tabs
return if filename.end_with?(".go.tt")
failing_lines = []
each_line(filename) do |line, number|
failing_lines << number + 1 if line.include?("\t")
end
return if failing_lines.empty?
"#{filename} has tab characters on lines #{failing_lines.join(", ")}"
end
def check_for_extra_spaces(filename)
failing_lines = []
each_line(filename) do |line, number|
next if /^\s+#.*\s+\n$/.match?(line)
failing_lines << number + 1 if /\s+\n$/.match?(line)
end
return if failing_lines.empty?
"#{filename} has spaces on the EOL on lines #{failing_lines.join(", ")}"
end
def check_for_extraneous_quotes(filename)
failing_lines = []
each_line(filename) do |line, number|
failing_lines << number + 1 if /\u{2019}/.match?(line)
end
return if failing_lines.empty?
"#{filename} has an extraneous quote on lines #{failing_lines.join(", ")}"
end
def check_for_expendable_words(filename)
failing_line_message = []
useless_words = %w[
actually
basically
clearly
just
obviously
really
simply
]
pattern = /\b#{Regexp.union(useless_words)}\b/i
each_line(filename) do |line, number|
next unless word_found = pattern.match(line)
failing_line_message << "#{filename}:#{number.succ} has '#{word_found}'. Avoid using these kinds of weak modifiers."
end
failing_line_message unless failing_line_message.empty?
end
def check_for_specific_pronouns(filename)
failing_line_message = []
specific_pronouns = /\b(he|she|his|hers|him|her|himself|herself)\b/i
each_line(filename) do |line, number|
next unless word_found = specific_pronouns.match(line)
failing_line_message << "#{filename}:#{number.succ} has '#{word_found}'. Use more generic pronouns in documentation."
end
failing_line_message unless failing_line_message.empty?
end
it "has no malformed whitespace" do
exempt = /\.gitmodules|fixtures|vendor|LICENSE|vcr_cassettes|rbreadline\.diff|index\.txt$/
error_messages = []
tracked_files.each do |filename|
next if filename&.match?(exempt)
error_messages << check_for_tab_characters(filename)
error_messages << check_for_extra_spaces(filename)
end
expect(error_messages.compact).to be_well_formed
end
it "has no extraneous quotes" do
exempt = /vendor|vcr_cassettes|LICENSE|rbreadline\.diff/
error_messages = []
tracked_files.each do |filename|
next if filename&.match?(exempt)
error_messages << check_for_extraneous_quotes(filename)
end
expect(error_messages.compact).to be_well_formed
end
it "does not include any unresolved merge conflicts" do
error_messages = []
exempt = %r{lock/lockfile_spec|quality_spec|vcr_cassettes|\.ronn|lockfile_parser\.rb}
tracked_files.each do |filename|
next if filename&.match?(exempt)
error_messages << check_for_git_merge_conflicts(filename)
end
expect(error_messages.compact).to be_well_formed
end
it "maintains language quality of the documentation" do
error_messages = []
man_tracked_files.each do |filename|
error_messages << check_for_expendable_words(filename)
error_messages << check_for_specific_pronouns(filename)
end
expect(error_messages.compact).to be_well_formed
end
it "maintains language quality of sentences used in source code" do
error_messages = []
exempt = /vendor|vcr_cassettes|CODE_OF_CONDUCT/
lib_tracked_files.each do |filename|
next if filename&.match?(exempt)
error_messages << check_for_expendable_words(filename)
error_messages << check_for_specific_pronouns(filename)
end
expect(error_messages.compact).to be_well_formed
end
it "documents all used settings" do
exemptions = %w[
gem.changelog
gem.ci
gem.coc
gem.linter
gem.mit
gem.bundle
gem.rubocop
gem.test
git.allow_insecure
inline
trust-policy
]
all_settings = Hash.new {|h, k| h[k] = [] }
documented_settings = []
Bundler::Settings::BOOL_KEYS.each {|k| all_settings[k] << "in Bundler::Settings::BOOL_KEYS" }
Bundler::Settings::NUMBER_KEYS.each {|k| all_settings[k] << "in Bundler::Settings::NUMBER_KEYS" }
Bundler::Settings::ARRAY_KEYS.each {|k| all_settings[k] << "in Bundler::Settings::ARRAY_KEYS" }
Bundler::Settings::STRING_KEYS.each {|k| all_settings[k] << "in Bundler::Settings::STRING_KEYS" }
key_pattern = /([a-z\._-]+)/i
lib_tracked_files.each do |filename|
each_line(filename) do |line, number|
line.scan(/Bundler\.settings\[:#{key_pattern}\]/).flatten.each {|s| all_settings[s] << "referenced at `#{filename}:#{number.succ}`" }
end
end
settings_section = File.read("lib/bundler/man/bundle-config.1.ronn").split(/^## /).find {|section| section.start_with?("LIST OF AVAILABLE KEYS") }
documented_settings = settings_section.scan(/^\* `#{key_pattern}`/).flatten
documented_settings.each do |s|
all_settings.delete(s)
expect(exemptions.delete(s)).to be_nil, "setting #{s} was exempted but was actually documented"
end
exemptions.each do |s|
expect(all_settings.delete(s)).to be_truthy, "setting #{s} was exempted but unused"
end
error_messages = all_settings.map do |setting, refs|
"The `#{setting}` setting is undocumented\n\t- #{refs.join("\n\t- ")}\n"
end
expect(error_messages.sort).to be_well_formed
expect(documented_settings).to be_sorted
end
it "can still be built" do
with_built_bundler do |gem_path|
expect(File.exist?(gem_path)).to be true
end
end
it "ships the correct set of files" do
git_list = tracked_files.reject {|f| f.start_with?("spec/") }
gem_list = loaded_gemspec.files
gem_list.map! {|f| f.sub(%r{\Aexe/}, "libexec/") } if ruby_core?
expect(git_list).to match_array(gem_list)
end
it "does not contain any warnings" do
exclusions = %w[
lib/bundler/capistrano.rb
lib/bundler/deployment.rb
lib/bundler/gem_tasks.rb
lib/bundler/vlad.rb
]
files_to_require = lib_tracked_files.grep(/\.rb$/) - exclusions
files_to_require.reject! {|f| f.start_with?("lib/bundler/vendor") }
files_to_require.map! {|f| File.expand_path(f, source_root) }
files_to_require.sort!
sys_exec("ruby -w") do |input, _, _|
files_to_require.each do |f|
input.puts "require '#{f}'"
end
end
warnings = stdboth.split("\n")
# ignore warnings around deprecated Object#=~ method in RubyGems
warnings.reject! {|w| w =~ %r{rubygems\/version.rb.*deprecated\ Object#=~} }
expect(warnings).to be_well_formed
end
it "does not use require internally, but require_relative" do
exempt = %r{templates/|\.5|\.1|vendor/}
all_bad_requires = []
lib_tracked_files.each do |filename|
next if filename&.match?(exempt)
each_line(filename) do |line, number|
line.scan(/^ *require "bundler/).each { all_bad_requires << "#{filename}:#{number.succ}" }
end
end
expect(all_bad_requires).to be_empty, "#{all_bad_requires.size} internal requires that should use `require_relative`: #{all_bad_requires}"
end
# We don't want our artifice code to activate bundler, but it needs to use the
# namespaced implementation of `Net::HTTP`. So we duplicate the file in
# bundler that loads that.
it "keeps vendored_net_http spec code in sync with the lib implementation" do
lib_implementation_path = File.join(source_lib_dir, "bundler", "vendored_net_http.rb")
expect(File.exist?(lib_implementation_path)).to be_truthy
lib_code = File.read(lib_implementation_path)
spec_implementation_path = File.join(spec_dir, "support", "vendored_net_http.rb")
expect(File.exist?(spec_implementation_path)).to be_truthy
spec_code = File.read(spec_implementation_path)
expect(lib_code).to eq(spec_code)
end
private
def each_line(filename, &block)
File.readlines(filename, encoding: "UTF-8").each_with_index(&block)
end
end