From 966890d99f76e4538e27325aa14bc1a34746ebab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADt=20Ondruch?= Date: Thu, 17 Sep 2020 16:09:54 +0200 Subject: [PATCH 1/2] Change directory to the path specified by -C option. The directory specified by `-C` option must be reflected, while the already loaded spec file must be used, wherever it came from. Fixes #3953 --- lib/rubygems/commands/build_command.rb | 3 +- .../test_gem_commands_build_command.rb | 41 +++++++++++++++++++ 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/lib/rubygems/commands/build_command.rb b/lib/rubygems/commands/build_command.rb index eaf8573d8fa8..790ff09e267c 100644 --- a/lib/rubygems/commands/build_command.rb +++ b/lib/rubygems/commands/build_command.rb @@ -85,8 +85,7 @@ def build_gem(gem_name) spec = Gem::Specification.load(gemspec) if options[:build_path] - Dir.chdir(File.dirname(gemspec)) do - spec = Gem::Specification.load(File.basename(gemspec)) + Dir.chdir(options[:build_path]) do build_package(spec) end else diff --git a/test/rubygems/test_gem_commands_build_command.rb b/test/rubygems/test_gem_commands_build_command.rb index 37c3c120a52c..2ef7b06fdbf1 100644 --- a/test/rubygems/test_gem_commands_build_command.rb +++ b/test/rubygems/test_gem_commands_build_command.rb @@ -272,6 +272,47 @@ def test_execute_outside_dir assert_equal "this is a summary", spec.summary end + def test_execute_outside_dir_with_external_gemspec + gemspec_dir = File.join @tempdir, 'gemspec_dir' + gemspec_file = File.join gemspec_dir, @gem.spec_name + + gemcode_dir = File.join @tempdir, 'build_command_gem' + readme_file = File.join gemcode_dir, 'README.md' + + FileUtils.mkdir_p gemspec_dir + FileUtils.mkdir_p gemcode_dir + + File.open readme_file, 'w' do |f| + f.write "My awesome gem in nested directory" + end + + File.open gemspec_file, 'w' do |gs| + gs.write @gem.to_ruby + end + + @cmd.options[:build_path] = gemcode_dir + @cmd.options[:args] = [gemspec_file] + + use_ui @ui do + @cmd.execute + end + + output = @ui.output.split "\n" + assert_equal " Successfully built RubyGem", output.shift + assert_equal " Name: some_gem", output.shift + assert_equal " Version: 2", output.shift + assert_equal " File: some_gem-2.gem", output.shift + assert_equal [], output + + gem_file = File.join gemcode_dir, File.basename(@gem.cache_file) + assert File.exist?(gem_file) + + spec = Gem::Package.new(gem_file).spec + + assert_equal "some_gem", spec.name + assert_equal "this is a summary", spec.summary + end + def test_can_find_gemspecs_without_dot_gemspec gemspec_file = File.join(@tempdir, @gem.spec_name) From a9fd7fcab63e386400d0ae9ae28b411d3c65f1c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADt=20Ondruch?= Date: Fri, 2 Oct 2020 14:15:14 +0200 Subject: [PATCH 2/2] Clarify `gem build -C` option help message. --- lib/rubygems/commands/build_command.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/rubygems/commands/build_command.rb b/lib/rubygems/commands/build_command.rb index 790ff09e267c..111f2326a00e 100644 --- a/lib/rubygems/commands/build_command.rb +++ b/lib/rubygems/commands/build_command.rb @@ -23,7 +23,9 @@ def initialize options[:output] = value end - add_option '-C PATH', '', 'Run as if gem build was started in instead of the current working directory.' do |value, options| + add_option '-C PATH', '', 'Look for the code of the gem to be built', + 'in . The GEMSPEC_FILE given as an', + 'argument is not influenced by this option' do |value, options| options[:build_path] = value end end