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