-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Expand file tree
/
Copy pathtest_gem.rb
More file actions
1908 lines (1435 loc) · 48.7 KB
/
Copy pathtest_gem.rb
File metadata and controls
1908 lines (1435 loc) · 48.7 KB
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# coding: US-ASCII
require_relative "helper"
require "rubygems"
require "rubygems/command"
require "rubygems/installer"
require "pathname"
require "tmpdir"
require "rbconfig"
class TestGem < Gem::TestCase
PLUGINS_LOADED = [] # rubocop:disable Style/MutableConstant
PROJECT_DIR = File.expand_path("../..", __dir__)
def setup
super
PLUGINS_LOADED.clear
common_installer_setup
@additional = %w[a b].map {|d| File.join @tempdir, d }
end
def test_self_finish_resolve
a1 = util_spec "a", "1", "b" => "> 0"
b1 = util_spec "b", "1", "c" => ">= 1"
b2 = util_spec "b", "2", "c" => ">= 2"
c1 = util_spec "c", "1"
c2 = util_spec "c", "2"
install_specs c1, c2, b1, b2, a1
a1.activate
assert_equal %w[a-1], loaded_spec_names
assert_equal ["b (> 0)"], unresolved_names
Gem.finish_resolve
assert_equal %w[a-1 b-2 c-2], loaded_spec_names
assert_equal [], unresolved_names
end
def test_self_finish_resolve_wtf
a1 = util_spec "a", "1", "b" => "> 0", "d" => "> 0" # this
b1 = util_spec "b", "1", { "c" => ">= 1" }, "lib/b.rb" # this
b2 = util_spec "b", "2", { "c" => ">= 2" }, "lib/b.rb"
c1 = util_spec "c", "1" # this
c2 = util_spec "c", "2"
d1 = util_spec "d", "1", { "c" => "< 2" }, "lib/d.rb"
d2 = util_spec "d", "2", { "c" => "< 2" }, "lib/d.rb" # this
install_specs c1, c2, b1, b2, d1, d2, a1
a1.activate
assert_equal %w[a-1], loaded_spec_names
assert_equal ["b (> 0)", "d (> 0)"], unresolved_names
Gem.finish_resolve
assert_equal %w[a-1 b-1 c-1 d-2], loaded_spec_names
assert_equal [], unresolved_names
end
def test_self_finish_resolve_respects_loaded_specs
a1 = util_spec "a", "1", "b" => "> 0"
b1 = util_spec "b", "1", "c" => ">= 1"
b2 = util_spec "b", "2", "c" => ">= 2"
c1 = util_spec "c", "1"
c2 = util_spec "c", "2"
install_specs c1, c2, b1, b2, a1
a1.activate
c1.activate
assert_equal %w[a-1 c-1], loaded_spec_names
assert_equal ["b (> 0)"], unresolved_names
Gem.finish_resolve
assert_equal %w[a-1 b-1 c-1], loaded_spec_names
assert_equal [], unresolved_names
end
def test_self_install
spec_fetcher do |f|
f.gem "a", 1
f.spec "a", 2
end
gemhome2 = "#{@gemhome}2"
installed = Gem.install "a", "= 1", install_dir: gemhome2
assert_equal %w[a-1], installed.map(&:full_name)
assert_path_exist File.join(gemhome2, "gems", "a-1")
end
def test_self_install_in_rescue
spec_fetcher do |f|
f.gem "a", 1
f.spec "a", 2
end
gemhome2 = "#{@gemhome}2"
installed =
begin
raise "Error"
rescue StandardError
Gem.install "a", "= 1", install_dir: gemhome2
end
assert_equal %w[a-1], installed.map(&:full_name)
end
def test_self_install_permissions
assert_self_install_permissions
end
def test_self_install_permissions_umask_0
umask = File.umask(0)
assert_self_install_permissions
ensure
File.umask(umask)
end
def test_self_install_permissions_umask_077
umask = File.umask(0o077)
assert_self_install_permissions(data_mode: 0o600)
ensure
File.umask(umask)
end
def test_self_install_permissions_with_format_executable
assert_self_install_permissions(format_executable: true)
end
def test_self_install_permissions_with_format_executable_and_non_standard_ruby_install_name
Gem::Installer.exec_format = nil
ruby_install_name "ruby27" do
assert_self_install_permissions(format_executable: true)
end
ensure
Gem::Installer.exec_format = nil
end
def assert_self_install_permissions(format_executable: false, data_mode: 0o640)
omit "FileUtils.install signature differs on JRuby/Windows" if Gem.win_platform? && Gem.java_platform?
mask = Gem.win_platform? ? 0o700 : 0o777
options = {
dir_mode: 0o500,
prog_mode: Gem.win_platform? ? 0o410 : 0o510,
data_mode: data_mode,
wrappers: true,
format_executable: format_executable,
}
Dir.chdir @tempdir do
Dir.mkdir "bin"
Dir.mkdir "data"
File.write "bin/foo", "#!/usr/bin/env ruby\n"
File.chmod 0o755, "bin/foo"
File.write "data/foo.txt", "blah\n"
spec_fetcher do |f|
f.gem "foo", 1 do |s|
s.executables = ["foo"]
s.files = %w[bin/foo data/foo.txt]
end
end
Gem.install "foo", Gem::Requirement.default, options
end
prog_mode = (options[:prog_mode] & mask).to_s(8)
dir_mode = (options[:dir_mode] & mask).to_s(8)
data_mode = (options[:data_mode] & mask).to_s(8)
prog_name = "foo"
prog_name = RbConfig::CONFIG["ruby_install_name"].sub("ruby", "foo") if options[:format_executable]
expected = {
"bin/#{prog_name}" => prog_mode,
"gems/foo-1" => dir_mode,
"gems/foo-1/bin" => dir_mode,
"gems/foo-1/data" => dir_mode,
"gems/foo-1/bin/foo" => prog_mode,
"gems/foo-1/data/foo.txt" => data_mode,
}
# add Windows script
expected["bin/#{prog_name}.bat"] = mask.to_s(8) if Gem.win_platform?
result = {}
Dir.chdir @gemhome do
expected.each_key do |n|
result[n] = (File.stat(n).mode & mask).to_s(8)
end
end
assert_equal(expected, result)
ensure
files = Dir.glob(@gemhome + "/gems/**/")
File.chmod(0o755, *files) unless files.empty?
end
def test_require_missing
assert_raise ::LoadError do
require "test_require_missing"
end
end
def test_require_does_not_glob
a1 = util_spec "a", "1", nil, "lib/a1.rb"
install_specs a1
assert_raise ::LoadError do
require "a*"
end
assert_equal [], loaded_spec_names
end
def test_self_bin_path_active
a1 = util_spec "a", "1" do |s|
s.executables = ["exec"]
end
util_spec "a", "2" do |s|
s.executables = ["exec"]
end
a1.activate
assert_match "a-1/bin/exec", Gem.bin_path("a", "exec", ">= 0")
end
def test_self_bin_path_picking_newest
a1 = util_spec "a", "1" do |s|
s.executables = ["exec"]
end
a2 = util_spec "a", "2" do |s|
s.executables = ["exec"]
end
install_specs a1, a2
assert_match "a-2/bin/exec", Gem.bin_path("a", "exec", ">= 0")
end
def test_self_activate_bin_path_no_exec_name
e = assert_raise ArgumentError do
Gem.activate_bin_path "a"
end
assert_equal "you must supply exec_name", e.message
end
def test_activate_bin_path_resolves_eagerly
a1 = util_spec "a", "1" do |s|
s.executables = ["exec"]
s.add_dependency "b"
end
b1 = util_spec "b", "1" do |s|
s.add_dependency "c", "2"
end
b2 = util_spec "b", "2" do |s|
s.add_dependency "c", "1"
end
c1 = util_spec "c", "1"
c2 = util_spec "c", "2"
install_specs c1, c2, b1, b2, a1
Gem.activate_bin_path("a", "exec", ">= 0")
# If we didn't eagerly resolve, this would activate c-2 and then the
# finish_resolve would cause a conflict
gem "c"
Gem.finish_resolve
assert_equal %w[a-1 b-2 c-1], loaded_spec_names
end
def test_activate_bin_path_does_not_error_if_a_gem_thats_not_finally_activated_has_orphaned_dependencies
a1 = util_spec "a", "1" do |s|
s.executables = ["exec"]
s.add_dependency "b"
end
b1 = util_spec "b", "1" do |s|
s.add_dependency "c", "1"
end
b2 = util_spec "b", "2" do |s|
s.add_dependency "c", "2"
end
c2 = util_spec "c", "2"
install_specs c2, b1, b2, a1
# c1 is missing, but not needed for activation, so we should not get any errors here
Gem.activate_bin_path("a", "exec", ">= 0")
assert_equal %w[a-1 b-2 c-2], loaded_spec_names
end
def test_activate_bin_path_backtracks_when_highest_version_has_orphaned_dependencies
a1 = util_spec "a", "1" do |s|
s.executables = ["exec"]
s.add_dependency "b"
end
b1 = util_spec "b", "1" do |s|
s.add_dependency "c", "1"
end
b2 = util_spec "b", "2" do |s|
s.add_dependency "c", "2"
end
c1 = util_spec "c", "1"
install_specs c1, b1, b2, a1
# c2 is missing, but the resolver backtracks from b2 to b1 which
# works with c1, finding a valid solution despite partial installation
load Gem.activate_bin_path("a", "exec", ">= 0")
assert_equal %w[a-1 b-1 c-1], loaded_spec_names
end
def test_activate_bin_path_in_debug_mode
a1 = util_spec "a", "1" do |s|
s.executables = ["exec"]
end
install_specs a1
require "open3"
output, status = Open3.capture2e(
{ "GEM_HOME" => Gem.paths.home, "DEBUG_RESOLVER" => "1" },
*ruby_with_rubygems_in_load_path, "-e", "\"Gem.activate_bin_path('a', 'exec', '>= 0')\""
)
assert status.success?, output
end
def test_activate_bin_path_selects_exact_bundler_version_if_present
bundler_latest = util_spec "bundler", "2.0.1" do |s|
s.executables = ["bundle"]
end
bundler_previous = util_spec "bundler", "2.0.0" do |s|
s.executables = ["bundle"]
end
install_specs bundler_latest, bundler_previous
File.open("Gemfile.lock", "w") do |f|
f.write <<~L
GEM
remote: https://rubygems.org/
specs:
PLATFORMS
ruby
DEPENDENCIES
BUNDLED WITH
2.0.0
L
end
File.open("Gemfile", "w") {|f| f.puts('source "https://rubygems.org"') }
load Gem.activate_bin_path("bundler", "bundle", ">= 0.a")
assert_equal %w[bundler-2.0.0], loaded_spec_names
end
def test_activate_bin_path_respects_underscore_selection_if_given
bundler_latest = util_spec "bundler", "2.0.1" do |s|
s.executables = ["bundle"]
end
bundler_previous = util_spec "bundler", "1.17.3" do |s|
s.executables = ["bundle"]
end
install_specs bundler_latest, bundler_previous
File.open("Gemfile.lock", "w") do |f|
f.write <<~L
GEM
remote: https://rubygems.org/
specs:
PLATFORMS
ruby
DEPENDENCIES
BUNDLED WITH
2.0.1
L
end
File.open("Gemfile", "w") {|f| f.puts('source "https://rubygems.org"') }
load Gem.activate_bin_path("bundler", "bundle", "= 1.17.3")
assert_equal %w[bundler-1.17.3], loaded_spec_names
end
def test_activate_bin_path_gives_proper_error_for_bundler_when_underscore_selection_given
File.open("Gemfile.lock", "w") do |f|
f.write <<~L
GEM
remote: https://rubygems.org/
specs:
PLATFORMS
ruby
DEPENDENCIES
BUNDLED WITH
2.1.4
L
end
File.open("Gemfile", "w") {|f| f.puts('source "https://rubygems.org"') }
e = assert_raise Gem::GemNotFoundException do
load Gem.activate_bin_path("bundler", "bundle", "= 2.2.8")
end
assert_equal "can't find gem bundler (= 2.2.8) with executable bundle", e.message
end
def test_self_bin_path_no_exec_name
e = assert_raise ArgumentError do
Gem.bin_path "a"
end
assert_equal "you must supply exec_name", e.message
end
def test_self_bin_path_bin_name
install_specs util_exec_gem
assert_equal @abin_path, Gem.bin_path("a", "abin")
end
def test_self_bin_path_bin_name_version
install_specs util_exec_gem
assert_equal @abin_path, Gem.bin_path("a", "abin", "4")
end
def test_self_bin_path_nonexistent_binfile
util_spec "a", "2" do |s|
s.executables = ["exec"]
end
assert_raise(Gem::GemNotFoundException) do
Gem.bin_path("a", "other", "2")
end
end
def test_self_bin_path_no_bin_file
util_spec "a", "1"
assert_raise(ArgumentError) do
Gem.bin_path("a", nil, "1")
end
end
def test_self_bin_path_not_found
assert_raise(Gem::GemNotFoundException) do
Gem.bin_path("non-existent", "blah")
end
end
def test_self_bin_path_bin_file_gone_in_latest
install_specs util_exec_gem
spec = util_spec "a", "10" do |s|
s.executables = []
end
install_specs spec
assert_equal @abin_path, Gem.bin_path("a", "abin")
end
def test_self_bindir
assert_equal File.join(@gemhome, "bin"), Gem.bindir
assert_equal File.join(@gemhome, "bin"), Gem.bindir(Gem.dir)
assert_equal File.join(@gemhome, "bin"), Gem.bindir(Pathname.new(Gem.dir))
end
def test_self_bindir_default_dir
default = Gem.default_dir
assert_equal Gem.default_bindir, Gem.bindir(default)
end
def test_self_clear_paths
assert_match(/gemhome$/, Gem.dir)
assert_match(/gemhome$/, Gem.path.first)
Gem.clear_paths
with_env("GEM_HOME" => "foo", "GEM_PATH" => "bar") do
assert_equal("foo", Gem.dir)
assert_equal("bar", Gem.path.first)
end
end
def test_self_configuration
expected = Gem::ConfigFile.new []
Gem.configuration = nil
assert_equal expected, Gem.configuration
end
def test_self_default_exec_format
ruby_install_name "ruby" do
assert_equal "%s", Gem.default_exec_format
end
end
def test_self_default_exec_format_18
ruby_install_name "ruby18" do
assert_equal "%s18", Gem.default_exec_format
end
end
def test_self_default_exec_format_jruby
ruby_install_name "jruby" do
assert_equal "j%s", Gem.default_exec_format
end
end
def test_default_path
vendordir(File.join(@tempdir, "vendor")) do
FileUtils.rm_rf Gem.user_home
expected = [Gem.default_dir]
assert_equal expected, Gem.default_path
end
end
def test_default_path_missing_vendor
vendordir(nil) do
FileUtils.rm_rf Gem.user_home
expected = [Gem.default_dir]
assert_equal expected, Gem.default_path
end
end
def test_default_path_user_home
vendordir(File.join(@tempdir, "vendor")) do
expected = [Gem.user_dir, Gem.default_dir]
assert_equal expected, Gem.default_path
end
end
def test_default_path_vendor_dir
vendordir(File.join(@tempdir, "vendor")) do
FileUtils.mkdir_p Gem.vendor_dir
FileUtils.rm_rf Gem.user_home
expected = [Gem.default_dir, Gem.vendor_dir]
assert_equal expected, Gem.default_path
end
end
def test_self_default_sources
Gem.remove_instance_variable :@default_sources
assert_equal %w[https://rubygems.org/], Gem.default_sources
end
def test_self_dir
assert_equal @gemhome, Gem.dir
end
def test_self_ensure_gem_directories
FileUtils.rm_r @gemhome
Gem.use_paths @gemhome
Gem.ensure_gem_subdirectories @gemhome
assert_path_exist File.join @gemhome, "build_info"
assert_path_exist File.join @gemhome, "cache"
assert_path_exist File.join @gemhome, "doc"
assert_path_exist File.join @gemhome, "extensions"
assert_path_exist File.join @gemhome, "gems"
assert_path_exist File.join @gemhome, "specifications"
end
def test_self_ensure_gem_directories_permissions
FileUtils.rm_r @gemhome
Gem.use_paths @gemhome
Gem.ensure_gem_subdirectories @gemhome, 0o750
assert_directory_exists File.join(@gemhome, "cache")
assert_equal 0o750, File::Stat.new(@gemhome).mode & 0o777
assert_equal 0o750, File::Stat.new(File.join(@gemhome, "cache")).mode & 0o777
end unless Gem.win_platform?
def test_self_ensure_gem_directories_safe_permissions
FileUtils.rm_r @gemhome
Gem.use_paths @gemhome
old_umask = File.umask
File.umask 0
Gem.ensure_gem_subdirectories @gemhome
assert_equal 0, File::Stat.new(@gemhome).mode & 0o002
assert_equal 0, File::Stat.new(File.join(@gemhome, "cache")).mode & 0o002
ensure
File.umask old_umask
end unless Gem.win_platform?
def test_self_ensure_gem_directories_missing_parents
gemdir = File.join @tempdir, "a/b/c/gemdir"
begin
FileUtils.rm_rf File.join(@tempdir, "a")
rescue StandardError
nil
end
refute File.exist?(File.join(@tempdir, "a")),
"manually remove #{File.join @tempdir, "a"}, tests are broken"
Gem.use_paths gemdir
Gem.ensure_gem_subdirectories gemdir
assert_directory_exists util_cache_dir
end
unless Gem.win_platform? || Process.uid.zero? # only for FS that support write protection
def test_self_ensure_gem_directories_write_protected
gemdir = File.join @tempdir, "egd"
begin
FileUtils.rm_r gemdir
rescue StandardError
nil
end
refute File.exist?(gemdir), "manually remove #{gemdir}, tests are broken"
FileUtils.mkdir_p gemdir
FileUtils.chmod 0o400, gemdir
Gem.use_paths gemdir
Gem.ensure_gem_subdirectories gemdir
refute File.exist?(util_cache_dir)
ensure
FileUtils.chmod 0o600, gemdir
end
def test_self_ensure_gem_directories_write_protected_parents
parent = File.join(@tempdir, "egd")
gemdir = "#{parent}/a/b/c"
begin
FileUtils.rm_r parent
rescue StandardError
nil
end
refute File.exist?(parent), "manually remove #{parent}, tests are broken"
FileUtils.mkdir_p parent
FileUtils.chmod 0o400, parent
Gem.use_paths(gemdir)
Gem.ensure_gem_subdirectories gemdir
refute File.exist? File.join(gemdir, "gems")
ensure
FileUtils.chmod 0o600, parent
end
def test_self_ensure_gem_directories_non_existent_paths
Gem.ensure_gem_subdirectories "/proc/0123456789/bogus" # should not raise
Gem.ensure_gem_subdirectories "classpath:/bogus/x" # JRuby embed scenario
end
end
def test_self_extension_dir_shared
enable_shared "yes" do
assert_equal Gem.ruby_api_version, Gem.extension_api_version
end
end
def test_self_extension_dir_static
enable_shared "no" do
assert_equal "#{Gem.ruby_api_version}-static", Gem.extension_api_version
end
end
def test_self_find_files
cwd = File.expand_path("test/rubygems", PROJECT_DIR)
$LOAD_PATH.unshift cwd
discover_path = File.join "lib", "sff", "discover.rb"
foo1, foo2 = %w[1 2].map do |version|
spec = quick_gem "sff", version do |s|
s.files << discover_path
end
write_file(File.join("gems", spec.full_name, discover_path)) do |fp|
fp.puts "# #{spec.full_name}"
end
spec
end
Gem.refresh
expected = [
File.expand_path("test/rubygems/sff/discover.rb", PROJECT_DIR),
File.join(foo2.full_gem_path, discover_path),
File.join(foo1.full_gem_path, discover_path),
]
assert_equal expected, Gem.find_files("sff/discover")
assert_equal expected, Gem.find_files("sff/**.rb"), "[ruby-core:31730]"
ensure
assert_equal cwd, $LOAD_PATH.shift
end
def test_self_find_latest_files
cwd = File.expand_path("test/rubygems", PROJECT_DIR)
$LOAD_PATH.unshift cwd
discover_path = File.join "lib", "sff", "discover.rb"
_, foo2 = %w[1 2].map do |version|
spec = quick_gem "sff", version do |s|
s.files << discover_path
end
write_file(File.join("gems", spec.full_name, discover_path)) do |fp|
fp.puts "# #{spec.full_name}"
end
spec
end
Gem.refresh
expected = [
File.expand_path("test/rubygems/sff/discover.rb", PROJECT_DIR),
File.join(foo2.full_gem_path, discover_path),
]
assert_equal expected, Gem.find_latest_files("sff/discover")
assert_equal expected, Gem.find_latest_files("sff/**.rb"), "[ruby-core:31730]"
ensure
assert_equal cwd, $LOAD_PATH.shift
end
def test_self_latest_spec_for
gems = spec_fetcher do |fetcher|
fetcher.spec "a", 1
fetcher.spec "a", "3.a"
fetcher.spec "a", 2
end
spec = Gem.latest_spec_for "a"
assert_equal gems["a-2"], spec
end
def test_self_latest_spec_for_multiple_sources
uri = "https://example.sample.com/"
source = Gem::Source.new(uri)
source_list = Gem::SourceList.new
source_list << Gem::Source.new(@uri)
source_list << source
Gem.sources.replace source_list
spec_fetcher(uri) do |fetcher|
fetcher.spec "a", 1.1
end
gems = spec_fetcher do |fetcher|
fetcher.spec "a", 1
fetcher.spec "a", "3.a"
fetcher.spec "a", 2
end
spec = Gem.latest_spec_for "a"
assert_equal gems["a-2"], spec
end
def test_self_latest_rubygems_version
spec_fetcher do |fetcher|
fetcher.spec "rubygems-update", "1.8.23"
fetcher.spec "rubygems-update", "1.8.24"
fetcher.spec "rubygems-update", "2.0.0.preview3"
end
version = Gem.latest_rubygems_version
assert_equal Gem::Version.new("1.8.24"), version
end
def test_self_latest_version_for
spec_fetcher do |fetcher|
fetcher.spec "a", 1
fetcher.spec "a", 2
fetcher.spec "a", "3.a"
end
version = Gem.latest_version_for "a"
assert_equal Gem::Version.new(2), version
end
def test_self_latest_version_for_multiple_sources
uri = "https://example.sample.com/"
source = Gem::Source.new(uri)
source_list = Gem::SourceList.new
source_list << Gem::Source.new(@uri)
source_list << source
Gem.sources.replace source_list
spec_fetcher(uri) do |fetcher|
fetcher.spec "a", 1.1
end
spec_fetcher do |fetcher|
fetcher.spec "a", 1
fetcher.spec "a", 2
fetcher.spec "a", "3.a"
end
version = Gem.latest_version_for "a"
assert_equal Gem::Version.new(2), version
end
def test_self_loaded_specs
foo = util_spec "foo"
install_gem foo
foo.activate
assert_equal true, Gem.loaded_specs.keys.include?("foo")
end
def test_self_path
assert_equal [Gem.dir], Gem.path
end
def test_self_path_default
ENV.delete "GEM_HOME"
ENV.delete "GEM_PATH"
Gem.instance_variable_set :@paths, nil
assert_equal [Gem.default_path, Gem.dir].flatten.uniq, Gem.path
end
def test_self_path_ENV_PATH
path_count = Gem.path.size
Gem.clear_paths
ENV["GEM_PATH"] = @additional.join(File::PATH_SEPARATOR)
assert_equal @additional, Gem.path[0,2]
assert_equal path_count + @additional.size, Gem.path.size,
"extra path components: #{Gem.path[2..-1].inspect}"
assert_equal Gem.dir, Gem.path.last
end
def test_self_path_duplicate
Gem.clear_paths
util_ensure_gem_dirs
dirs = @additional + [@gemhome] + [File.join(@tempdir, "a")]
ENV["GEM_HOME"] = @gemhome
ENV["GEM_PATH"] = dirs.join File::PATH_SEPARATOR
assert_equal @gemhome, Gem.dir
paths = [Gem.dir]
assert_equal @additional + paths, Gem.path
end
def test_self_path_overlap
Gem.clear_paths
util_ensure_gem_dirs
ENV["GEM_HOME"] = @gemhome
ENV["GEM_PATH"] = @additional.join(File::PATH_SEPARATOR)
assert_equal @gemhome, Gem.dir
paths = [Gem.dir]
assert_equal @additional + paths, Gem.path
end
def test_self_platforms
assert_equal [Gem::Platform::RUBY, Gem::Platform.local], Gem.platforms
end
def test_self_prefix
assert_equal PROJECT_DIR, Gem.prefix
end
def test_self_prefix_libdir
orig_libdir = RbConfig::CONFIG["libdir"]
RbConfig::CONFIG["libdir"] = PROJECT_DIR
assert_nil Gem.prefix
ensure
RbConfig::CONFIG["libdir"] = orig_libdir
end
def test_self_prefix_sitelibdir
orig_sitelibdir = RbConfig::CONFIG["sitelibdir"]
RbConfig::CONFIG["sitelibdir"] = PROJECT_DIR
assert_nil Gem.prefix
ensure
RbConfig::CONFIG["sitelibdir"] = orig_sitelibdir
end
def test_self_read_binary
File.open "test", "w" do |io|
io.write "\xCF\x80"
end
assert_equal ["\xCF", "\x80"], Gem.read_binary("test").chars.to_a
pend "chmod not supported" if Gem.win_platform?
begin
File.chmod 0o444, "test"
assert_equal ["\xCF", "\x80"], Gem.read_binary("test").chars.to_a
ensure
File.chmod 0o644, "test"
end
end
def test_self_refresh
util_make_gems
a1_spec = @a1.spec_file
moved_path = File.join @tempdir, File.basename(a1_spec)
FileUtils.mv a1_spec, moved_path
Gem.refresh
refute_includes Gem::Specification.all_names, @a1.full_name
FileUtils.mv moved_path, a1_spec
Gem.refresh
assert_includes Gem::Specification.all_names, @a1.full_name
end
def test_self_refresh_keeps_loaded_specs_activated
util_make_gems
a1_spec = @a1.spec_file
moved_path = File.join @tempdir, File.basename(a1_spec)
FileUtils.mv a1_spec, moved_path
Gem.refresh
s = Gem::Specification.first
s.activate
Gem.refresh
Gem::Specification.each {|spec| assert spec.activated? if spec == s }
Gem.loaded_specs.delete(s)
Gem.refresh
end