File: buffer.rb

package info (click to toggle)
ruby-rspec-files 1.1.3-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 152 kB
  • sloc: ruby: 114; makefile: 4
file content (34 lines) | stat: -rw-r--r-- 574 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2019, by Samuel Williams.

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