Skip to content
This repository was archived by the owner on Nov 30, 2024. It is now read-only.

Commit 80fcb24

Browse files
committed
Improve output from rspec -v.
Before this change, this confusing thing could happen: ``` $ gem install rspec -v 3.5.0 > /dev/null; rspec -v 3.5.1 ``` The user installed RSpec 3.5.0 but `rspec -v` prints 3.5.1. This happened because 3.5.0 of the rspec gem depends on 3.5.x of all the sub-gems, and 3.5.1 is the latest rspec-core. With this change, it now prints something like: ``` $ rspec -v RSpec 3.5 - rspec-core 3.5.1 - rspec-expectations 3.5.0 - rspec-mocks 3.5.0 - rspec-rails not installed - rspec-support 3.5.0 ```
1 parent f81ff4a commit 80fcb24

File tree

3 files changed

+58
-5
lines changed

3 files changed

+58
-5
lines changed

Changelog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ Enhancements:
88
* Improve metadata filtering so that it can match against any object
99
that implements `===` instead of treating regular expressions as
1010
special. (Myron Marston, #2294)
11+
* Improve `rspec -v` so that it prints out the versions of each part of
12+
RSpec to prevent confusion. (Myron Marston, #2304)
1113

1214
Bug Fixes:
1315

lib/rspec/core/invocations.rb

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,24 @@ def bisect_formatter_for(argument)
4949
# @private
5050
class PrintVersion
5151
def call(_options, _err, out)
52-
out.puts RSpec::Core::Version::STRING
52+
overall_version = RSpec::Core::Version::STRING
53+
unless overall_version =~ /[a-zA-Z]+/
54+
overall_version = overall_version.split('.').first(2).join('.')
55+
end
56+
57+
out.puts "RSpec #{overall_version}"
58+
59+
[:Core, :Expectations, :Mocks, :Rails, :Support].each do |const_name|
60+
lib_name = const_name.to_s.downcase
61+
begin
62+
require "rspec/#{lib_name}/version"
63+
rescue LoadError
64+
out.puts " - rspec-#{lib_name} not installed"
65+
else
66+
out.puts " - rspec-#{lib_name} #{RSpec.const_get(const_name)::Version::STRING}"
67+
end
68+
end
69+
5370
0
5471
end
5572
end

spec/rspec/core/invocations_spec.rb

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,12 +131,46 @@ def run_invocation
131131
end
132132

133133
describe Invocations::PrintVersion do
134-
it "prints the version and returns a zero exit code" do
134+
it "prints the major.minor version of RSpec as a whole" do
135+
stub_const("RSpec::Core::Version::STRING", "9.18.23")
136+
run_invocation
137+
expect(out.string).to include("RSpec 9.18\n")
138+
end
135139

136-
exit_code = run_invocation
140+
it "prints off the whole version if it's a pre-release" do
141+
stub_const("RSpec::Core::Version::STRING", "9.18.0-beta1")
142+
run_invocation
143+
expect(out.string).to include("RSpec 9.18.0-beta1\n")
144+
end
137145

138-
expect(exit_code).to eq(0)
139-
expect(out.string).to include("#{RSpec::Core::Version::STRING}\n")
146+
it "prints off the version of each part of RSpec" do
147+
[:Core, :Expectations, :Mocks, :Support].each_with_index do |const_name, index|
148+
# validate that this is an existing const
149+
expect(RSpec.const_get(const_name)::Version::STRING).to be_a String
150+
151+
stub_const("RSpec::#{const_name}::Version::STRING", "9.2.#{index}")
152+
end
153+
154+
run_invocation
155+
156+
expect(out.string).to include(
157+
"- rspec-core 9.2.0",
158+
"- rspec-expectations 9.2.1",
159+
"- rspec-mocks 9.2.2",
160+
"- rspec-support 9.2.3",
161+
)
162+
end
163+
164+
it "indicates a part is not installed if it cannot be loaded" do
165+
expect { require 'rspec/rails/version' }.to raise_error(LoadError)
166+
167+
run_invocation
168+
169+
expect(out.string).to include(" - rspec-rails not installed")
170+
end
171+
172+
it "returns a zero exit code" do
173+
expect(run_invocation).to eq 0
140174
end
141175
end
142176

0 commit comments

Comments
 (0)