Skip to content

Commit

Permalink
Land #579, Add rubocop to CI
Browse files Browse the repository at this point in the history
  • Loading branch information
adfoster-r7 authored Aug 15, 2023
2 parents f20c829 + 0961a7f commit 32777bf
Show file tree
Hide file tree
Showing 18 changed files with 313 additions and 247 deletions.
23 changes: 23 additions & 0 deletions .github/workflows/rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Rubocop

on: [push, pull_request]

permissions:
contents: read

jobs:
rubocop:
runs-on: ubuntu-latest
env:
BUNDLE_ONLY: rubocop

steps:
- uses: actions/checkout@v3

- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
bundler-cache: true

- name: Run Rubocop
run: bundle exec rubocop --parallel --format simple
41 changes: 41 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
inherit_from: .rubocop_todo.yml

AllCops:
TargetRubyVersion: 2.5.0
SuggestExtensions: false
NewCops: disable

Layout/LineLength:
Enabled: false

Style/NumericPredicate:
Description: 'This adds no efficiency nor space saving'
Enabled: false

Style/ZeroLengthPredicate:
Description: 'This adds no efficiency nor space saving'
Enabled: false

Style/StderrPuts:
Description: 'Replaces $stderr.puts with warn() which can suppress messages'
Enabled: false

Metrics/AbcSize:
Enabled: false
Description: 'This is often a red-herring'

Metrics/MethodLength:
Description: 'The style guide suggests 10 lines, bumping it up to the current maximum'
Max: 21

Metrics/BlockLength:
Description: 'The style guide suggests 10 lines, bumping it up to the current maximum'
Max: 135

Layout/CommentIndentation:
Exclude:
- 'spec/spec_helper.rb'

Style/BlockComments:
Exclude:
- 'spec/spec_helper.rb'
19 changes: 19 additions & 0 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2023-06-21 22:59:09 UTC using RuboCop version 1.52.1.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
# versions of RuboCop, may require this file to be generated again.

# Offense count: 3
Security/Open:
Exclude:
- 'misc/convert_mysql_err'
- 'spec/lib/fingerprint_self_test_spec.rb'

# Offense count: 6
# Configuration parameters: AllowedVariables.
Style/GlobalVars:
Exclude:
- 'bin/recog_standardize'
1 change: 1 addition & 0 deletions .ruby-gemset
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
recog
6 changes: 6 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

source 'https://rubygems.org'

gemspec name: 'recog-content'
Expand All @@ -8,3 +10,7 @@ group :test do
gem 'rake'
gem 'regexp_parser'
end

group :rubocop do
gem 'rubocop'
end
14 changes: 8 additions & 6 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
require "bundler/gem_tasks"
# frozen_string_literal: true

require 'bundler/gem_tasks'

require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new do |t|
t.pattern = 'spec/**/*_spec.rb'
t.pattern = 'spec/**/*_spec.rb'
end

require 'yard'
require 'yard/rake/yardoc_task'
YARD::Rake::YardocTask.new do |t|
t.files = ['bin/*.rb', '-', 'README.md']
t.files = ['bin/*.rb', '-', 'README.md']
end

require 'cucumber'
require 'cucumber/rake/task'

Cucumber::Rake::Task.new(:features) do |t|
t.cucumber_opts = %w(features --format pretty)
t.cucumber_opts = %w[features --format pretty]
end

task :default => [ :tests, :yard ]
task :tests => [ :spec, :features ]
task default: %i[tests yard]
task tests: %i[spec features]
15 changes: 8 additions & 7 deletions bin/recog_cleanup
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

require 'optparse'
require 'ostruct'

# Cleanup trailing whitespace around fingerprints
Dir[ File.expand_path(File.join(File.dirname(__FILE__), "..", "xml")) + "/*.xml" ].each do |f|
data = File.read(f).
gsub(/\s+$/, ''). # Trailing whitespace and empty lines
gsub("</fingerprint>", "</fingerprint>\n"). # Every fingerprint should have an empty line after it
gsub("-->", "-->\n") # Every comment should have an empty line after it
Dir["#{File.expand_path(File.join(File.dirname(__FILE__), '..', 'xml'))}/*.xml"].each do |f|
data = File.read(f)
.gsub(/\s+$/, '') # Trailing whitespace and empty lines
.gsub('</fingerprint>', "</fingerprint>\n") # Every fingerprint should have an empty line after it
.gsub('-->', "-->\n") # Every comment should have an empty line after it

File.write(f, data)
end
File.write(f, data)
end
42 changes: 20 additions & 22 deletions bin/recog_export
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

require 'optparse'
require 'ostruct'
Expand All @@ -8,46 +9,44 @@ def squash_lines(str)
str.split(/\n/).join(' ').gsub(/\s+/, ' ')
end

def export_text(options)
end
def export_text(options); end

