Skip to content

Commit

Permalink
Use better params for stub_system_call
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorthurlow committed Nov 12, 2018
1 parent 9209055 commit ffad0f2
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 12 deletions.
6 changes: 3 additions & 3 deletions spec/components/ascii_text_art_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
subject(:component) { create(:ascii_text_art) }

it 'prints the properly colored art' do
stub_system_call(component, 'helloworld')
stub_system_call(component, returns: 'helloworld')
component.process

expect(component.results).to start_with "\e[0;31;49m" # code for red
expect(component.results).to end_with "\e[0m"
end

it 'prints the art' do
stub_system_call(component, 'helloworld')
stub_system_call(component, returns: 'helloworld')
component.process

expect(component.to_s).not_to be_nil
Expand All @@ -27,7 +27,7 @@
}

it 'adds an error to the component' do
stub_system_call(component, 'helloworld')
stub_system_call(component, returns: 'helloworld')
component.process

expect(component.errors.count).to eq 1
Expand Down
10 changes: 5 additions & 5 deletions spec/components/service_status_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
subject(:component) { create(:service_status) }

it 'returns the list of statuses' do
stub_system_call(component, 'active')
stub_system_call(component, returns: 'active')
component.process

expect(component.results).to eq(Plex: :active, Sonarr: :active)
end

it 'prints the list of statuses' do
stub_system_call(component, 'active')
stub_system_call(component, returns: 'active')
component.process

results = component.to_s.delete(' ') # handle variable whitespace
Expand All @@ -23,15 +23,15 @@

context 'when printing different statuses' do
it 'prints active in green' do
stub_system_call(component, 'active')
stub_system_call(component, returns: 'active')
component.process

component.instance_variable_set(:@results, servicename: :active)
expect(component.to_s).to include 'active'.green
end

it 'prints inactive in red' do
stub_system_call(component, 'active')
stub_system_call(component, returns: 'active')
component.process

component.instance_variable_set(:@results, servicename: :inactive)
Expand All @@ -41,7 +41,7 @@

context 'when system call output is empty' do
it 'adds an error to the component' do
stub_system_call(component, '')
stub_system_call(component, returns: '')
component.process
errors = component.errors

Expand Down
7 changes: 5 additions & 2 deletions spec/components/ssl_certificates_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@

context 'when the certificate expiration date cannot be found' do
it 'adds an error to the component' do
stub_system_call(component, '')
stub_system_call(component, returns: '')
allow(File).to receive(:exist?).and_return(true) # valid cert path
component.process

Expand All @@ -51,7 +51,10 @@

context 'when the certificate expiration date cannot be parsed' do
it 'adds an error to the component' do
stub_system_call(component, "notAfter=Wtf 69 42:42:42 2077 LOL\n")
stub_system_call(
component,
returns: "notAfter=Wtf 69 42:42:42 2077 LOL\n"
)
allow(File).to receive(:exist?).and_return(true) # valid cert path
component.process

Expand Down
8 changes: 6 additions & 2 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,12 @@ def instance_with_configuration(described_class, config_hash)
described_class.new(motd)
end

def stub_system_call(subject, returns = command_output(subject.class))
allow(subject).to receive(:`).and_return(returns)
def stub_system_call(subject, with: nil, returns: command_output(subject.class))
if with
allow(subject).to receive(:`).with(with).and_return(returns)
else
allow(subject).to receive(:`).and_return(returns)
end
end

def command_output(component_class, file_name = 'output')
Expand Down

0 comments on commit ffad0f2

Please sign in to comment.