Skip to content

Commit

Permalink
Enable pending cops
Browse files Browse the repository at this point in the history
Note that `Gemspec/RequireMFA` is enabled.

Please refer to the article below for RubyGems MFA status.
https://blog.rubygems.org/2022/08/15/requiring-mfa-on-popular-gems.html
  • Loading branch information
koic committed Aug 30, 2022
1 parent cca1e25 commit d113aab
Show file tree
Hide file tree
Showing 30 changed files with 80 additions and 68 deletions.
10 changes: 10 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
AllCops:
TargetRubyVersion: 2.7
NewCops: enable

# Suppress noise for obvious operator precedence.
Lint/AmbiguousOperatorPrecedence:
Description: Checks for expressions containing multiple binary operations with ambiguous precedence.
Enabled: false

Layout/LineLength:
Description: This cop checks the length of lines in the source code. The maximum length is configurable.
Expand Down Expand Up @@ -53,6 +59,10 @@ Style/Documentation:
Description: This cop checks for missing top-level documentation of classes and modules.
Enabled: false

Style/DocumentDynamicEvalDefinition:
Description: When using `class_eval` (or other `eval`) with string interpolation, add a comment block showing its appearance if interpolated.
Enabled: false

Style/EvalWithLocation:
Description: This cop checks eval method usage. eval can receive source location metadata, that are filename and line number.
Exclude:
Expand Down
2 changes: 2 additions & 0 deletions faker.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
$LOAD_PATH.push File.expand_path('lib', __dir__)
require 'faker/version'

# rubocop:todo Gemspec/RequireMFA
Gem::Specification.new do |spec|
spec.name = 'faker'
spec.version = Faker::VERSION
Expand Down Expand Up @@ -42,3 +43,4 @@ Gem::Specification.new do |spec|
spec.add_development_dependency('timecop', '0.9.5')
spec.add_development_dependency('yard', '0.9.27')
end
# rubocop:enable Gemspec/RequireMFA
2 changes: 1 addition & 1 deletion lib/faker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def regexify(reg)
.gsub(/(\\?.)\{(\d+),(\d+)\}/) { |_match| Regexp.last_match(1) * sample(Array(Range.new(Regexp.last_match(2).to_i, Regexp.last_match(3).to_i))) } # A{1,2} becomes A or AA or \d{3} becomes \d\d\d
.gsub(/\((.*?)\)/) { |match| sample(match.gsub(/[()]/, '').split('|')) } # (this|that) becomes 'this' or 'that'
.gsub(/\[([^\]]+)\]/) { |match| match.gsub(/(\w-\w)/) { |range| sample(Array(Range.new(*range.split('-')))) } } # All A-Z inside of [] become C (or X, or whatever)
.gsub(/\[([^\]]+)\]/) { |_match| sample(Regexp.last_match(1).split('')) } # All [ABC] become B (or A or C)
.gsub(/\[([^\]]+)\]/) { |_match| sample(Regexp.last_match(1).chars) } # All [ABC] become B (or A or C)
.gsub('\d') { |_match| sample(Numbers) }
.gsub('\w') { |_match| sample(Letters) }
end
Expand Down
2 changes: 1 addition & 1 deletion lib/faker/blockchain/aeternity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def oracle
def rand_strings(length = 50)
hex_alphabet = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
var = +''
length.times { var << sample(shuffle(hex_alphabet.split(''))) }
length.times { var << sample(shuffle(hex_alphabet.chars)) }
var
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/faker/blockchain/ethereum.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class << self
def address
hex_alphabet = '0123456789abcdef'
var = +'0x'
40.times { var << sample(shuffle(hex_alphabet.split(''))) }
40.times { var << sample(shuffle(hex_alphabet.chars)) }
var
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/faker/blockchain/tezos.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def secret_key
# @return [String]
def encode_tz(prefix, payload_size)
prefix = PREFIXES.fetch(prefix)
packed = prefix.map(&:chr).join('') + Faker::Config.random.bytes(payload_size)
packed = prefix.map(&:chr).join + Faker::Config.random.bytes(payload_size)
checksum = OpenSSL::Digest::SHA256.digest(OpenSSL::Digest::SHA256.digest(packed))[0..3]
Faker::Base58.encode(packed + checksum)
end
Expand Down
6 changes: 3 additions & 3 deletions lib/faker/default/bank.rb
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ def bsb_number
private

