Skip to content

Commit

Permalink
ignore empty config files, closes #133
Browse files Browse the repository at this point in the history
  • Loading branch information
toy committed May 26, 2016
1 parent 60795be commit 7f0ec90
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## unreleased

* Ignore empty config files [#133](https://github.com/toy/image_optim/issues/133) [@toy](https://github.com/toy)
* Use `FileUtils.move` in `ImagePath#replace` to rename file instead of copying on same device [#134](https://github.com/toy/image_optim/issues/134) [@toy](https://github.com/toy)
* Make `:allow_lossy` an individual option for workers that can use it, so it will be in the list of worker options [#130](https://github.com/toy/image_optim/issues/130) [@toy](https://github.com/toy)
* Use first 8 characters of sha1 hex for jpegrescan version [#131](https://github.com/toy/image_optim/issues/131) [@toy](https://github.com/toy)
Expand Down
5 changes: 3 additions & 2 deletions lib/image_optim/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,16 @@ class Config

class << self
# Read options at path: expand path (warn on failure), return {} if file
# does not exist, read yaml, check if it is a Hash, deep symbolise keys
# does not exist or is empty, read yaml, check if it is a Hash, deep
# symbolise keys
def read_options(path)
begin
full_path = File.expand_path(path)
rescue ArgumentError => e
warn "Can't expand path #{path}: #{e}"
return {}
end
return {} unless File.file?(full_path)
return {} unless File.size?(full_path)
config = YAML.load_file(full_path)
unless config.is_a?(Hash)
fail "expected hash, got #{config.inspect}"
Expand Down
12 changes: 6 additions & 6 deletions spec/image_optim/config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -159,16 +159,16 @@ def image_formats
expect(IOConfig).to receive(:warn)
expect(File).to receive(:expand_path).
with(path).and_raise(ArgumentError)
expect(File).not_to receive(:file?)
expect(File).not_to receive(:size?)

expect(IOConfig.read_options(path)).to eq({})
end

it 'returns empty hash if path is not a file' do
it 'returns empty hash if path is not a file or is an empty file' do
expect(IOConfig).not_to receive(:warn)
expect(File).to receive(:expand_path).
with(path).and_return(full_path)
expect(File).to receive(:file?).
expect(File).to receive(:size?).
with(full_path).and_return(false)

expect(IOConfig.read_options(path)).to eq({})
Expand All @@ -181,7 +181,7 @@ def image_formats
expect(IOConfig).not_to receive(:warn)
expect(File).to receive(:expand_path).
with(path).and_return(full_path)
expect(File).to receive(:file?).
expect(File).to receive(:size?).
with(full_path).and_return(true)
expect(YAML).to receive(:load_file).
with(full_path).and_return(stringified)
Expand All @@ -193,7 +193,7 @@ def image_formats
expect(IOConfig).to receive(:warn)
expect(File).to receive(:expand_path).
with(path).and_return(full_path)
expect(File).to receive(:file?).
expect(File).to receive(:size?).
with(full_path).and_return(true)
expect(YAML).to receive(:load_file).
with(full_path).and_return([:config])
Expand All @@ -205,7 +205,7 @@ def image_formats
expect(IOConfig).to receive(:warn)
expect(File).to receive(:expand_path).
with(path).and_return(full_path)
expect(File).to receive(:file?).
expect(File).to receive(:size?).
with(full_path).and_return(true)
expect(YAML).to receive(:load_file).
with(full_path).and_raise
Expand Down

0 comments on commit 7f0ec90

Please sign in to comment.