summaryrefslogtreecommitdiff
path: root/jni/ruby/test/rubygems/test_gem_install_update_options.rb
blob: de09d7a1719776bf269dfdbdcd78f53692ff9b2a (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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
require 'rubygems/installer_test_case'
require 'rubygems/install_update_options'
require 'rubygems/command'
require 'rubygems/dependency_installer'

class TestGemInstallUpdateOptions < Gem::InstallerTestCase

  def setup
    super

    @cmd = Gem::Command.new 'dummy', 'dummy',
                            Gem::DependencyInstaller::DEFAULT_OPTIONS
    @cmd.extend Gem::InstallUpdateOptions
    @cmd.add_install_update_options
  end

  def test_add_install_update_options
    args = %w[
      --document
      --build-root build_root
      --format-exec
      --ignore-dependencies
      --rdoc
      --ri
      -E
      -f
      -i /install_to
      -w
      --vendor
    ]

    args.concat %w[-P HighSecurity] if defined?(OpenSSL::SSL)

    assert @cmd.handles?(args)
  end

  def test_build_root
    @cmd.handle_options %w[--build-root build_root]

    assert_equal File.expand_path('build_root'), @cmd.options[:build_root]
  end

  def test_doc
    @cmd.handle_options %w[--doc]

    assert_equal %w[ri], @cmd.options[:document].sort
  end

  def test_doc_rdoc
    @cmd.handle_options %w[--doc=rdoc]

    assert_equal %w[rdoc], @cmd.options[:document]

    @cmd.handle_options %w[--doc ri]

    assert_equal %w[ri], @cmd.options[:document]
  end

  def test_doc_rdoc_ri
    @cmd.handle_options %w[--doc=rdoc,ri]

    assert_equal %w[rdoc ri], @cmd.options[:document]
  end

  def test_doc_no
    @cmd.handle_options %w[--no-doc]

    assert_equal [], @cmd.options[:document]
  end

  def test_document
    @cmd.handle_options %w[--document]

    assert_equal %w[ri], @cmd.options[:document].sort
  end

  def test_document_no
    @cmd.handle_options %w[--no-document]

    assert_equal %w[], @cmd.options[:document]
  end

  def test_document_rdoc
    @cmd.handle_options %w[--document=rdoc]

    assert_equal %w[rdoc], @cmd.options[:document]

    @cmd.handle_options %w[--document ri]

    assert_equal %w[ri], @cmd.options[:document]
  end

  def test_rdoc
    @cmd.handle_options %w[--rdoc]

    assert_equal %w[rdoc ri], @cmd.options[:document].sort
  end

  def test_rdoc_no
    @cmd.handle_options %w[--no-rdoc]

    assert_equal %w[ri], @cmd.options[:document]
  end

  def test_ri
    @cmd.handle_options %w[--no-ri]

    assert_equal %w[], @cmd.options[:document]
  end

  def test_security_policy
    skip 'openssl is missing' unless defined?(OpenSSL::SSL)

    @cmd.handle_options %w[-P HighSecurity]

    assert_equal Gem::Security::HighSecurity, @cmd.options[:security_policy]
  end

  def test_security_policy_unknown
    @cmd.add_install_update_options

    assert_raises OptionParser::InvalidArgument do
      @cmd.handle_options %w[-P UnknownSecurity]
    end
  end

  def test_user_install_enabled
    @cmd.handle_options %w[--user-install]

    assert @cmd.options[:user_install]

    @installer = Gem::Installer.new @gem, @cmd.options
    @installer.install
    assert_path_exists File.join(Gem.user_dir, 'gems')
    assert_path_exists File.join(Gem.user_dir, 'gems', @spec.full_name)
  end

  def test_user_install_disabled_read_only
    if win_platform?
      skip('test_user_install_disabled_read_only test skipped on MS Windows')
    else
      @cmd.handle_options %w[--no-user-install]

      refute @cmd.options[:user_install]

      FileUtils.chmod 0755, @userhome
      FileUtils.chmod 0000, @gemhome

      Gem.use_paths @gemhome, @userhome

      assert_raises(Gem::FilePermissionError) do
        Gem::Installer.new(@gem, @cmd.options).install
      end
    end
  ensure
    FileUtils.chmod 0755, @gemhome
  end

  def test_vendor
    @cmd.handle_options %w[--vendor]

    assert @cmd.options[:vendor]
    assert_equal Gem.vendor_dir, @cmd.options[:install_dir]
  end

  def test_vendor_missing
    orig_vendordir = RbConfig::CONFIG['vendordir']
    RbConfig::CONFIG.delete 'vendordir'

    e = assert_raises OptionParser::InvalidOption do
      @cmd.handle_options %w[--vendor]
    end

    assert_equal 'invalid option: --vendor your platform is not supported',
                 e.message

    refute @cmd.options[:vendor]
    refute @cmd.options[:install_dir]

  ensure
    RbConfig::CONFIG['vendordir'] = orig_vendordir
  end

end