aboutsummaryrefslogtreecommitdiffstats
path: root/Library/Homebrew/test/test_resource.rb
blob: 03cb11c1be320660a6544ff4ff0eb45d8198ca69 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
require 'testing_env'
require 'resource'

class ResourceTests < Homebrew::TestCase
  def setup
    @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_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
    assert !@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 @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 @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_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.sha1(TEST_SHA1)
    assert_equal Checksum.new(:sha1, TEST_SHA1), @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(:sha1)

    shutup { @resource.verify_download_integrity(fn) }
  end

  def test_verify_download_integrity_mismatch
    fn = stub(:file? => true)
    checksum = @resource.sha1(TEST_SHA1)

    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