require 'process_spec_helper' require 'support/heredoc_helper' RSpec.describe CLOCScanner, type: :process do describe "#scan" do it "returns a CLOC object when cloc is successful" do stub_dir_exists cloc_output = <<-EOS.heredoc_unindent files,language,blank,comment,code,"github.com/AlDanial/cloc v 1.70 T=0.29 s (290.6 files/s, 66866.8 lines/s)" 20,PHP,4571,0,6641 2,JSON,0,0,1770 1,JavaScript,260,184,1263 EOS system_cloc = fake_system_cloc(result: cloc_output, process_status: successful_process_status) expect(CLOCScanner.new(system_cloc).scan("my_directory")).to be_a(CLOC) end # EXCEPTION HANDLING: ##################### it "raises an error when no directory is passed" do expect{ CLOCScanner.new(fake_system_cloc).scan(nil) }.to raise_error CLOCScanner::ArgumentError end it "raises an error when the directory doesn't exist" do allow(Dir).to receive(:exist?).and_return false expect{ CLOCScanner.new(fake_system_cloc).scan("my_directory") }.to raise_error CLOCScanner::NoDirectory end it "raises an error when cloc was not available" do stub_dir_exists system_cloc = fake_system_cloc(which_result: which_failure) expect{ CLOCScanner.new(system_cloc).scan("my_directory") }.to raise_error CLOCScanner::Unavailable end it 'raises an error when cloc returns an unrecognised error' do stub_dir_exists system_cloc = fake_system_cloc(result: "Some nonsense", process_status: failed_process_status) expect{ CLOCScanner.new(system_cloc).scan("my_directory") }.to raise_error CLOCScanner::Exception end it 'raises an error when the output could not be parsed' do stub_dir_exists cloc_output = <<-EOS.heredoc_unindent files,language,blank,comment,code,"github.com/AlDanial/cloc v 1.70 T=0.29 s (290.6 files/s, 66866.8 lines/s)" 20",PHP,4571,0,6641 EOS system_cloc = fake_system_cloc(result: cloc_output) expect{ CLOCScanner.new(system_cloc).scan("my_directory") }.to raise_error CLOCScanner::CSVError end def fake_system_cloc(result: "foo", process_status: successful_process_status, which_result: which_success('cloc')) instance_double( SystemCloc, call: [result, process_status], which: which_result ) end def stub_dir_exists allow(Dir).to receive(:exist?).and_return true end end end