def export_ruby(options)
$stdout.puts "# Recog fingerprint database export [ #{File.basename(options.xml_file)} ] on #{Time.now.to_s}"
$stdout.puts "# Recog fingerprint database export [ #{File.basename(options.xml_file)} ] on #{Time.now}"
$stdout.puts "fp_str = '' # Set this value to the match string"
$stdout.puts "fp_match = {} # Match results are stored here"
$stdout.puts ""
$stdout.puts "case fp_str"
$stdout.puts 'fp_match = {} # Match results are stored here'
$stdout.puts ''
$stdout.puts 'case fp_str'
options.db.fingerprints.each do |fp|
puts " # #{squash_lines fp.name}"
puts " when /#{fp.regex.to_s}/"
puts " when /#{fp.regex}/"
fp.tests.each do |test|
puts " # Example: #{squash_lines test}"
end
fp.params.each_pair do |k,v|
fp.params.each_pair do |k, v|
if v[0] == 0
puts " fp_match[#{k.inspect}] = #{v[1].inspect}"
else
puts " fp_match[#{k.inspect}] = $#{v[0].to_s}"
puts " fp_match[#{k.inspect}] = $#{v[0]}"
end
end
puts ""
puts ''
end
$stdout.puts "end"
$stdout.puts 'end'
end


options = OpenStruct.new(etype: :ruby)

option_parser = OptionParser.new do |opts|
opts.banner = "Usage: #{$0} [options] XML_FINGERPRINTS_FILE"
opts.separator "Exports an XML fingerprint database to another format."
opts.separator ""
opts.separator "Options"
opts.banner = "Usage: #{$PROGRAM_NAME} [options] XML_FINGERPRINTS_FILE"
opts.separator 'Exports an XML fingerprint database to another format.'
opts.separator ''
opts.separator 'Options'

opts.on("-t", "--type type",
"Choose a type of export.",
" [r]uby (default - export a ruby case statement with regular expressions)",
" [t]ext (export a text description of the fingerprints)") do |etype|
opts.on('-t', '--type type',
'Choose a type of export.',
' [r]uby (default - export a ruby case statement with regular expressions)',
' [t]ext (export a text description of the fingerprints)') do |etype|
case etype.downcase
when /^r/
options.etype = :ruby
Expand All @@ -56,7 +55,7 @@ option_parser = OptionParser.new do |opts|
end
end

opts.on("-h", "--help", "Show this message.") do
opts.on('-h', '--help', 'Show this message.') do
puts opts
exit
end
Expand All @@ -77,4 +76,3 @@ when :ruby
when :text
export_text(options)
end

41 changes: 21 additions & 20 deletions bin/recog_match
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

require 'optparse'
require 'ostruct'
Expand All @@ -8,47 +9,47 @@ require 'recog/matcher_factory'
options = OpenStruct.new(color: false, detail: false, json_format: false, fail_fast: false, multi_match: false)

option_parser = OptionParser.new do |opts|
opts.banner = "Usage: #{$0} [options] XML_FINGERPRINT_FILE [BANNERS_FILE]"
opts.separator "Identifies the matches and misses between the fingerprints and the banners file or STDIN"
opts.separator ""
opts.separator "Options"
opts.banner = "Usage: #{$PROGRAM_NAME} [options] XML_FINGERPRINT_FILE [BANNERS_FILE]"
opts.separator 'Identifies the matches and misses between the fingerprints and the banners file or STDIN'
opts.separator ''
opts.separator 'Options'

opts.on("-f", "--format FORMATTER", [:summary, :detail, :json],
"Choose a formatter.",
" [s]ummary (default - failure/match msgs)",
" [d]etail (msgs with total counts)",
" [j]son (JSON failure/match msgs)") do |format|
if format == :summary
opts.on('-f', '--format FORMATTER', %i[summary detail json],
'Choose a formatter.',
' [s]ummary (default - failure/match msgs)',
' [d]etail (msgs with total counts)',
' [j]son (JSON failure/match msgs)') do |format|
case format
when :summary
options.detail = false
options.json_format = false
elsif format == :detail
when :detail
options.detail = true
elsif format == :json
when :json
options.json_format = true
end
end

opts.on("--fail-fast [NUM]",
"Stop after number of failures (default: 10).") do |num|
opts.on('--fail-fast [NUM]',
'Stop after number of failures (default: 10).') do |num|
options.fail_fast = true
options.stop_after = (num.to_i == 0) ? 10 : num.to_i
options.stop_after = num.to_i == 0 ? 10 : num.to_i
end

opts.on("-c", "--color", "Enable color in the output.") do
opts.on('-c', '--color', 'Enable color in the output.') do
options.color = true
end

opts.on("--[no-]multi-match", "Enable or disable multiple matches (defaults to disabled)") do |o|
opts.on('--[no-]multi-match', 'Enable or disable multiple matches (defaults to disabled)') do |o|
options.multi_match = o
end

opts.on("-h", "--help", "Show this message.") do
opts.on('-h', '--help', 'Show this message.') do
puts opts
exit
end
end


begin
option_parser.parse!(ARGV)
rescue OptionParser::ParseError => e
Expand All @@ -65,4 +66,4 @@ end
ndb = Recog::DB.new(ARGV.shift)
options.fingerprints = ndb.fingerprints
matcher = Recog::MatcherFactory.build(options)
matcher.match_banners(ARGV.shift || "-")
matcher.match_banners(ARGV.shift || '-')
Loading

0 comments on commit 32777bf

Please sign in to comment.