Skip to content

Commit

Permalink
Merge pull request #8314 from reitermarkus/audit-language
Browse files Browse the repository at this point in the history
Fix auditing of cask languages.
  • Loading branch information
reitermarkus authored Aug 11, 2020
2 parents 3e5ff1f + 9ea1dcf commit 2125d49
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 23 deletions.
9 changes: 3 additions & 6 deletions Library/Homebrew/cask/audit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -330,14 +330,11 @@ def check_generic_artifacts
end

def check_languages
invalid = []
@cask.languages.each do |language|
invalid << language.to_s unless language.match?(/^[a-z]{2}$/) || language.match?(/^[a-z]{2}-[A-Z]{2}$/)
Locale.parse(language)
rescue Locale::ParserError
add_error "Locale '#{language}' is invalid."
end

return if invalid.empty?

add_error "locale #{invalid.join(", ")} are invalid"
end

def check_token_conflicts
Expand Down
2 changes: 1 addition & 1 deletion Library/Homebrew/cask/dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def language(*args, default: false, &block)
end

def language_eval
return @language if instance_variable_defined?(:@language)
return @language if defined?(@language)

return @language = nil if @language_blocks.nil? || @language_blocks.empty?

Expand Down
62 changes: 48 additions & 14 deletions Library/Homebrew/locale.rb
Original file line number Diff line number Diff line change
@@ -1,27 +1,56 @@
# frozen_string_literal: true

# Representation of a system locale.
#
# Used to compare the system language and languages defined using cask `language` stanza.
#
# @api private
class Locale
# Error when a string cannot be parsed to a `Locale`.
class ParserError < StandardError
end

LANGUAGE_REGEX = /(?:[a-z]{2,3})/.freeze # ISO 639-1 or ISO 639-2
REGION_REGEX = /(?:[A-Z]{2}|\d{3})/.freeze # ISO 3166-1 or UN M.49
SCRIPT_REGEX = /(?:[A-Z][a-z]{3})/.freeze # ISO 15924
# ISO 639-1 or ISO 639-2
LANGUAGE_REGEX = /(?:[a-z]{2,3})/.freeze
private_constant :LANGUAGE_REGEX

# ISO 3166-1 or UN M.49
REGION_REGEX = /(?:[A-Z]{2}|\d{3})/.freeze
private_constant :REGION_REGEX

# ISO 15924
SCRIPT_REGEX = /(?:[A-Z][a-z]{3})/.freeze
private_constant :SCRIPT_REGEX

LOCALE_REGEX = /\A((?:#{LANGUAGE_REGEX}|#{REGION_REGEX}|#{SCRIPT_REGEX})(?:-|$)){1,3}\Z/.freeze
private_constant :LOCALE_REGEX

def self.parse(string)
string = string.to_s
if locale = try_parse(string)
return locale
end

raise ParserError, "'#{string}' cannot be parsed to a #{self}"
end

def self.try_parse(string)
return if string.blank?

raise ParserError, "'#{string}' cannot be parsed to a #{self}" unless string.match?(LOCALE_REGEX)
scanner = StringScanner.new(string)

scan = proc do |regex|
string.scan(/(?:-|^)(#{regex})(?:-|$)/).flatten.first
if language = scanner.scan(LANGUAGE_REGEX)
sep = scanner.scan(/-/)
return if (sep && scanner.eos?) || (sep.nil? && !scanner.eos?)
end

language = scan.call(LANGUAGE_REGEX)
region = scan.call(REGION_REGEX)
script = scan.call(SCRIPT_REGEX)
if region = scanner.scan(REGION_REGEX)
sep = scanner.scan(/-/)
return if (sep && scanner.eos?) || (sep.nil? && !scanner.eos?)
end

script = scanner.scan(SCRIPT_REGEX)

return unless scanner.eos?

new(language, region, script)
end
Expand All @@ -46,7 +75,10 @@ def initialize(language, region, script)
end

def include?(other)
other = self.class.parse(other) unless other.is_a?(self.class)
unless other.is_a?(self.class)
other = self.class.try_parse(other)
return false if other.nil?
end

[:language, :region, :script].all? do |var|
if other.public_send(var).nil?
Expand All @@ -58,12 +90,14 @@ def include?(other)
end

def eql?(other)
other = self.class.parse(other) unless other.is_a?(self.class)
unless other.is_a?(self.class)
other = self.class.try_parse(other)
return false if other.nil?
end

[:language, :region, :script].all? do |var|
public_send(var) == other.public_send(var)
end
rescue ParserError
false
end
alias == eql?

Expand Down
5 changes: 3 additions & 2 deletions Library/Homebrew/test/cask/audit_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,6 @@ def tmp_cask(name, text)
end

describe "locale validation" do
let(:strict) { true }
let(:cask) do
tmp_cask "locale-cask-test", <<~RUBY
cask 'locale-cask-test' do
Expand Down Expand Up @@ -318,7 +317,9 @@ def tmp_cask(name, text)

context "when cask locale is invalid" do
it "error with invalid locale" do
expect(subject).to fail_with(/locale ZH-CN, zh-, zh-cn are invalid/)
expect(subject).to fail_with(/Locale 'ZH-CN' is invalid\./)
expect(subject).to fail_with(/Locale 'zh-' is invalid\./)
expect(subject).to fail_with(/Locale 'zh-cn' is invalid\./)
end
end
end
Expand Down
3 changes: 3 additions & 0 deletions Library/Homebrew/test/locale_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
expect { described_class.parse("zh-CN_Hans") }.to raise_error(Locale::ParserError)
expect { described_class.parse("zhCN") }.to raise_error(Locale::ParserError)
expect { described_class.parse("zh_Hans") }.to raise_error(Locale::ParserError)
expect { described_class.parse("zh-") }.to raise_error(Locale::ParserError)
expect { described_class.parse("ZH-CN") }.to raise_error(Locale::ParserError)
expect { described_class.parse("zh-cn") }.to raise_error(Locale::ParserError)
end
end
end
Expand Down

0 comments on commit 2125d49

Please sign in to comment.