Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove usage of Dir.chdir that only execute a subprocess #6930

Merged
merged 1 commit into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion bundler/lib/bundler/build_metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def self.git_commit_sha
# commit instance variable then we can't determine its commits SHA.
git_dir = File.expand_path("../../../.git", __dir__)
if File.directory?(git_dir)
return @git_commit_sha = Dir.chdir(git_dir) { `git rev-parse --short HEAD`.strip.freeze }
return @git_commit_sha = IO.popen(%w[git rev-parse --short HEAD], { :chdir => git_dir }, &:read).strip.freeze
end

@git_commit_sha ||= "unknown"
Expand Down
4 changes: 1 addition & 3 deletions bundler/lib/bundler/cli/gem.rb
Original file line number Diff line number Diff line change
Expand Up @@ -233,9 +233,7 @@ def run
end

if use_git
Dir.chdir(target) do
`git add .`
end
IO.popen(%w[git add .], { :chdir => target }, &:read)
end

# Open gemspec in editor
Expand Down
12 changes: 5 additions & 7 deletions bundler/lib/bundler/cli/open.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,11 @@ def run
Bundler.ui.info "Unable to open #{name} because it's a default gem, so the directory it would normally be installed to does not exist."
else
root_path = spec.full_gem_path
Dir.chdir(root_path) do
require "shellwords"
command = Shellwords.split(editor) << File.join([root_path, path].compact)
Bundler.with_original_env do
system(*command)
end || Bundler.ui.info("Could not run '#{command.join(" ")}'")
end
require "shellwords"
command = Shellwords.split(editor) << File.join([root_path, path].compact)
Bundler.with_original_env do
system(*command, { :chdir => root_path })
end || Bundler.ui.info("Could not run '#{command.join(" ")}'")
end
end
end
Expand Down
4 changes: 1 addition & 3 deletions lib/rubygems/commands/open_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,7 @@ def open_gem(name)
end

def open_editor(path)
Dir.chdir(path) do
system(*@editor.split(/\s+/) + [path])
end
system(*@editor.split(/\s+/) + [path], { :chdir => path })
end

def spec_for(name)
Expand Down
7 changes: 5 additions & 2 deletions test/rubygems/test_gem_commands_open_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,23 @@ def gem(name, version = "1.0")

def test_execute
@cmd.options[:args] = %w[foo]
@cmd.options[:editor] = "#{ruby_with_rubygems_in_load_path} -eexit --"
@cmd.options[:editor] = (ruby_with_rubygems_in_load_path + ["-e", "puts(ARGV,Dir.pwd)", "--"]).join(" ")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ShellWords.shelljoin maybe? Not a big deal.


gem "foo", "1.0.0"
spec = gem "foo", "1.0.1"

assert_nothing_raised Gem::MockGemUi::TermError do
Dir.stub(:chdir, spec.full_gem_path) do
stdout, stderr = capture_subprocess_io do
use_ui @ui do
@cmd.execute
end
end
assert_equal [spec.full_gem_path, spec.full_gem_path], stdout.split("\n")
assert_equal "", stderr
end

assert_equal "", @ui.error
assert_equal "", @ui.output
end

def test_wrong_version
Expand Down