2015-08-03 13:09:07 +01:00
|
|
|
require "testing_env"
|
|
|
|
require "download_strategy"
|
2013-04-07 00:49:56 -05:00
|
|
|
|
2013-09-30 21:36:38 -05:00
|
|
|
class ResourceDouble
|
2017-01-02 12:56:20 +09:00
|
|
|
attr_reader :url, :specs, :version, :mirrors
|
2013-04-07 00:49:56 -05:00
|
|
|
|
2015-08-03 13:09:07 +01:00
|
|
|
def initialize(url = "http://example.com/foo.tar.gz", specs = {})
|
2013-04-07 00:49:56 -05:00
|
|
|
@url = url
|
|
|
|
@specs = specs
|
2017-01-02 12:56:20 +09:00
|
|
|
@mirrors = []
|
2013-04-07 00:49:56 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-06-18 20:32:51 -05:00
|
|
|
class AbstractDownloadStrategyTests < Homebrew::TestCase
|
2016-01-14 20:33:10 +08:00
|
|
|
include FileUtils
|
|
|
|
|
2013-04-07 00:49:56 -05:00
|
|
|
def setup
|
2017-01-21 11:21:30 +00:00
|
|
|
super
|
2013-04-07 00:49:56 -05:00
|
|
|
@name = "foo"
|
2013-09-30 21:36:38 -05:00
|
|
|
@resource = ResourceDouble.new
|
|
|
|
@strategy = AbstractDownloadStrategy.new(@name, @resource)
|
2015-08-03 13:09:07 +01:00
|
|
|
@args = %w[foo bar baz]
|
2013-04-07 00:49:56 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_expand_safe_system_args_with_explicit_quiet_flag
|
2016-09-17 15:32:44 +01:00
|
|
|
@args << { quiet_flag: "--flag" }
|
2013-04-07 00:49:56 -05:00
|
|
|
expanded_args = @strategy.expand_safe_system_args(@args)
|
2015-08-03 13:09:07 +01:00
|
|
|
assert_equal %w[foo bar baz --flag], expanded_args
|
2013-04-07 00:49:56 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_expand_safe_system_args_with_implicit_quiet_flag
|
|
|
|
expanded_args = @strategy.expand_safe_system_args(@args)
|
2015-08-03 13:09:07 +01:00
|
|
|
assert_equal %w[foo bar -q baz], expanded_args
|
2013-04-07 00:49:56 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_expand_safe_system_args_does_not_mutate_argument
|
|
|
|
result = @strategy.expand_safe_system_args(@args)
|
2015-08-03 13:09:07 +01:00
|
|
|
assert_equal %w[foo bar baz], @args
|
2014-06-10 22:43:47 -05:00
|
|
|
refute_same @args, result
|
2013-04-07 00:49:56 -05:00
|
|
|
end
|
2016-01-14 20:33:10 +08:00
|
|
|
|
|
|
|
def test_source_modified_time
|
|
|
|
mktemp "mtime" do
|
2016-09-17 15:32:44 +01:00
|
|
|
touch "foo", mtime: Time.now - 10
|
|
|
|
touch "bar", mtime: Time.now - 100
|
2016-01-14 20:33:10 +08:00
|
|
|
ln_s "not-exist", "baz"
|
|
|
|
assert_equal File.mtime("foo"), @strategy.source_modified_time
|
|
|
|
end
|
|
|
|
end
|
2013-04-07 00:49:56 -05:00
|
|
|
end
|
2012-10-15 01:19:31 -05:00
|
|
|
|
2014-06-18 20:32:51 -05:00
|
|
|
class VCSDownloadStrategyTests < Homebrew::TestCase
|
2014-07-16 23:19:58 -05:00
|
|
|
def test_cache_filename
|
|
|
|
resource = ResourceDouble.new("http://example.com/bar")
|
2015-08-03 13:09:07 +01:00
|
|
|
strategy = Class.new(VCSDownloadStrategy) do
|
|
|
|
def cache_tag
|
|
|
|
"foo"
|
2015-08-03 21:18:00 +08:00
|
|
|
end
|
2015-08-03 13:09:07 +01:00
|
|
|
end
|
2014-07-16 23:19:58 -05:00
|
|
|
downloader = strategy.new("baz", resource)
|
2014-12-06 12:29:15 -05:00
|
|
|
assert_equal HOMEBREW_CACHE.join("baz--foo"), downloader.cached_location
|
2013-09-30 21:36:38 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-01-08 16:44:54 +09:00
|
|
|
class GitHubPrivateRepositoryDownloadStrategyTests < Homebrew::TestCase
|
2017-01-02 12:56:20 +09:00
|
|
|
def setup
|
2017-01-21 11:21:30 +00:00
|
|
|
super
|
2017-01-08 16:44:54 +09:00
|
|
|
resource = ResourceDouble.new("https://github.com/owner/repo/archive/1.1.5.tar.gz")
|
2017-01-03 14:58:08 +09:00
|
|
|
ENV["HOMEBREW_GITHUB_API_TOKEN"] = "token"
|
2017-01-08 18:00:31 +09:00
|
|
|
GitHub.stubs(:repository).returns {}
|
2017-01-08 16:44:54 +09:00
|
|
|
@strategy = GitHubPrivateRepositoryDownloadStrategy.new("foo", resource)
|
2017-01-02 12:56:20 +09:00
|
|
|
end
|
|
|
|
|
2017-01-08 16:44:54 +09:00
|
|
|
def test_set_github_token
|
2017-01-02 12:56:20 +09:00
|
|
|
assert_equal "token", @strategy.instance_variable_get(:@github_token)
|
2017-01-08 16:44:54 +09:00
|
|
|
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
|
2017-01-21 11:21:30 +00:00
|
|
|
super
|
2017-01-08 16:44:54 +09:00
|
|
|
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"
|
2017-01-08 18:00:31 +09:00
|
|
|
GitHub.stubs(:repository).returns {}
|
2017-01-08 16:44:54 +09:00
|
|
|
@strategy = GitHubPrivateRepositoryReleaseDownloadStrategy.new("foo", resource)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_parse_url_pattern
|
2017-01-02 12:56:20 +09:00
|
|
|
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
|
|
|
|
|
2017-01-08 16:44:54 +09:00
|
|
|
def test_download_url
|
2017-01-02 12:56:20 +09:00
|
|
|
@strategy.stubs(:resolve_asset_id).returns(456)
|
|
|
|
expected = "https://token@api.github.com/repos/owner/repo/releases/assets/456"
|
2017-01-08 16:44:54 +09:00
|
|
|
assert_equal expected, @strategy.download_url
|
2017-01-02 12:56:20 +09:00
|
|
|
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",
|
|
|
|
},
|
2017-01-08 18:29:20 +09:00
|
|
|
],
|
2017-01-02 12:56:20 +09:00
|
|
|
}
|
|
|
|
@strategy.stubs(:fetch_release_metadata).returns(release_metadata)
|
|
|
|
assert_equal 456, @strategy.send(:resolve_asset_id)
|
|
|
|
end
|
|
|
|
|
2017-01-08 16:44:54 +09:00
|
|
|
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
|
2017-01-02 12:56:20 +09:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-05-25 20:11:27 +03:00
|
|
|
class GitDownloadStrategyTests < Homebrew::TestCase
|
|
|
|
include FileUtils
|
|
|
|
|
|
|
|
def setup
|
2017-01-21 11:21:30 +00:00
|
|
|
super
|
2016-05-25 20:11:27 +03:00
|
|
|
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
|
2017-01-22 20:54:37 +00:00
|
|
|
@cached_location.cd do
|
|
|
|
shutup do
|
|
|
|
system "git", "init"
|
|
|
|
system "git", "remote", "add", "origin", "https://github.com/Homebrew/homebrew-foo"
|
2016-05-25 20:11:27 +03:00
|
|
|
end
|
2017-01-22 20:54:37 +00:00
|
|
|
touch "README"
|
|
|
|
git_commit_all
|
2016-05-25 20:11:27 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-07-27 17:49:43 +03:00
|
|
|
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
|
|
|
|
|
2016-05-25 20:11:27 +03:00
|
|
|
def test_source_modified_time
|
|
|
|
setup_git_repo
|
2017-01-22 20:45:15 +00:00
|
|
|
assert_equal 1_485_115_153, @strategy.source_modified_time.to_i
|
2016-05-25 20:11:27 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_last_commit
|
|
|
|
setup_git_repo
|
2017-01-22 20:54:37 +00:00
|
|
|
@cached_location.cd do
|
|
|
|
touch "LICENSE"
|
|
|
|
git_commit_all
|
2016-05-25 20:11:27 +03:00
|
|
|
end
|
2017-01-22 20:45:15 +00:00
|
|
|
assert_equal "f68266e", @strategy.last_commit
|
2016-05-25 20:11:27 +03:00
|
|
|
end
|
2016-07-27 17:49:43 +03:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
2017-01-22 20:54:37 +00:00
|
|
|
remote_repo.cd do
|
|
|
|
shutup do
|
|
|
|
system "git", "init"
|
|
|
|
system "git", "remote", "add", "origin", "https://github.com/Homebrew/homebrew-foo"
|
2016-07-27 17:49:43 +03:00
|
|
|
end
|
2017-01-22 20:54:37 +00:00
|
|
|
touch "README"
|
|
|
|
git_commit_all
|
|
|
|
touch "LICENSE"
|
|
|
|
git_commit_all
|
2016-07-27 17:49:43 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
@strategy.shutup!
|
2017-01-22 20:45:15 +00:00
|
|
|
assert_equal "f68266e", @strategy.fetch_last_commit
|
2016-07-27 17:49:43 +03:00
|
|
|
ensure
|
|
|
|
remote_repo.rmtree if remote_repo.directory?
|
|
|
|
end
|
2016-05-25 20:11:27 +03:00
|
|
|
end
|
|
|
|
|
2014-06-18 20:32:51 -05:00
|
|
|
class DownloadStrategyDetectorTests < Homebrew::TestCase
|
2012-10-15 01:19:31 -05:00
|
|
|
def setup
|
2017-01-21 11:21:30 +00:00
|
|
|
super
|
2012-10-15 01:19:31 -05:00
|
|
|
@d = DownloadStrategyDetector.new
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_detect_git_download_startegy
|
2014-06-12 17:58:12 -05:00
|
|
|
@d = DownloadStrategyDetector.detect("git://example.com/foo.git")
|
2012-10-15 01:19:31 -05:00
|
|
|
assert_equal GitDownloadStrategy, @d
|
|
|
|
end
|
|
|
|
|
2016-07-27 17:49:43 +03:00
|
|
|
def test_detect_github_git_download_strategy
|
|
|
|
@d = DownloadStrategyDetector.detect("https://github.com/homebrew/brew.git")
|
|
|
|
assert_equal GitHubGitDownloadStrategy, @d
|
|
|
|
end
|
|
|
|
|
2012-10-15 01:19:31 -05:00
|
|
|
def test_default_to_curl_strategy
|
|
|
|
@d = DownloadStrategyDetector.detect(Object.new)
|
|
|
|
assert_equal CurlDownloadStrategy, @d
|
|
|
|
end
|
2013-09-26 16:59:45 -05:00
|
|
|
|
|
|
|
def test_raises_when_passed_unrecognized_strategy
|
|
|
|
assert_raises(TypeError) do
|
|
|
|
DownloadStrategyDetector.detect("foo", Class.new)
|
|
|
|
end
|
|
|
|
end
|
2012-10-15 01:19:31 -05:00
|
|
|
end
|