aboutsummaryrefslogtreecommitdiffstats
path: root/Library/Homebrew
diff options
context:
space:
mode:
authorMarkus Reiter2017-02-16 23:53:06 +0100
committerMarkus Reiter2017-02-19 18:35:44 +0100
commit92710c50c65beccfc2fca744cc8ee1be19a87aad (patch)
tree9fe81d1eb7883214c7dbdca384b404b6115dca91 /Library/Homebrew
parent9f31d41fb263718bff98e194df411b8aaea72aba (diff)
downloadbrew-92710c50c65beccfc2fca744cc8ee1be19a87aad.tar.bz2
Convert Download Strategies test to spec.
Diffstat (limited to 'Library/Homebrew')
-rw-r--r--Library/Homebrew/test/download_strategies_spec.rb232
-rw-r--r--Library/Homebrew/test/download_strategies_test.rb245
2 files changed, 232 insertions, 245 deletions
diff --git a/Library/Homebrew/test/download_strategies_spec.rb b/Library/Homebrew/test/download_strategies_spec.rb
new file mode 100644
index 000000000..f466b97f4
--- /dev/null
+++ b/Library/Homebrew/test/download_strategies_spec.rb
@@ -0,0 +1,232 @@
+require "download_strategy"
+
+describe AbstractDownloadStrategy do
+ subject { described_class.new(name, resource) }
+ let(:name) { "foo" }
+ let(:url) { "http://example.com/foo.tar.gz" }
+ let(:resource) { double(Resource, url: url, mirrors: [], specs: {}, version: nil) }
+ let(:args) { %w[foo bar baz] }
+
+ describe "#expand_safe_system_args" do
+ it "works with an explicit quiet flag" do
+ args << { quiet_flag: "--flag" }
+ expanded_args = subject.expand_safe_system_args(args)
+ expect(expanded_args).to eq(%w[foo bar baz --flag])
+ end
+
+ it "adds an implicit quiet flag" do
+ expanded_args = subject.expand_safe_system_args(args)
+ expect(expanded_args).to eq(%w[foo bar -q baz])
+ end
+
+ it "does not mutate the arguments" do
+ result = subject.expand_safe_system_args(args)
+ expect(args).to eq(%w[foo bar baz])
+ expect(result).not_to be args
+ end
+ end
+
+ specify "#source_modified_time" do
+ FileUtils.mktemp "mtime" do
+ FileUtils.touch "foo", mtime: Time.now - 10
+ FileUtils.touch "bar", mtime: Time.now - 100
+ FileUtils.ln_s "not-exist", "baz"
+ expect(subject.source_modified_time).to eq(File.mtime("foo"))
+ end
+ end
+end
+
+describe VCSDownloadStrategy do
+ let(:url) { "http://example.com/bar" }
+ let(:resource) { double(Resource, url: url, mirrors: [], specs: {}, version: nil) }
+
+ describe "#cached_location" do
+ it "returns the path of the cached resource" do
+ allow_any_instance_of(described_class).to receive(:cache_tag).and_return("foo")
+ downloader = described_class.new("baz", resource)
+ expect(downloader.cached_location).to eq(HOMEBREW_CACHE/"baz--foo")
+ end
+ end
+end
+
+describe GitHubPrivateRepositoryDownloadStrategy do
+ subject { described_class.new("foo", resource) }
+ let(:url) { "https://github.com/owner/repo/archive/1.1.5.tar.gz" }
+ let(:resource) { double(Resource, url: url, mirrors: [], specs: {}, version: nil) }
+
+ before(:each) do
+ ENV["HOMEBREW_GITHUB_API_TOKEN"] = "token"
+ allow(GitHub).to receive(:repository).and_return({})
+ end
+
+ it "sets the @github_token instance variable" do
+ expect(subject.instance_variable_get(:@github_token)).to eq("token")
+ end
+
+ it "parses the URL and sets the corresponding instance variables" do
+ expect(subject.instance_variable_get(:@owner)).to eq("owner")
+ expect(subject.instance_variable_get(:@repo)).to eq("repo")
+ expect(subject.instance_variable_get(:@filepath)).to eq("archive/1.1.5.tar.gz")
+ end
+
+ its(:download_url) { is_expected.to eq("https://token@github.com/owner/repo/archive/1.1.5.tar.gz") }
+end
+
+describe GitHubPrivateRepositoryReleaseDownloadStrategy do
+ subject { described_class.new("foo", resource) }
+ let(:url) { "https://github.com/owner/repo/releases/download/tag/foo_v0.1.0_darwin_amd64.tar.gz" }
+ let(:resource) { double(Resource, url: url, mirrors: [], specs: {}, version: nil) }
+
+ before(:each) do
+ ENV["HOMEBREW_GITHUB_API_TOKEN"] = "token"
+ allow(GitHub).to receive(:repository).and_return({})
+ end
+
+ it "parses the URL and sets the corresponding instance variables" do
+ expect(subject.instance_variable_get(:@owner)).to eq("owner")
+ expect(subject.instance_variable_get(:@repo)).to eq("repo")
+ expect(subject.instance_variable_get(:@tag)).to eq("tag")
+ expect(subject.instance_variable_get(:@filename)).to eq("foo_v0.1.0_darwin_amd64.tar.gz")
+ end
+
+ describe "#download_url" do
+ it "returns the download URL for a given resource" do
+ allow(subject).to receive(:resolve_asset_id).and_return(456)
+ expect(subject.download_url).to eq("https://token@api.github.com/repos/owner/repo/releases/assets/456")
+ end
+ end
+
+ specify "#resolve_asset_id" do
+ release_metadata = {
+ "assets" => [
+ {
+ "id" => 123,
+ "name" => "foo_v0.1.0_linux_amd64.tar.gz",
+ },
+ {
+ "id" => 456,
+ "name" => "foo_v0.1.0_darwin_amd64.tar.gz",
+ },
+ ],
+ }
+ allow(subject).to receive(:fetch_release_metadata).and_return(release_metadata)
+ expect(subject.send(:resolve_asset_id)).to eq(456)
+ end
+
+ describe "#fetch_release_metadata" do
+ it "fetches release metadata from GitHub" do
+ expected_release_url = "https://api.github.com/repos/owner/repo/releases/tags/tag"
+ expect(GitHub).to receive(:open).with(expected_release_url).and_return({})
+ subject.send(:fetch_release_metadata)
+ end
+ end
+end
+
+describe GitHubGitDownloadStrategy do
+ subject { described_class.new(name, resource) }
+ let(:name) { "brew" }
+ let(:url) { "https://github.com/homebrew/brew.git" }
+ let(:resource) { double(Resource, url: url, mirrors: [], specs: {}, version: nil) }
+
+ it "parses the URL and sets the corresponding instance variables" do
+ expect(subject.instance_variable_get(:@user)).to eq("homebrew")
+ expect(subject.instance_variable_get(:@repo)).to eq("brew")
+ end
+end
+
+describe GitDownloadStrategy do
+ subject { described_class.new(name, resource) }
+ let(:name) { "baz" }
+ let(:url) { "https://github.com/homebrew/foo" }
+ let(:resource) { double(Resource, url: url, mirrors: [], specs: {}, version: nil) }
+ let(:cached_location) { subject.cached_location }
+
+ before(:each) do
+ @commit_id = 1
+ FileUtils.mkpath cached_location
+ end
+
+ def git_commit_all
+ shutup do
+ system "git", "add", "--all"
+ system "git", "commit", "-m", "commit number #{@commit_id}"
+ @commit_id += 1
+ end
+ end
+
+ def setup_git_repo
+ shutup do
+ system "git", "init"
+ system "git", "remote", "add", "origin", "https://github.com/Homebrew/homebrew-foo"
+ end
+ FileUtils.touch "README"
+ git_commit_all
+ end
+
+ describe "#source_modified_time" do
+ it "returns the right modification time" do
+ cached_location.cd do
+ setup_git_repo
+ end
+ expect(subject.source_modified_time.to_i).to eq(1_485_115_153)
+ end
+ end
+
+ specify "#last_commit" do
+ cached_location.cd do
+ setup_git_repo
+ FileUtils.touch "LICENSE"
+ git_commit_all
+ end
+ expect(subject.last_commit).to eq("f68266e")
+ end
+
+ describe "#fetch_last_commit" do
+ let(:url) { "file://#{remote_repo}" }
+ let(:version) { Version.create("HEAD") }
+ let(:resource) { double(Resource, url: url, mirrors: [], specs: {}, version: version) }
+ let(:remote_repo) { HOMEBREW_PREFIX/"remote_repo" }
+
+ before(:each) { remote_repo.mkpath }
+
+ after(:each) { FileUtils.rm_rf remote_repo }
+
+ it "fetches the hash of the last commit" do
+ remote_repo.cd do
+ setup_git_repo
+ FileUtils.touch "LICENSE"
+ git_commit_all
+ end
+
+ subject.shutup!
+ expect(subject.fetch_last_commit).to eq("f68266e")
+ end
+ end
+end
+
+describe DownloadStrategyDetector do
+ describe "::detect" do
+ subject { described_class.detect(url) }
+ let(:url) { Object.new }
+
+ context "when given Git URL" do
+ let(:url) { "git://example.com/foo.git" }
+ it { is_expected.to eq(GitDownloadStrategy) }
+ end
+
+ context "when given a GitHub Git URL" do
+ let(:url) { "https://github.com/homebrew/brew.git" }
+ it { is_expected.to eq(GitHubGitDownloadStrategy) }
+ end
+
+ it "defaults to cURL" do
+ expect(subject).to eq(CurlDownloadStrategy)
+ end
+
+ it "raises an error when passed an unrecognized strategy" do
+ expect {
+ described_class.detect("foo", Class.new)
+ }.to raise_error(TypeError)
+ end
+ end
+end
diff --git a/Library/Homebrew/test/download_strategies_test.rb b/Library/Homebrew/test/download_strategies_test.rb
deleted file mode 100644
index 40236b420..000000000
--- a/Library/Homebrew/test/download_strategies_test.rb
+++ /dev/null
@@ -1,245 +0,0 @@
-require "testing_env"
-require "download_strategy"
-
-class ResourceDouble
- attr_reader :url, :specs, :version, :mirrors
-
- def initialize(url = "http://example.com/foo.tar.gz", specs = {})
- @url = url
- @specs = specs
- @mirrors = []
- end
-end
-
-class AbstractDownloadStrategyTests < Homebrew::TestCase
- include FileUtils
-
- def setup
- super
- @name = "foo"
- @resource = ResourceDouble.new
- @strategy = AbstractDownloadStrategy.new(@name, @resource)
- @args = %w[foo bar baz]
- end
-
- def test_expand_safe_system_args_with_explicit_quiet_flag
- @args << { quiet_flag: "--flag" }
- expanded_args = @strategy.expand_safe_system_args(@args)
- assert_equal %w[foo bar baz --flag], expanded_args
- end
-
- def test_expand_safe_system_args_with_implicit_quiet_flag
- expanded_args = @strategy.expand_safe_system_args(@args)
- assert_equal %w[foo bar -q baz], expanded_args
- end
-
- def test_expand_safe_system_args_does_not_mutate_argument
- result = @strategy.expand_safe_system_args(@args)
- assert_equal %w[foo bar baz], @args
- refute_same @args, result
- end
-
- def test_source_modified_time
- mktemp "mtime" do
- touch "foo", mtime: Time.now - 10
- touch "bar", mtime: Time.now - 100
- ln_s "not-exist", "baz"
- assert_equal File.mtime("foo"), @strategy.source_modified_time
- end
- end
-end
-
-class VCSDownloadStrategyTests < Homebrew::TestCase
- def test_cache_filename
- resource = ResourceDouble.new("http://example.com/bar")
- strategy = Class.new(VCSDownloadStrategy) do
- def cache_tag
- "foo"
- end
- end
- downloader = strategy.new("baz", resource)
- assert_equal HOMEBREW_CACHE.join("baz--foo"), downloader.cached_location
- end
-end
-
-class GitHubPrivateRepositoryDownloadStrategyTests < Homebrew::TestCase
- def setup
- super
- resource = ResourceDouble.new("https://github.com/owner/repo/archive/1.1.5.tar.gz")
- ENV["HOMEBREW_GITHUB_API_TOKEN"] = "token"
- GitHub.stubs(:repository).returns {}
- @strategy = GitHubPrivateRepositoryDownloadStrategy.new("foo", resource)
- end
-
- def test_set_github_token
- assert_equal "token", @strategy.instance_variable_get(:@github_token)
- end
-
- def test_parse_url_pattern
- assert_equal "owner", @strategy.instance_variable_get(:@owner)
- assert_equal "repo", @strategy.instance_variable_get(:@repo)
- assert_equal "archive/1.1.5.tar.gz", @strategy.instance_variable_get(:@filepath)
- end
-
- def test_download_url
- expected = "https://token@github.com/owner/repo/archive/1.1.5.tar.gz"
- assert_equal expected, @strategy.download_url
- end
-end
-
-class GitHubPrivateRepositoryReleaseDownloadStrategyTests < Homebrew::TestCase
- def setup
- super
- resource = ResourceDouble.new("https://github.com/owner/repo/releases/download/tag/foo_v0.1.0_darwin_amd64.tar.gz")
- ENV["HOMEBREW_GITHUB_API_TOKEN"] = "token"
- GitHub.stubs(:repository).returns {}
- @strategy = GitHubPrivateRepositoryReleaseDownloadStrategy.new("foo", resource)
- end
-
- def test_parse_url_pattern
- assert_equal "owner", @strategy.instance_variable_get(:@owner)
- assert_equal "repo", @strategy.instance_variable_get(:@repo)
- assert_equal "tag", @strategy.instance_variable_get(:@tag)
- assert_equal "foo_v0.1.0_darwin_amd64.tar.gz", @strategy.instance_variable_get(:@filename)
- end
-
- def test_download_url
- @strategy.stubs(:resolve_asset_id).returns(456)
- expected = "https://token@api.github.com/repos/owner/repo/releases/assets/456"
- assert_equal expected, @strategy.download_url
- end
-
- def test_resolve_asset_id
- release_metadata = {
- "assets" => [
- {
- "id" => 123,
- "name" => "foo_v0.1.0_linux_amd64.tar.gz",
- },
- {
- "id" => 456,
- "name" => "foo_v0.1.0_darwin_amd64.tar.gz",
- },
- ],
- }
- @strategy.stubs(:fetch_release_metadata).returns(release_metadata)
- assert_equal 456, @strategy.send(:resolve_asset_id)
- end
-
- def test_fetch_release_metadata
- expected_release_url = "https://api.github.com/repos/owner/repo/releases/tags/tag"
- github_mock = MiniTest::Mock.new
- github_mock.expect :call, {}, [expected_release_url]
- GitHub.stub :open, github_mock do
- @strategy.send(:fetch_release_metadata)
- end
- github_mock.verify
- end
-end
-
-class GitDownloadStrategyTests < Homebrew::TestCase
- include FileUtils
-
- def setup
- super
- resource = ResourceDouble.new("https://github.com/homebrew/foo")
- @commit_id = 1
- @strategy = GitDownloadStrategy.new("baz", resource)
- @cached_location = @strategy.cached_location
- mkpath @cached_location
- end
-
- def git_commit_all
- shutup do
- system "git", "add", "--all"
- system "git", "commit", "-m", "commit number #{@commit_id}"
- @commit_id += 1
- end
- end
-
- def setup_git_repo
- @cached_location.cd do
- shutup do
- system "git", "init"
- system "git", "remote", "add", "origin", "https://github.com/Homebrew/homebrew-foo"
- end
- touch "README"
- git_commit_all
- end
- end
-
- def test_github_git_download_strategy_user_repo
- resource = ResourceDouble.new("https://github.com/homebrew/brew.git")
- strategy = GitHubGitDownloadStrategy.new("brew", resource)
-
- assert_equal strategy.instance_variable_get(:@user), "homebrew"
- assert_equal strategy.instance_variable_get(:@repo), "brew"
- end
-
- def test_source_modified_time
- setup_git_repo
- assert_equal 1_485_115_153, @strategy.source_modified_time.to_i
- end
-
- def test_last_commit
- setup_git_repo
- @cached_location.cd do
- touch "LICENSE"
- git_commit_all
- end
- assert_equal "f68266e", @strategy.last_commit
- end
-
- def test_fetch_last_commit
- remote_repo = HOMEBREW_PREFIX.join("remote_repo")
- remote_repo.mkdir
-
- resource = ResourceDouble.new("file://#{remote_repo}")
- resource.instance_variable_set(:@version, Version.create("HEAD"))
- @strategy = GitDownloadStrategy.new("baz", resource)
-
- remote_repo.cd do
- shutup do
- system "git", "init"
- system "git", "remote", "add", "origin", "https://github.com/Homebrew/homebrew-foo"
- end
- touch "README"
- git_commit_all
- touch "LICENSE"
- git_commit_all
- end
-
- @strategy.shutup!
- assert_equal "f68266e", @strategy.fetch_last_commit
- ensure
- remote_repo.rmtree if remote_repo.directory?
- end
-end
-
-class DownloadStrategyDetectorTests < Homebrew::TestCase
- def setup
- super
- @d = DownloadStrategyDetector.new
- end
-
- def test_detect_git_download_startegy
- @d = DownloadStrategyDetector.detect("git://example.com/foo.git")
- assert_equal GitDownloadStrategy, @d
- end
-
- def test_detect_github_git_download_strategy
- @d = DownloadStrategyDetector.detect("https://github.com/homebrew/brew.git")
- assert_equal GitHubGitDownloadStrategy, @d
- end
-
- def test_default_to_curl_strategy
- @d = DownloadStrategyDetector.detect(Object.new)
- assert_equal CurlDownloadStrategy, @d
- end
-
- def test_raises_when_passed_unrecognized_strategy
- assert_raises(TypeError) do
- DownloadStrategyDetector.detect("foo", Class.new)
- end
- end
-end