forked from conda/conda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_create.py
2732 lines (2336 loc) · 95.7 KB
/
test_create.py
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
# Copyright (C) 2012 Anaconda, Inc
# SPDX-License-Identifier: BSD-3-Clause
import json
import os
import re
import sys
from datetime import datetime
from glob import glob
from importlib.metadata import version as metadata_version
from itertools import zip_longest
from json import loads as json_loads
from logging import getLogger
from os.path import (
basename,
exists,
isdir,
isfile,
join,
)
from pathlib import Path
from shutil import copyfile, rmtree
from subprocess import PIPE, Popen, check_call, check_output
from textwrap import dedent
from typing import Literal
from unittest.mock import patch
from uuid import uuid4
import menuinst
import pytest
from pytest import CaptureFixture, FixtureRequest, MonkeyPatch
from pytest_mock import MockerFixture
from conda import CondaError, CondaMultiError
from conda.auxlib.ish import dals
from conda.base.constants import (
CONDA_PACKAGE_EXTENSIONS,
PREFIX_MAGIC_FILE,
ChannelPriority,
SafetyChecks,
)
from conda.base.context import conda_tests_ctxt_mgmt_def_pol, context, reset_context
from conda.common.compat import ensure_text_type, on_linux, on_mac, on_win
from conda.common.io import env_var, env_vars, stderr_log_level
from conda.common.iterators import groupby_to_dict as groupby
from conda.common.path import (
get_bin_directory_short_path,
get_python_site_packages_short_path,
pyc_path,
)
from conda.common.serialize import json_dump, yaml_round_trip_load
from conda.core.index import get_reduced_index
from conda.core.package_cache_data import PackageCacheData
from conda.core.prefix_data import PrefixData, get_python_version_for_prefix
from conda.core.subdir_data import create_cache_dir
from conda.exceptions import (
ArgumentError,
CondaValueError,
DirectoryNotACondaEnvironmentError,
DisallowedPackageError,
DryRunExit,
EnvironmentNotWritableError,
LinkError,
OperationNotAllowed,
PackageNotInstalledError,
PackagesNotFoundError,
RemoveError,
SpecsConfigurationConflictError,
UnsatisfiableError,
)
from conda.gateways.disk.create import compile_multiple_pyc
from conda.gateways.disk.delete import path_is_clean, rm_rf
from conda.gateways.disk.permissions import make_read_only
from conda.gateways.subprocess import (
Response,
subprocess_call,
subprocess_call_with_clean_env,
)
from conda.models.channel import Channel
from conda.models.match_spec import MatchSpec
from conda.models.version import VersionOrder
from conda.resolve import Resolve
from conda.testing import CondaCLIFixture, PathFactoryFixture, TmpEnvFixture
from conda.testing.integration import (
BIN_DIRECTORY,
PYTHON_BINARY,
TEST_LOG_LEVEL,
Commands,
env_or_set,
get_shortcut_dir,
make_temp_channel,
make_temp_env,
make_temp_prefix,
package_is_installed,
run_command,
tempdir,
which_or_where,
)
log = getLogger(__name__)
stderr_log_level(TEST_LOG_LEVEL, "conda")
stderr_log_level(TEST_LOG_LEVEL, "requests")
# all tests in this file are integration tests
pytestmark = [
pytest.mark.integration,
pytest.mark.usefixtures("parametrized_solver_fixture"),
]
@pytest.fixture(autouse=True)
def clear_package_cache() -> None:
PackageCacheData.clear()
def test_install_python_and_search(
path_factory: PathFactoryFixture,
mocker: MockerFixture,
monkeypatch: MonkeyPatch,
tmp_env: TmpEnvFixture,
conda_cli: CondaCLIFixture,
):
environment_txt = path_factory(suffix=".txt")
environment_txt.touch()
mocker.patch(
"conda.core.envs_manager.get_user_environments_txt_file",
return_value=environment_txt,
)
monkeypatch.setenv("CONDA_REGISTER_ENVS", "true")
# regression test for #4513
monkeypatch.setenv("CONDA_ALLOW_NON_CHANNEL_URLS", "true")
channels = (
"https://repo.continuum.io/pkgs/not-a-channel",
"defaults",
"conda-forge",
)
monkeypatch.setenv("CONDA_CHANNELS", ",".join(channels))
reset_context()
assert context.register_envs
assert context.allow_non_channel_urls
assert context.channels == channels
with tmp_env("python") as prefix:
assert (prefix / PYTHON_BINARY).exists()
assert package_is_installed(prefix, "python")
stdout, stderr, err = conda_cli("search", "python", "--json")
assert len(json.loads(stdout)) == 1
assert not stderr
assert not err
stdout, stderr, err = conda_cli("search", "python", "--json", "--envs")
assert any(prefix.samefile(env["location"]) for env in json.loads(stdout))
assert not stderr
assert not err
stdout, stderr, err = conda_cli("search", "python", "--envs")
assert str(prefix) in stdout
assert not stderr
assert not err
def test_run_preserves_arguments(tmp_env: TmpEnvFixture, conda_cli: CondaCLIFixture):
with tmp_env("python=3") as prefix:
echo_args_py = prefix / "echo-args.py"
echo_args_py.write_text("import sys\nfor arg in sys.argv[1:]: print(arg)")
# If 'two two' were 'two' this test would pass.
args = ("one", "two two", "three")
stdout, stderr, code = conda_cli(
"run",
f"--prefix={prefix}",
"python",
echo_args_py,
*args,
)
for value, expected in zip_longest(stdout.strip().splitlines(), args):
assert value == expected
assert not stderr
assert not code
def test_create_install_update_remove_smoketest(
tmp_env: TmpEnvFixture,
conda_cli: CondaCLIFixture,
):
with tmp_env("python=3") as prefix:
assert (prefix / PYTHON_BINARY).exists()
assert package_is_installed(prefix, "python=3")
conda_cli("install", f"--prefix={prefix}", "flask=2.0.1", "--yes")
PrefixData._cache_.clear()
assert package_is_installed(prefix, "flask=2.0.1")
assert package_is_installed(prefix, "python=3")
conda_cli(
"install",
f"--prefix={prefix}",
"--force-reinstall",
"flask=2.0.1",
"--yes",
)
PrefixData._cache_.clear()
assert package_is_installed(prefix, "flask=2.0.1")
assert package_is_installed(prefix, "python=3")
conda_cli("update", f"--prefix={prefix}", "flask", "--yes")
PrefixData._cache_.clear()
assert not package_is_installed(prefix, "flask=2.0.1")
assert package_is_installed(prefix, "flask")
assert package_is_installed(prefix, "python=3")
conda_cli("remove", f"--prefix={prefix}", "flask", "--yes")
PrefixData._cache_.clear()
assert not package_is_installed(prefix, "flask")
assert package_is_installed(prefix, "python=3")
stdout, stderr, code = conda_cli("list", f"--prefix={prefix}", "--revisions")
assert not stderr
assert " (rev 4)\n" in stdout
assert " (rev 5)\n" not in stdout
conda_cli("install", f"--prefix={prefix}", "--revision", "0", "--yes")
PrefixData._cache_.clear()
assert not package_is_installed(prefix, "flask")
assert package_is_installed(prefix, "python=3")
def test_install_broken_post_install_keeps_existing_folders(
test_recipes_channel: Path,
tmp_env: TmpEnvFixture,
conda_cli: CondaCLIFixture,
):
# regression test for #8258
with tmp_env("small-executable") as prefix:
assert (prefix / BIN_DIRECTORY).exists()
assert package_is_installed(prefix, "small-executable")
_, _, exc = conda_cli(
"install",
f"--prefix={prefix}",
"failing_post_link",
"--yes",
raises=CondaMultiError,
)
# CondaMultiError contains a non-Exception, why?
# see, e.g., insertion of axngroup into CondaMultiError in
# https://github.com/conda/conda/commit/c765d6a48151710040539bb82c51fce4c87ba81e
# assert len(exc.value.errors) == 1
assert isinstance(exc.value.errors[0], LinkError)
assert exc.match("post-link script failed")
assert (prefix / BIN_DIRECTORY).exists()
assert package_is_installed(prefix, "small-executable")
def test_safety_checks_enabled(
tmp_env: TmpEnvFixture,
monkeypatch: MonkeyPatch,
conda_cli: CondaCLIFixture,
):
with tmp_env() as prefix:
monkeypatch.setenv("CONDA_SAFETY_CHECKS", "enabled")
monkeypatch.setenv("CONDA_EXTRA_SAFETY_CHECKS", "true")
reset_context()
assert context.safety_checks is SafetyChecks.enabled
assert context.extra_safety_checks
with pytest.raises(CondaMultiError) as exc:
conda_cli(
"install",
f"--prefix={prefix}",
"--channel=conda-test",
"spiffy-test-app=0.5",
"--yes",
)
# conda-test::spiffy-test-app=0.5 is a modified version of conda-test::spiffy-test-app=1.0
assert dals(
"""
The path 'site-packages/spiffy_test_app-1.0-py2.7.egg-info/top_level.txt'
has an incorrect size.
reported size: 32 bytes
actual size: 16 bytes
"""
) in str(exc.value)
assert "has a sha256 mismatch." in str(exc.value)
assert not package_is_installed(prefix, "spiffy-test-app=0.5")
def test_safety_checks_warn(
tmp_env: TmpEnvFixture,
monkeypatch: MonkeyPatch,
conda_cli: CondaCLIFixture,
):
with tmp_env() as prefix:
monkeypatch.setenv("CONDA_SAFETY_CHECKS", "warn")
monkeypatch.setenv("CONDA_EXTRA_SAFETY_CHECKS", "true")
reset_context()
assert context.safety_checks is SafetyChecks.warn
assert context.extra_safety_checks
stdout, stderr, code = conda_cli(
"install",
f"--prefix={prefix}",
"--channel=conda-test",
"spiffy-test-app=0.5",
"--yes",
)
assert stdout
# conda-test::spiffy-test-app=0.5 is a modified version of conda-test::spiffy-test-app=1.0
assert (
dals(
"""
The path 'site-packages/spiffy_test_app-1.0-py2.7.egg-info/top_level.txt'
has an incorrect size.
reported size: 32 bytes
actual size: 16 bytes
"""
)
in stderr
)
assert "has a sha256 mismatch." in stderr
assert not code
assert package_is_installed(prefix, "spiffy-test-app=0.5")
def test_safety_checks_disabled(
tmp_env: TmpEnvFixture,
monkeypatch: MonkeyPatch,
conda_cli: CondaCLIFixture,
):
with tmp_env() as prefix:
monkeypatch.setenv("CONDA_SAFETY_CHECKS", "disabled")
reset_context()
assert context.safety_checks is SafetyChecks.disabled
assert not context.extra_safety_checks
stdout, stderr, code = conda_cli(
"install",
f"--prefix={prefix}",
"--channel=conda-test",
"spiffy-test-app=0.5",
"--yes",
)
assert stdout
# conda-test::spiffy-test-app=0.5 is a modified version of conda-test::spiffy-test-app=1.0
assert (
dals(
"""
The path 'site-packages/spiffy_test_app-1.0-py2.7.egg-info/top_level.txt'
has an incorrect size.
reported size: 32 bytes
actual size: 16 bytes
"""
)
not in stderr
)
assert "has a sha256 mismatch." not in stderr
assert not code
assert package_is_installed(prefix, "spiffy-test-app=0.5")
def test_json_create_install_update_remove(
path_factory: PathFactoryFixture,
conda_cli: CondaCLIFixture,
capsys: CaptureFixture,
):
# regression test for #5384
def assert_json_parsable(content):
string = None
try:
for string in content and content.split("\0") or ():
json.loads(string)
except Exception as e:
log.warn(
"Problem parsing json output.\n"
" content: %s\n"
" string: %s\n"
" error: %r",
content,
string,
e,
)
raise
prefix = path_factory()
with pytest.raises(DryRunExit):
conda_cli(
"create",
f"--prefix={prefix}",
"zlib",
"--json",
"--dry-run",
)
stdout, stderr = capsys.readouterr()
assert_json_parsable(stdout)
# regression test for #5825
# contents of LINK and UNLINK is expected to have dist format
json_obj = json.loads(stdout)
dist_dump = json_obj["actions"]["LINK"][0]
assert "dist_name" in dist_dump
stdout, stderr, _ = conda_cli(
"create",
f"--prefix={prefix}",
"zlib",
"--json",
"--yes",
)
assert_json_parsable(stdout)
assert not stderr
json_obj = json.loads(stdout)
dist_dump = json_obj["actions"]["LINK"][0]
assert "dist_name" in dist_dump
stdout, stderr, _ = conda_cli(
"install",
f"--prefix={prefix}",
"ca-certificates<2023",
"--json",
"--yes",
)
assert_json_parsable(stdout)
assert not stderr
assert package_is_installed(prefix, "ca-certificates<2023")
assert package_is_installed(prefix, "zlib")
# Test force reinstall
stdout, stderr, _ = conda_cli(
"install",
f"--prefix={prefix}",
"--force-reinstall",
"ca-certificates<2023",
"--json",
"--yes",
)
assert_json_parsable(stdout)
assert not stderr
assert package_is_installed(prefix, "ca-certificates<2023")
assert package_is_installed(prefix, "zlib")
stdout, stderr, _ = conda_cli(
"update",
f"--prefix={prefix}",
"ca-certificates",
"--json",
"--yes",
)
assert_json_parsable(stdout)
assert not stderr
assert package_is_installed(prefix, "ca-certificates>=2023")
assert package_is_installed(prefix, "zlib")
stdout, stderr, _ = conda_cli(
"remove",
f"--prefix={prefix}",
"ca-certificates",
"--json",
"--yes",
)
assert_json_parsable(stdout)
assert not stderr
assert not package_is_installed(prefix, "ca-certificates")
assert package_is_installed(prefix, "zlib")
# regression test for #5825
# contents of LINK and UNLINK is expected to have Dist format
json_obj = json.loads(stdout)
dist_dump = json_obj["actions"]["UNLINK"][0]
assert "dist_name" in dist_dump
stdout, stderr, _ = conda_cli("list", f"--prefix={prefix}", "--revisions", "--json")
assert not stderr
json_obj = json.loads(stdout)
assert len(json_obj) == 5
assert json_obj[4]["rev"] == 4
stdout, stderr, _ = conda_cli(
"install",
f"--prefix={prefix}",
"--revision=0",
"--json",
"--yes",
)
assert_json_parsable(stdout)
assert not stderr
assert not package_is_installed(prefix, "ca-certificates")
assert package_is_installed(prefix, "zlib")
def test_not_writable_env_raises_EnvironmentNotWritableError(
tmp_env: TmpEnvFixture, conda_cli: CondaCLIFixture
):
"""
Make sure that an ``EnvironmentNotWritableError`` is raised when the ``PREFIX_MAGIC_FILE`` is
not writable. This magic file is used to determined whether it's possible to write to an
environment.
"""
with tmp_env() as prefix:
make_read_only(prefix / PREFIX_MAGIC_FILE)
_, _, exc = conda_cli(
"install",
f"--prefix={prefix}",
"ca-certificates",
"--yes",
raises=CondaMultiError,
)
assert len(exc.value.errors) == 1
assert isinstance(exc.value.errors[0], EnvironmentNotWritableError)
def test_conda_update_package_not_installed(
tmp_env: TmpEnvFixture, conda_cli: CondaCLIFixture
):
"""
Runs the update command twice with invalid input:
1. Package is not currently installed (package should not exist)
2. Invalid specification for a packaage
"""
with tmp_env() as prefix:
conda_cli(
"update",
f"--prefix={prefix}",
"test-test-test",
raises=PackageNotInstalledError,
)
with pytest.raises(CondaError, match="Invalid spec for 'conda update'"):
conda_cli("update", f"--prefix={prefix}", "conda-forge::*")
def test_noarch_python_package_with_entry_points(
tmp_env: TmpEnvFixture, conda_cli: CondaCLIFixture
):
"""
Makes sure that entry point file is installed.
This test uses "pygments" as a Python package because it has no other dependencies and has an
entry point script, "pygmentize".
"""
with tmp_env("pygments") as prefix:
py_ver = get_python_version_for_prefix(prefix)
sp_dir = get_python_site_packages_short_path(py_ver)
py_file = sp_dir + "/pygments/__init__.py"
pyc_file = pyc_path(py_file, py_ver)
assert (prefix / py_file).is_file()
assert (prefix / pyc_file).is_file()
exe_path = (
prefix
/ get_bin_directory_short_path()
/ ("pygmentize.exe" if on_win else "pygmentize")
)
assert exe_path.is_file()
output = check_output([exe_path, "--help"], text=True)
assert "usage: pygmentize" in output
conda_cli("remove", f"--prefix={prefix}", "pygments", "--yes")
assert not (prefix / py_file).is_file()
assert not (prefix / pyc_file).is_file()
assert not exe_path.is_file()
def test_noarch_python_package_without_entry_points(
tmp_env: TmpEnvFixture, conda_cli: CondaCLIFixture
):
"""
Regression test for issue:
- https://github.com/conda/conda/issues/4546
This test uses "itsdangerous" as a dependency because it is a relatively small package and
has no entry point scripts.
"""
with tmp_env("itsdangerous") as prefix:
py_ver = get_python_version_for_prefix(prefix)
sp_dir = get_python_site_packages_short_path(py_ver)
py_file = sp_dir + "/itsdangerous/__init__.py"
pyc_file = pyc_path(py_file, py_ver)
assert (prefix / py_file).is_file()
assert (prefix / pyc_file).is_file()
conda_cli("remove", f"--prefix={prefix}", "itsdangerous", "--yes")
assert not (prefix / py_file).is_file()
assert not (prefix / pyc_file).is_file()
def test_noarch_python_package_reinstall_on_pyver_change(
tmp_env: TmpEnvFixture, conda_cli: CondaCLIFixture
):
"""
When Python changes versions (e.g. from 3.10 to 3.11) it is important to verify that all the previous
dependencies were transferred over to the new version in ``lib/python3.x/site-packages/*``.
"""
with tmp_env("itsdangerous", "python=3.10") as prefix:
py_ver = get_python_version_for_prefix(prefix)
assert py_ver.startswith("3.10")
sp_dir = get_python_site_packages_short_path(py_ver)
py_file = sp_dir + "/itsdangerous/__init__.py"
pyc_file_py310 = pyc_path(py_file, py_ver)
assert (prefix / py_file).is_file()
assert (prefix / pyc_file_py310).is_file()
conda_cli("install", f"--prefix={prefix}", "python=3.11", "--yes")
# python 3.10 pyc file should be gone
assert not (prefix / pyc_file_py310).is_file()
py_ver = get_python_version_for_prefix(prefix)
assert py_ver.startswith("3.11")
sp_dir = get_python_site_packages_short_path(py_ver)
py_file = sp_dir + "/itsdangerous/__init__.py"
pyc_file_py311 = pyc_path(py_file, py_ver)
assert (prefix / py_file).is_file()
assert (prefix / pyc_file_py311).is_file()
def test_noarch_generic_package(test_recipes_channel: Path, tmp_env: TmpEnvFixture):
with tmp_env("font-ttf-inconsolata") as prefix:
assert (prefix / "fonts" / "Inconsolata-Regular.ttf").is_file()
def test_override_channels(
monkeypatch: MonkeyPatch,
conda_cli: CondaCLIFixture,
path_factory: PathFactoryFixture,
):
monkeypatch.setenv("CONDA_OVERRIDE_CHANNELS_ENABLED", "no")
reset_context()
assert not context.override_channels_enabled
conda_cli(
"create",
f"--prefix={path_factory()}",
"--override-channels",
"python",
"--yes",
raises=OperationNotAllowed,
)
monkeypatch.setenv("CONDA_OVERRIDE_CHANNELS_ENABLED", "yes")
reset_context()
assert context.override_channels_enabled
conda_cli(
"create",
f"--prefix={path_factory()}",
"--override-channels",
"python",
"--yes",
raises=ArgumentError,
)
stdout, stderr, code = conda_cli(
"search",
"--override-channels",
"conda-test::flask",
"--json",
)
assert not stderr
assert len(json.loads(stdout)["flask"]) < 3
assert json.loads(stdout)["flask"][0]["noarch"] == "python"
assert not code
def test_create_empty_env(tmp_env: TmpEnvFixture, conda_cli: CondaCLIFixture):
with tmp_env() as prefix:
assert (prefix / "conda-meta" / "history").exists()
stdout, stderr, code = conda_cli("list", f"--prefix={prefix}")
assert stdout == dals(
f"""
# packages in environment at {prefix}:
#
# Name Version Build Channel
"""
)
assert not stderr
assert not code
stdout, stderr, code = conda_cli(
"list",
f"--prefix={prefix}",
"--revisions",
"--json",
)
revisions = json.loads(stdout)
assert len(revisions) == 1
assert datetime.fromisoformat(revisions[0]["date"])
assert revisions[0]["downgrade"] == []
assert revisions[0]["install"] == []
assert revisions[0]["remove"] == []
assert revisions[0]["rev"] == 0
assert revisions[0]["upgrade"] == []
assert not stderr
assert not code
@pytest.mark.skipif(reason="conda-forge doesn't have a full set of packages")
def test_strict_channel_priority(
conda_cli: CondaCLIFixture, path_factory: PathFactoryFixture
):
prefix = path_factory()
stdout, stderr, code = conda_cli(
"create",
f"--prefix={prefix}",
"--channel=conda-forge",
"--channel=defaults",
"python=3.6",
"quaternion",
"--strict-channel-priority",
"--dry-run",
"--json",
"--yes",
)
assert not code
json_obj = json_loads(stdout)
# We see:
# libcxx pkgs/main/osx-64::libcxx-4.0.1-h579ed51_0
# Rather than spending more time looking for another package, just filter it out.
# Same thing for Windows, this is because we use MKL always. Perhaps there's a
# way to exclude it, I tried the "nomkl" package but that did not work.
json_obj["actions"]["LINK"] = [
link
for link in json_obj["actions"]["LINK"]
if link["name"] not in ("libcxx", "libcxxabi", "mkl", "intel-openmp")
]
channel_groups = set(groupby(lambda x: x["channel"], json_obj["actions"]["LINK"]))
assert channel_groups == {"conda-forge"}
def test_strict_resolve_get_reduced_index(monkeypatch: MonkeyPatch):
channels = (Channel("defaults"),)
specs = (MatchSpec("anaconda"),)
index = get_reduced_index(None, channels, context.subdirs, specs, "repodata.json")
r = Resolve(index, channels=channels)
monkeypatch.setenv("CONDA_CHANNEL_PRIORITY", "strict")
reset_context()
assert context.channel_priority == ChannelPriority.STRICT
reduced_index = r.get_reduced_index(specs)
channel_name_groups = {
name: {prec.channel.name for prec in group}
for name, group in groupby(lambda x: x["name"], reduced_index).items()
}
channel_name_groups = {
name: channel_names
for name, channel_names in channel_name_groups.items()
if len(channel_names) > 1
}
assert {} == channel_name_groups
# cleanup
monkeypatch.delenv("CONDA_CHANNEL_PRIORITY")
reset_context()
def test_list_with_pip_no_binary(tmp_env: TmpEnvFixture, conda_cli: CondaCLIFixture):
from conda.exports import rm_rf as _rm_rf
# For this test to work on Windows, you can either pass use_restricted_unicode=on_win
# to make_temp_env(), or you can set PYTHONUTF8 to 1 (and use Python 3.7 or above).
# We elect to test the more complex of the two options.
py_ver = "3.10"
with tmp_env(f"python={py_ver}", "pip") as prefix:
check_call(
f"{PYTHON_BINARY} -m pip install --no-binary flask flask==1.0.2",
cwd=prefix,
shell=True,
)
PrefixData._cache_.clear()
stdout, stderr, err = conda_cli("list", f"--prefix={prefix}")
assert any(
line.endswith("pypi")
for line in stdout.split("\n")
if line.lower().startswith("flask")
)
assert not stderr
assert not err
# regression test for #5847
# when using rm_rf on a directory
assert prefix in PrefixData._cache_
_rm_rf(prefix / get_python_site_packages_short_path(py_ver))
assert prefix not in PrefixData._cache_
def test_list_with_pip_wheel(tmp_env: TmpEnvFixture, conda_cli: CondaCLIFixture):
with tmp_env("python=3.10", "pip") as prefix:
check_call(
f"{PYTHON_BINARY} -m pip install flask==1.0.2",
cwd=prefix,
shell=True,
)
PrefixData._cache_.clear()
stdout, stderr, err = conda_cli("list", f"--prefix={prefix}")
assert any(
line.endswith("pypi")
for line in stdout.split("\n")
if line.lower().startswith("flask")
)
assert not stderr
assert not err
# regression test for #3433
conda_cli("install", f"--prefix={prefix}", "python=3.9", "--yes")
assert package_is_installed(prefix, "python=3.9")
def test_rm_rf(clear_package_cache: None, tmp_env: TmpEnvFixture):
# regression test for #5980, related to #5847
from conda.exports import rm_rf as _rm_rf
py_ver = "3.10"
with tmp_env(f"python={py_ver}") as prefix:
# regression test for #5847
# when using rm_rf on a file
assert prefix in PrefixData._cache_
_rm_rf(prefix / get_python_site_packages_short_path(py_ver), "os.py")
assert prefix not in PrefixData._cache_
with tmp_env() as prefix:
assert isdir(prefix)
assert prefix in PrefixData._cache_
rmtree(prefix)
assert not isdir(prefix)
assert prefix in PrefixData._cache_
_rm_rf(prefix)
assert not isdir(prefix)
assert prefix not in PrefixData._cache_
def test_install_tarball_from_file_based_channel(
tmp_path: Path,
monkeypatch: MonkeyPatch,
tmp_env: TmpEnvFixture,
conda_cli: CondaCLIFixture,
):
# Regression test for #2812
# handle file-based channels
monkeypatch.setenv("CONDA_BLD_PATH", str(tmp_path))
reset_context()
assert context.bld_path == str(tmp_path)
with tmp_env() as prefix, make_temp_channel(["flask-2.1.3"]) as channel:
conda_cli(
"install",
f"--prefix={prefix}",
f"--channel={channel}",
"flask=2.1.3",
"--json",
"--yes",
)
assert package_is_installed(prefix, f"{channel}::flask")
flask_fname = PrefixData(prefix).get("flask")["fn"]
conda_cli("remove", f"--prefix={prefix}", "flask", "--yes")
assert not package_is_installed(prefix, "flask")
# Regression test for 2970
# install from build channel as a tarball
tar_path = Path(PackageCacheData.first_writable().pkgs_dir, flask_fname)
if not tar_path.is_file():
tar_path = tar_path.with_suffix(".tar.bz2")
# create a temporary conda-bld
conda_bld_sub = tmp_path / context.subdir
conda_bld_sub.mkdir(exist_ok=True)
tar_bld_path = str(conda_bld_sub / tar_path.name)
copyfile(tar_path, tar_bld_path)
conda_cli("install", f"--prefix={prefix}", tar_bld_path, "--yes")
assert package_is_installed(prefix, "flask")
# Regression test for #462
with tmp_env(tar_bld_path) as prefix2:
assert package_is_installed(prefix2, "flask")
def test_tarball_install(
test_recipes_channel: Path,
tmp_env: TmpEnvFixture,
conda_cli: CondaCLIFixture,
):
with tmp_env(test_recipes_channel / "noarch" / "dependent-1.0-0.tar.bz2") as prefix:
assert package_is_installed(prefix, "dependent")
assert not package_is_installed(prefix, "dependency")
conda_cli("remove", f"--prefix={prefix}", "dependent", "--yes")
assert not package_is_installed(prefix, "dependent")
def test_tarball_install_and_bad_metadata(
test_recipes_channel: Path, tmp_env: TmpEnvFixture, conda_cli: CondaCLIFixture
):
with tmp_env("small-executable", "dependent", "another_dependent") as prefix:
assert package_is_installed(prefix, "another_dependent")
conda_cli("remove", f"--prefix={prefix}", "dependent", "--yes")
assert package_is_installed(prefix, "small-executable")
assert not package_is_installed(prefix, "dependent")
# make sure all dependencies of "dependent" were removed
assert not package_is_installed(prefix, "dependency")
assert not package_is_installed(prefix, "another_dependent")
tar_path = test_recipes_channel / "noarch" / "dependent-1.0-0.tar.bz2"
with pytest.raises(DryRunExit):
conda_cli("install", f"--prefix={prefix}", tar_path, "--dry-run")
conda_cli("install", f"--prefix={prefix}", tar_path, "--yes")
assert package_is_installed(prefix, "dependent")
bad_metadata = prefix / "bad_metadata.yml"
bad_metadata.write_text(
dals(
"""
name: no-good-metadata
dependencies:
- something-made-up
"""
)
)
with pytest.raises(PackagesNotFoundError):
conda_cli("install", f"--prefix={prefix}", bad_metadata, "--yes")
assert not package_is_installed(prefix, "something-made-up")
def test_update_with_pinned_packages(
test_recipes_channel: Path,
tmp_env: TmpEnvFixture,
conda_cli: CondaCLIFixture,
):
"""
When a dependency is updated we update the dependent package too.
Regression test for #6914
"""
with tmp_env("dependent=1.0") as prefix:
assert package_is_installed(prefix, "dependent=1.0")
assert package_is_installed(prefix, "dependency=1.0")
# removing the history allows dependent to be updated too
(prefix / "conda-meta" / "history").write_text("")
conda_cli("update", f"--prefix={prefix}", "dependency", "--yes")
PrefixData._cache_.clear()
assert not package_is_installed(prefix, "dependent=1.0")
assert not package_is_installed(prefix, "dependency=1.0")
assert package_is_installed(prefix, "dependent=2.0")
assert package_is_installed(prefix, "dependency=2.0")
def test_pinned_override_with_explicit_spec(
test_recipes_channel: Path,
tmp_env: TmpEnvFixture,
conda_cli: CondaCLIFixture,
):
with tmp_env("dependent=1.0") as prefix:
conda_cli(
"config",
f"--file={prefix / 'condarc'}",
*("--add", "pinned_packages", "dependent=1.0"),
)
if context.solver == "libmamba":
# LIBMAMBA ADJUSTMENT
# Incompatible pin overrides forbidden in conda-libmamba-solver 23.9.0+
# See https://github.com/conda/conda-libmamba-solver/pull/294
with pytest.raises(SpecsConfigurationConflictError):
conda_cli("install", f"--prefix={prefix}", "dependent=2.0", "--yes")
else:
conda_cli("install", f"--prefix={prefix}", "dependent=2.0", "--yes")
assert package_is_installed(prefix, "dependent=2.0")
def test_allow_softlinks(
test_recipes_channel: Path,
mocker: MockerFixture,
monkeypatch: MonkeyPatch,
tmp_env: TmpEnvFixture,
):
"""
When hardlinks are unsupported but softlinks are allowed we expect
non-executables to always be symlinked.
"""
mocker.patch("conda.core.link.hardlink_supported", return_value=False)
monkeypatch.setenv("CONDA_ALLOW_SOFTLINKS", "true")