spec/spec_helper.rb in nanoc-4.7.11 vs spec/spec_helper.rb in nanoc-4.7.12
- old
+ new
@@ -275,5 +275,44 @@
failure_message_when_negated do |_actual|
"expected that proc would not send notification #{name.inspect} with args #{expected_args.inspect}"
end
end
+
+RSpec::Matchers.define :leak_open_files do |_name, *_expected_args|
+ # Some remarks:
+ #
+ # • This matcher relies on global state (list of file descriptors and their
+ # state), which means that a test case that uses this matcher can fail if
+ # tests are being run in parallel.
+ #
+ # • This matcher assumes that file descriptors are closed explicitly, rather
+ # than implicitly through garbage collection. This means that a test case
+ # that uses this matcher can fail if the code exercised by the test case
+ # relies on file descriptors to be closed when they are garbage collected.
+
+ supports_block_expectations
+
+ match do |actual|
+ open_files_before = []
+ ObjectSpace.each_object(File) { |f| open_files_before << f.fileno unless f.closed? }
+
+ actual.call
+
+ open_files_after = []
+ ObjectSpace.each_object(File) { |f| open_files_after << f.fileno unless f.closed? }
+
+ open_files_before.sort != open_files_after.sort
+ end
+
+ description do
+ 'leak open files'
+ end
+
+ failure_message do |_actual|
+ 'expected that proc would leak open files'
+ end
+
+ failure_message_when_negated do |_actual|
+ 'expected that proc would not leak open files'
+ end
+end