diff --git a/Library/Homebrew/test/resource_spec.rb b/Library/Homebrew/test/resource_spec.rb new file mode 100644 index 0000000000..6a04195389 --- /dev/null +++ b/Library/Homebrew/test/resource_spec.rb @@ -0,0 +1,147 @@ +require "resource" + +describe Resource do + subject { described_class.new("test") } + + describe "#url" do + it "sets the URL" do + subject.url("foo") + expect(subject.url).to eq("foo") + end + + it "can set the URL with specifications" do + subject.url("foo", branch: "master") + expect(subject.url).to eq("foo") + expect(subject.specs).to eq(branch: "master") + end + + it "can set the URL with a custom download strategy class" do + strategy = Class.new(AbstractDownloadStrategy) + subject.url("foo", using: strategy) + expect(subject.url).to eq("foo") + expect(subject.download_strategy).to eq(strategy) + end + + it "can set the URL with specifications and a custom download strategy class" do + strategy = Class.new(AbstractDownloadStrategy) + subject.url("foo", using: strategy, branch: "master") + expect(subject.url).to eq("foo") + expect(subject.specs).to eq(branch: "master") + expect(subject.download_strategy).to eq(strategy) + end + + it "can set the URL with a custom download strategy symbol" do + subject.url("foo", using: :git) + expect(subject.url).to eq("foo") + expect(subject.download_strategy).to eq(GitDownloadStrategy) + end + + it "raises an error if the download strategy class is unkown" do + expect { subject.url("foo", using: Class.new) }.to raise_error(TypeError) + end + + it "does not mutate the specifications hash" do + specs = { using: :git, branch: "master" } + subject.url("foo", specs) + expect(subject.specs).to eq(branch: "master") + expect(subject.using).to eq(:git) + expect(specs).to eq(using: :git, branch: "master") + end + end + + describe "#version" do + it "sets the version" do + subject.version("1.0") + expect(subject.version).to eq(Version.parse("1.0")) + expect(subject.version).not_to be_detected_from_url + end + + it "can detect the version from a URL" do + subject.url("http://example.com/foo-1.0.tar.gz") + expect(subject.version).to eq(Version.parse("1.0")) + expect(subject.version).to be_detected_from_url + end + + it "can set the version with a scheme" do + klass = Class.new(Version) + subject.version klass.new("1.0") + expect(subject.version).to eq(Version.parse("1.0")) + expect(subject.version).to be_a(klass) + end + + it "can set the version from a tag" do + subject.url("http://example.com/foo-1.0.tar.gz", tag: "v1.0.2") + expect(subject.version).to eq(Version.parse("1.0.2")) + expect(subject.version).to be_detected_from_url + end + + it "rejects non-string versions" do + expect { subject.version(1) }.to raise_error(TypeError) + expect { subject.version(2.0) }.to raise_error(TypeError) + expect { subject.version(Object.new) }.to raise_error(TypeError) + end + + it "returns nil if unset" do + expect(subject.version).to be nil + end + end + + describe "#mirrors" do + it "is empty by defaults" do + expect(subject.mirrors).to be_empty + end + + it "returns an array of mirrors added with #mirror" do + subject.mirror("foo") + subject.mirror("bar") + expect(subject.mirrors).to eq(%w[foo bar]) + end + end + + describe "#checksum" do + it "returns nil if unset" do + expect(subject.checksum).to be nil + end + + it "returns the checksum set with #sha256" do + subject.sha256(TEST_SHA256) + expect(subject.checksum).to eq(Checksum.new(:sha256, TEST_SHA256)) + end + end + + describe "#download_strategy" do + it "returns the download strategy" do + strategy = Object.new + expect(DownloadStrategyDetector) + .to receive(:detect).with("foo", nil).and_return(strategy) + subject.url("foo") + expect(subject.download_strategy).to eq(strategy) + end + end + + specify "#verify_download_integrity_missing" do + fn = Pathname.new("test") + + allow(fn).to receive(:file?).and_return(true) + expect(fn).to receive(:verify_checksum).and_raise(ChecksumMissingError) + expect(fn).to receive(:sha256) + + shutup do + subject.verify_download_integrity(fn) + end + end + + specify "#verify_download_integrity_mismatch" do + fn = double(file?: true) + checksum = subject.sha256(TEST_SHA256) + + expect(fn).to receive(:verify_checksum).with(checksum) + .and_raise(ChecksumMismatchError.new(fn, checksum, Object.new)) + + shutup do + expect { + subject.verify_download_integrity(fn) + }.to raise_error(ChecksumMismatchError) + end + end +end diff --git a/Library/Homebrew/test/resource_test.rb b/Library/Homebrew/test/resource_test.rb deleted file mode 100644 index d982a7c337..0000000000 --- a/Library/Homebrew/test/resource_test.rb +++ /dev/null @@ -1,133 +0,0 @@ -require "testing_env" -require "resource" - -class ResourceTests < Homebrew::TestCase - def setup - super - @resource = Resource.new("test") - end - - def test_url - @resource.url("foo") - assert_equal "foo", @resource.url - end - - def test_url_with_specs - @resource.url("foo", branch: "master") - assert_equal "foo", @resource.url - assert_equal({ branch: "master" }, @resource.specs) - end - - def test_url_with_custom_download_strategy_class - strategy = Class.new(AbstractDownloadStrategy) - @resource.url("foo", using: strategy) - assert_equal "foo", @resource.url - assert_equal strategy, @resource.download_strategy - end - - def test_url_with_specs_and_download_strategy - strategy = Class.new(AbstractDownloadStrategy) - @resource.url("foo", using: strategy, branch: "master") - assert_equal "foo", @resource.url - assert_equal({ branch: "master" }, @resource.specs) - assert_equal strategy, @resource.download_strategy - end - - def test_url_with_custom_download_strategy_symbol - @resource.url("foo", using: :git) - assert_equal "foo", @resource.url - assert_equal GitDownloadStrategy, @resource.download_strategy - end - - def test_raises_for_unknown_download_strategy_class - assert_raises(TypeError) { @resource.url("foo", using: Class.new) } - end - - def test_does_not_mutate_specs_hash - specs = { using: :git, branch: "master" } - @resource.url("foo", specs) - assert_equal({ branch: "master" }, @resource.specs) - assert_equal(:git, @resource.using) - assert_equal({ using: :git, branch: "master" }, specs) - end - - def test_version - @resource.version("1.0") - assert_version_equal "1.0", @resource.version - refute_predicate @resource.version, :detected_from_url? - end - - def test_version_from_url - @resource.url("http://example.com/foo-1.0.tar.gz") - assert_version_equal "1.0", @resource.version - assert_predicate @resource.version, :detected_from_url? - end - - def test_version_with_scheme - klass = Class.new(Version) - @resource.version klass.new("1.0") - assert_version_equal "1.0", @resource.version - assert_instance_of klass, @resource.version - end - - def test_version_from_tag - @resource.url("http://example.com/foo-1.0.tar.gz", tag: "v1.0.2") - assert_version_equal "1.0.2", @resource.version - assert_predicate @resource.version, :detected_from_url? - end - - def test_rejects_non_string_versions - assert_raises(TypeError) { @resource.version(1) } - assert_raises(TypeError) { @resource.version(2.0) } - assert_raises(TypeError) { @resource.version(Object.new) } - end - - def test_version_when_url_is_not_set - assert_nil @resource.version - end - - def test_mirrors - assert_empty @resource.mirrors - @resource.mirror("foo") - @resource.mirror("bar") - assert_equal %w[foo bar], @resource.mirrors - end - - def test_checksum_setters - assert_nil @resource.checksum - @resource.sha256(TEST_SHA256) - assert_equal Checksum.new(:sha256, TEST_SHA256), @resource.checksum - end - - def test_download_strategy - strategy = Object.new - DownloadStrategyDetector - .expects(:detect).with("foo", nil).returns(strategy) - @resource.url("foo") - assert_equal strategy, @resource.download_strategy - end - - def test_verify_download_integrity_missing - fn = Pathname.new("test") - - fn.stubs(file?: true) - fn.expects(:verify_checksum).raises(ChecksumMissingError) - fn.expects(:sha256) - - shutup { @resource.verify_download_integrity(fn) } - end - - def test_verify_download_integrity_mismatch - fn = stub(file?: true) - checksum = @resource.sha256(TEST_SHA256) - - fn.expects(:verify_checksum).with(checksum) - .raises(ChecksumMismatchError.new(fn, checksum, Object.new)) - - shutup do - assert_raises(ChecksumMismatchError) do - @resource.verify_download_integrity(fn) - end - end - end -end