Skip to content

Commit

Permalink
Squashed 'spec/mspec/' changes from 7a19a59..f509c1a
Browse files Browse the repository at this point in the history
f509c1a Deprecate extended_on.
3c64ffd Avoid splat calls to fail_with as it could produce a confusing ArgumentError
69d0e88 failure_message and negative_failure_message are expected to return 2-elements arrays
6f47969 Show more details when have_data fails.
8674ed6 Refactor RaiseErrorMatcher messages in simpler and more expressive code
eeeea9f Simpler logic in RaiseErrorMatcher#negative_failure_message
203ec67 Avoid String#<< for Opal
24a80c4 Normalize home_directory for Windows
f90efa0 Merge pull request #15 from ruby/vais/windows
6c110e4 Fix ruby executable path resolution on Windows
8c04853 Check subprocess leaks in the LeakChecker
5b1f519 Deprecate new_hash and hash_class.
46f800d Deprecate raise_exception in favor of raise_error
d87f746 Introduce a simple deprecatation mechanism in MSpec

git-subtree-dir: spec/mspec
git-subtree-split: f509c1a418e4a2794f04b606504a04f947008cbc
  • Loading branch information
eregon committed Apr 30, 2016
1 parent 7d49ba4 commit 90f598d
Show file tree
Hide file tree
Showing 13 changed files with 77 additions and 18 deletions.
6 changes: 4 additions & 2 deletions lib/mspec/expectations/should.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ def should(matcher=NO_MATCHER_GIVEN)
MSpec.actions :expectation, MSpec.current.state
unless matcher.equal?(NO_MATCHER_GIVEN)
unless matcher.matches?(self)
SpecExpectation.fail_with(*matcher.failure_message)
expected, actual = matcher.failure_message
SpecExpectation.fail_with(expected, actual)
end
else
SpecPositiveOperatorMatcher.new(self)
Expand All @@ -17,7 +18,8 @@ def should_not(matcher=NO_MATCHER_GIVEN)
MSpec.actions :expectation, MSpec.current.state
unless matcher.equal?(NO_MATCHER_GIVEN)
if matcher.matches?(self)
SpecExpectation.fail_with(*matcher.negative_failure_message)
expected, actual = matcher.negative_failure_message
SpecExpectation.fail_with(expected, actual)
end
else
SpecNegativeOperatorMatcher.new(self)
Expand Down
2 changes: 2 additions & 0 deletions lib/mspec/guards/extensions.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'mspec/guards/guard'
require 'mspec/utils/deprecate'

class ExtensionsGuard < SpecGuard
def match?
Expand All @@ -11,6 +12,7 @@ def match?

class Object
def extended_on(*args)
MSpec.deprecate "extended_on", "implementation-specific specs"
g = ExtensionsGuard.new(*args)
g.name = :extended_on
yield if g.yield?
Expand Down
4 changes: 4 additions & 0 deletions lib/mspec/guards/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'mspec/utils/deprecate'
require 'mspec/utils/version'
require 'mspec/guards/guard'

Expand All @@ -9,6 +10,9 @@ def initialize(version)
when Range
a = SpecVersion.new version.begin
b = SpecVersion.new version.end
unless version.exclude_end?
MSpec.deprecate "ruby_version_is with an inclusive range", 'an exclusive range ("2.1"..."2.3")'
end
@version = version.exclude_end? ? a...b : a..b
end
self.parameters = [version]
Expand Down
8 changes: 6 additions & 2 deletions lib/mspec/helpers/environment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,12 @@ def username
end

def home_directory
return ENV['HOME'] unless PlatformGuard.windows?
windows_env_echo('HOMEDRIVE') + windows_env_echo('HOMEPATH')
if PlatformGuard.windows?
path = windows_env_echo('HOMEDRIVE') + windows_env_echo('HOMEPATH')
path.tr('\\', '/').chomp('/')
else
ENV['HOME']
end
end

def dev_null
Expand Down
4 changes: 4 additions & 0 deletions lib/mspec/helpers/hash.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
require 'mspec/utils/deprecate'

class Object
# The following helpers provide a level of indirection for running the specs
# against a Hash implementation that has a different name than Hash.

# Returns the Hash class.
unless method_defined?(:hash_class)
def hash_class
MSpec.deprecate "hash_class", "Hash"
Hash
end
end

# Returns a new instance of hash_class.
def new_hash(*args, &block)
MSpec.deprecate "new_hash", "hash literals"
if block
hash_class.new(&block)
elsif args.size == 1
Expand Down
4 changes: 3 additions & 1 deletion lib/mspec/helpers/ruby_exe.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,9 @@ def resolve_ruby_exe
exe, *rest = cmd.split(" ")

