Browse Source

Rubocop fixes

develop
Christian Aust 3 months ago
parent
commit
488e83a235
  1. 84
      lib/clover.rb
  2. 1
      lib/sanity/output/junit.rb
  3. 2
      lib/sanity/output/report.rb
  4. 4
      lib/sanity/output/ssl_calendar.rb
  5. 14
      lib/sanity/result.rb
  6. 10
      lib/sanity/runner.rb
  7. 3
      lib/sanity/tests/bamboo.rb
  8. 7
      lib/sanity/tests/bitbucket.rb
  9. 2
      lib/sanity/tests/ssl.rb
  10. 12
      lib/string_ext.rb
  11. 30
      sanity.gemspec
  12. 6
      spec/sanity/output/junit_spec.rb
  13. 4
      spec/sanity/output/ssl_calendar_spec.rb
  14. 4
      spec/sanity/output/stat_spec.rb
  15. 7
      spec/sanity/tests/dns_spec.rb
  16. 8
      spec/sanity/tests/ssl_spec.rb
  17. 6
      spec/sanity/tests/static_web_spec.rb
  18. 4
      spec/string_ext_spec.rb

84
lib/clover.rb

@ -1,52 +1,54 @@
# frozen_string_literal: true
require 'nokogiri'
require 'pp'
class SimpleCov::Formatter::Clover
module SimpleCov
module Formatter
class Clover
def format(result)
SimpleCov::Formatter::HTMLFormatter.new.format(result)
ts = result.created_at.to_i
builder = Nokogiri::XML::Builder.new do |xml|
xml.coverage(generated: ts, clover: "SimpleCov #{SimpleCov::VERSION}") do
xml.project(timestamp: ts) do
xml.metrics(
files: result.files.count,
elements: result.total_lines,
coveredelements: result.covered_lines,
loc: result.covered_lines + result.missed_lines,
ncloc: result.total_lines
)
def format(result)
SimpleCov::Formatter::HTMLFormatter.new.format(result)
ts = result.created_at.to_i
builder = Nokogiri::XML::Builder.new do |xml|
xml.coverage(generated: ts, clover: "SimpleCov #{SimpleCov::VERSION}") {
xml.project(timestamp: ts) {
xml.metrics(
files: result.files.count,
elements: result.total_lines,
coveredelements: result.covered_lines,
loc: result.covered_lines + result.missed_lines,
ncloc: result.total_lines
)
xml.package(name: 'All files') {
result.files.each do |file|
xml.file(name: File.basename(file.filename), path:shortened_filename(file)) {
xml.metrics(
classes: 1,
loc: file.lines.count,
ncloc: file.covered_lines.count + file.missed_lines.count
)
file.lines.each do |line|
xml.line(
line: line.number,
type: 'stmt',
count: (line.covered? ? line.coverage : 0)
)
xml.package(name: 'All files') do
result.files.each do |file|
xml.file(name: File.basename(file.filename), path: shortened_filename(file)) do
xml.metrics(
classes: 1,
loc: file.lines.count,
ncloc: file.covered_lines.count + file.missed_lines.count
)
file.lines.each do |line|
xml.line(
line: line.number,
type: 'stmt',
count: (line.covered? ? line.coverage : 0)
)
end
end
end
}
end
end
}
}
}
end
end
end
File.open(File.join(SimpleCov.coverage_path, 'clover.xml'), 'w') do |f|
f << builder.to_xml
end
end
File.open(File.join(SimpleCov.coverage_path, 'clover.xml'), 'w') do |f|
f << builder.to_xml
end
end
def shortened_filename(source_file)
source_file.filename.sub(SimpleCov.root, ".").gsub(/^\.\//, "")
def shortened_filename(source_file)
source_file.filename.sub(SimpleCov.root, '.').gsub(%r{^\./}, '')
end
end
end
end

1
lib/sanity/output/junit.rb

@ -48,6 +48,7 @@ module Sanity
def should_ignore?(var_name)
return true if IGNORED_ENV_VARS.include?(var_name)
return true if var_name =~ /rvm_/
false
end

2
lib/sanity/output/report.rb

@ -87,7 +87,7 @@ module Sanity
File.expand_path('report.html.erb', File.dirname(__FILE__))
)
@hostname = Socket.gethostname
@suites = suites.sort{|s1, s2| s1.result_name <=> s2.result_name }
@suites = suites.sort { |s1, s2| s1.result_name <=> s2.result_name }
erb = ERB.new(template, trim_mode: '%')
erb.result(binding)

