From c808dede1fe5cf6307e57c0f3d640c2f4982858f Mon Sep 17 00:00:00 2001 From: Chris Welham <71787007+apexatoll@users.noreply.github.com> Date: Sat, 23 Sep 2023 14:26:44 +0100 Subject: [PATCH 1/2] Create spec file matchers --- spec/spec_helper.rb | 2 ++ spec/support/file_helper.rb | 31 +++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 spec/support/file_helper.rb diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 5ca9ac5..45190db 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -16,4 +16,6 @@ def load_support_files! config.mock_with(:rspec) { |mocks| mocks.verify_partial_doubles = true } config.order = :random + + config.include FileHelper 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 From 3e51cddbce6aa74c19fe0f12e0dd6cb152ccfa30 Mon Sep 17 00:00:00 2001 From: Chris Welham <71787007+apexatoll@users.noreply.github.com> Date: Sat, 23 Sep 2023 14:29:09 +0100 Subject: [PATCH 2/2] Create spec temp dir helper --- spec/spec_helper.rb | 1 + spec/support/temp_dir_helper.rb | 15 +++++++++++++++ 2 files changed, 16 insertions(+) create mode 100644 spec/support/temp_dir_helper.rb diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 45190db..27e7847 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -18,4 +18,5 @@ def load_support_files! config.order = :random config.include FileHelper + config.include TempDirHelper 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