diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 5ca9ac5..27e7847 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -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 diff --git a/spec/support/file_helper.rb b/spec/support/file_helper.rb new file mode 100644 index 0000000..84a70b5 --- /dev/null +++ b/spec/support/file_helper.rb @@ -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 diff --git a/spec/support/temp_dir_helper.rb b/spec/support/temp_dir_helper.rb new file mode 100644 index 0000000..1e45a6b --- /dev/null +++ b/spec/support/temp_dir_helper.rb @@ -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