4
lib/sanity/output/ssl_calendar.rb

@ -29,7 +29,7 @@ module Sanity
certs = suite.result.incidents.map { |incident| incident.facts[:cert] }.compact.uniq
certs.each do |cert|
common_name = cert.subject.to_a.find{|a| a[0] == 'CN'}[1]
common_name = cert.subject.to_a.find { |a| a[0] == 'CN' }[1]
cal.event do |e|
e.dtstart = Icalendar::Values::Date.new(cert.not_after)
e.dtend = Icalendar::Values::Date.new(cert.not_after)
@ -38,7 +38,7 @@ module Sanity
e.alarm do |a|
a.action = 'DISPLAY'
a.summary = "Prepare SSL certificate renewal"
a.summary = 'Prepare SSL certificate renewal'
a.description = "#{common_name} expires on #{cert.not_after}"
# see https://datatracker.ietf.org/doc/html/rfc5545#section-3.3.6
a.trigger = '-P4W' # 4 weeks before

14
lib/sanity/result.rb

@ -10,7 +10,7 @@ module Sanity
def initialize(name, method)
@assertion_count = 0
@facts = Hash.new
@facts = {}
@method = method
@name = name
@time_start = Time.now
@ -94,12 +94,12 @@ module Sanity
def rating
case (100.0 * successes.size.to_f / @incidents.size.to_f).to_i
when 90..100 then 'A'
when 80..90 then 'B'
when 70..80 then 'C'
when 60..70 then 'D'
when 50..60 then 'E'
else 'F'
when 90..100 then 'A'
when 80..90 then 'B'
when 70..80 then 'C'
when 60..70 then 'D'
when 50..60 then 'E'
else 'F'
end
end
end

10
lib/sanity/runner.rb

@ -20,13 +20,11 @@ module Sanity
log.info "Loaded from IO #{options.targets}"
elsif options.targets.is_a?(Array)
# file-path IO, multiple sources
@specs = Hash.new
@specs = {}
options.targets.each do |s|
files = Dir.glob(s)
if files.empty?
Runner.log.error "No file matched #{s}"
end
files.each do|file|
Runner.log.error "No file matched #{s}" if files.empty?
files.each do |file|
File.open(file) do |f|
@specs.merge! JSON.parse(File.new(f).read)
end
@ -40,7 +38,7 @@ module Sanity
@specs = JSON.parse File.new(f).read
Runner.log.info "Loaded #{options.targets}"
end
rescue Errno::ENOENT => ex
rescue Errno::ENOENT => e
Runner.log.error "No such file #{options.targets}"
end
else

3
lib/sanity/tests/bamboo.rb

