Sync RubyGems

This commit is contained in:
David Rodríguez 2025-07-02 21:25:57 +02:00 committed by Hiroshi SHIBATA
parent f679202a0f
commit 81da38b308
14 changed files with 100 additions and 46 deletions

View file

@ -4,21 +4,26 @@ module Bundler
# Represents metadata from when the Bundler gem was built.
module BuildMetadata
# begin ivars
@release = false
@built_at = nil
# end ivars
# A hash representation of the build metadata.
def self.to_h
{
"Built At" => built_at,
"Timestamp" => timestamp,
"Git SHA" => git_commit_sha,
"Released Version" => release?,
}
end
# A timestamp representing the date the bundler gem was built, or the
# current time if never built
def self.timestamp
@timestamp ||= @built_at || Time.now.utc.strftime("%Y-%m-%d").freeze
end
# A string representing the date the bundler gem was built.
def self.built_at
@built_at ||= Time.now.utc.strftime("%Y-%m-%d").freeze
@built_at
end
# The SHA for the git commit the bundler gem was built from.
@ -34,10 +39,5 @@ module Bundler
@git_commit_sha ||= "unknown"
end
# Whether this is an official release build of Bundler.
def self.release?
@release
end
end
end

View file

@ -486,7 +486,7 @@ module Bundler
def version
cli_help = current_command.name == "cli_help"
if cli_help || ARGV.include?("version")
build_info = " (#{BuildMetadata.built_at} commit #{BuildMetadata.git_commit_sha})"
build_info = " (#{BuildMetadata.timestamp} commit #{BuildMetadata.git_commit_sha})"
end
if !cli_help && Bundler.feature_flag.bundler_4_mode?

6
spec/bin/bundle Executable file
View file

@ -0,0 +1,6 @@
#!/usr/bin/env ruby
# frozen_string_literal: true
require_relative "../bundler/support/activate"
load File.expand_path("bundle", Spec::Path.exedir)

View file

@ -6,18 +6,20 @@ require "bundler/build_metadata"
RSpec.describe Bundler::BuildMetadata do
before do
allow(Time).to receive(:now).and_return(Time.at(0))
Bundler::BuildMetadata.instance_variable_set(:@timestamp, nil)
end
describe "#timestamp" do
it "returns %Y-%m-%d formatted current time if built_at not set" do
Bundler::BuildMetadata.instance_variable_set(:@built_at, nil)
expect(Bundler::BuildMetadata.timestamp).to eq "1970-01-01"
end
describe "#built_at" do
it "returns %Y-%m-%d formatted time" do
expect(Bundler::BuildMetadata.built_at).to eq "1970-01-01"
end
end
describe "#release?" do
it "returns false as default" do
expect(Bundler::BuildMetadata.release?).to be_falsey
it "returns %Y-%m-%d formatted current time if built_at not set" do
Bundler::BuildMetadata.instance_variable_set(:@built_at, "2025-01-01")
expect(Bundler::BuildMetadata.timestamp).to eq "2025-01-01"
ensure
Bundler::BuildMetadata.instance_variable_set(:@built_at, nil)
end
end
@ -40,10 +42,9 @@ RSpec.describe Bundler::BuildMetadata do
describe "#to_h" do
subject { Bundler::BuildMetadata.to_h }
it "returns a hash includes Built At, Git SHA and Released Version" do
expect(subject["Built At"]).to eq "1970-01-01"
it "returns a hash includes Timestamp, and Git SHA" do
expect(subject["Timestamp"]).to eq "1970-01-01"
expect(subject["Git SHA"]).to be_instance_of(String)
expect(subject["Released Version"]).to be_falsey
end
end
end

View file

@ -423,7 +423,7 @@ RSpec.describe Bundler::SharedHelpers do
it "sets BUNDLE_BIN_PATH to the bundle executable file" do
subject.set_bundle_environment
bin_path = ENV["BUNDLE_BIN_PATH"]
expect(bin_path).to eq(bindir.join("bundle").to_s)
expect(bin_path).to eq(exedir.join("bundle").to_s)
expect(File.exist?(bin_path)).to be true
end
end

View file