def checksum(num_string)
num_array = num_string.split('').map(&:to_i)
num_array = num_string.chars.map(&:to_i)
(
7 * (num_array[0] + num_array[3] + num_array[6]) +
3 * (num_array[1] + num_array[4] + num_array[7]) +
Expand Down Expand Up @@ -194,8 +194,8 @@ def valid_checksum?(routing_number, checksum)

def compile_fraction(routing_num)
prefix = (1..50).to_a.map(&:to_s).sample
numerator = routing_num.split('')[5..8].join.to_i.to_s
denominator = routing_num.split('')[0..4].join.to_i.to_s
numerator = routing_num.chars[5..8].join.to_i.to_s
denominator = routing_num.chars[0..4].join.to_i.to_s
"#{prefix}-#{numerator}/#{denominator}"
end

Expand Down
2 changes: 1 addition & 1 deletion lib/faker/default/chile_rut.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def rut(legacy_min_rut = NOT_GIVEN, legacy_fixed = NOT_GIVEN, min_rut: 1, fixed:
#
# @faker.version 1.9.2
def dv
split_reversed_rut = @last_rut.to_s.reverse.split('')
split_reversed_rut = @last_rut.to_s.reverse.chars
seq = [2, 3, 4, 5, 6, 7]
i = 0
digit_sum = split_reversed_rut.reduce(0) do |sum, n|
Expand Down
12 changes: 6 additions & 6 deletions lib/faker/default/code.rb
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ def generate_imei
str[len - 1] = (10 - (sum % 10)) % 10

# Output the IMEI value.
str.join('')
str.join
end

def generate_base10_isbn
Expand All @@ -222,34 +222,34 @@ def generate_base13_isbn
end

def sum(values)
values.split(//).each_with_index.inject(0) do |sum, (value, index)|
values.chars.each_with_index.inject(0) do |sum, (value, index)|
sum + yield(value, index)
end
end

def generate_base8_ean
values = regexify(/\d{7}/)
check_digit = 10 - values.split(//).each_with_index.inject(0) { |s, (v, i)| s + v.to_i * EAN_CHECK_DIGIT8[i] } % 10
check_digit = 10 - values.chars.each_with_index.inject(0) { |s, (v, i)| s + v.to_i * EAN_CHECK_DIGIT8[i] } % 10
values << (check_digit == 10 ? 0 : check_digit).to_s
end

def generate_base13_ean
values = regexify(/\d{12}/)
check_digit = 10 - values.split(//).each_with_index.inject(0) { |s, (v, i)| s + v.to_i * EAN_CHECK_DIGIT13[i] } % 10
check_digit = 10 - values.chars.each_with_index.inject(0) { |s, (v, i)| s + v.to_i * EAN_CHECK_DIGIT13[i] } % 10
values << (check_digit == 10 ? 0 : check_digit).to_s
end

EAN_CHECK_DIGIT8 = [3, 1, 3, 1, 3, 1, 3].freeze
EAN_CHECK_DIGIT13 = [1, 3, 1, 3, 1, 3, 1, 3, 1, 3, 1, 3].freeze

def rut_verificator_digit(rut)
total = rut.to_s.rjust(8, '0').split(//).zip(%w[3 2 7 6 5 4 3 2]).collect { |a, b| a.to_i * b.to_i }.inject(:+)
total = rut.to_s.rjust(8, '0').chars.zip(%w[3 2 7 6 5 4 3 2]).collect { |a, b| a.to_i * b.to_i }.inject(:+)
(11 - total % 11).to_s.gsub(/10/, 'k').gsub(/11/, '0')
end

def generate_nric_check_alphabet(values, prefix)
weight = %w[2 7 6 5 4 3 2]
total = values.split(//).zip(weight).collect { |a, b| a.to_i * b.to_i }.inject(:+)
total = values.chars.zip(weight).collect { |a, b| a.to_i * b.to_i }.inject(:+)
total += 4 if prefix == 'T'
%w[A B C D E F G H I Z J][10 - total % 11]
end
Expand Down
8 changes: 4 additions & 4 deletions lib/faker/default/company.rb
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ def polish_taxpayer_identification_number
result = Array.new(3) { rand(1..9) } + Array.new(7) { rand(10) }
break if (weight_sum(result, weights) % 11) == result[9]
end
result.join('')
result.join
end

##
Expand All @@ -321,7 +321,7 @@ def polish_register_of_national_economy(legacy_length = NOT_GIVEN, length: 9)
random_digits = Array.new(length) { rand(10) }
break if collect_regon_sum(random_digits) == random_digits.last
end
random_digits.join('')
random_digits.join
end

##
Expand Down Expand Up @@ -459,7 +459,7 @@ def mod11(number)
def luhn_algorithm(number)
multiplications = []

number.to_s.reverse.split(//).each_with_index do |digit, i|
number.to_s.reverse.chars.each_with_index do |digit, i|
multiplications << if i.even?
digit.to_i * 2
else
Expand Down Expand Up @@ -553,7 +553,7 @@ def inn_checksum(factor, number)
def spanish_cif_control_digit(organization_type, code)
letters = %w[J A B C D E F G H I]

control = code.split('').each_with_index.inject(0) do |sum, (value, index)|
control = code.chars.each_with_index.inject(0) do |sum, (value, index)|
if (index + 1).even?
sum + value.to_i
else
Expand Down
4 changes: 2 additions & 2 deletions lib/faker/default/finance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ def credit_card(*types)

# calculate the luhn checksum digit
multiplier = 1
luhn_sum = template.gsub(/[^0-9]/, '').split('').reverse.map(&:to_i).inject(0) do |sum, digit|
luhn_sum = template.gsub(/[^0-9]/, '').chars.reverse.map(&:to_i).inject(0) do |sum, digit|
multiplier = (multiplier == 2 ? 1 : 2)
sum + (digit * multiplier).to_s.split('').map(&:to_i).inject(0) { |digit_sum, cur| digit_sum + cur }
sum + (digit * multiplier).to_s.chars.map(&:to_i).inject(0) { |digit_sum, cur| digit_sum + cur }
end

# the sum plus whatever the last digit is must be a multiple of 10. So, the
Expand Down
2 changes: 1 addition & 1 deletion lib/faker/default/id_number.rb
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ def south_african_id_checksum_digit(id_number)
.with_index { |_, i| (i + 1).odd? }

sum_of_odd_digits = odd_digits_without_last_character.map(&:to_i).reduce(:+)
even_digits_times_two = (even_digits.join('').to_i * 2).to_s
even_digits_times_two = (even_digits.join.to_i * 2).to_s
sum_of_even_digits = even_digits_times_two.chars.map(&:to_i).reduce(:+)

total_sum = sum_of_odd_digits + sum_of_even_digits
Expand Down
6 changes: 3 additions & 3 deletions lib/faker/default/internet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ def fix_umlauts(legacy_string = NOT_GIVEN, string: '')
# @example
# Faker::Internet.domain_word #=> "senger"
def domain_word
with_locale(:en) { Char.prepare(Company.name.split(' ').first) }
with_locale(:en) { Char.prepare(Company.name.split.first) }
end

## Returns the domain suffix e.g. com, org, co, biz, info etc.
Expand Down Expand Up @@ -613,10 +613,10 @@ def sanitize_email_local_part(local_part)
Array('0'..'9'),
Array('A'..'Z'),
Array('a'..'z'),
"!#$%&'*+-/=?^_`{|}~.".split(//)
"!#$%&'*+-/=?^_`{|}~.".chars
].flatten

local_part.split(//).map do |char|
local_part.chars.map do |char|
char_range.include?(char) ? char : '#'
end.join
end
Expand Down
6 changes: 3 additions & 3 deletions lib/faker/default/markdown.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def headers
# @faker.version 1.8.0
def emphasis
paragraph = Faker::Lorem.paragraph(sentence_count: 3)
words = paragraph.split(' ')
words = paragraph.split
position = rand(0..words.length - 1)
formatting = fetch('markdown.emphasis')
words[position] = "#{formatting}#{words[position]}#{formatting}"
Expand All @@ -50,7 +50,7 @@ def ordered_list
number.times do |i|
result << "#{i}. #{Faker::Lorem.sentence(word_count: 1)} \n"
end
result.join('')
result.join
end

##
Expand All @@ -69,7 +69,7 @@ def unordered_list
number.times do |_i|
result << "* #{Faker::Lorem.sentence(word_count: 1)} \n"
end
result.join('')
result.join
end

##
Expand Down
2 changes: 1 addition & 1 deletion lib/faker/default/nhs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def british_number
.chars
.insert(3, ' ')
.insert(7, ' ')
.join('')
.join
end

##
Expand Down
10 changes: 5 additions & 5 deletions lib/faker/default/vehicle.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Vehicle < Base
VIN_LETTERS = 'ABCDEFGHJKLMNPRSTUVWXYZ'
VIN_MAP = '0123456789X'
VIN_WEIGHTS = '8765432X098765432'
VIN_REGEX = /^([A-HJ-NPR-Z0-9]){3}[A-HJ-NPR-Z0-9]{5}[A-HJ-NPR-Z0-9]{1}[A-HJ-NPR-Z0-9]{1}[A-HJ-NPR-Z0-0]{1}[A-HJ-NPR-Z0-9]{1}\d{5}$/.freeze
VIN_REGEX = /^([A-HJ-NPR-Z0-9]){3}[A-HJ-NPR-Z0-9]{5}[A-HJ-NPR-Z0-9]{1}[A-HJ-NPR-Z0-9]{1}[A-HJ-NPR-Z0-]{1}[A-HJ-NPR-Z0-9]{1}\d{5}$/.freeze

This comment has been minimized.

Copy link
@rmm5t

rmm5t Sep 14, 2022

Contributor

@koic This regex change can cause invalid VINs to be generated.

I presume the character class should have originally been 0-9 instead of 0-0, but the new character class of [0-] allows for dashes in a Vehicle's VIN (which is invalid).

SG_CHECKSUM_WEIGHTS = [3, 14, 2, 12, 2, 11, 1].freeze
SG_CHECKSUM_CHARS = 'AYUSPLJGDBZXTRMKHEC'

Expand Down Expand Up @@ -333,22 +333,22 @@ def calculate_vin_check_digit(vin)
end

def vin_char_to_number(char)
index = VIN_LETTERS.split('').index(char)
index = VIN_LETTERS.chars.index(char)

return char.to_i if index.nil?

VIN_MAP[index]
end

def singapore_checksum(plate_number)
padded_alphabets = format('%3s', plate_number[/^[A-Z]+/]).tr(' ', '-').split('')
padded_digits = format('%04d', plate_number[/\d+/]).split('').map(&:to_i)
padded_alphabets = format('%3s', plate_number[/^[A-Z]+/]).tr(' ', '-').chars
padded_digits = format('%04d', plate_number[/\d+/]).chars.map(&:to_i)
sum = [*padded_alphabets, *padded_digits].each_with_index.reduce(0) do |memo, (char, i)|
value = char.is_a?(Integer) ? char : char.ord - 64
memo + (SG_CHECKSUM_WEIGHTS[i] * value)
end

SG_CHECKSUM_CHARS.split('')[sum % 19]
SG_CHECKSUM_CHARS.chars[sum % 19]
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/faker/japanese_media/kamen_rider.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,14 @@ def eras
end

def from_eras(*input_eras, field:)
selected_eras = (ERAS & input_eras).yield_self do |selected|
selected_eras = (ERAS & input_eras).then do |selected|
selected.empty? ? eras : selected
end.dup
yield(selected_eras) if block_given?

raise UnavailableInEra, "#{field} is unavailable in the selected eras." if selected_eras.empty?

selected_eras.sample.yield_self do |era|
selected_eras.sample.then do |era|
fetch("kamen_rider.#{era}.#{field}")
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/helpers/base58.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def self.encode(str)
base = alphabet.size

lv = 0
str.split('').reverse.each_with_index { |v, i| lv += v.unpack1('C') * 256**i }
str.chars.reverse.each_with_index { |v, i| lv += v.unpack1('C') * 256**i }

ret = +''
while lv.positive?
Expand Down
2 changes: 1 addition & 1 deletion script/txt2html
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ else
exit!
end

template = ERB.new(File.open(template).read)
template = ERB.new(File.read(template))

title = nil
body = nil
Expand Down
2 changes: 1 addition & 1 deletion tasks/website.rake
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ desc 'Upload website files to rubyforge'
task :website_upload do
remote_dir = '/var/www/gforge-projects/faker/'
local_dir = 'website'
sh %(rsync -acCv #{local_dir}/ #{ENV['USER']}@rubyforge.org:#{remote_dir})
sh %(rsync -acCv #{local_dir}/ #{ENV.fetch('USER', nil)}@rubyforge.org:#{remote_dir})
end

desc 'Generate and upload website files'
Expand Down
4 changes: 2 additions & 2 deletions test/faker/default/test_alphanum.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ def test_alphanumeric_with_invalid_mins
end

def test_alphanumeric_with_min_alpha
letters = @tester.alphanumeric(number: 5, min_alpha: 2).split('').map do |char|
letters = @tester.alphanumeric(number: 5, min_alpha: 2).chars.map do |char|
char =~ /[[:alpha:]]/
end
assert letters.compact.size >= 2
end

def test_alphanumeric_with_min_numeric
numbers = @tester.alphanumeric(number: 5, min_numeric: 4).split('').map do |char|
numbers = @tester.alphanumeric(number: 5, min_numeric: 4).chars.map do |char|
char =~ /[[:digit:]]/
end
assert numbers.compact.size >= 4
Expand Down
Loading

2 comments on commit d113aab

@koic
Copy link
Member Author

@koic koic commented on d113aab Aug 30, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vbrazo This commit makes Gemspec/RequireMFA cop pending. Please note that MFA will be required upon release.
https://guides.rubygems.org/setting-up-multifactor-authentication/

@koic
Copy link
Member Author

@koic koic commented on d113aab Aug 30, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've opened #2549 to enable rubygems_mfa_required.

Please sign in to comment.