if File.file?(exe) and File.executable?(exe)
return [File.expand_path(exe), *rest].join(" ")
exe = File.expand_path(exe)
exe = exe.tr('/', '\\') if PlatformGuard.windows?
return [exe, *rest].join(" ")
end
end
nil
Expand Down
2 changes: 1 addition & 1 deletion lib/mspec/matchers/be_computed_by.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def matches?(array)
def method_call
method_call = "#{@receiver.inspect}.#{@method}"
unless @arguments.empty?
method_call << " from #{@arguments.map { |x| x.inspect }.join(", ")}"
method_call = "#{method_call} from #{@arguments.map { |x| x.inspect }.join(", ")}"
end
method_call
end
Expand Down
2 changes: 1 addition & 1 deletion lib/mspec/matchers/be_computed_by_function.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def matches?(array)
def function_call
function_call = "#{@function}"
unless @arguments.empty?
function_call << "(#{@arguments.map { |x| x.inspect }.join(", ")})"
function_call = "#{function_call}(#{@arguments.map { |x| x.inspect }.join(", ")})"
end
function_call
end
Expand Down
8 changes: 5 additions & 3 deletions lib/mspec/matchers/have_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,16 @@ def matches?(name)
size = @data.size
end

File.open @name, fmode(@mode) do |f|
return f.read(size) == @data
@contents = File.open @name, fmode(@mode) do |f|
f.read(size)
end
@contents == @data
end

def failure_message
["Expected #{@name}",
"to have data #{@data.pretty_inspect}"]
"to have data #{@data.pretty_inspect}" +
"but starts with #{@contents.pretty_inspect}"]
end

def negative_failure_message
Expand Down
35 changes: 29 additions & 6 deletions lib/mspec/matchers/raise_error.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'mspec/utils/deprecate'

class RaiseErrorMatcher
def initialize(exception, message, &block)
@exception = exception
Expand Down Expand Up @@ -33,21 +35,39 @@ def matching_exception?(exc)
return true
end

def exception_class_and_message(exception_class, message)
if message
"#{exception_class} (#{message})"
else
"#{exception_class}"
end
end

def format_expected_exception
exception_class_and_message(@exception, @message)
end

def format_exception(exception)
exception_class_and_message(exception.class, exception.message)
end

def failure_message
message = ["Expected #{@exception}#{%[ (#{@message})] if @message}"]
message = ["Expected #{format_expected_exception}"]

if @actual then
message << "but got #{@actual.class}#{%[ (#{@actual.message})] if @actual.message}"
message << "but got #{format_exception(@actual)}"
else
message << "but no exception was raised (#@result was returned)"
message << "but no exception was raised (#{@result} was returned)"
end

message
end

def negative_failure_message
message = ["Expected to not get #{@exception}#{%[ (#{@message})] if @message}", ""]
message[1] = "but got #{@actual.class}#{%[ (#{@actual.message})] if @actual.message}" unless @actual.class == @exception
message = ["Expected to not get #{format_expected_exception}", ""]
unless @actual.class == @exception
message[1] = "but got #{format_exception(@actual)}"
end
message
end
end
Expand All @@ -62,5 +82,8 @@ def raise_error(exception=Exception, message=nil, &block)
RaiseExceptionMatcher = RaiseErrorMatcher

class Object
alias_method :raise_exception, :raise_error
def raise_exception(exception=Exception, message=nil, &block)
MSpec.deprecate "raise_exception", "raise_error"
raise_error(exception, message, &block)
end
end
11 changes: 10 additions & 1 deletion lib/mspec/runner/actions/leakchecker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@ def check(test_name)
@no_leaks = true
leaks = [
check_fd_leak(test_name),
check_thread_leak(test_name),
check_tempfile_leak(test_name),
check_thread_leak(test_name),
check_process_leak(test_name),
check_env(test_name),
check_argv(test_name),
check_encodings(test_name)
Expand Down Expand Up @@ -198,6 +199,14 @@ def check_thread_leak(test_name)
return leaked
end

def check_process_leak(test_name)
subprocesses_leaked = Process.waitall
subprocesses_leaked.each { |pid, status|
puts "Leaked subprocess: #{pid}: #{status}"
}
return !subprocesses_leaked.empty?
end

def find_env
ENV.to_h
end
Expand Down
7 changes: 7 additions & 0 deletions lib/mspec/utils/deprecate.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module MSpec
def self.deprecate(what, replacement)
$stderr.puts "\n#{what} is deprecated, use #{replacement} instead."
user_caller = caller.find { |line| !line.include?('lib/mspec') }
$stderr.puts "from #{user_caller}"
end
end
2 changes: 1 addition & 1 deletion spec/matchers/have_data_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
matcher = HaveDataMatcher.new("abc1")
matcher.matches?(@name)
matcher.failure_message.should == [
"Expected #{@name}", "to have data \"abc1\"\n"
"Expected #{@name}", "to have data \"abc1\"\nbut starts with \"123a\"\n"
]
end

Expand Down

0 comments on commit 90f598d

Please sign in to comment.