1require 'rubygems/test_case' 2require 'rubygems/commands/unpack_command' 3 4class TestGemCommandsUnpackCommand < Gem::TestCase 5 6 def setup 7 super 8 9 Dir.chdir @tempdir do 10 @cmd = Gem::Commands::UnpackCommand.new 11 end 12 end 13 14 def test_find_in_cache 15 util_make_gems 16 17 assert_equal( 18 @cmd.find_in_cache(File.basename @a1.cache_file), 19 @a1.cache_file, 20 'found a-1.gem in the cache' 21 ) 22 end 23 24 def test_get_path 25 util_setup_fake_fetcher 26 util_clear_gems 27 util_setup_spec_fetcher @a1 28 29 a1_data = nil 30 31 open @a1.cache_file, 'rb' do |fp| 32 a1_data = fp.read 33 end 34 35 Gem::RemoteFetcher.fetcher.data['http://gems.example.com/gems/a-1.gem'] = 36 a1_data 37 38 dep = Gem::Dependency.new(@a1.name, @a1.version) 39 assert_equal( 40 @cmd.get_path(dep), 41 @a1.cache_file, 42 'fetches a-1 and returns the cache path' 43 ) 44 45 FileUtils.rm @a1.cache_file 46 47 assert_equal( 48 @cmd.get_path(dep), 49 @a1.cache_file, 50 'when removed from cache, refetches a-1' 51 ) 52 end 53 54 def test_execute 55 util_make_gems 56 57 @cmd.options[:args] = %w[a b] 58 59 use_ui @ui do 60 Dir.chdir @tempdir do 61 @cmd.execute 62 end 63 end 64 65 assert File.exist?(File.join(@tempdir, 'a-3.a')), 'a should be unpacked' 66 assert File.exist?(File.join(@tempdir, 'b-2')), 'b should be unpacked' 67 end 68 69 def test_execute_gem_path 70 util_setup_fake_fetcher 71 util_setup_spec_fetcher 72 73 Gem.clear_paths 74 75 gemhome2 = File.join @tempdir, 'gemhome2' 76 77 Gem.paths = { "GEM_PATH" => [gemhome2, @gemhome], "GEM_HOME" => gemhome2 } 78 79 @cmd.options[:args] = %w[a] 80 81 use_ui @ui do 82 Dir.chdir @tempdir do 83 @cmd.execute 84 end 85 end 86 87 assert File.exist?(File.join(@tempdir, 'a-3.a')) 88 end 89 90 def test_execute_gem_path_missing 91 util_setup_fake_fetcher 92 util_setup_spec_fetcher 93 94 Gem.clear_paths 95 96 gemhome2 = File.join @tempdir, 'gemhome2' 97 98 Gem.paths = { "GEM_PATH" => [gemhome2, @gemhome], "GEM_HOME" => gemhome2 } 99 100 @cmd.options[:args] = %w[z] 101 102 use_ui @ui do 103 Dir.chdir @tempdir do 104 @cmd.execute 105 end 106 end 107 108 assert_equal '', @ui.output 109 end 110 111 def test_execute_remote 112 util_setup_fake_fetcher 113 util_setup_spec_fetcher @a1, @a2 114 util_clear_gems 115 116 a2_data = nil 117 open @a2.cache_file, 'rb' do |fp| 118 a2_data = fp.read 119 end 120 121 Gem::RemoteFetcher.fetcher.data['http://gems.example.com/gems/a-2.gem'] = 122 a2_data 123 124 Gem.configuration.verbose = :really 125 @cmd.options[:args] = %w[a] 126 127 use_ui @ui do 128 Dir.chdir @tempdir do 129 @cmd.execute 130 end 131 end 132 133 assert File.exist?(File.join(@tempdir, 'a-2')), 'a should be unpacked' 134 end 135 136 def test_execute_spec 137 util_make_gems 138 139 @cmd.options[:args] = %w[a b] 140 @cmd.options[:spec] = true 141 142 use_ui @ui do 143 Dir.chdir @tempdir do 144 @cmd.execute 145 end 146 end 147 148 assert File.exist?(File.join(@tempdir, 'a-3.a.gemspec')) 149 assert File.exist?(File.join(@tempdir, 'b-2.gemspec')) 150 end 151 152 def test_execute_sudo 153 skip 'Cannot perform this test on windows (chmod)' if win_platform? 154 155 util_make_gems 156 157 FileUtils.chmod 0555, @gemhome 158 159 @cmd.options[:args] = %w[b] 160 161 use_ui @ui do 162 Dir.chdir @tempdir do 163 @cmd.execute 164 end 165 end 166 167 assert File.exist?(File.join(@tempdir, 'b-2')), 'b should be unpacked' 168 ensure 169 FileUtils.chmod 0755, @gemhome 170 end 171 172 def test_execute_with_target_option 173 util_make_gems 174 175 target = 'with_target' 176 @cmd.options[:args] = %w[a] 177 @cmd.options[:target] = target 178 179 use_ui @ui do 180 Dir.chdir @tempdir do 181 @cmd.execute 182 end 183 end 184 185 assert File.exist?(File.join(@tempdir, target, 'a-3.a')) 186 end 187 188 def test_execute_exact_match 189 foo_spec = quick_spec 'foo' 190 foo_bar_spec = quick_spec 'foo_bar' 191 192 use_ui @ui do 193 Dir.chdir @tempdir do 194 Gem::Package.build foo_spec 195 Gem::Package.build foo_bar_spec 196 end 197 end 198 199 foo_path = File.join(@tempdir, "#{foo_spec.full_name}.gem") 200 foo_bar_path = File.join(@tempdir, "#{foo_bar_spec.full_name}.gem") 201 Gem::Installer.new(foo_path).install 202 Gem::Installer.new(foo_bar_path).install 203 204 @cmd.options[:args] = %w[foo] 205 206 use_ui @ui do 207 Dir.chdir @tempdir do 208 @cmd.execute 209 end 210 end 211 212 assert_path_exists File.join(@tempdir, foo_spec.full_name) 213 end 214 215 def test_handle_options_metadata 216 refute @cmd.options[:spec] 217 218 @cmd.send :handle_options, %w[--spec a] 219 220 assert @cmd.options[:spec] 221 end 222 223end 224 225