@ -32,13 +32,34 @@ RSpec.describe "bundle version" do
end
context "with version" do
context "when released", :ruby_repo do
before do
system_gems "bundler-2.9.9", released: true
end
it "outputs the version, virtual version if set, and build metadata" do
bundle "version"
expect(out).to match(/\ABundler version #{Regexp.escape(Bundler::VERSION)} \(\d{4}-\d{2}-\d{2} commit #{COMMIT_HASH}\)\z/)
expect(out).to match(/\ABundler version 2\.9\.9 \(2100-01-01 commit #{COMMIT_HASH}\)\z/)
bundle "config simulate_version 4"
bundle "version"
expect(out).to match(/\A#{Regexp.escape(Bundler::VERSION)} \(simulating Bundler 4\) \(\d{4}-\d{2}-\d{2} commit #{COMMIT_HASH}\)\z/)
expect(out).to match(/\A2\.9\.9 \(simulating Bundler 4\) \(2100-01-01 commit #{COMMIT_HASH}\)\z/)
end
end
context "when not released" do
before do
system_gems "bundler-2.9.9", released: false
end
it "outputs the version, virtual version if set, and build metadata" do
bundle "version"
expect(out).to match(/\ABundler version 2\.9\.9 \(20\d{2}-\d{2}-\d{2} commit #{COMMIT_HASH}\)\z/)
bundle "config simulate_version 4"
bundle "version"
expect(out).to match(/\A2\.9\.9 \(simulating Bundler 4\) \(20\d{2}-\d{2}-\d{2} commit #{COMMIT_HASH}\)\z/)
end
end
end
end

View file

@ -141,8 +141,7 @@ RSpec.shared_examples "bundle install --standalone" do
describe "with default gems and a lockfile", :ruby_repo do
it "works and points to the vendored copies, not to the default copies" do
base_system_gems "psych", "etc", path: scoped_gem_path(bundled_app("bundle"))
base_system_gems "stringio", path: scoped_gem_path(bundled_app("bundle")) if Gem.ruby_version < Gem::Version.new("3.3.0.a") || Gem.rubygems_version < Gem::Version.new("3.6.0.a")
base_system_gems "stringio", "psych", "etc", path: scoped_gem_path(bundled_app("bundle"))
build_gem "foo", "1.0.0", to_system: true, default: true do |s|
s.add_dependency "bar"
@ -179,8 +178,7 @@ RSpec.shared_examples "bundle install --standalone" do
it "works for gems with extensions and points to the vendored copies, not to the default copies" do
simulate_platform "arm64-darwin-23" do
base_system_gems "psych", "etc", "shellwords", "open3", path: scoped_gem_path(bundled_app("bundle"))
base_system_gems "stringio", path: scoped_gem_path(bundled_app("bundle")) if Gem.ruby_version < Gem::Version.new("3.3.0.a") || Gem.rubygems_version < Gem::Version.new("3.6.0.a")
base_system_gems "stringio", "psych", "etc", "shellwords", "open3", path: scoped_gem_path(bundled_app("bundle"))
build_gem "baz", "1.0.0", to_system: true, default: true, &:add_c_extension

View file

@ -8,11 +8,10 @@ module Spec
include Spec::Path
include Spec::Helpers
def write_build_metadata(dir: source_root)
def write_build_metadata(dir: source_root, version: Bundler::VERSION)
build_metadata = {
git_commit_sha: git_commit_sha,
built_at: loaded_gemspec.date.utc.strftime("%Y-%m-%d"),
release: true,
built_at: release_date_for(version, dir: dir),
}
replace_build_metadata(build_metadata, dir: dir)
@ -20,7 +19,7 @@ module Spec
def reset_build_metadata(dir: source_root)
build_metadata = {
release: false,
built_at: nil,
}
replace_build_metadata(build_metadata, dir: dir)
@ -44,6 +43,11 @@ module Spec
ruby_core_tarball? ? "unknown" : git("rev-parse --short HEAD", source_root).strip
end
def release_date_for(version, dir:)
changelog = File.expand_path("CHANGELOG.md", dir)
File.readlines(changelog)[2].scan(/^## #{Regexp.escape(version)} \((.*)\)/).first&.first if File.exist?(changelog)
end
extend self
end
end

View file

@ -450,9 +450,10 @@ module Spec
end
@context.replace_version_file(@version, dir: build_path)
@context.replace_changelog(@version, dir: build_path) if options[:released]
@context.replace_required_ruby_version(@required_ruby_version, dir: build_path) if @required_ruby_version
Spec::BuildMetadata.write_build_metadata(dir: build_path)
Spec::BuildMetadata.write_build_metadata(dir: build_path, version: @version)
@context.gem_command "build #{@context.relative_gemspec}", dir: build_path

6
spec/bundler/support/bundle Executable file
View file

@ -0,0 +1,6 @@
#!/usr/bin/env ruby
# frozen_string_literal: true
require_relative "../bundler/support/activate"
load File.expand_path("bundle", Spec::Path.exedir)

View file

@ -1,5 +1,7 @@
# frozen_string_literal: true
require_relative "activate"
require_relative "path"
load File.expand_path("bundle", Spec::Path.bindir)
warn "#{__FILE__} is deprecated. Please use #{Spec::Path.dev_binstub} instead"
load Spec::Path.dev_binstub

View file

@ -316,7 +316,7 @@ module Spec
gem_name = g.to_s
if gem_name.start_with?("bundler")
version = gem_name.match(/\Abundler-(?<version>.*)\z/)[:version] if gem_name != "bundler"
with_built_bundler(version) {|gem_path| install_gem(gem_path, install_dir, default) }
with_built_bundler(version, released: options.fetch(:released, false)) {|gem_path| install_gem(gem_path, install_dir, default) }
elsif %r{\A(?:[a-zA-Z]:)?/.*\.gem\z}.match?(gem_name)
install_gem(gem_name, install_dir, default)
else
@ -341,10 +341,10 @@ module Spec
gem_command "install #{args} '#{path}'"
end
def with_built_bundler(version = nil, &block)
def with_built_bundler(version = nil, opts = {}, &block)
require_relative "builders"
Builders::BundlerBuilder.new(self, "bundler", version)._build(&block)
Builders::BundlerBuilder.new(self, "bundler", version)._build(opts, &block)
end
def with_gem_path_as(path)

View file

@ -45,8 +45,16 @@ module Spec
@dev_gemfile ||= tool_dir.join("dev_gems.rb")
end
def dev_binstub
@dev_binstub ||= bindir.join("bundle")
end
def bindir
@bindir ||= source_root.join(ruby_core? ? "libexec" : "exe")
@bindir ||= source_root.join(ruby_core? ? "spec/bin" : "bin")
end
def exedir
@exedir ||= source_root.join(ruby_core? ? "libexec" : "exe")
end
def installed_bindir
@ -63,7 +71,7 @@ module Spec
def path
env_path = ENV["PATH"]
env_path = env_path.split(File::PATH_SEPARATOR).reject {|path| path == bindir.to_s }.join(File::PATH_SEPARATOR) if ruby_core?
env_path = env_path.split(File::PATH_SEPARATOR).reject {|path| path == exedir.to_s }.join(File::PATH_SEPARATOR) if ruby_core?
env_path
end
@ -280,6 +288,13 @@ module Spec
File.open(gemspec_file, "w") {|f| f << contents }
end
def replace_changelog(version, dir:)
changelog = File.expand_path("CHANGELOG.md", dir)
contents = File.readlines(changelog)
contents = [contents[0], contents[1], "## #{version} (2100-01-01)\n", *contents[3..-1]].join
File.open(changelog, "w") {|f| f << contents }
end
def git_root
ruby_core? ? source_root : source_root.parent
end

View file

@ -52,7 +52,7 @@ module Spec
def setup_test_paths
ENV["BUNDLE_PATH"] = nil
ENV["PATH"] = [Path.system_gem_path("bin"), ENV["PATH"]].join(File::PATH_SEPARATOR)
ENV["PATH"] = [Path.bindir, ENV["PATH"]].join(File::PATH_SEPARATOR) if Path.ruby_core?
ENV["PATH"] = [Path.exedir, ENV["PATH"]].join(File::PATH_SEPARATOR) if Path.ruby_core?
end
def install_test_deps
@ -100,7 +100,7 @@ module Spec
require "shellwords"
# We don't use `Open3` here because it does not work on JRuby + Windows
output = `ruby #{File.expand_path("support/bundle.rb", Path.spec_dir)} #{args.shelljoin}`
output = `ruby #{Path.dev_binstub} #{args.shelljoin}`
raise output unless $?.success?
output
ensure