From 08534a8164699017aa847fef62602abf6ec59e8f Mon Sep 17 00:00:00 2001 From: Szymon Szukalski Date: Thu, 24 Oct 2024 12:27:13 +1100 Subject: Add ActionFileExecutor and execute from CLI - Updated tests to use tempfile - Implemented initial ActionFileExecutor tests --- lib/action_file_executor.rb | 26 +++++++++++++++++++ lib/cli.rb | 5 +++- spec/action_file_executor_spec.rb | 45 ++++++++++++++++++++++++++++++++ spec/cli_spec.rb | 54 ++++++++++----------------------------- 4 files changed, 89 insertions(+), 41 deletions(-) create mode 100644 lib/action_file_executor.rb create mode 100644 spec/action_file_executor_spec.rb diff --git a/lib/action_file_executor.rb b/lib/action_file_executor.rb new file mode 100644 index 0000000..15d4f63 --- /dev/null +++ b/lib/action_file_executor.rb @@ -0,0 +1,26 @@ +# frozen_string_literal: true + +class ActionFileExecutor + def initialize(file_path) + @file_path = file_path + validate_file + end + + def execute_actions + File.open(@file_path, 'r') do |file| + file.each_line do |line| + action, *params = line.split(' ') + puts "Executing action: #{action} with params: #{params.join(', ')}" + end + end + end + + private + + def validate_file + return if File.exist?(@file_path) + + puts "Error: The file '#{@file_path}' does not exist." + exit 1 + end +end diff --git a/lib/cli.rb b/lib/cli.rb index f2b185d..3087212 100644 --- a/lib/cli.rb +++ b/lib/cli.rb @@ -1,5 +1,7 @@ # frozen_string_literal: true +require_relative 'action_file_executor' + class CLI def initialize(args) @args = args @@ -8,7 +10,8 @@ class CLI def run file_path = @args[0] - puts "Running actions from file: #{file_path} against the family tree." + action_file_executor = ActionFileExecutor.new(file_path) + action_file_executor.execute_actions end private diff --git a/spec/action_file_executor_spec.rb b/spec/action_file_executor_spec.rb new file mode 100644 index 0000000..c909bf5 --- /dev/null +++ b/spec/action_file_executor_spec.rb @@ -0,0 +1,45 @@ +# frozen_string_literal: true + +require 'tempfile' +require_relative '../lib/action_file_executor' + +RSpec.describe ActionFileExecutor do + let(:tempfile) { Tempfile.new('actions.txt') } + let(:invalid_file_path) { 'non_existent_file.txt' } + + before do + tempfile.write('ADD_CHILD Mother Child Male') + tempfile.rewind + end + + after do + tempfile.close + tempfile.unlink + end + + describe '#initialize' do + context 'when the file does not exist' do + it 'prints an error message and exits' do + expect do + ActionFileExecutor.new(invalid_file_path) + end.to output("Error: The file 'non_existent_file.txt' does not exist.\n").to_stdout.and raise_error(SystemExit) + end + end + + context 'when the file exists' do + it 'initializes successfully' do + action_file_executor = ActionFileExecutor.new(tempfile.path) + expect(action_file_executor).to be_an_instance_of(ActionFileExecutor) + end + end + end + + describe '#execute_actions' do + it 'prints message indicating action being executed and the parameters which were passed' do + action_file_executor = ActionFileExecutor.new(tempfile.path) + expect do + action_file_executor.execute_actions + end.to output("Executing action: ADD_CHILD with params: Mother, Child, Male\n").to_stdout + end + end +end diff --git a/spec/cli_spec.rb b/spec/cli_spec.rb index 9d448d0..fe92274 100644 --- a/spec/cli_spec.rb +++ b/spec/cli_spec.rb @@ -1,17 +1,22 @@ # frozen_string_literal: true +require 'tempfile' require_relative '../lib/cli' RSpec.describe CLI do - let(:valid_file_path) { 'test_actions.txt' } + let(:tempfile) { Tempfile.new('actions.txt') } let(:invalid_file_path) { 'non_existent_file.txt' } + let(:action_file_executor) { instance_double('ActionFileExecutor') } + let(:args) { [tempfile.path] } before do - File.write(valid_file_path, 'some actions') + allow(ActionFileExecutor).to receive(:new).with(tempfile.path).and_return(action_file_executor) + allow(action_file_executor).to receive(:execute_actions) end after do - File.delete(valid_file_path) if File.exist?(valid_file_path) + tempfile.close + tempfile.unlink end describe '#initialize' do @@ -33,50 +38,19 @@ RSpec.describe CLI do context 'when a valid file path is provided' do it 'initializes successfully' do - cli = CLI.new([valid_file_path]) + cli = CLI.new([tempfile.path]) expect(cli).to be_an_instance_of(CLI) end end end describe '#run' do - it 'prints a message that it is running actions from the actions file' do - expect do - cli = CLI.new([valid_file_path]) - cli.run - end.to output(/Running actions from file: test_actions.txt against the family tree./).to_stdout - end - end - - describe '#validate_arguments' do - context 'when no arguments are provided' do - it 'prints usage message and exits' do - expect do - CLI.new([]) - end.to output(%r{Usage: family_tree }).to_stdout.and raise_error(SystemExit) - end - end - - context 'when an invalid file path is provided' do - it 'prints error message and exits' do - expect do - CLI.new([invalid_file_path]) - end.to output(/Error: The file 'non_existent_file.txt' does not exist./).to_stdout.and raise_error(SystemExit) - end - end + it 'creates an ActionFileExecutor and executes actions' do + cli = CLI.new(args) + cli.run - context 'when a valid file path is provided' do - it 'does not print any message' do - expect do - CLI.new([valid_file_path]) - end.not_to output.to_stdout - end - - it 'does not raise any errors' do - expect do - CLI.new([valid_file_path]) - end.not_to raise_error - end + expect(ActionFileExecutor).to have_received(:new).with(tempfile.path) + expect(action_file_executor).to have_received(:execute_actions) end end end -- cgit v1.2.3