simple_executor_spec.rb 6.09 KB
Newer Older
Gabriel Mazetto's avatar
Gabriel Mazetto committed
1 2 3
require 'spec_helper'
require 'rake_helper'

4
describe SystemCheck::SimpleExecutor do
Gabriel Mazetto's avatar
Gabriel Mazetto committed
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
  class SimpleCheck < SystemCheck::BaseCheck
    set_name 'my simple check'

    def check?
      true
    end
  end

  class OtherCheck < SystemCheck::BaseCheck
    set_name 'other check'

    def check?
      false
    end

    def show_error
      $stdout.puts 'this is an error text'
    end
  end

  class SkipCheck < SystemCheck::BaseCheck
    set_name 'skip check'
    set_skip_reason 'this is a skip reason'

    def skip?
      true
    end

    def check?
      raise 'should not execute this'
    end
  end

38 39 40 41 42 43 44 45 46 47 48 49 50 51
  class DynamicSkipCheck < SystemCheck::BaseCheck
    set_name 'dynamic skip check'
    set_skip_reason 'this is a skip reason'

    def skip?
      self.skip_reason = 'this is a dynamic skip reason'
      true
    end

    def check?
      raise 'should not execute this'
    end
  end

Gabriel Mazetto's avatar
Gabriel Mazetto committed
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
  class MultiCheck < SystemCheck::BaseCheck
    set_name 'multi check'

    def multi_check
      $stdout.puts 'this is a multi output check'
    end

    def check?
      raise 'should not execute this'
    end
  end

  class SkipMultiCheck < SystemCheck::BaseCheck
    set_name 'skip multi check'

    def skip?
      true
    end

    def multi_check
      raise 'should not execute this'
    end
  end

  class RepairCheck < SystemCheck::BaseCheck
    set_name 'repair check'

    def check?
      false
    end

    def repair!
      true
    end

    def show_error
      $stdout.puts 'this is an error message'
    end
  end

92 93 94 95 96 97 98 99 100
  class BugousCheck < SystemCheck::BaseCheck
    CustomError = Class.new(StandardError)
    set_name 'my bugous check'

    def check?
      raise CustomError, 'omg'
    end
  end

101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
  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
135 136 137 138

    it 'errors out when passing multiple items' do
      expect { subject << [SimpleCheck, OtherCheck] }.to raise_error(ArgumentError)
    end
139 140
  end

Gabriel Mazetto's avatar
Gabriel Mazetto committed
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
  subject { described_class.new('Test') }

  describe '#execute' do
    before do
      silence_output

      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
168
        expect { subject.run_check(SimpleCheck) }.to output(/ \.\.\. yes/).to_stdout
Gabriel Mazetto's avatar
Gabriel Mazetto committed
169 170 171 172 173 174
      end
    end

    context 'when check fails' do
      it 'prints no' do
        expect_any_instance_of(OtherCheck).to receive(:check?).and_call_original
175
        expect { subject.run_check(OtherCheck) }.to output(/ \.\.\. no/).to_stdout
Gabriel Mazetto's avatar
Gabriel Mazetto committed
176 177 178 179 180 181 182 183 184 185
      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!)
186

Gabriel Mazetto's avatar
Gabriel Mazetto committed
187 188 189 190 191 192 193
          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)
194

Gabriel Mazetto's avatar
Gabriel Mazetto committed
195 196 197 198 199 200 201 202
            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)
203

Gabriel Mazetto's avatar
Gabriel Mazetto committed
204 205 206 207 208 209 210 211 212
            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
213

Gabriel Mazetto's avatar
Gabriel Mazetto committed
214 215 216
        subject.run_check(SkipCheck)
      end

217
      it 'displays .skip_reason' do
Gabriel Mazetto's avatar
Gabriel Mazetto committed
218 219 220
        expect { subject.run_check(SkipCheck) }.to output(/this is a skip reason/).to_stdout
      end

221 222 223 224
      it 'displays #skip_reason' do
        expect { subject.run_check(DynamicSkipCheck) }.to output(/this is a dynamic skip reason/).to_stdout
      end

Gabriel Mazetto's avatar
Gabriel Mazetto committed
225 226
      it 'does not execute #check when #skip? is true' do
        expect_any_instance_of(SkipCheck).not_to receive(:check?)
227

Gabriel Mazetto's avatar
Gabriel Mazetto committed
228 229 230 231 232 233 234
        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)
235

Gabriel Mazetto's avatar
Gabriel Mazetto committed
236 237 238 239 240
        subject.run_check(MultiCheck)
      end

      it 'does not execute #check method' do
        expect_any_instance_of(MultiCheck).not_to receive(:check)
241

Gabriel Mazetto's avatar
Gabriel Mazetto committed
242 243 244 245 246 247
        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
248

Gabriel Mazetto's avatar
Gabriel Mazetto committed
249 250 251 252
          subject.run_check(SkipMultiCheck)
        end
      end
    end
253 254 255

    context 'when there is an exception' do
      it 'rescues the exception' do
256
        expect { subject.run_check(BugousCheck) }.not_to raise_exception
257 258
      end
    end
Gabriel Mazetto's avatar
Gabriel Mazetto committed
259 260
  end
end