Skip to content
Permalink
Browse files

Fix Metrics/LineLength and Metrics/AbcSize offences

  • Loading branch information...
jigarius committed Apr 29, 2019
1 parent 0c7e4a2 commit 22e6bfb18b963ed483a4292db70a13b28aba62fc
@@ -66,7 +66,18 @@ Style/StructInheritance:
- 'lib/sitediff/result.rb'

# Offense count: 36
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes,
# IgnoreCopDirectives, IgnoredPatterns.
# URISchemes: http, https
Metrics/LineLength:
Max: 178
AllowURI: true
IgnoredPatterns:
- '^\s*SiteDiff\.log'
Exclude:
- 'Thorfile'
- 'spec/unit/sanitize_spec.rb'
- 'lib/sitediff/cache.rb'
- 'lib/sitediff/cli.rb'
- 'lib/sitediff/uriwrapper.rb'
- 'lib/sitediff/config/creator.rb'
- 'lib/sitediff/sanitize.rb'
@@ -2,7 +2,9 @@
# frozen_string_literal: true

# when run as gem, $0 is /usr/local/bin/sitediff not this file
$LOAD_PATH.unshift File.expand_path('../lib', __dir__) if $PROGRAM_NAME == __FILE__
if $PROGRAM_NAME == __FILE__
$LOAD_PATH.unshift File.expand_path('../lib', __dir__)
end

require 'sitediff/cli'

@@ -78,7 +78,14 @@ def after
end

# Initialize SiteDiff.
def initialize(config, cache, concurrency, interval, verbose = true, debug = false)
def initialize(
config,
cache,
concurrency,
interval,
verbose = true,
debug = false
)
@cache = cache
@verbose = verbose
@debug = debug
@@ -157,8 +164,15 @@ def run(curl_opts = {}, debug = true)
# so passing this instead but @config.after['curl_opts'] is ignored.
config_curl_opts = @config.before['curl_opts']
curl_opts = config_curl_opts.clone.merge(curl_opts) if config_curl_opts
fetcher = Fetch.new(@cache, @config.paths, @interval, @concurrency, curl_opts, debug,
before: before, after: after)
fetcher = Fetch.new(
@cache,
@config.paths,
@interval,
@concurrency,
curl_opts,
debug,
before: before, after: after
)
fetcher.run(&method(:process_results))

# Order by original path order
@@ -69,17 +69,19 @@ def self.normalize(conf)
# (h3) before: {selector: foo, sanitization: [pattern: foo, pattern: bar]}
def self.merge(first, second)
result = { 'paths' => {}, 'before' => {}, 'after' => {} }
result['paths'] = (first['paths'] || []) + (second['paths'] || []) # rule 1
# Rule 1.
result['paths'] = (first['paths'] || []) + (second['paths'] || [])
%w[before after].each do |pos|
unless first[pos]
result[pos] = second[pos] || {}
next
end
result[pos] = first[pos].merge!(second[pos]) do |key, a, b|
result[pos][key] = if Sanitizer::TOOLS[:array].include? key # rule 2a
# Rule 2a.
result[pos][key] = if Sanitizer::TOOLS[:array].include? key
(a || []) + (b || [])
else
a || b # rule 2b
a || b # Rule 2b.
end
end
end
@@ -136,11 +138,17 @@ def self.normalize_paths(paths)
def self.load_raw_yaml(file)
SiteDiff.log "Reading config file: #{Pathname.new(file).expand_path}"
conf = YAML.load_file(file) || {}
raise InvalidConfig, "Invalid configuration file: '#{file}'" unless conf.is_a? Hash

unless conf.is_a? Hash
raise InvalidConfig, "Invalid configuration file: '#{file}'"
end

conf.each_key do |k, _v|
raise InvalidConfig, "Unknown configuration key (#{file}): '#{k}'" unless CONF_KEYS.include? k
unless CONF_KEYS.include? k
raise InvalidConfig, "Unknown configuration key (#{file}): '#{k}'"
end
end

conf
end

@@ -149,7 +157,9 @@ def self.load_raw_yaml(file)
def self.load_conf(file, visited = [])
# don't get fooled by a/../a/ or symlinks
file = File.realpath(file)
raise InvalidConfig, "Circular dependency: #{file}" if visited.include? file
if visited.include? file
raise InvalidConfig, "Circular dependency: #{file}"
end

conf = load_raw_yaml(file) # not normalized yet
visited << file
@@ -115,7 +115,8 @@ def find_links(doc)
# Filter out links we don't want. Links passed in are absolute URIs.
def filter_links(uris)
uris.find_all do |u|
is_sub_uri = (u.host == @base_uri.host) && u.path.start_with?(@base_uri.path)
is_sub_uri = (u.host == @base_uri.host) &&
u.path.start_with?(@base_uri.path)
if is_sub_uri
is_whitelisted = @whitelist.nil? ? false : @whitelist.match(u.path)
is_blacklisted = @blacklist.nil? ? false : @blacklist.match(u.path)
@@ -7,7 +7,15 @@

class SiteDiff
# SiteDiff Result Object.
class Result < Struct.new(:path, :before, :after, :before_encoding, :after_encoding, :error, :verbose)
class Result < Struct.new(
:path,
:before,
:after,
:before_encoding,
:after_encoding,
:error,
:verbose
)
STATUS_SUCCESS = 0 # Identical before and after
STATUS_FAILURE = 1 # Different before and after
STATUS_ERROR = 2 # Couldn't fetch page
@@ -21,7 +29,12 @@ def initialize(*args)
@status = STATUS_ERROR
else
if !before_encoding || !after_encoding
@diff = Diff.binary_diffy(before, after, before_encoding, after_encoding)
@diff = Diff.binary_diffy(
before,
after,
before_encoding,
after_encoding
)
else
@diff = Diff.html_diffy(before, after)
end
@@ -65,7 +65,7 @@ def do_GET(req, res)
path = req.path_info
if path != '/diff'
res['content-type'] = 'text/plain'
res.body = 'ERROR: Only /run/diff is supported by the /run API at the moment'
res.body = 'ERROR: Only /run/diff is supported at the moment.'
return
end
# Thor assumes only one command is called and some values like
@@ -61,8 +61,8 @@
'diffs',
Digest::SHA1.hexdigest('/Hash.html') + '.html'
)

warn(diff)

expect(File.file?(diff)).to be true
expect(File.read(diff)).to include '#method-i-to_h'
end

0 comments on commit 22e6bfb

Please sign in to comment.
You can’t perform that action at this time.