summaryrefslogtreecommitdiff
path: root/spec/cli_spec.rb
blob: fe9227473a615c7fdc02411b65d9c26b6fb89349 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# frozen_string_literal: true

require 'tempfile'
require_relative '../lib/cli'

RSpec.describe CLI do
  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
    allow(ActionFileExecutor).to receive(:new).with(tempfile.path).and_return(action_file_executor)
    allow(action_file_executor).to receive(:execute_actions)
  end

  after do
    tempfile.close
    tempfile.unlink
  end

  describe '#initialize' 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

    context 'when a valid file path is provided' do
      it 'initializes successfully' do
        cli = CLI.new([tempfile.path])
        expect(cli).to be_an_instance_of(CLI)
      end
    end
  end

  describe '#run' do
    it 'creates an ActionFileExecutor and executes actions' do
      cli = CLI.new(args)
      cli.run

      expect(ActionFileExecutor).to have_received(:new).with(tempfile.path)
      expect(action_file_executor).to have_received(:execute_actions)
    end
  end
end