pax_global_header00006660000000000000000000000064140234741730014517gustar00rootroot0000000000000052 comment=f9e11c69b9a5448d0f2a32902211f066bb8c05d7 ruby-rspec-files-1.1.1/000077500000000000000000000000001402347417300147125ustar00rootroot00000000000000ruby-rspec-files-1.1.1/.editorconfig000066400000000000000000000000641402347417300173670ustar00rootroot00000000000000root = true [*] indent_style = tab indent_size = 2 ruby-rspec-files-1.1.1/.github/000077500000000000000000000000001402347417300162525ustar00rootroot00000000000000ruby-rspec-files-1.1.1/.github/workflows/000077500000000000000000000000001402347417300203075ustar00rootroot00000000000000ruby-rspec-files-1.1.1/.github/workflows/development.yml000066400000000000000000000015701402347417300233570ustar00rootroot00000000000000name: Development on: [push, pull_request] jobs: test: runs-on: ${{matrix.os}}-latest continue-on-error: ${{matrix.experimental}} strategy: matrix: os: - ubuntu - macos ruby: - 2.5 - 2.6 - 2.7 - 3.0 experimental: [false] env: [""] include: - os: ubuntu ruby: truffleruby experimental: true - os: ubuntu ruby: jruby experimental: true - os: ubuntu ruby: head experimental: true steps: - uses: actions/checkout@v2 - uses: ruby/setup-ruby@v1 with: ruby-version: ${{matrix.ruby}} bundler-cache: true - name: Run tests timeout-minutes: 5 run: ${{matrix.env}} bundle exec rspec ruby-rspec-files-1.1.1/.gitignore000066400000000000000000000002121402347417300166750ustar00rootroot00000000000000/.bundle/ /.yardoc /gems.locked /_yardoc/ /coverage/ /doc/ /pkg/ /spec/reports/ /tmp/ # rspec failure tracking .rspec_status .covered.db ruby-rspec-files-1.1.1/.rspec000066400000000000000000000000701402347417300160240ustar00rootroot00000000000000--format documentation --warnings --require spec_helper ruby-rspec-files-1.1.1/README.md000066400000000000000000000057051402347417300162000ustar00rootroot00000000000000# RSpec::Files Detect leaked file descriptors and provides convenient file buffers. [![Development Status](https://github.com/socketry/rspec-files/workflows/Development/badge.svg)](https://github.com/socketry/rspec-files/actions?workflow=Development) ## Installation Add this line to your application's Gemfile: ``` ruby gem 'rspec-files' ``` And then execute: $ bundle Or install it yourself as: $ gem install rspec-files Finally, add this require statement to the top of `spec/spec_helper.rb` ``` ruby require 'rspec/files' ``` ## Usage ### Leaks Leaking sockets and other kinds of IOs are a problem for long running services. `RSpec::Files::Leaks` tracks all open sockets both before and after the spec. If any are left open, a `RuntimeError` is raised and the spec fails. ``` ruby RSpec.describe "leaky ios" do include_context RSpec::Files::Leaks # The following fails: it "leaks io" do @input, @output = IO.pipe end end ``` In some cases, the Ruby garbage collector will close IOs. In the above case, it's possible that just writing `IO.pipe` will not leak as Ruby will garbage collect the resulting IOs immediately. It's still incorrect to not close IOs, so don't depend on this behaviour. ### Buffers File buffers are useful for specs which implement I/O operations. This context automatically manages a disk-based file which can be used for buffering. ``` ruby RSpec.describe "buffer" do include_context RSpec::Files::Buffer it "can read and write data" do buffer.write("Hello World") buffer.seek(0) expect(buffer.read).to be == "Hello World" end end ``` ## Contributing 1. Fork it 2. Create your feature branch (`git checkout -b my-new-feature`) 3. Commit your changes (`git commit -am 'Add some feature'`) 4. Push to the branch (`git push origin my-new-feature`) 5. Create new Pull Request ## License Released under the MIT license. Copyright, 2017, by [Samuel G. D. Williams](http://www.codeotaku.com/samuel-williams). Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ruby-rspec-files-1.1.1/gems.rb000066400000000000000000000002651402347417300161750ustar00rootroot00000000000000source 'https://rubygems.org' # Specify your gem's dependencies in rspec-files.gemspec gemspec group :maintenance, optional: true do gem "bake-modernize" gem "bake-bundler" end ruby-rspec-files-1.1.1/lib/000077500000000000000000000000001402347417300154605ustar00rootroot00000000000000ruby-rspec-files-1.1.1/lib/rspec/000077500000000000000000000000001402347417300165745ustar00rootroot00000000000000ruby-rspec-files-1.1.1/lib/rspec/files.rb000066400000000000000000000023151402347417300202240ustar00rootroot00000000000000# Copyright, 2019, by Samuel G. D. Williams. # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. require_relative "files/version" require_relative "files/leaks" require_relative "files/buffer" ruby-rspec-files-1.1.1/lib/rspec/files/000077500000000000000000000000001402347417300176765ustar00rootroot00000000000000ruby-rspec-files-1.1.1/lib/rspec/files/buffer.rb000066400000000000000000000031021402347417300214700ustar00rootroot00000000000000# Copyright, 2018, by Samuel G. D. Williams. # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. require 'securerandom' module RSpec module Files module Buffer TMP = "/tmp" def self.open(mode = 'w+', root: TMP) path = File.join(root, SecureRandom.hex(32)) file = File.open(path, mode) File.unlink(path) return file unless block_given? begin yield file ensure file.close end end end RSpec.shared_context Buffer do let(:buffer) {Buffer.open} after(:each) {buffer.close} end end end ruby-rspec-files-1.1.1/lib/rspec/files/leaks.rb000066400000000000000000000037711402347417300213320ustar00rootroot00000000000000# Copyright, 2017, by Samuel G. D. Williams. # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. module RSpec module Files module Leaks def current_ios(gc: true) GC.start if gc all_ios = ObjectSpace.each_object(::IO).to_a.sort_by(&:object_id) # We are not interested in ios that have been closed already: return all_ios.reject(&:closed?) rescue RuntimeError => error # This occurs on JRuby. warn error.message return [] end end RSpec.shared_context Leaks do include Leaks let(:before_ios) {current_ios} let(:leaked_ios) {current_ios - before_ios} # We use around(:each) because it's the highest priority. around(:each) do |example| # Here inspect is used to avoid reporting on handles that cannot # be read from, as otherwise RSpec will attempt to test if they are # readable and get stuck forever. before_ios example.run.tap do expect(leaked_ios).to be_empty end end end end end ruby-rspec-files-1.1.1/lib/rspec/files/version.rb000066400000000000000000000022451402347417300217130ustar00rootroot00000000000000# Copyright, 2017, by Samuel G. D. Williams. # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. module RSpec module Files VERSION = "1.1.1" end end ruby-rspec-files-1.1.1/rspec-files.gemspec000066400000000000000000000010521402347417300204710ustar00rootroot00000000000000 require_relative "lib/rspec/files/version" Gem::Specification.new do |spec| spec.name = "rspec-files" spec.version = RSpec::Files::VERSION spec.summary = "RSpec helpers for buffering and detecting file descriptor leaks." spec.authors = ["Samuel Williams"] spec.license = "MIT" spec.homepage = "https://github.com/socketry/rspec-files" spec.files = Dir.glob('{lib}/**/*', File::FNM_DOTMATCH, base: __dir__) spec.add_dependency "rspec", "~> 3.0" spec.add_development_dependency "bundler" spec.add_development_dependency "covered" end ruby-rspec-files-1.1.1/spec/000077500000000000000000000000001402347417300156445ustar00rootroot00000000000000ruby-rspec-files-1.1.1/spec/rspec/000077500000000000000000000000001402347417300167605ustar00rootroot00000000000000ruby-rspec-files-1.1.1/spec/rspec/files/000077500000000000000000000000001402347417300200625ustar00rootroot00000000000000ruby-rspec-files-1.1.1/spec/rspec/files/buffer_spec.rb000066400000000000000000000025761402347417300227040ustar00rootroot00000000000000# Copyright, 2018, by Samuel G. D. Williams. # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. require 'rspec/files/buffer' RSpec.describe RSpec::Files::Buffer do include_context RSpec::Files::Buffer it "behaves like a file" do expect(buffer).to be_instance_of(File) end it "should not exist on disk" do expect(File).to_not be_exist(buffer.path) end end ruby-rspec-files-1.1.1/spec/rspec/files/leaks_spec.rb000066400000000000000000000034401402347417300225210ustar00rootroot00000000000000# Copyright, 2017, by Samuel G. D. Williams. # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. require 'rspec/files/leaks' require 'rspec/core/sandbox' require 'tempfile' RSpec.describe RSpec::Files::Leaks do include_context RSpec::Files::Leaks it "leaks IO instances" do expect(before_ios).to be == current_ios input, output = IO.pipe expect(before_ios).to_not be == current_ios input.close output.close expect(before_ios).to be == current_ios end it "fails if it leaks IO instances" do group = RSpec::Core::Sandbox.sandboxed{RSpec.describe} group.include_context RSpec::Files::Leaks pipe = nil group.example("leaky example") do pipe = IO.pipe end result = group.run pipe.each(&:close) expect(result).to be_falsy end end ruby-rspec-files-1.1.1/spec/spec_helper.rb000066400000000000000000000005221402347417300204610ustar00rootroot00000000000000 require 'covered/rspec' RSpec.configure do |config| # Enable flags like --only-failures and --next-failure config.example_status_persistence_file_path = ".rspec_status" # Disable RSpec exposing methods globally on `Module` and `main` config.disable_monkey_patching! config.expect_with :rspec do |c| c.syntax = :expect end end