diff options
author | Matt Brictson <[email protected]> | 2024-08-22 17:29:01 -0700 |
---|---|---|
committer | git <[email protected]> | 2024-08-26 14:56:26 +0000 |
commit | 7c794c287ec727f4634579556228cc018056e6f9 (patch) | |
tree | 44e02c33f9087fc995046644d326785d9ea413fd | |
parent | 20664826840f1ae1ba83c97764996a71105212e5 (diff) |
[rubygems/rubygems] Emit progress to stderr when `--print` is passed to `bundle lock`
`bundle lock --print --update` can take a long time to fetch sources and
resolve the lock file.
Before, `--print` caused output to be completely silenced, so nothing
was printed at all until the resolved lock file is finally emitted to
stdout.
With this change, `--print` now prints progress to stderr. E.g.:
```
Fetching gem metadata from https://rubygems.org/.........
Resolving dependencies...
```
This provides a better user experience, especially when
`lock --print --update` takes several seconds or more.
The lock file is still printed to stdout, so tools consuming the lock
file on stdout will not be affected.
https://github.com/rubygems/rubygems/commit/6719baa700
-rw-r--r-- | lib/bundler/cli/lock.rb | 6 | ||||
-rw-r--r-- | lib/bundler/ui/shell.rb | 10 | ||||
-rw-r--r-- | lib/bundler/ui/silent.rb | 7 | ||||
-rw-r--r-- | spec/bundler/bundler/ui/shell_spec.rb | 21 | ||||
-rw-r--r-- | spec/bundler/commands/lock_spec.rb | 19 |
5 files changed, 60 insertions, 3 deletions
diff --git a/lib/bundler/cli/lock.rb b/lib/bundler/cli/lock.rb index dac3d2a09a..86c49df867 100644 --- a/lib/bundler/cli/lock.rb +++ b/lib/bundler/cli/lock.rb @@ -15,8 +15,8 @@ module Bundler end print = options[:print] - previous_ui_level = Bundler.ui.level - Bundler.ui.level = "silent" if print + previous_output_stream = Bundler.ui.output_stream + Bundler.ui.output_stream = :stderr if print Bundler::Fetcher.disable_endpoint = options["full-index"] @@ -68,7 +68,7 @@ module Bundler end end - Bundler.ui.level = previous_ui_level + Bundler.ui.output_stream = previous_output_stream end end end diff --git a/lib/bundler/ui/shell.rb b/lib/bundler/ui/shell.rb index 16e7cc70b5..32367a44f0 100644 --- a/lib/bundler/ui/shell.rb +++ b/lib/bundler/ui/shell.rb @@ -6,14 +6,17 @@ module Bundler module UI class Shell LEVELS = %w[silent error warn confirm info debug].freeze + OUTPUT_STREAMS = [:stdout, :stderr].freeze attr_writer :shell + attr_reader :output_stream def initialize(options = {}) Thor::Base.shell = options["no-color"] ? Thor::Shell::Basic : nil @shell = Thor::Base.shell.new @level = ENV["DEBUG"] ? "debug" : "info" @warning_history = [] + @output_stream = :stdout end def add_color(string, *color) @@ -101,6 +104,11 @@ module Bundler index <= LEVELS.index(@level) end + def output_stream=(symbol) + raise ArgumentError unless OUTPUT_STREAMS.include?(symbol) + @output_stream = symbol + end + def trace(e, newline = nil, force = false) return unless debug? || force msg = "#{e.class}: #{e.message}\n#{e.backtrace.join("\n ")}" @@ -119,6 +127,8 @@ module Bundler # valimism def tell_me(msg, color = nil, newline = nil) + return tell_err(msg, color, newline) if output_stream == :stderr + msg = word_wrap(msg) if newline.is_a?(Hash) && newline[:wrap] if newline.nil? @shell.say(msg, color) diff --git a/lib/bundler/ui/silent.rb b/lib/bundler/ui/silent.rb index 104b3d993d..9ca2a8b485 100644 --- a/lib/bundler/ui/silent.rb +++ b/lib/bundler/ui/silent.rb @@ -53,6 +53,13 @@ module Bundler false end + def output_stream=(_symbol) + end + + def output_stream + nil + end + def ask(message) end diff --git a/spec/bundler/bundler/ui/shell_spec.rb b/spec/bundler/bundler/ui/shell_spec.rb index e1340e5923..422c850a65 100644 --- a/spec/bundler/bundler/ui/shell_spec.rb +++ b/spec/bundler/bundler/ui/shell_spec.rb @@ -10,6 +10,13 @@ RSpec.describe Bundler::UI::Shell do it "prints to stdout" do expect { subject.info("info") }.to output("info\n").to_stdout end + + context "when output_stream is :stderr" do + before { subject.output_stream = :stderr } + it "prints to stderr" do + expect { subject.info("info") }.to output("info\n").to_stderr + end + end end describe "#confirm" do @@ -17,6 +24,13 @@ RSpec.describe Bundler::UI::Shell do it "prints to stdout" do expect { subject.confirm("confirm") }.to output("confirm\n").to_stdout end + + context "when output_stream is :stderr" do + before { subject.output_stream = :stderr } + it "prints to stderr" do + expect { subject.confirm("confirm") }.to output("confirm\n").to_stderr + end + end end describe "#warn" do @@ -33,6 +47,13 @@ RSpec.describe Bundler::UI::Shell do it "prints to stdout" do expect { subject.debug("debug") }.to output("debug\n").to_stdout end + + context "when output_stream is :stderr" do + before { subject.output_stream = :stderr } + it "prints to stderr" do + expect { subject.debug("debug") }.to output("debug\n").to_stderr + end + end end describe "#error" do diff --git a/spec/bundler/commands/lock_spec.rb b/spec/bundler/commands/lock_spec.rb index 6d005cfc96..706eaed533 100644 --- a/spec/bundler/commands/lock_spec.rb +++ b/spec/bundler/commands/lock_spec.rb @@ -202,6 +202,25 @@ RSpec.describe "bundle lock" do expect(read_lockfile).to eq(expected_lockfile) end + it "prints an updated lockfile when there is an outdated lockfile using --print --update" do + lockfile outdated_lockfile + + bundle "lock --print --update" + + expect(out).to eq(expected_lockfile.rstrip) + end + + it "emits info messages to stderr when updating an outdated lockfile using --print --update" do + lockfile outdated_lockfile + + bundle "lock --print --update" + + expect(err).to eq(<<~STDERR.rstrip) + Fetching gem metadata from https://gem.repo4/... + Resolving dependencies... + STDERR + end + it "writes a lockfile when there is an outdated lockfile and bundle is frozen" do lockfile outdated_lockfile |