From b99e8626e28ef3f3941d7bf9b755fbddcdbcd8b9 Mon Sep 17 00:00:00 2001 From: Sam Ford <1584702+samford@users.noreply.github.com> Date: Fri, 7 Aug 2020 17:25:08 -0400 Subject: [PATCH] Raise TypeError in Livecheck DSL for invalid arg We previously added `raise TypeError` logic to the `Livecheck` DSL's `#strategy` method. This updates the existing methods to follow suit and modifies the tests accordingly. --- Library/Homebrew/livecheck.rb | 26 ++++++++++++++++++------- Library/Homebrew/test/livecheck_spec.rb | 17 ++++++++++++++++ 2 files changed, 36 insertions(+), 7 deletions(-) diff --git a/Library/Homebrew/livecheck.rb b/Library/Homebrew/livecheck.rb index bb1458f2a1..237118345b 100644 --- a/Library/Homebrew/livecheck.rb +++ b/Library/Homebrew/livecheck.rb @@ -22,9 +22,14 @@ class Livecheck # Sets the regex instance variable to the argument given, returns the # regex instance variable when no argument is given. def regex(pattern = nil) - return @regex if pattern.nil? - - @regex = pattern + case pattern + when nil + @regex + when Regexp + @regex = pattern + else + raise TypeError, "Livecheck#regex expects a Regexp" + end end # Sets the skip instance variable to true, indicating that livecheck @@ -32,8 +37,13 @@ class Livecheck # its value is assigned to the skip_msg instance variable, else nil is # assigned. def skip(skip_msg = nil) + if skip_msg.is_a?(String) + @skip_msg = skip_msg + elsif skip_msg.present? + raise TypeError, "Livecheck#skip expects a String" + end + @skip = true - @skip_msg = skip_msg.presence end # Should livecheck be skipped for the formula? @@ -60,15 +70,17 @@ class Livecheck # Sets the url instance variable to the argument given, returns the url # instance variable when no argument is given. def url(val = nil) - return @url if val.nil? - @url = case val + when nil + return @url when :head, :stable, :devel @formula.send(val).url when :homepage @formula.homepage - else + when String val + else + raise TypeError, "Livecheck#url expects a String or valid Symbol" end end diff --git a/Library/Homebrew/test/livecheck_spec.rb b/Library/Homebrew/test/livecheck_spec.rb index fdeb9627a7..3b1ed99960 100644 --- a/Library/Homebrew/test/livecheck_spec.rb +++ b/Library/Homebrew/test/livecheck_spec.rb @@ -20,6 +20,12 @@ describe Livecheck do livecheckable.regex(/foo/) expect(livecheckable.regex).to eq(/foo/) end + + it "raises a TypeError if the argument isn't a Regexp" do + expect { + livecheckable.regex("foo") + }.to raise_error(TypeError, "Livecheck#regex expects a Regexp") + end end describe "#skip" do @@ -34,6 +40,12 @@ describe Livecheck do expect(livecheckable.instance_variable_get(:@skip)).to be true expect(livecheckable.instance_variable_get(:@skip_msg)).to eq("foo") end + + it "raises a TypeError if the argument isn't a String" do + expect { + livecheckable.skip(/foo/) + }.to raise_error(TypeError, "Livecheck#skip expects a String") + end end describe "#skip?" do @@ -69,6 +81,11 @@ describe Livecheck do it "returns the URL if set" do livecheckable.url("foo") expect(livecheckable.url).to eq("foo") + + it "raises a TypeError if the argument isn't a String or Symbol" do + expect { + livecheckable.url(/foo/) + }.to raise_error(TypeError, "Livecheck#url expects a String or valid Symbol") end end