Skip to content
This repository has been archived by the owner on Feb 29, 2024. It is now read-only.

Commit

Permalink
Account for source not existing
Browse files Browse the repository at this point in the history
  • Loading branch information
jgarber623 committed Aug 2, 2019
1 parent 1087d54 commit 7981569
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
10 changes: 6 additions & 4 deletions lib/svgeez/builder.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module Svgeez
class Builder
SOURCE_IS_DESTINATION_MESSAGE = "Setting `source` and `destination` to the same path isn't allowed!".freeze
SOURCE_DOES_NOT_EXIST = 'Provided `source` folder does not exist.'.freeze
NO_SVGS_IN_SOURCE_MESSAGE = 'No SVGs were found in `source` folder.'.freeze

attr_reader :source, :destination
Expand All @@ -11,6 +12,7 @@ def initialize(options = {})
@svgo = options.fetch('svgo', false)

raise SOURCE_IS_DESTINATION_MESSAGE if source_is_destination?
raise SOURCE_DOES_NOT_EXIST unless source_exists?
rescue RuntimeError => exception
logger.error exception.message
exit
Expand Down Expand Up @@ -53,12 +55,12 @@ def logger
@logger ||= Svgeez.logger
end

def source_files_count
source_file_paths.length
def source_exists?
File.directory?(source.folder_path)
end

def source_file_paths
source.file_paths
def source_files_count
source.file_paths.length
end

def source_is_destination?
Expand Down
22 changes: 19 additions & 3 deletions spec/lib/svgeez/builder_build_spec.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,31 @@
describe Svgeez::Builder, '#build' do
let(:file) { class_double(File) }
let(:logger) { Svgeez.logger }
let(:error_message) { "Setting `source` and `destination` to the same path isn't allowed!" }
let(:warning_message) { 'No SVGs were found in `source` folder.' }

before do
allow(File).to receive(:directory?).and_return(true)

allow(logger).to receive(:error)
allow(logger).to receive(:info)
allow(logger).to receive(:warn)
end

context 'when @source does not exist' do
let(:builder) { described_class.new }
let(:error_message) { 'Provided `source` folder does not exist.' }

before do
allow(File).to receive(:directory?).and_return(false)
end

it 'logs an error' do
expect { builder.build }.to raise_error(SystemExit)
expect(logger).to have_received(:error).with(error_message)
end
end

context 'when @source and @destination are the same' do
let(:builder) do
described_class.new(
Expand All @@ -18,7 +35,7 @@
end

it 'logs an error' do
expect{ builder.build }.to raise_error(SystemExit)
expect { builder.build }.to raise_error(SystemExit)
expect(logger).to have_received(:error).with(error_message)
end
end
Expand All @@ -32,7 +49,7 @@
end

it 'logs an error' do
expect{ builder.build }.to raise_error(SystemExit)
expect { builder.build }.to raise_error(SystemExit)
expect(logger).to have_received(:error).with(error_message)
end
end
Expand All @@ -48,7 +65,6 @@
end

context 'when @source contains SVG files' do
let(:file) { class_double(File) }
let(:source) { instance_double(Svgeez::Source) }
let(:source_folder_path) { './spec/fixtures/icons' }

Expand Down

0 comments on commit 7981569

Please sign in to comment.