Skip to content

Commit

Permalink
More robustly detect cargo metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
ianks committed Apr 18, 2024
1 parent 1234c63 commit 5664daf
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 3 deletions.
10 changes: 8 additions & 2 deletions gem/exe/rb-sys-dock
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@ OPTIONS = {
}

def cargo_metadata
@cargo_metadata ||= RbSys::Cargo::Metadata.new("rb-sys-dock", deps: true)
return @cargo_metadata if defined?(@cargo_metadata)

@cargo_metadata = begin
RbSys::Cargo::Metadata.inferred(deps: true)
rescue
nil
end
end

def logger
Expand Down Expand Up @@ -421,7 +427,7 @@ def set_env
end

def lint_rb_sys
cargo_version = cargo_metadata.rb_sys_version
cargo_version = cargo_metadata&.rb_sys_version || RbSys::VERSION
return if cargo_version == RbSys::VERSION
logger.warn("Cargo rb-sys version (#{cargo_version}) does not match Ruby gem version (#{RbSys::VERSION})")
rescue => e
Expand Down
30 changes: 30 additions & 0 deletions gem/lib/rb_sys/cargo/metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,26 @@ module Cargo
class Metadata
attr_reader :name

class << self
# Infers the Cargo project's name from the Cargo.toml file.
#
# @return [RbSys::Cargo::Metadata]
def inferred(deps: false)
new(File.read("Cargo.toml").match(/^name = "(.*)"/)[1], deps: deps)
rescue
new(File.basename(Dir.pwd), deps: deps)
end

# Initializes a new Cargo::Metadata instance or infers the Cargo project's name.
#
# @param name [String] the name of the Cargo project
def new_or_inferred(name, deps: false)
new(name, deps: deps).load!
rescue CargoMetadataError
inferred
end
end

# Initializes a new Cargo::Metadata instance.
#
# @param name [String] the name of the Cargo project
Expand Down Expand Up @@ -92,12 +112,22 @@ def metadata
end

# Returns the rb-sys version, if any.
#
# @return [String]
def rb_sys_version
pkg = packages.find { |p| p.fetch("name") == "rb-sys" }
return unless pkg
pkg["version"]
end

# Eagerly run `cargo metadata`, raising a RbSys::CargoCargoMetadataError` if it fails.
#
# @return [RbSys::Cargo::Metadata]
def load!
cargo_metadata
self
end

private

def package_metadata
Expand Down
2 changes: 1 addition & 1 deletion gem/lib/rb_sys/extensiontask.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def define
end

def cargo_metadata
@cargo_metadata ||= Cargo::Metadata.new(@name)
@cargo_metadata ||= Cargo::Metadata.new_or_inferred(@name)
end

def extconf
Expand Down
67 changes: 67 additions & 0 deletions gem/test/test_cargo_metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,73 @@ def test_fails_when_cargo_metadata_fails
end
end

def test_load_raises_error_when_cargo_metadata_fails
skip if win_target?

Dir.mktmpdir do |dir|
Dir.chdir(dir) do
assert_raises(RbSys::CargoMetadataError) do
metadata = RbSys::Cargo::Metadata.new("foo")
metadata.load!
end
end
end
end

def test_load_returns_self
skip if win_target?

in_new_crate("foo") do |dir|
metadata = RbSys::Cargo::Metadata.new("foo")

assert_equal metadata, metadata.load!
end
end

def test_inferred_returns_valid_info
skip if win_target?

in_new_crate("fooyoo") do |dir|
metadata = RbSys::Cargo::Metadata.inferred

assert_equal "fooyoo", metadata.name
end
end

def test_inferred_falls_back_to_dir_name
skip if win_target?

Dir.mktmpdir do |dir|
Dir.chdir(dir) do
metadata = RbSys::Cargo::Metadata.inferred

assert_equal File.basename(dir), metadata.name
end
end
end

def test_new_or_inferred_returns_valid_info
skip if win_target?

in_new_crate("foo") do |dir|
metadata = RbSys::Cargo::Metadata.new_or_inferred("foofer")

assert_equal "foofer", metadata.name
end
end

def test_new_or_inferred_falls_back_to_inferred
skip if win_target?

Dir.mktmpdir do |dir|
Dir.chdir(dir) do
metadata = RbSys::Cargo::Metadata.new_or_inferred("foofer")

assert_equal File.basename(dir), metadata.name
end
end
end

private

def in_new_crate(name, &blk)
Expand Down

0 comments on commit 5664daf

Please sign in to comment.