summaryrefslogtreecommitdiff
path: root/spec
diff options
context:
space:
mode:
authorSzymon Szukalski <szymon@skas.io>2024-10-24 12:27:13 +1100
committerSzymon Szukalski <szymon@skas.io>2024-10-24 12:27:13 +1100
commit08534a8164699017aa847fef62602abf6ec59e8f (patch)
treea18b56a9ccb9bebc4e64814391935756e6a03b26 /spec
parent28722066aa4b03a1b981f14e375497940e9f40e4 (diff)
Add ActionFileExecutor and execute from CLI
- Updated tests to use tempfile - Implemented initial ActionFileExecutor tests
Diffstat (limited to 'spec')
-rw-r--r--spec/action_file_executor_spec.rb45
-rw-r--r--spec/cli_spec.rb54
2 files changed, 59 insertions, 40 deletions
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 <path/to/actions.txt>}).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