Permalink
Browse files

Cleanup according to rubocop (rebase) (#2043)

  • Loading branch information...
1 parent 989f756 commit 9e8b7e333f53c8b548b2b9daa8c356f0557c73f8 @stephengroat stephengroat committed with Carlgo11 Oct 16, 2016
Showing with 90 additions and 96 deletions.
  1. +1 −0 Gemfile
  2. +6 −2 Rakefile
  3. +83 −94 verify.rb
View
@@ -9,4 +9,5 @@ gem 'kramdown', '~> 1.10.0'
group :test do
gem 'rake'
gem 'fastimage'
+ gem 'rubocop'
end
View
@@ -1,5 +1,9 @@
-task :default => [:verify]
+require 'rubocop/rake_task'
+
+RuboCop::RakeTask.new
+
+task default: %w(verify rubocop)
task :verify do
- ruby "./verify.rb"
+ ruby './verify.rb'
end
View
@@ -17,122 +17,111 @@
# TFA forms
@tfa_forms = %w(email hardware software sms phone)
-# Should the script ignore checking the image size?
-@ignore_image_size = false
-
# Image max size (in bytes)
-@image_max_size = 2500
+@img_max_size = 2500
-# Image format used for all images in the 'img/' directories.
-@image_extension = ".png"
+# Image dimensions
+@img_dimensions = [32, 32]
-begin
+# Image format used for all images in the 'img/' directories.
+@img_extension = '.png'
- # Send error message
- def error(msg)
- @output += 1
- puts "<------------ ERROR ------------>\n" if @output == 1
- puts "#{@output}. #{msg}"
- end
+# Send error message
+def error(msg)
+ @output += 1
+ puts "<------------ ERROR ------------>\n" if @output == 1
+ puts "#{@output}. #{msg}"
+end
- # Validate an individual YAML tag
- def check_tag(tag, required, tfa_state, website, only_true = false)
- if website[tag].nil?
- if website['tfa'] == tfa_state && required
- error("#{website['name']}: The required YAML tag \'#{tag}\' tag is not present.")
- end
- else
- if website['tfa'] != tfa_state
- state = website['tfa'] ? "enabled" : "disabled"
- error("#{website['name']}: The YAML tag \'#{tag}\' should NOT be present when TFA is #{state}.")
- end
- if only_true && website[tag] != true
- error("#{website['name']}: The YAML tag \'#{tag}\' should either have a value set to \'Yes\' or not be used at all. (Current value: \'#{website[tag]}\')")
- end
- end
+# Test an individual YAML tag
+# rubocop:disable AbcSize,CyclomaticComplexity,MethodLength,PerceivedComplexity
+def test_tag(tag, required, tfa_state, website, only_true = false)
+ if website[tag].nil?
+ error("#{website['name']}: The required YAML tag \'#{tag}\' tag is "\
+ 'not present.') if website['tfa'] == tfa_state && required
+ return
end
-
- # Validate the YAML tags
- def validate_tags(website)
- tfa = website['tfa']
- if tfa != true && tfa != false
- error("#{website['name']}: The YAML tag \'#{tag}\' should be either \'Yes\' or \'No\'. (#{tfa})")
- end
-
- # Validate tags that are obligatory
- @obligatory_tags.each do |t|
- tag = website[t]
- next unless tag.nil?
- error("#{website['name']}: The required YAML tag \'#{t}\' tag is not present.")
- end
-
- # Validate tags associated with TFA 'YES'
- @tfa_yes_tags.each do |tfa_form|
- check_tag(tfa_form, false, true, website)
- end
-
- # Validate TFA form tags'
- @tfa_forms.each do |tfa_form|
- check_tag(tfa_form, false, true, website, true)
- end
-
- # Validate tags associated with TFA 'NO'
- @tfa_no_tags.each do |tfa_form|
- check_tag(tfa_form, false, false, website)
- end
+ error("#{website['name']}: The YAML tag \'#{tag}\' should NOT be "\
+ "present when TFA is #{website['tfa'] ? 'enabled' : 'disabled'}.")\
+ if website['tfa'] != tfa_state
+ error("#{website['name']}: The YAML tag \'#{tag}\' should either have"\
+ " a value set to \'Yes\' or not be used at all. (Current value:"\
+ " \'#{website[tag]}\')") if only_true && website[tag] != true
+end
+# rubocop:enable PerceivedComplexity
+
+# Check the YAML tags
+def test_tags(website)
+ tfa = website['tfa']
+ # rubocop:disable DoubleNegation
+ error("#{website['name']}: The YAML tag \'{tfa}\' should be either "\
+ "\'Yes\' or \'No\'. (#{tfa})") if !!tfa != tfa
+ # rubocop:endable DoubleNegation
+
+ # Test tags that are obligatory
+ @obligatory_tags.each do |t|
+ next unless website[t].nil?
+ error("#{website['name']}: The required YAML tag \'#{t}\' tag is not"\
+ ' present.')
end
- def validate_image(image, name, images)
- if File.exist?(image)
- if images.index(image) != nil
- images.delete_at(images.index(image))
- end
-
- image_dimensions = [32, 32]
-
- unless FastImage.size(image) == image_dimensions
- error("#{image} is not #{image_dimensions.join('x')} pixels.")
- end
+ # Test tags associated with TFA 'YES'
+ @tfa_yes_tags.each { |tfa_form| test_tag(tfa_form, false, true, website) }
- error("#{image} is not using the #{@image_extension} format.") unless File.extname(image) == @image_extension
+ # Test TFA form tags'
+ @tfa_forms.each { |tfa_form| test_tag(tfa_form, false, true, website, true) }
- unless @ignore_image_size
- image_size = File.size(image)
- error("#{image} should not be larger than #{@image_max_size} bytes. It is currently #{image_size} bytes.") unless image_size <= @image_max_size
- end
+ # Test tags associated with TFA 'NO'
+ @tfa_no_tags.each { |tfa_form| test_tag(tfa_form, false, false, website) }
+end
+# rubocop:enable MethodLength
+
+def test_img(img, name, imgs)
+ # Exception if image file not found
+ raise "#{name} image not found." unless File.exist?(img)
+ # Remove img from array unless it doesn't exist (double reference case)
+ imgs.delete_at(imgs.index(img)) unless imgs.index(img).nil?
+
+ # Check image dimensions
+ error("#{img} is not #{@img_dimensions.join('x')} pixels.")\
+ unless FastImage.size(img) == @img_dimensions
+
+ # Check image file extension and type
+ error("#{img} is not using the #{@img_extension} format.")\
+ unless File.extname(img) == @img_extension && FastImage.type(img) == :png
+
+ # Check image file size
+ img_size = File.size(img)
+ error("#{img} should not be larger than #{@img_max_size} bytes. It is"\
+ " currently #{img_size} bytes.") unless img_size <= @img_max_size
+end
+# rubocop:enable AbcSize,CyclomaticComplexity
- else
- error("#{name} image not found.")
- end
- end
+begin
# Load each section, check for errors such as invalid syntax
# as well as if an image is missing
-
sections = YAML.load_file('_data/sections.yml')
sections.each do |section|
-
data = YAML.load_file('_data/' + section['id'] + '.yml')
-
- if data['websites'] != data['websites'].sort_by { |h| h['name'].downcase }
- error("_data/#{section['id']}.yml is not alphabetized by name")
- end
+ websites = data['websites']
- images = Dir["img/#{section['id']}/*"]
+ # Check section alphabetization
+ error("_data/#{section['id']}.yml is not alphabetized by name") \
+ if websites != websites.sort_by { |website| website['name'].downcase }
- data['websites'].each do |website|
-
- validate_tags(website)
- validate_image("img/#{section['id']}/#{website['img']}", website['name'], images)
-
- end
+ # Collect list of all images for section
+ imgs = Dir["img/#{section['id']}/*"]
- if not images.empty?
- images.each do |image|
- error("#{image} is an unused file")
- end
+ websites.each do |website|
+ test_tags(website)
+ test_img("img/#{section['id']}/#{website['img']}", website['name'],
+ imgs)
end
+ # After removing images associated with entries in test_img, alert
+ # for unused or orphaned images
+ imgs.each { |img| next unless img.nil? error("#{img} is not used") }
end
exit 1 if @output > 0

0 comments on commit 9e8b7e3

Please sign in to comment.