2019-08-22 06:57:44 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-05-25 19:46:54 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2021-06-08 11:10:00 -04:00
|
|
|
RSpec.describe SystemCheck::SimpleExecutor, :silence_stdout do
|
2020-05-19 17:08:05 -04:00
|
|
|
before do
|
|
|
|
stub_const('SimpleCheck', Class.new(SystemCheck::BaseCheck))
|
|
|
|
stub_const('OtherCheck', Class.new(SystemCheck::BaseCheck))
|
|
|
|
stub_const('SkipCheck', Class.new(SystemCheck::BaseCheck))
|
|
|
|
stub_const('DynamicSkipCheck', Class.new(SystemCheck::BaseCheck))
|
|
|
|
stub_const('MultiCheck', Class.new(SystemCheck::BaseCheck))
|
|
|
|
stub_const('SkipMultiCheck', Class.new(SystemCheck::BaseCheck))
|
|
|
|
stub_const('RepairCheck', Class.new(SystemCheck::BaseCheck))
|
|
|
|
stub_const('BugousCheck', Class.new(SystemCheck::BaseCheck))
|
2017-05-25 19:46:54 -04:00
|
|
|
|
2020-05-19 17:08:05 -04:00
|
|
|
SimpleCheck.class_eval do
|
|
|
|
set_name 'my simple check'
|
|
|
|
|
|
|
|
def check?
|
|
|
|
true
|
|
|
|
end
|
2017-05-25 19:46:54 -04:00
|
|
|
end
|
|
|
|
|
2020-05-19 17:08:05 -04:00
|
|
|
OtherCheck.class_eval do
|
|
|
|
set_name 'other check'
|
2017-05-25 19:46:54 -04:00
|
|
|
|
2020-05-19 17:08:05 -04:00
|
|
|
def check?
|
|
|
|
false
|
|
|
|
end
|
2017-05-25 19:46:54 -04:00
|
|
|
|
2020-05-19 17:08:05 -04:00
|
|
|
def show_error
|
|
|
|
$stdout.puts 'this is an error text'
|
|
|
|
end
|
2017-05-25 19:46:54 -04:00
|
|
|
end
|
|
|
|
|
2020-05-19 17:08:05 -04:00
|
|
|
SkipCheck.class_eval do
|
|
|
|
set_name 'skip check'
|
|
|
|
set_skip_reason 'this is a skip reason'
|
2017-05-25 19:46:54 -04:00
|
|
|
|
2020-05-19 17:08:05 -04:00
|
|
|
def skip?
|
|
|
|
true
|
|
|
|
end
|
2017-05-25 19:46:54 -04:00
|
|
|
|
2020-05-19 17:08:05 -04:00
|
|
|
def check?
|
|
|
|
raise 'should not execute this'
|
|
|
|
end
|
2017-05-25 19:46:54 -04:00
|
|
|
end
|
|
|
|
|
2020-05-19 17:08:05 -04:00
|
|
|
DynamicSkipCheck.class_eval do
|
|
|
|
set_name 'dynamic skip check'
|
|
|
|
set_skip_reason 'this is a skip reason'
|
2017-09-03 21:58:54 -04:00
|
|
|
|
2020-05-19 17:08:05 -04:00
|
|
|
def skip?
|
|
|
|
self.skip_reason = 'this is a dynamic skip reason'
|
|
|
|
true
|
|
|
|
end
|
2017-09-03 21:58:54 -04:00
|
|
|
|
2020-05-19 17:08:05 -04:00
|
|
|
def check?
|
|
|
|
raise 'should not execute this'
|
|
|
|
end
|
2017-09-03 21:58:54 -04:00
|
|
|
end
|
|
|
|
|
2020-05-19 17:08:05 -04:00
|
|
|
MultiCheck.class_eval do
|
|
|
|
set_name 'multi check'
|
2017-05-25 19:46:54 -04:00
|
|
|
|
2020-05-19 17:08:05 -04:00
|
|
|
def multi_check
|
|
|
|
$stdout.puts 'this is a multi output check'
|
|
|
|
end
|
2017-05-25 19:46:54 -04:00
|
|
|
|
2020-05-19 17:08:05 -04:00
|
|
|
def check?
|
|
|
|
raise 'should not execute this'
|
|
|
|
end
|
2017-05-25 19:46:54 -04:00
|
|
|
end
|
|
|
|
|
2020-05-19 17:08:05 -04:00
|
|
|
SkipMultiCheck.class_eval do
|
|
|
|
set_name 'skip multi check'
|
2017-05-25 19:46:54 -04:00
|
|
|
|
2020-05-19 17:08:05 -04:00
|
|
|
def skip?
|
|
|
|
true
|
|
|
|
end
|
2017-05-25 19:46:54 -04:00
|
|
|
|
2020-05-19 17:08:05 -04:00
|
|
|
def multi_check
|
|
|
|
raise 'should not execute this'
|
|
|
|
end
|
2017-05-25 19:46:54 -04:00
|
|
|
end
|
|
|
|
|
2020-05-19 17:08:05 -04:00
|
|
|
RepairCheck.class_eval do
|
|
|
|
set_name 'repair check'
|
2017-05-25 19:46:54 -04:00
|
|
|
|
2020-05-19 17:08:05 -04:00
|
|
|
def check?
|
|
|
|
false
|
|
|
|
end
|
2017-05-25 19:46:54 -04:00
|
|
|
|
2020-05-19 17:08:05 -04:00
|
|
|
def repair!
|
|
|
|
true
|
|
|
|
end
|
2017-05-25 19:46:54 -04:00
|
|
|
|
2020-05-19 17:08:05 -04:00
|
|
|
def show_error
|
|
|
|
$stdout.puts 'this is an error message'
|
|
|
|
end
|
2017-05-25 19:46:54 -04:00
|
|
|
end
|
|
|
|
|
2020-05-19 17:08:05 -04:00
|
|
|
BugousCheck.class_eval do
|
|
|
|
set_name 'my bugous check'
|
2017-06-27 11:41:58 -04:00
|
|
|
|
2020-05-19 17:08:05 -04:00
|
|
|
def check?
|
|
|
|
raise StandardError, 'omg'
|
|
|
|
end
|
2017-06-27 11:41:58 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-05-31 14:45:00 -04:00
|
|
|
describe '#component' do
|
|
|
|
it 'returns stored component name' do
|
|
|
|
expect(subject.component).to eq('Test')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#checks' do
|
|
|
|
before do
|
|
|
|
subject << SimpleCheck
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns a set of classes' do
|
|
|
|
expect(subject.checks).to include(SimpleCheck)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#<<' do
|
|
|
|
before do
|
|
|
|
subject << SimpleCheck
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'appends a new check to the Set' do
|
|
|
|
subject << OtherCheck
|
|
|
|
stored_checks = subject.checks.to_a
|
|
|
|
|
|
|
|
expect(stored_checks.first).to eq(SimpleCheck)
|
|
|
|
expect(stored_checks.last).to eq(OtherCheck)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'inserts unique itens only' do
|
|
|
|
subject << SimpleCheck
|
|
|
|
|
|
|
|
expect(subject.checks.size).to eq(1)
|
|
|
|
end
|
2017-09-03 21:58:54 -04:00
|
|
|
|
|
|
|
it 'errors out when passing multiple items' do
|
|
|
|
expect { subject << [SimpleCheck, OtherCheck] }.to raise_error(ArgumentError)
|
|
|
|
end
|
2017-05-31 14:45:00 -04:00
|
|
|
end
|
|
|
|
|
2017-05-25 19:46:54 -04:00
|
|
|
subject { described_class.new('Test') }
|
|
|
|
|
|
|
|
describe '#execute' do
|
|
|
|
before do
|
|
|
|
subject << SimpleCheck
|
|
|
|
subject << OtherCheck
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'runs included checks' do
|
|
|
|
expect(subject).to receive(:run_check).with(SimpleCheck)
|
|
|
|
expect(subject).to receive(:run_check).with(OtherCheck)
|
|
|
|
|
|
|
|
subject.execute
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#run_check' do
|
|
|
|
it 'prints check name' do
|
|
|
|
expect(SimpleCheck).to receive(:display_name).and_call_original
|
|
|
|
expect { subject.run_check(SimpleCheck) }.to output(/my simple check/).to_stdout
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when check pass' do
|
|
|
|
it 'prints yes' do
|
|
|
|
expect_any_instance_of(SimpleCheck).to receive(:check?).and_call_original
|
2017-06-27 14:18:23 -04:00
|
|
|
expect { subject.run_check(SimpleCheck) }.to output(/ \.\.\. yes/).to_stdout
|
2017-05-25 19:46:54 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when check fails' do
|
|
|
|
it 'prints no' do
|
|
|
|
expect_any_instance_of(OtherCheck).to receive(:check?).and_call_original
|
2017-06-27 14:18:23 -04:00
|
|
|
expect { subject.run_check(OtherCheck) }.to output(/ \.\.\. no/).to_stdout
|
2017-05-25 19:46:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'displays error message from #show_error' do
|
|
|
|
expect_any_instance_of(OtherCheck).to receive(:show_error).and_call_original
|
|
|
|
expect { subject.run_check(OtherCheck) }.to output(/this is an error text/).to_stdout
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when check implements #repair!' do
|
|
|
|
it 'executes #repair!' do
|
|
|
|
expect_any_instance_of(RepairCheck).to receive(:repair!)
|
2017-05-31 14:45:00 -04:00
|
|
|
|
2017-05-25 19:46:54 -04:00
|
|
|
subject.run_check(RepairCheck)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when repair succeeds' do
|
|
|
|
it 'does not execute #show_error' do
|
|
|
|
expect_any_instance_of(RepairCheck).to receive(:repair!).and_call_original
|
|
|
|
expect_any_instance_of(RepairCheck).not_to receive(:show_error)
|
2017-05-31 14:45:00 -04:00
|
|
|
|
2017-05-25 19:46:54 -04:00
|
|
|
subject.run_check(RepairCheck)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when repair fails' do
|
|
|
|
it 'does not execute #show_error' do
|
|
|
|
expect_any_instance_of(RepairCheck).to receive(:repair!) { false }
|
|
|
|
expect_any_instance_of(RepairCheck).to receive(:show_error)
|
2017-05-31 14:45:00 -04:00
|
|
|
|
2017-05-25 19:46:54 -04:00
|
|
|
subject.run_check(RepairCheck)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when check implements skip?' do
|
|
|
|
it 'executes #skip? method' do
|
|
|
|
expect_any_instance_of(SkipCheck).to receive(:skip?).and_call_original
|
2017-05-31 14:45:00 -04:00
|
|
|
|
2017-05-25 19:46:54 -04:00
|
|
|
subject.run_check(SkipCheck)
|
|
|
|
end
|
|
|
|
|
2017-09-03 21:58:54 -04:00
|
|
|
it 'displays .skip_reason' do
|
2017-05-25 19:46:54 -04:00
|
|
|
expect { subject.run_check(SkipCheck) }.to output(/this is a skip reason/).to_stdout
|
|
|
|
end
|
|
|
|
|
2017-09-03 21:58:54 -04:00
|
|
|
it 'displays #skip_reason' do
|
|
|
|
expect { subject.run_check(DynamicSkipCheck) }.to output(/this is a dynamic skip reason/).to_stdout
|
|
|
|
end
|
|
|
|
|
2017-05-25 19:46:54 -04:00
|
|
|
it 'does not execute #check when #skip? is true' do
|
|
|
|
expect_any_instance_of(SkipCheck).not_to receive(:check?)
|
2017-05-31 14:45:00 -04:00
|
|
|
|
2017-05-25 19:46:54 -04:00
|
|
|
subject.run_check(SkipCheck)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when implements a #multi_check' do
|
|
|
|
it 'executes #multi_check method' do
|
|
|
|
expect_any_instance_of(MultiCheck).to receive(:multi_check)
|
2017-05-31 14:45:00 -04:00
|
|
|
|
2017-05-25 19:46:54 -04:00
|
|
|
subject.run_check(MultiCheck)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not execute #check method' do
|
|
|
|
expect_any_instance_of(MultiCheck).not_to receive(:check)
|
2017-05-31 14:45:00 -04:00
|
|
|
|
2017-05-25 19:46:54 -04:00
|
|
|
subject.run_check(MultiCheck)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when check implements #skip?' do
|
|
|
|
it 'executes #skip? method' do
|
|
|
|
expect_any_instance_of(SkipMultiCheck).to receive(:skip?).and_call_original
|
2017-05-31 14:45:00 -04:00
|
|
|
|
2017-05-25 19:46:54 -04:00
|
|
|
subject.run_check(SkipMultiCheck)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-06-27 11:41:58 -04:00
|
|
|
|
|
|
|
context 'when there is an exception' do
|
|
|
|
it 'rescues the exception' do
|
2017-08-09 05:52:22 -04:00
|
|
|
expect { subject.run_check(BugousCheck) }.not_to raise_exception
|
2017-06-27 11:41:58 -04:00
|
|
|
end
|
|
|
|
end
|
2017-05-25 19:46:54 -04:00
|
|
|
end
|
|
|
|
end
|