summaryrefslogtreecommitdiff
path: root/test/rubygems/test_gem_source_local.rb
blob: 2d4ddbc3a460208ad772c5065deea01f4c28d5c1 (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
# frozen_string_literal: true
require_relative 'helper'
require 'rubygems/source'

require 'fileutils'

class TestGemSourceLocal < Gem::TestCase
  def setup
    super

    @sl = Gem::Source::Local.new

    @a, @a_gem = util_gem "a", '1'
    @ap, @ap_gem = util_gem "a", '2.a'
    @b, @b_gem = util_gem "b", '1'

    FileUtils.mv @a_gem, @tempdir
    FileUtils.mv @ap_gem, @tempdir
    FileUtils.mv @b_gem, @tempdir
  end

  def test_load_specs_released
    assert_equal [@a.name_tuple, @b.name_tuple].sort,
                 @sl.load_specs(:released).sort
  end

  def test_load_specs_prerelease
    assert_equal [@ap.name_tuple], @sl.load_specs(:prerelease)
  end

  def test_load_specs_latest
    a2, a2_gem = util_gem "a", "2"

    FileUtils.mv a2_gem, @tempdir

    assert_equal [a2.name_tuple, @b.name_tuple].sort,
                 @sl.load_specs(:latest).sort
  end

  def test_find_gem
    assert_equal "a-1", @sl.find_gem("a").full_name
  end

  def test_find_gem_highest_version
    _, a2_gem = util_gem "a", "2"
    FileUtils.mv a2_gem, @tempdir

    assert_equal "a-2", @sl.find_gem("a").full_name
  end

  def test_find_gem_specific_version
    _, a2_gem = util_gem "a", "2"
    FileUtils.mv a2_gem, @tempdir

    req = Gem::Requirement.create("= 1")

    assert_equal "a-1", @sl.find_gem("a", req).full_name
  end

  def test_find_gem_prerelease
    req = Gem::Requirement.create(">= 0")
    assert_equal "a-2.a", @sl.find_gem("a", req, true).full_name
  end

  def test_fetch_spec
    s = @sl.fetch_spec @a.name_tuple
    assert_equal s, @a
  end

  def test_inspect
    assert_equal '#<Gem::Source::Local specs: "NOT LOADED">', @sl.inspect

    @sl.load_specs :released

    inner = [@a, @ap, @b].map {|t| t.name_tuple }.inspect

    assert_equal "#<Gem::Source::Local specs: #{inner}>", @sl.inspect
  end

  def test_download
    path = @sl.download @a

    assert_equal File.expand_path(@a.file_name), path
  end

  def test_spaceship
    a1 = quick_gem 'a', '1'
    util_build_gem a1

    remote    = Gem::Source.new @gem_repo
    specific  = Gem::Source::SpecificFile.new a1.cache_file
    installed = Gem::Source::Installed.new
    local     = Gem::Source::Local.new

    assert_equal(0, local.<=>(local), 'local <=> local')

    assert_equal(-1, remote.<=>(local), 'remote <=> local')
    assert_equal(1, local.<=>(remote), 'local <=> remote')

    assert_equal(1, installed.<=>(local), 'installed <=> local')
    assert_equal(-1, local.<=>(installed), 'local <=> installed')

    assert_equal(-1, specific.<=>(local), 'specific <=> local')
    assert_equal(1, local.<=>(specific), 'local <=> specific')
  end
end