-
-
Notifications
You must be signed in to change notification settings - Fork 270
/
new.jl
3246 lines (3100 loc) · 132 KB
/
new.jl
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
module NewTests
using Test, UUIDs, Dates, TOML
import ..Pkg, LibGit2
using Pkg.Types: PkgError
using Pkg.Resolve: ResolverError
import Pkg.Artifacts: artifact_meta, artifact_path
import Base.BinaryPlatforms: HostPlatform, Platform, platforms_match
using ..Utils
import ..HistoricalStdlibVersions
using Logging
general_uuid = UUID("23338594-aafe-5451-b93e-139f81909106") # UUID for `General`
exuuid = UUID("7876af07-990d-54b4-ab0e-23690620f79a") # UUID for `Example.jl`
json_uuid = UUID("682c06a0-de6a-54ab-a142-c8b1cf79cde6")
parsers_uuid = UUID("69de0a69-1ddd-5017-9359-2bf0b02dc9f0")
markdown_uuid = UUID("d6f4376e-aef5-505a-96c1-9c027394607a")
test_stdlib_uuid = UUID("8dfed614-e22c-5e08-85e1-65c5234f0b40")
unicode_uuid = UUID("4ec0a83e-493e-50e2-b9ac-8f72acf5a8f5")
unregistered_uuid = UUID("dcb67f36-efa0-11e8-0cef-2fc465ed98ae")
simple_package_uuid = UUID("fc6b7c0f-8a2f-4256-bbf4-8c72c30df5be")
pngjll_uuid = UUID("b53b4c65-9356-5827-b1ea-8c7a1a84506f")
# Disable auto-gc for these tests
Pkg._auto_gc_enabled[] = false
#
# # Depot Changes
#
@testset "Depot setup" begin
isolate() do
# Lets make sure we start with a clean slate.
rm(LOADED_DEPOT; force=true, recursive=true)
mkdir(LOADED_DEPOT)
# And set the loaded depot as our working depot.
empty!(DEPOT_PATH)
push!(DEPOT_PATH, LOADED_DEPOT)
Base.append_bundled_depot_path!(DEPOT_PATH)
# Now we double check we have a clean slate.
@test isempty(Pkg.dependencies())
# A simple `add` should set up some things for us:
Pkg.add(name="Example", version="0.5.3")
# - `General` should be initiated by default.
regs = Pkg.Registry.reachable_registries()
@test length(regs) == 1
reg = regs[1]
@test reg.name == "General"
@test reg.uuid == general_uuid
# - The package should be installed correctly.
source053, source053_time = nothing, nothing
Pkg.dependencies(exuuid) do pkg
@test isdir(pkg.source)
source053 = pkg.source
source053_time = mtime(pkg.source)
end
# - The active project was automatically created.
@test haskey(Pkg.project().dependencies, "Example")
@test length(Pkg.project().dependencies) == 1
# Now we install the same package at a different version:
Pkg.add(name="Example", version="0.5.1")
# - Check that the package was installed correctly.
Pkg.dependencies(exuuid) do pkg
@test pkg.version == v"0.5.1"
@test isdir(pkg.source)
# - We also check the interaction between the previously installed version.
@test pkg.source != source053
end
# Now a few more versions:
Pkg.add(name="Example", version="0.5.0")
Pkg.add(name="Example")
Pkg.add(name="Example", version="0.3.0")
Pkg.add(name="Example", version="0.3.3")
# With similar checks
Pkg.dependencies(exuuid) do pkg
@test pkg.version == v"0.3.3"
@test isdir(pkg.source)
end
# Now we try adding a second dependency.
# We repeat the same class of tests.
Pkg.add(name="JSON", version="0.18.0")
sourcej018 = nothing
Pkg.dependencies(json_uuid) do pkg
@test pkg.version == v"0.18.0"
@test isdir(pkg.source)
end
Pkg.add(name="JSON", version="0.20.0")
Pkg.dependencies(json_uuid) do pkg
@test isdir(pkg.source)
@test pkg.source != sourcej018
end
# Now check packages which track repos instead of registered versions
Pkg.add(url="https://github.com/JuliaLang/Example.jl", rev="v0.5.3")
Pkg.dependencies(exuuid) do pkg
@test !pkg.is_tracking_registry
@test isdir(pkg.source)
@test isdir(Pkg.Types.add_repo_cache_path(pkg.git_source))
end
Pkg.add(name="Example", rev="master")
Pkg.dependencies(exuuid) do pkg
@test !pkg.is_tracking_registry
@test isdir(pkg.source)
@test isdir(Pkg.Types.add_repo_cache_path(pkg.git_source))
end
# Also check that unregistered packages are installed properly.
Pkg.add(url="https://github.com/00vareladavid/Unregistered.jl")
Pkg.dependencies(unregistered_uuid) do pkg
@test isdir(pkg.source)
@test isdir(Pkg.Types.add_repo_cache_path(pkg.git_source))
end
# Check `develop`
Pkg.develop(name="Example")
Pkg.dependencies(exuuid) do pkg
@test isdir(pkg.source) # TODO check for full git clone, have to implement saving original URL first
end
Pkg.develop(name="JSON")
Pkg.dependencies(json_uuid) do pkg
@test isdir(pkg.source) # TODO check for full git clone, have to implement saving original URL first
end
# Check that the original installation was undisturbed.
regs = Pkg.Registry.reachable_registries()
@test length(regs) == 1
reg = regs[1]
@test reg.name == "General"
@test reg.uuid == general_uuid
@test mtime(source053) == source053_time
# Now we clean up so that `isolate` can reuse the loaded depot properly
rm(joinpath(LOADED_DEPOT, "environments"); force=true, recursive=true)
rm(joinpath(LOADED_DEPOT, "clones"); force=true, recursive=true)
rm(joinpath(LOADED_DEPOT, "logs"); force=true, recursive=true)
rm(joinpath(LOADED_DEPOT, "dev"); force=true, recursive=true)
for (root, dirs, files) in walkdir(LOADED_DEPOT)
for file in files
filepath = joinpath(root, file)
fmode = filemode(filepath)
try
chmod(filepath, fmode & (typemax(fmode) ⊻ 0o222))
catch
end
end
end
end
end
#
# ## Sandboxing
#
inside_test_sandbox(fn, name; kwargs...) = Pkg.test(name; test_fn=fn, kwargs...)
inside_test_sandbox(fn; kwargs...) = Pkg.test(;test_fn=fn, kwargs...)
@testset "test: printing" begin
isolate(loaded_depot=true) do
Pkg.add(name="Example")
io = Base.BufferStream()
Pkg.test("Example"; io=io)
closewrite(io)
output = read(io, String)
@test occursin(r"Testing Example", output)
@test occursin(r"Status `.+Project\.toml`", output)
@test occursin(r"Status `.+Manifest\.toml`", output)
@test occursin(r"Testing Running tests...", output)
@test occursin(r"Testing Example tests passed", output)
end
end
@testset "test: sandboxing" begin
# explicit test dependencies and the tested project are available within the test sandbox
isolate(loaded_depot=true) do; mktempdir() do tempdir
foo_uuid = UUID("02250abe-2050-11e9-017e-b301a2b5bcc4")
path = copy_test_package(tempdir, "BasicSandbox")
# we set readonly here to simulate the permissions in the `$DEPOT/packages` directory
Pkg.Types.set_readonly(path)
Pkg.develop(path=path)
inside_test_sandbox("BasicSandbox") do
Pkg.dependencies(foo_uuid) do pkg
@test length(pkg.dependencies) == 1
@test haskey(pkg.dependencies, "Random")
end
@test haskey(Pkg.project().dependencies, "Test")
@test haskey(Pkg.project().dependencies, "BasicSandbox")
end
end end
# the active dependency graph is transferred to the test sandbox
isolate(loaded_depot=true) do; mktempdir() do tempdir
path = copy_test_package(tempdir, "TransferSubgraph")
Pkg.activate(path)
active_json_version = Pkg.dependencies()[json_uuid].version
inside_test_sandbox("Unregistered") do
@test Pkg.dependencies()[json_uuid].version == active_json_version
end
end end
# the active dep graph is transferred to test sandbox, even when tracking unregistered repos
isolate(loaded_depot=true) do; mktempdir() do tempdir
path = copy_test_package(tempdir, "TestSubgraphTrackingRepo")
Pkg.activate(path)
inside_test_sandbox() do
Pkg.dependencies(unregistered_uuid) do pkg
@test pkg.git_source == "https://github.com/00vareladavid/Unregistered.jl"
@test !pkg.is_tracking_registry
end
end
end end
# a test dependency can track a path
isolate(loaded_depot=true) do; mktempdir() do tempdir
path = copy_test_package(tempdir, "TestDepTrackingPath")
Pkg.activate(path)
inside_test_sandbox() do
@test Pkg.dependencies()[unregistered_uuid].is_tracking_path
end
end end
# a test dependency can track a repo
isolate(loaded_depot=true) do; mktempdir() do tempdir
path = copy_test_package(tempdir, "TestDepTrackingRepo")
Pkg.activate(path)
inside_test_sandbox() do
Pkg.dependencies(unregistered_uuid) do pkg
@test !pkg.is_tracking_registry
@test pkg.git_source == "https://github.com/00vareladavid/Unregistered.jl"
end
end
end end
# `compat` for test dependencies is honored
isolate(loaded_depot=true) do; mktempdir() do tempdir
path = copy_test_package(tempdir, "TestDepCompat")
Pkg.activate(path)
inside_test_sandbox() do
deps = Pkg.dependencies()
@test deps[exuuid].version == v"0.3.0"
@test deps[UUID("9cb9b0df-a8d1-4a6c-a371-7d2ae60a2f25")].version == v"0.1.0"
end
end end
end
# These tests cover the original "targets" API for specifying test dependencies
@testset "test: 'targets' based testing" begin
# `Pkg.test` should work on dependency graphs with nodes sharing the same name but not the same UUID
isolate(loaded_depot=true) do; mktempdir() do tempdir
Pkg.activate(joinpath(@__DIR__, "test_packages", "SameNameDifferentUUID"))
inside_test_sandbox("Example") do
Pkg.dependencies(UUID("6876af07-990d-54b4-ab0e-23690620f79a")) do pkg
@test pkg.name == "Example"
@test realpath(pkg.source) == realpath(joinpath(@__DIR__, "test_packages", "SameNameDifferentUUID", "dev", "Example"))
end
end
end end
isolate(loaded_depot=true) do; mktempdir() do tempdir
basic_test_target = UUID("50adb811-5a1f-4be4-8146-2725c7f5d900")
path = copy_test_package(tempdir, "BasicTestTarget")
# we set readonly here to simulate the permissions in the `$DEPOT/packages` directory
Pkg.Types.set_readonly(path)
Pkg.develop(path=path)
inside_test_sandbox("BasicTestTarget") do
@test haskey(Pkg.project().dependencies, "Markdown")
@test haskey(Pkg.project().dependencies, "Test")
@test haskey(Pkg.project().dependencies, "BasicTestTarget")
Pkg.dependencies(basic_test_target) do pkg
@test pkg.is_tracking_path == true
@test haskey(pkg.dependencies, "UUIDs")
@test !haskey(pkg.dependencies, "Markdown")
@test !haskey(pkg.dependencies, "Test")
end
end
end end
# dependency of test dependency (#567)
isolate(loaded_depot=true) do; mktempdir() do tempdir
for x in ["x1", "x2", "x3"]
path = copy_test_package(tempdir, x)
Pkg.develop(Pkg.PackageSpec(path = path))
end
Pkg.test("x3")
end end
# preserve root of active project if it is a dependency (#1423)
isolate(loaded_depot=false) do; mktempdir() do tempdir
path = copy_test_package(tempdir, "ActiveProjectInTestSubgraph")
Pkg.activate(path)
inside_test_sandbox("B") do
deps = Pkg.dependencies()
@test deps[UUID("c86f0f68-174e-41db-bd5e-b032223de205")].version == v"1.2.3"
end
end end
# test targets should also honor compat
isolate(loaded_depot=false) do; mktempdir() do tempdir
path = copy_test_package(tempdir, "TestTargetCompat")
Pkg.activate(path)
inside_test_sandbox() do
deps = Pkg.dependencies()
@test deps[exuuid].version == v"0.3.0"
end
end end
end
@testset "test: fallback when no project file exists" begin
isolate(loaded_depot=true) do
Pkg.add(name="Permutations", version="0.3.2")
if Sys.WORD_SIZE == 32
# The Permutations.jl v0.3.2 tests are known to fail on 32-bit Julia
@test_skip Pkg.test("Permutations")
else
Pkg.test("Permutations")
end
end
end
@testset "using a test/REQUIRE file" begin
isolate() do
Pkg.add(name="EnglishText", version="0.6.0")
Pkg.test("EnglishText")
end
end
#
# # Activate
#
@testset "activate: repl" begin
isolate(loaded_depot=true) do
Pkg.REPLMode.TEST_MODE[] = true
# - activate shared env
api, args, opts = first(Pkg.pkg"activate --shared Foo")
@test api == Pkg.activate
@test args == "Foo"
@test opts == Dict(:shared => true)
# - activate shared env using special syntax
api, args, opts = first(Pkg.pkg"activate @Foo")
@test api == Pkg.activate
@test args == "Foo"
@test opts == Dict(:shared => true)
# - no arg activate
api, opts = first(Pkg.pkg"activate")
@test api == Pkg.activate
@test isempty(opts)
# - regular activate
api, args, opts = first(Pkg.pkg"activate FooBar")
@test api == Pkg.activate
@test args == "FooBar"
@test isempty(opts)
# - activating a temporary project
api, opts = first(Pkg.pkg"activate --temp")
@test api == Pkg.activate
@test opts == Dict(:temp => true)
# - activating the previous project
api, opts = first(Pkg.pkg"activate -")
@test api == Pkg.activate
@test opts == Dict(:prev => true)
# github branch rewriting
api, args, opts = first(Pkg.pkg"add https://github.com/JuliaLang/Pkg.jl/tree/aa/gitlab")
arg = args[1]
@test arg.url == "https://github.com/JuliaLang/Pkg.jl"
@test arg.rev == "aa/gitlab"
end
end
@testset "activate" begin
isolate(loaded_depot=true) do
io = IOBuffer()
Pkg.activate("Foo"; io=io)
output = String(take!(io))
@test occursin(r"Activating.*project at.*`.*Foo`", output)
Pkg.activate(; io=io, temp=true)
output = String(take!(io))
@test occursin(r"Activating new project at `.*`", output)
prev_env = Base.active_project()
# - activating the previous project
Pkg.activate(; temp=true)
@test Base.active_project() != prev_env
Pkg.activate(; prev=true)
@test prev_env == Base.active_project()
Pkg.activate(; temp=true)
@test Base.active_project() != prev_env
Pkg.activate(; prev=true)
@test Base.active_project() == prev_env
Pkg.activate("")
@test Base.active_project() != prev_env
Pkg.activate(; prev=true)
@test Base.active_project() == prev_env
load_path_before = copy(LOAD_PATH)
try
empty!(LOAD_PATH) # unset active env
Pkg.activate() # shouldn't error
Pkg.activate(; prev=true) # shouldn't error
finally
append!(empty!(LOAD_PATH), load_path_before)
end
end
end
#
# # Add
#
#
# ## Input Checking
#
# Here we check against invalid input.
@testset "add: input checking" begin
isolate(loaded_depot=true) do
# Julia is not a valid package name.
@test_throws PkgError("`julia` is not a valid package name") Pkg.add(name="julia")
# Package names must be valid Julia identifiers.
@test_throws PkgError("`***` is not a valid package name") Pkg.add(name="***")
@test_throws PkgError("`Foo Bar` is not a valid package name") Pkg.add(name="Foo Bar")
# Names which are invalid and are probably URLs or paths.
@test_throws PkgError("""
`https://github.com` is not a valid package name
The argument appears to be a URL or path, perhaps you meant `Pkg.add(url="...")` or `Pkg.add(path="...")`.""") Pkg.add("https://github.com")
@test_throws PkgError("""
`./Foobar` is not a valid package name
The argument appears to be a URL or path, perhaps you meant `Pkg.add(url="...")` or `Pkg.add(path="...")`.""") Pkg.add("./Foobar")
# An empty spec is invalid.
@test_throws PkgError(
"name, UUID, URL, or filesystem path specification required when calling `add`"
) Pkg.add(Pkg.PackageSpec())
# Versions imply that we are tracking a registered version.
@test_throws PkgError(
"version specification invalid when tracking a repository: `0.5.0` specified for package `Example`"
) Pkg.add(name="Example", rev="master", version="0.5.0")
# Adding with a slight typo gives suggestions
try
Pkg.add("Examplle")
@test false # to fail if add doesn't error
catch err
@test err isa PkgError
@test occursin("The following package names could not be resolved:", err.msg)
@test occursin("Examplle (not found in project, manifest or registry)", err.msg)
@test occursin("Suggestions:", err.msg)
# @test occursin("Example", err.msg) # can't test this as each char in "Example" is individually colorized
end
@test_throws PkgError(
"name, UUID, URL, or filesystem path specification required when calling `add`"
) Pkg.add(Pkg.PackageSpec())
# Adding an unregistered package
@test_throws PkgError Pkg.add("ThisIsHopefullyRandom012856014925701382")
# Wrong UUID
@test_throws PkgError Pkg.add(Pkg.PackageSpec("Example", UUID(UInt128(1))))
# Missing UUID
@test_throws PkgError Pkg.add(Pkg.PackageSpec(uuid = uuid4()))
# Two packages with the same name
@test_throws PkgError(
"it is invalid to specify multiple packages with the same name: `Example`"
) Pkg.add([(;name="Example"), (;name="Example",version="0.5.0")])
end
# Unregistered UUID in manifest
isolate(loaded_depot=true) do; mktempdir() do tempdir
package_path = copy_test_package(tempdir, "UnregisteredUUID")
Pkg.activate(package_path)
@test_throws PkgError("expected package `Example [142fd7e7]` to be registered") Pkg.add("JSON")
end end
# empty git repo (no commits)
isolate(loaded_depot=true) do; mktempdir() do tempdir
close(LibGit2.init(tempdir))
try Pkg.add(path=tempdir)
@test false # to fail if add doesn't error
catch err
@test err isa PkgError
@test match(r"^invalid git HEAD", err.msg) !== nothing
end
end end
end
#
# ## Changes to the active project
#
# Here we can use a loaded depot because we are only checking changes to the active project.
# We check that `add` supports basic operations on a clean project.
# The package should be added as a direct dependency.
@testset "add: changes to the active project" begin
# Basic add
isolate(loaded_depot=true) do
Pkg.add(Pkg.PackageSpec("Example"))
Pkg.dependencies(exuuid) do ex
@test ex.is_tracking_registry
end
@test haskey(Pkg.project().dependencies, "Example")
end
# Basic add by version
isolate(loaded_depot=true) do
Pkg.add(name="Example", version="0.5.0")
Pkg.dependencies(exuuid) do ex
@test ex.is_tracking_registry
@test ex.version == v"0.5.0"
end
@test haskey(Pkg.project().dependencies, "Example")
end
# Basic Add by VersionRange
#= TODO
isolate(loaded_depot=true) do
# TODO this test is leaky. Will version="0.3.0-0.3.2" suffice?
range = VersionRange("0.3.0-0.3.2")
Pkg.add(Pkg.PackageSpec(TEST_PKG.name, Pkg.Types.VersionSpec(range)))
Pkg.dependencies(exuuid) do pkg
@test pkg.is_tracking_registry
@test pkg.version in range
end
@test Pkg.dependencies()[TEST_PKG.uuid].version == v"0.3.2"
end
=#
# Basic add by URL
isolate(loaded_depot=true) do
Pkg.add(url="https://github.com/JuliaLang/Example.jl", rev="v0.5.3")
Pkg.dependencies(exuuid) do ex
@test !ex.is_tracking_registry
@test ex.git_source == "https://github.com/JuliaLang/Example.jl"
@test ex.git_revision == "v0.5.3"
end
@test haskey(Pkg.project().dependencies, "Example")
end
# Basic add by git revision
isolate(loaded_depot=true) do
Pkg.add(name="Example", rev="master")
Pkg.dependencies(exuuid) do ex
@test !ex.is_tracking_registry
@test ex.git_source == "https://github.com/JuliaLang/Example.jl.git"
@test ex.git_revision == "master"
end
@test haskey(Pkg.project().dependencies, "Example")
end
# Adding stdlibs should work.
isolate(loaded_depot=true) do
profile_uuid = UUID("9abbd945-dff8-562f-b5e8-e1ebf5ef1b79")
# - Adding a stdlib by name.
Pkg.add("Markdown")
Pkg.dependencies(markdown_uuid) do pkg
@test pkg.name == "Markdown"
end
# - Adding a stdlib by UUID.
Pkg.add(uuid=profile_uuid)
Pkg.dependencies(profile_uuid) do pkg
@test pkg.name == "Profile"
end
# - Adding a stdlib by name/UUID.
Pkg.add(name="Markdown", uuid=markdown_uuid)
Pkg.dependencies(markdown_uuid) do pkg
@test pkg.name == "Markdown"
end
end
# Basic add by local path.
isolate(loaded_depot=true) do; mktempdir() do tempdir
path = git_init_package(tempdir, joinpath(@__DIR__, "test_packages", "SimplePackage"))
Pkg.add(path=path)
Pkg.dependencies(simple_package_uuid) do pkg
@test pkg.git_source == realpath(path)
# We take care to check that the project file has been parsed correctly.
@test pkg.name == "SimplePackage"
@test pkg.version == v"0.2.0"
@test haskey(pkg.dependencies, "Example")
@test haskey(pkg.dependencies, "Markdown")
end
@test haskey(Pkg.project().dependencies, "SimplePackage")
@test length(Pkg.project().dependencies) == 1
end end
# add when depot does not exist should create the default project in the correct location
isolate() do; mktempdir() do tempdir
empty!(DEPOT_PATH)
push!(DEPOT_PATH, tempdir)
Base.append_bundled_depot_path!(DEPOT_PATH)
rm(tempdir; force=true, recursive=true)
@test !isdir(first(DEPOT_PATH))
Pkg.add("JSON")
@test dirname(dirname(Pkg.project().path)) == realpath(joinpath(tempdir, "environments"))
end end
end
# Here we can use a loaded depot because we are only checking changes to the active project.
@testset "add: package state changes" begin
# Check that `add` on an already added stdlib works.
# Stdlibs are special cased throughout the codebase.
isolate(loaded_depot=true) do
Pkg.add("Markdown")
Pkg.add("Markdown")
Pkg.dependencies(markdown_uuid) do pkg
@test pkg.name == "Markdown"
end
@test haskey(Pkg.project().dependencies, "Markdown")
end
# Double add should not change state, this would be an unnecessary change.
isolate(loaded_depot=true) do
@test !haskey(Pkg.Types.Context().env.project.compat, "Example")
Pkg.add(name="Example", version="0.3.0")
@test Pkg.dependencies()[exuuid].version == v"0.3.0"
@test !haskey(Pkg.Types.Context().env.project.compat, "Example")
Pkg.add("Example")
@test Pkg.dependencies()[exuuid].version == v"0.3.0"
@test !haskey(Pkg.Types.Context().env.project.compat, "Example")
end
# Adding a new package should not alter the version of existing packages.
isolate(loaded_depot=true) do
Pkg.add(name="Example", version="0.3.0")
@test Pkg.dependencies()[exuuid].version == v"0.3.0"
Pkg.add("Test")
@test Pkg.dependencies()[exuuid].version == v"0.3.0"
end
# Add by version should not override pinned version.
isolate(loaded_depot=true) do
Pkg.add(name="Example", version="0.3.0")
Pkg.pin("Example")
Pkg.dependencies(exuuid) do ex
@test ex.version == v"0.3.0"
@test ex.is_tracking_registry
@test ex.is_pinned
end
Pkg.add(name="Example", version="0.5.0")
# We check that the package state is left unchanged.
Pkg.dependencies(exuuid) do ex
@test ex.version == v"0.3.0"
@test ex.is_tracking_registry
@test ex.is_pinned
end
end
# Add by version should override add by repo.
isolate(loaded_depot=true) do
Pkg.add(name="Example", rev="master")
# First we check that we are not tracking a registered version.
Pkg.dependencies(exuuid) do ex
@test ex.git_revision == "master"
@test !ex.is_tracking_registry
end
Pkg.add(name="Example", version="0.3.0")
# We should now be tracking a registered version.
Pkg.dependencies(exuuid) do ex
@test ex.version == v"0.3.0"
@test ex.git_revision === nothing
@test ex.is_tracking_registry
end
end
# Add by version should override add by repo, even for indirect dependencies.
isolate(loaded_depot=true) do; mktempdir() do tempdir
path = git_init_package(tempdir, joinpath(@__DIR__, "test_packages", "DependsOnExample"))
Pkg.add(path=path)
Pkg.add(name="Example", rev="master")
@test !Pkg.dependencies()[exuuid].is_tracking_registry
# Now we remove the package as a direct dependency.
# The package should still exist as an indirect dependency because `DependsOnExample` depends on it.
Pkg.rm("Example")
Pkg.add(name="Example", version="0.3.0")
# Now we check that we are tracking a registered version.
Pkg.dependencies(exuuid) do ex
@test ex.version == v"0.3.0"
@test ex.is_tracking_registry
end
end end
# Add by URL should not override pin.
isolate(loaded_depot=true) do
Pkg.add(name="Example", version="0.3.0")
Pkg.pin(name="Example")
Pkg.dependencies(exuuid) do ex
@test ex.is_pinned
@test ex.is_tracking_registry
@test ex.version == v"0.3.0"
end
Pkg.add(url="https://github.com/JuliaLang/Example.jl")
Pkg.dependencies(exuuid) do ex
@test ex.is_pinned
@test ex.is_tracking_registry
@test ex.version == v"0.3.0"
end
end
# It should be possible to switch branches by reusing the URL.
isolate(loaded_depot=true) do
Pkg.add(url="https://github.com/00vareladavid/Unregistered.jl", rev="0.2.0")
Pkg.dependencies(unregistered_uuid) do pkg
@test pkg.git_source == "https://github.com/00vareladavid/Unregistered.jl"
@test !pkg.is_tracking_registry
@test pkg.git_revision == "0.2.0"
# We check that we have the correct branch by checking its dependencies.
@test haskey(pkg.dependencies, "Example")
end
# Now we refer to it by name so to check that we reuse the URL.
Pkg.add(name="Unregistered", rev="0.1.0")
Pkg.dependencies(unregistered_uuid) do pkg
@test pkg.git_source == "https://github.com/00vareladavid/Unregistered.jl"
@test !pkg.is_tracking_registry
@test pkg.git_revision == "0.1.0"
# We check that we have the correct branch by checking its dependencies.
@test !haskey(pkg.dependencies, "Example")
end
end
# add should resolve the correct versions even when the manifest is out of sync with the project compat
isolate(loaded_depot=true) do; mktempdir() do tempdir
Pkg.activate(copy_test_package(tempdir, "CompatOutOfSync"))
Pkg.add("Libdl")
Pkg.dependencies(exuuid) do pkg
@test pkg.version == v"0.3.0"
end
end end
# Preserve syntax
# These tests mostly check the REPL side correctness.
# make sure the default behavior is invoked
withenv("JULIA_PKG_PRESERVE_TIERED_INSTALLED" => false) do
# - Normal add should not change the existing version.
isolate(loaded_depot=true) do
Pkg.add(name="libpng_jll", version=v"1.6.37+4")
@test Pkg.dependencies()[pngjll_uuid].version == v"1.6.37+4"
Pkg.add(name="Example", version="0.3.0")
@test Pkg.dependencies()[exuuid].version == v"0.3.0"
@test Pkg.dependencies()[pngjll_uuid].version == v"1.6.37+4"
Pkg.add(name="JSON", version="0.18.0")
@test Pkg.dependencies()[exuuid].version == v"0.3.0"
@test Pkg.dependencies()[json_uuid].version == v"0.18.0"
@test Pkg.dependencies()[pngjll_uuid].version == v"1.6.37+4"
end
# - `tiered_installed`.
isolate(loaded_depot=false) do
Pkg.add(name="libpng_jll", version=v"1.6.37+4")
Pkg.add(name="Example", version="0.3.0")
@test Pkg.dependencies()[exuuid].version == v"0.3.0"
@test Pkg.dependencies()[pngjll_uuid].version == v"1.6.37+4"
@test_logs(
(:debug, "tiered_resolve: trying PRESERVE_ALL_INSTALLED"),
(:debug, "tiered_resolve: trying PRESERVE_ALL"),
min_level=Logging.Debug,
match_mode=:any,
Pkg.add(Pkg.PackageSpec(;name="JSON", version="0.18.0"); preserve=Pkg.PRESERVE_TIERED_INSTALLED)
)
@test Pkg.dependencies()[exuuid].version == v"0.3.0"
@test Pkg.dependencies()[json_uuid].version == v"0.18.0"
@test Pkg.dependencies()[pngjll_uuid].version == v"1.6.37+4"
Pkg.activate(temp=true)
@test_logs(
(:debug, "tiered_resolve: trying PRESERVE_ALL_INSTALLED"),
min_level=Logging.Debug,
match_mode=:any,
Pkg.add("Example"; preserve=Pkg.PRESERVE_TIERED_INSTALLED) # should only add v0.3.0 as it was installed earlier
)
@test Pkg.dependencies()[exuuid].version == v"0.3.0"
withenv("JULIA_PKG_PRESERVE_TIERED_INSTALLED" => true) do
Pkg.activate(temp=true)
@test_logs(
(:debug, "tiered_resolve: trying PRESERVE_ALL_INSTALLED"),
min_level=Logging.Debug,
match_mode=:any,
Pkg.add(name="Example")
)
@test Pkg.dependencies()[exuuid].version == v"0.3.0"
end
Pkg.activate(temp=true)
@test_logs(
(:debug, "tiered_resolve: trying PRESERVE_ALL"),
min_level=Logging.Debug,
match_mode=:any,
Pkg.add(name="Example") # default 'add' should serve a newer version
)
@test Pkg.dependencies()[exuuid].version > v"0.3.0"
end
# - `tiered` is the default option.
isolate(loaded_depot=false) do
Pkg.add(name="libpng_jll", version=v"1.6.37+4")
Pkg.add(name="Example", version="0.3.0")
@test Pkg.dependencies()[exuuid].version == v"0.3.0"
@test Pkg.dependencies()[pngjll_uuid].version == v"1.6.37+4"
Pkg.add(Pkg.PackageSpec(;name="JSON", version="0.18.0"); preserve=Pkg.PRESERVE_TIERED)
@test Pkg.dependencies()[exuuid].version == v"0.3.0"
@test Pkg.dependencies()[json_uuid].version == v"0.18.0"
@test Pkg.dependencies()[pngjll_uuid].version == v"1.6.37+4"
end
# - `installed`.
isolate(loaded_depot=false) do
Pkg.add(name="libpng_jll", version=v"1.6.37+4")
Pkg.add(name="Example", version="0.3.0")
@test Pkg.dependencies()[exuuid].version == v"0.3.0"
@test Pkg.dependencies()[pngjll_uuid].version == v"1.6.37+4"
@test_throws Pkg.Resolve.ResolverError Pkg.add(Pkg.PackageSpec(;name="JSON", version="0.18.0"); preserve=Pkg.PRESERVE_ALL_INSTALLED) # no installed version
end
# - `all` should succeed in the same way as `tiered`.
isolate(loaded_depot=false) do
Pkg.add(name="libpng_jll", version=v"1.6.37+4")
Pkg.add(name="Example", version="0.3.0")
@test Pkg.dependencies()[exuuid].version == v"0.3.0"
@test Pkg.dependencies()[pngjll_uuid].version == v"1.6.37+4"
Pkg.add(Pkg.PackageSpec(;name="JSON", version="0.18.0"); preserve=Pkg.PRESERVE_ALL)
@test Pkg.dependencies()[exuuid].version == v"0.3.0"
@test Pkg.dependencies()[json_uuid].version == v"0.18.0"
@test Pkg.dependencies()[pngjll_uuid].version == v"1.6.37+4"
Pkg.rm("JSON")
Pkg.add(Pkg.PackageSpec(;name="JSON"); preserve=Pkg.PRESERVE_ALL_INSTALLED)
@test Pkg.dependencies()[exuuid].version == v"0.3.0"
@test Pkg.dependencies()[json_uuid].version == v"0.18.0"
@test Pkg.dependencies()[pngjll_uuid].version == v"1.6.37+4"
end
# - `direct` should also succeed in the same way.
isolate(loaded_depot=true) do
Pkg.add(name="libpng_jll", version=v"1.6.37+4")
Pkg.add(name="Example", version="0.3.0")
@test Pkg.dependencies()[exuuid].version == v"0.3.0"
@test Pkg.dependencies()[pngjll_uuid].version == v"1.6.37+4"
Pkg.add(Pkg.PackageSpec(;name="JSON", version="0.18.0"); preserve=Pkg.PRESERVE_DIRECT)
@test Pkg.dependencies()[exuuid].version == v"0.3.0"
@test Pkg.dependencies()[json_uuid].version == v"0.18.0"
@test Pkg.dependencies()[pngjll_uuid].version == v"1.6.37+4"
end
# - `semver` should update `Example` and the jll to the highest semver compatible version.
isolate(loaded_depot=true) do
Pkg.add(name="libpng_jll", version=v"1.6.37+4")
Pkg.add(name="Example", version="0.3.0")
@test Pkg.dependencies()[exuuid].version == v"0.3.0"
@test Pkg.dependencies()[pngjll_uuid].version == v"1.6.37+4"
Pkg.add(Pkg.PackageSpec(;name="JSON", version="0.18.0"); preserve=Pkg.PRESERVE_SEMVER)
@test Pkg.dependencies()[exuuid].version == v"0.3.3"
@test Pkg.dependencies()[json_uuid].version == v"0.18.0"
@test Pkg.dependencies()[pngjll_uuid].version > v"1.6.37+4"
end
#- `none` should update `Example` and the jll to the highest compatible version.
isolate(loaded_depot=true) do
Pkg.add(name="libpng_jll", version=v"1.6.37+4")
Pkg.add(name="Example", version="0.3.0")
@test Pkg.dependencies()[exuuid].version == v"0.3.0"
@test Pkg.dependencies()[pngjll_uuid].version == v"1.6.37+4"
Pkg.add(Pkg.PackageSpec(;name="JSON", version="0.18.0"); preserve=Pkg.PRESERVE_NONE)
@test Pkg.dependencies()[exuuid].version > v"0.3.0"
@test Pkg.dependencies()[json_uuid].version == v"0.18.0"
@test Pkg.dependencies()[pngjll_uuid].version > v"1.6.37+4"
end
isolate(loaded_depot=true) do
Pkg.add(name="libpng_jll", version=v"1.6.37+5")
@test Pkg.dependencies()[pngjll_uuid].version == v"1.6.37+5"
end
# Adding a new package to a package should add compat entries
isolate(loaded_depot=true) do
mktempdir() do tempdir
Pkg.activate(tempdir)
mkpath(joinpath(tempdir, "src"))
touch(joinpath(tempdir, "src", "Foo.jl"))
ctx = Pkg.Types.Context()
ctx.env.project.name = "Foo"
ctx.env.project.uuid = UUIDs.UUID(0)
Pkg.Types.write_project(ctx.env)
Pkg.add(name="Example", version="0.3.0")
@test Pkg.dependencies()[exuuid].version == v"0.3.0"
@test Pkg.Types.Context().env.project.compat["Example"] == Pkg.Types.Compat(Pkg.Types.VersionSpec("0.3"), "0.3.0")
Pkg.add(name="Example", version="0.3.1")
@test Pkg.Types.Context().env.project.compat["Example"] == Pkg.Types.Compat(Pkg.Types.VersionSpec("0.3"), "0.3.0")
end
end
end # withenv
end
#
# ## Repo Handling
#
@testset "add: repo handling" begin
# Dependencies added with an absolute path should be stored as absolute paths.
# This tests shows that, packages added with an absolute path will not break
# if the project is moved to a new position.
# We can use the loaded depot here, it will help us avoid the original clone.
isolate(loaded_depot=true) do; mktempdir() do tempdir
empty_package = UUID("26187899-7657-4a90-a2f6-e79e0214bedc")
path = git_init_package(tempdir, joinpath(@__DIR__, "test_packages", "EmptyPackage"))
path = abspath(path)
Pkg.add(path=path)
# Now we try to find the package.
rm(joinpath(DEPOT_PATH[1], "packages"); recursive=true)
@test !isdir(Pkg.dependencies()[empty_package].source)
Pkg.instantiate()
@test isdir(Pkg.dependencies()[empty_package].source)
# Now we move the project and should still be able to find the package.
mktempdir() do other_dir
cp(dirname(Base.active_project()), other_dir; force=true)
Pkg.activate(other_dir)
rm(joinpath(DEPOT_PATH[1], "packages"); recursive=true)
@test !isdir(Pkg.dependencies()[empty_package].source)
Pkg.instantiate()
end
end end
# Dependencies added with relative paths should be stored relative to the active project.
# This test shows that packages added with a relative path will not break
# as long as they maintain the same relative position to the project.
# We can use the loaded depot here, it will help us avoid the original clone.
isolate(loaded_depot=true) do; mktempdir() do tempdir
empty_package = UUID("26187899-7657-4a90-a2f6-e79e0214bedc")
path = git_init_package(tempdir, joinpath(@__DIR__, "test_packages", "EmptyPackage"))
# We add the package using a relative path.
cd(path) do
Pkg.add(path=".")
manifest = Pkg.Types.read_manifest(joinpath(dirname(Base.active_project()), "Manifest.toml"))
# Test that the relative path is canonicalized.
repo = string("../../../", basename(tempdir), "/EmptyPackage")
@test manifest[empty_package].repo.source == repo
end
# Now we try to find the package.
rm(joinpath(DEPOT_PATH[1], "packages"); recursive=true)
rm(joinpath(DEPOT_PATH[1], "clones"); recursive=true)
Pkg.instantiate()
# Test that Operations.is_instantiated works with relative path
@test Pkg.Operations.is_instantiated(Pkg.Types.EnvCache())
# Now we destroy the relative position and should not be able to find the package.
rm(joinpath(DEPOT_PATH[1], "packages"); recursive=true)
# Test that Operations.is_instantiated works with relative path
@test !Pkg.Operations.is_instantiated(Pkg.Types.EnvCache())
mktempdir() do other_dir
cp(dirname(Base.active_project()), other_dir; force=true)
Pkg.activate(other_dir)
@test_throws PkgError Pkg.instantiate() # TODO is there a way to pattern match on just part of the err message?
end
end end
# Now we test packages added by URL.
isolate(loaded_depot=true) do
# Details: `master` is past `0.1.0`
Pkg.add(url="https://github.com/00vareladavid/Unregistered.jl", rev="0.1.0")
Pkg.dependencies(unregistered_uuid) do pkg
@test pkg.name == "Unregistered"
@test isdir(pkg.source)
end
@test haskey(Pkg.project().dependencies, "Unregistered")
# Now we remove the source so that we have to load it again.
# We should reuse the existing clone in this case.
rm(joinpath(DEPOT_PATH[1], "packages"); recursive=true)
Pkg.instantiate()
Pkg.dependencies(unregistered_uuid) do pkg
@test pkg.name == "Unregistered"
@test isdir(pkg.source)
end
@test haskey(Pkg.project().dependencies, "Unregistered")
# Now we remove the source _and_ our cache, we have no choice to re-clone the remote.
# We should still be able to find the source.
rm(joinpath(DEPOT_PATH[1], "packages"); recursive=true)
rm(joinpath(DEPOT_PATH[1], "clones"); recursive=true)
Pkg.instantiate()
Pkg.dependencies(unregistered_uuid) do pkg
@test pkg.name == "Unregistered"
@test isdir(pkg.source)
end
@test haskey(Pkg.project().dependencies, "Unregistered")
end
end
#
# ## Resolve tiers
#
@testset "add: resolve tiers" begin
# The MetaGraphs version tested below relied on a JLD2 version
# that couldn't actually be loaded on julia 1.9+ so General
# will be patched. This checks out a commit before then to maintain
# these tests.
registry_url = "https://github.com/JuliaRegistries/General.git"
registry_commit = "030d6dae0df2ad6c3b2f90d41749df3eedb8d1b1"
Utils.isolate_and_pin_registry(; registry_url, registry_commit) do; mktempdir() do tmp
# All
copy_test_package(tmp, "ShouldPreserveAll"; use_pkg=false)
Pkg.activate(joinpath(tmp, "ShouldPreserveAll"))
parsers_uuid = UUID("69de0a69-1ddd-5017-9359-2bf0b02dc9f0")
original_parsers_version = Pkg.dependencies()[parsers_uuid].version
Pkg.add(name="Example", version="0.5.0")
@test Pkg.dependencies()[parsers_uuid].version == original_parsers_version
# Direct
copy_test_package(tmp, "ShouldPreserveDirect"; use_pkg=false)
Pkg.activate(joinpath(tmp, "ShouldPreserveDirect"))
ordered_collections = UUID("bac558e1-5e72-5ebc-8fee-abe8a469f55d")
Pkg.add(uuid=ordered_collections, version="1.0.1")
lazy_json = UUID("fc18253b-5e1b-504c-a4a2-9ece4944c004")
data_structures = UUID("864edb3b-99cc-5e75-8d2d-829cb0a9cfe8")
@test Pkg.dependencies()[lazy_json].version == v"0.1.0" # stayed the same
@test Pkg.dependencies()[data_structures].version == v"0.16.1" # forced to change
@test Pkg.dependencies()[ordered_collections].version == v"1.0.1" # sanity check
# SEMVER
copy_test_package(tmp, "ShouldPreserveSemver"; use_pkg=false)
Pkg.activate(joinpath(tmp, "ShouldPreserveSemver"))
light_graphs = UUID("093fc24a-ae57-5d10-9952-331d41423f4d")
meta_graphs = UUID("626554b9-1ddb-594c-aa3c-2596fe9399a5")
light_graphs_version = Pkg.dependencies()[light_graphs].version
Pkg.add(uuid=meta_graphs, version="0.6.4")
@test Pkg.dependencies()[meta_graphs].version == v"0.6.4" # sanity check
# did not break semver
@test Pkg.dependencies()[light_graphs].version in Pkg.Types.semver_spec("$(light_graphs_version)")
# did change version
@test Pkg.dependencies()[light_graphs].version != light_graphs_version
# NONE
copy_test_package(tmp, "ShouldPreserveNone"; use_pkg=false)
Pkg.activate(joinpath(tmp, "ShouldPreserveNone"))
array_interface = UUID("4fba245c-0d91-5ea0-9b3e-6abc04ee57a9")
diff_eq_diff_tools = UUID("01453d9d-ee7c-5054-8395-0335cb756afa")
Pkg.add(uuid=diff_eq_diff_tools, version="1.0.0")
@test Pkg.dependencies()[diff_eq_diff_tools].version == v"1.0.0" # sanity check
@test Pkg.dependencies()[array_interface].version in Pkg.Types.semver_spec("1") # had to make breaking change
end end
end
#
# ## REPL
#
@testset "add: REPL" begin
isolate() do
Pkg.REPLMode.TEST_MODE[] = true
# Add using UUID syntax
api, args, opts = first(Pkg.pkg"add 7876af07-990d-54b4-ab0e-23690620f79a")
@test api == Pkg.add
@test args == [Pkg.PackageSpec(;uuid=UUID("7876af07-990d-54b4-ab0e-23690620f79a"))]
@test isempty(opts)
# Add using `name=UUID` syntax.
api, args, opts = first(Pkg.pkg"add Example=7876af07-990d-54b4-ab0e-23690620f79a")