@ -9,7 +9,8 @@ module Sanity
BASE_URL = 'https://bamboo.zurich.com'
def setup
raise "BAMBOO_TOKEN unavailable" if token.nil?
raise 'BAMBOO_TOKEN unavailable' if token.nil?
@client = RestClient::Resource.new(
"#{BASE_URL}/rest",
{

7
lib/sanity/tests/bitbucket.rb

@ -12,7 +12,8 @@ module Sanity
MIN_README_LENGTH = 100
def setup
raise "BITBUCKET_TOKEN unavailable" if token.nil?
raise 'BITBUCKET_TOKEN unavailable' if token.nil?
@project_key, @repository_slug = target.split('/')
@client = RestClient::Resource.new(
"#{BASE_URL}/rest",
@ -35,7 +36,7 @@ module Sanity
data = JSON.parse(response.body)['values']
if develop_restriction = data.find do |e|
e['type'] == 'pull-request-only' &&
%w[develop development].include?(e['matcher']['id'])
%w[develop development].include?(e['matcher']['id'])
end
assert_true develop_restriction['matcher']['active'], 'Development PR Restriction is not active'
else
@ -50,7 +51,7 @@ module Sanity
data = JSON.parse(response.body)['values']
if develop_restriction = data.find do |e|
e['type'] == 'pull-request-only' &&
%w[master production].include?(e['matcher']['id'])
%w[master production].include?(e['matcher']['id'])
end
assert_true develop_restriction['matcher']['active'], 'Production PR Restriction is not active'
else

2
lib/sanity/tests/ssl.rb

@ -32,7 +32,7 @@ module Sanity
def test_not_near_expiration
in_30_days = Time.now + (30 * 24 * 60 * 60)
assert_present @cert, "No SSL certificate given"
assert_present @cert, 'No SSL certificate given'
assert_greater @cert.not_after, in_30_days, "Expiration date #{@cert.not_after} is within 30 days"
end
end

12
lib/string_ext.rb

@ -5,12 +5,12 @@ class String
# https://apidock.com/rails/String/camelize
def camelize(uppercase_first_letter = true)
string = self
if uppercase_first_letter
string = string.sub(/^[a-z\d]*/) { |match| match.capitalize }
else
string = string.sub(/^(?:(?=\b|[A-Z_])|\w)/) { |match| match.downcase }
end
string.gsub(/(?:_|(\/))([a-z\d]*)/) { "#{$1}#{$2.capitalize}" }.gsub("/", "::")
string = if uppercase_first_letter
string.sub(/^[a-z\d]*/, &:capitalize)
else
string.sub(/^(?:(?=\b|[A-Z_])|\w)/, &:downcase)
end
string.gsub(%r{(?:_|(/))([a-z\d]*)}) { "#{Regexp.last_match(1)}#{Regexp.last_match(2).capitalize}" }.gsub('/', '::')
end
def to_class

30
sanity.gemspec

@ -1,29 +1,31 @@
# frozen_string_literal: true
require_relative 'lib/sanity/version'
Gem::Specification.new do |spec|
spec.name = "sanity"
spec.name = 'sanity'
spec.version = Sanity::Version.to_s
spec.authors = ["Christian Aust", "Nicolas Mertens", "Malte Anthes"]
spec.email = ["christian.aust@zurich.com"]
spec.authors = ['Christian Aust', 'Nicolas Mertens', 'Malte Anthes']
spec.email = ['christian.aust@zurich.com']
spec.summary = %q{Sanity runs sanity checks on targets you define.}
spec.description = %q{Sanity runs sanity checks on targets you define. It's meant to be run by CI pipelines and the like, producing output like a unit test suite.}
spec.homepage = "https://bitbucket.zurich.com/projects/ZGDIT/repos/de-zurich-sanity"
spec.license = "MIT"
spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
spec.summary = 'Sanity runs sanity checks on targets you define.'
spec.description = "Sanity runs sanity checks on targets you define. It's meant to be run by CI pipelines and the like, producing output like a unit test suite."
spec.homepage = 'https://bitbucket.zurich.com/projects/ZGDIT/repos/de-zurich-sanity'
spec.license = 'MIT'
spec.required_ruby_version = Gem::Requirement.new('>= 2.3.0')
spec.metadata["homepage_uri"] = spec.homepage
spec.metadata["source_code_uri"] = spec.homepage
spec.metadata["changelog_uri"] = "https://bitbucket.zurich.com/projects/ZGDIT/repos/de-zurich-sanity/browse/semver.txt"
spec.metadata['homepage_uri'] = spec.homepage
spec.metadata['source_code_uri'] = spec.homepage
spec.metadata['changelog_uri'] = 'https://bitbucket.zurich.com/projects/ZGDIT/repos/de-zurich-sanity/browse/semver.txt'
# Specify which files should be added to the gem when it is released.
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
spec.files = Dir.chdir(File.expand_path(__dir__)) do
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
end
spec.bindir = "exe"
spec.bindir = 'exe'
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
spec.require_paths = ["lib"]
spec.require_paths = ['lib']
spec.add_runtime_dependency 'colorize', '~> 0.8.1'
spec.add_runtime_dependency 'dnsruby', '~> 1.61.5'

6
spec/sanity/output/junit_spec.rb

@ -15,14 +15,14 @@ RSpec.describe Sanity::Output::Junit do
let(:suite) do
Sanity::Suite.new('mock').tap do |s|
s.result << Sanity::Result::Incident.new('mock-test', 'rspec').tap do |i|
i.instance_eval{ @time_end = @time_start + 1 }
i.instance_eval { @time_end = @time_start + 1 }
end
s.result << Sanity::Result::Incident.new('mock-test', 'rspec').tap do |i|
i.instance_eval{ @time_end = @time_start + 1 }
i.instance_eval { @time_end = @time_start + 1 }
i.failure = 'A failure'
end
s.result << Sanity::Result::Incident.new('mock-test', 'rspec').tap do |i|
i.instance_eval{ @time_end = @time_start + 1 }
i.instance_eval { @time_end = @time_start + 1 }
i.error = 'An error'
end
end

4
spec/sanity/output/ssl_calendar_spec.rb

@ -13,7 +13,7 @@ RSpec.describe Sanity::Output::SslCalendar do
let(:suite) do
Sanity::Suite.new('mock').tap do |s|
s.result << Sanity::Result::Incident.new('mock-test', 'rspec').tap do |i|
i.instance_eval{ @time_end = @time_start + 1 }
i.instance_eval { @time_end = @time_start + 1 }
end
end
end
@ -22,7 +22,7 @@ RSpec.describe Sanity::Output::SslCalendar do
it 'produces ics as String' do
expect(output).to be_a String
expect(output).to match /\ABEGIN:VCALENDAR(.*)END:VCALENDAR/m
expect(output).to match(/\ABEGIN:VCALENDAR(.*)END:VCALENDAR/m)
end
end
end

4
spec/sanity/output/stat_spec.rb

@ -2,8 +2,8 @@
RSpec.describe Sanity::Output::Stat do
let(:success) { Sanity::Result::Incident.new('name', 'test_some_method') }
let(:failure) { Sanity::Result::Incident.new('name', 'test_some_method').tap{|i| i.failure = 'Some failure'} }
let(:error) { Sanity::Result::Incident.new('name', 'test_some_method').tap{|i| i.error = 'Some error'} }
let(:failure) { Sanity::Result::Incident.new('name', 'test_some_method').tap { |i| i.failure = 'Some failure' } }
let(:error) { Sanity::Result::Incident.new('name', 'test_some_method').tap { |i| i.error = 'Some error' } }
subject do
described_class.new('foo').tap do |s|

7
spec/sanity/tests/dns_spec.rb

@ -2,9 +2,7 @@
RSpec.describe Sanity::Tests::DNS do
subject do
Sanity::Tests::DNS.new('VALID_TARGET').tap do |s|
s.setup
end
Sanity::Tests::DNS.new('VALID_TARGET').tap(&:setup)
end
describe '#initialize' do
@ -15,7 +13,7 @@ RSpec.describe Sanity::Tests::DNS do
describe '#to_s' do
it 'mentions DNS' do
expect(subject.to_s).to match /DNS/
expect(subject.to_s).to match(/DNS/)
end
end
@ -34,5 +32,4 @@ RSpec.describe Sanity::Tests::DNS do
expect { subject.test_if_resolvable }.not_to raise_error
end
end
end

8
spec/sanity/tests/ssl_spec.rb

@ -5,7 +5,7 @@ RSpec.describe Sanity::Tests::SSL do
[
true,
nil,
OpenSSL::X509::Certificate.new.tap{|c| c.not_after = Time.now + (90 * 24 * 60 * 60) }
OpenSSL::X509::Certificate.new.tap { |c| c.not_after = Time.now + (90 * 24 * 60 * 60) }
]
end
@ -14,9 +14,7 @@ RSpec.describe Sanity::Tests::SSL do
end
subject do
Sanity::Tests::SSL.new('VALID_TARGET').tap do |s|
s.setup
end
Sanity::Tests::SSL.new('VALID_TARGET').tap(&:setup)
end
describe '#initialize' do
@ -27,7 +25,7 @@ RSpec.describe Sanity::Tests::SSL do
describe '#to_s' do
it 'mentions SSL' do
expect(subject.to_s).to match /SSL/
expect(subject.to_s).to match(/SSL/)
end
end

6
spec/sanity/tests/static_web_spec.rb

@ -2,9 +2,7 @@
RSpec.describe Sanity::Tests::StaticWeb do
subject do
Sanity::Tests::StaticWeb.new('http://www.foo.com').tap do |s|
s.setup
end
Sanity::Tests::StaticWeb.new('http://www.foo.com').tap(&:setup)
end
before do
@ -26,7 +24,7 @@ RSpec.describe Sanity::Tests::StaticWeb do
describe '#to_s' do
it 'mentions Static web' do
expect(subject.to_s).to match /Static\ web/
expect(subject.to_s).to match(/Static\ web/)
end
end

4
spec/string_ext_spec.rb

@ -2,12 +2,12 @@
RSpec.describe 'String extensions' do
describe '#camelize' do
let(:tests) {
let(:tests) do
{
'foo_bar' => 'FooBar',
'baz/foo_bar' => 'Baz::FooBar'
}
}
end
it 'handles tests successfully' do
tests.each do |src, dst|

Loading…
Cancel
Save