Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
source "https://rubygems.org"

gem "colorize"

group :development, :test do
gem "byebug"
gem "rspec"
Expand Down
2 changes: 1 addition & 1 deletion lib/hound.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
require "colorize"
require "delegate"
require "json"
require "yaml"

require "hound/version"
require "hound/config"
require "hound/format"
require "hound/linters/base"
require "hound/linters/ruby"
require "hound/linters/javascript"
Expand Down
7 changes: 7 additions & 0 deletions lib/hound/format.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module Hound
module Format
def self.error(text)
"\e[31m#{text}\e[0m"
end
end
end
11 changes: 8 additions & 3 deletions lib/hound/linters/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,15 @@ def filepath
options["config_file"]
end

def handle_config_errors
def parse_config_result &parse_command
if filepath
yield
parse_command.call(file_content)
"Using #{filepath}"
else
"Not provided -- using default"
end
rescue Errno::ENOENT
"#{filepath} does not exist".colorize(:red)
Format.error("#{filepath} does not exist")
end

private
Expand All @@ -41,6 +42,10 @@ def enabled?
options.fetch("enabled", true)
end

def file_content
File.read(filepath)
end

def options
@options || {}
end
Expand Down
13 changes: 2 additions & 11 deletions lib/hound/linters/javascript.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,9 @@ module Hound
module Linter
class Javascript < SimpleDelegator
def config
handle_config_errors do
parse_config
end
end

private

def parse_config
JSON.parse(File.read(filepath))
"Using #{filepath}"
parse_config_result { |file_content| JSON.parse(file_content) }
rescue JSON::ParserError
"#{filepath} is invalid JSON".colorize(:red)
Format.error("#{filepath} is invalid JSON")
end
end
end
Expand Down
13 changes: 2 additions & 11 deletions lib/hound/linters/ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,9 @@ module Hound
module Linter
class Ruby < SimpleDelegator
def config
handle_config_errors do
parse_config
end
end

private

def parse_config
YAML.load(File.read(filepath))
"Using #{filepath}"
parse_config_result { |file_content| YAML.load(file_content) }
rescue Psych::SyntaxError
"#{filepath} is invalid YAML".colorize(:red)
Format.error("#{filepath} is invalid YAML")
end
end
end
Expand Down
4 changes: 3 additions & 1 deletion spec/hound_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,19 @@
stub_file ".hound.yml", <<-CONFIG
ruby:
enabled: true
config_file: .ruby-style.yml
go:
enabled: false
CONFIG
stub_file(".ruby-style.yml", "").and_raise(Errno::ENOENT)

result = Hound.config

expect(result).to eq <<-RESULT
Ruby
---------
Status: Enabled
Config: Not provided -- using default
Config: \e[31m.ruby-style.yml does not exist\e[0m

Javascript
---------
Expand Down