Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement file and temp dir spec helpers #8

Merged
merged 2 commits into from
Sep 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,7 @@ def load_support_files!
config.mock_with(:rspec) { |mocks| mocks.verify_partial_doubles = true }

config.order = :random

config.include FileHelper
config.include TempDirHelper
end
31 changes: 31 additions & 0 deletions spec/support/file_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
module FileHelper
extend RSpec::Matchers::DSL

matcher :create_dir do |filename|
supports_block_expectations

match do |action|
path = File.join(@dir, filename)

expect(&action).to change { Dir.exist?(path) }.from(false).to(true)
end

chain :in do |dir|
@dir = dir
end
end

matcher :create_file do |filename|
supports_block_expectations

match do |action|
path = File.join(@dir, filename)

expect(&action).to change { File.exist?(path) }.from(false).to(true)
end

chain :in do |dir|
@dir = dir
end
end
end
15 changes: 15 additions & 0 deletions spec/support/temp_dir_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module TempDirHelper
RSpec.configure do |config|
config.around(with_temp_dir: true) do |spec|
self.class.class_eval { attr_reader :temp_dir }

Dir.mktmpdir do |temp_dir|
@temp_dir = temp_dir

spec.run

remove_instance_variable(:@temp_dir)
end
end
end
end