forked from conda/conda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_env.py
657 lines (539 loc) · 18.3 KB
/
test_env.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
# Copyright (C) 2012 Anaconda, Inc
# SPDX-License-Identifier: BSD-3-Clause
import json
from pathlib import Path
from uuid import uuid4
import pytest
from conda.auxlib.ish import dals
from conda.base.constants import ROOT_ENV_NAME
from conda.base.context import context
from conda.common.serialize import yaml_safe_load
from conda.core.envs_manager import list_all_known_prefixes
from conda.exceptions import (
CondaEnvException,
EnvironmentFileExtensionNotValid,
EnvironmentFileNotFound,
EnvironmentLocationNotFound,
SpecNotFound,
)
from conda.gateways.disk.delete import rm_rf
from conda.testing import CondaCLIFixture, PathFactoryFixture
pytestmark = pytest.mark.usefixtures("parametrized_solver_fixture")
# Environment names we use during our tests
TEST_ENV1 = "env1"
# Environment config files we use for out tests
ENVIRONMENT_1 = dals(
f"""
name: {TEST_ENV1}
dependencies:
- python
channels:
- defaults
"""
)
ENVIRONMENT_1_WITH_VARIABLES = dals(
f"""
name: {TEST_ENV1}
dependencies:
- python
channels:
- defaults
variables:
DUDE: woah
SWEET: yaaa
API_KEY: AaBbCcDd===EeFf
"""
)
ENVIRONMENT_2 = dals(
f"""
name: {TEST_ENV1}
dependencies:
- python
- flask
channels:
- defaults
"""
)
ENVIRONMENT_3_INVALID = dals(
f"""
name: {TEST_ENV1}
dependecies:
- python
- flask
channels:
- defaults
foo: bar
"""
)
ENVIRONMENT_PYTHON_PIP_CLICK = dals(
f"""
name: {TEST_ENV1}
dependencies:
- python=3
- pip
- pip:
- click
channels:
- defaults
"""
)
ENVIRONMENT_PYTHON_PIP_CLICK_ATTRS = dals(
f"""
name: {TEST_ENV1}
dependencies:
- python=3
- pip
- pip:
- click
- attrs
channels:
- defaults
"""
)
ENVIRONMENT_PYTHON_PIP_NONEXISTING = dals(
f"""
name: {TEST_ENV1}
dependencies:
- python=3
- pip
- pip:
- nonexisting_
channels:
- defaults
"""
)
def create_env(content, filename="environment.yml"):
Path(filename).write_text(content)
@pytest.fixture
def env1(conda_cli: CondaCLIFixture) -> str:
conda_cli("remove", "--name", TEST_ENV1, "--all", "--yes")
yield TEST_ENV1
conda_cli("remove", "--name", TEST_ENV1, "--all", "--yes")
rm_rf("environment.yml")
@pytest.mark.integration
def test_conda_env_create_no_file(conda_cli: CondaCLIFixture):
"""
Test `conda env create` without an environment.yml file
Should fail
"""
with pytest.raises(EnvironmentFileNotFound):
conda_cli("env", "create")
@pytest.mark.integration
def test_conda_env_create_no_existent_file(conda_cli: CondaCLIFixture):
"""
Test `conda env create --file=not_a_file.txt` with a file that does not
exist.
"""
with pytest.raises(EnvironmentFileNotFound):
conda_cli("env", "create", "--file", "not_a_file.txt")
@pytest.mark.integration
def test_conda_env_create_no_existent_file_with_name(conda_cli: CondaCLIFixture):
"""
Test `conda env create --file=not_a_file.txt` with a file that does not
exist.
"""
with pytest.raises(EnvironmentFileNotFound):
conda_cli("env", "create", "--file", "not_a_file.txt", "--name", "foo")
@pytest.mark.integration
def test_create_valid_remote_env(conda_cli: CondaCLIFixture):
"""
Test retrieving an environment using the BinstarSpec (i.e. it retrieves it from anaconda.org)
This tests the `remote_origin` command line argument.
"""
try:
conda_cli("env", "create", "conda-test/env-42")
assert env_is_created("env-42")
stdout, _, _ = conda_cli("info", "--json")
parsed = json.loads(stdout)
assert [env for env in parsed["envs"] if env.endswith("env-42")]
finally:
# manual cleanup
conda_cli("remove", "--name=env-42", "--all", "--yes")
@pytest.mark.integration
def test_create_valid_env(env1: str, conda_cli: CondaCLIFixture):
"""
Creates an environment.yml file and
creates and environment with it
"""
create_env(ENVIRONMENT_1)
conda_cli("env", "create")
assert env_is_created(env1)
stdout, _, _ = conda_cli("info", "--json")
parsed = json.loads(stdout)
assert [env for env in parsed["envs"] if env.endswith(env1)]
@pytest.mark.integration
def test_create_dry_run_yaml(env1: str, conda_cli: CondaCLIFixture):
create_env(ENVIRONMENT_1)
stdout, _, _ = conda_cli("env", "create", "--dry-run")
assert not env_is_created(env1)
# Find line where the YAML output starts (stdout might change if plugins involved)
lines = stdout.splitlines()
for lineno, line in enumerate(lines):
if line.startswith("name:"):
break
else:
pytest.fail("Didn't find YAML data in output")
output = yaml_safe_load("\n".join(lines[lineno:]))
assert output["name"] == env1
assert len(output["dependencies"]) > 0
@pytest.mark.integration
def test_create_dry_run_json(env1: str, conda_cli: CondaCLIFixture):
create_env(ENVIRONMENT_1)
stdout, _, _ = conda_cli("env", "create", "--dry-run", "--json")
assert not env_is_created(env1)
output = json.loads(stdout)
assert output.get("name") == env1
assert len(output["dependencies"])
@pytest.mark.integration
def test_create_valid_env_with_variables(env1: str, conda_cli: CondaCLIFixture):
"""
Creates an environment.yml file and
creates and environment with it
"""
create_env(ENVIRONMENT_1_WITH_VARIABLES)
conda_cli("env", "create")
assert env_is_created(env1)
stdout, _, _ = conda_cli(
*("env", "config", "vars", "list"),
f"--name={env1}",
"--json",
)
output_env_vars = json.loads(stdout)
assert output_env_vars == {
"DUDE": "woah",
"SWEET": "yaaa",
"API_KEY": "AaBbCcDd===EeFf",
}
stdout, _, _ = conda_cli("info", "--json")
parsed = json.loads(stdout)
assert [env for env in parsed["envs"] if env.endswith(env1)]
@pytest.mark.integration
def test_conda_env_create_empty_file(
conda_cli: CondaCLIFixture, path_factory: PathFactoryFixture
):
"""Test `conda env create --file=file_name.yml` where file_name.yml is empty."""
tmp_file = path_factory(suffix=".yml")
tmp_file.touch()
with pytest.raises(SpecNotFound):
conda_cli("env", "create", "--file", tmp_file)
@pytest.mark.integration
def test_conda_env_create_http(conda_cli: CondaCLIFixture):
"""Test `conda env create --file=https://some-website.com/environment.yml`."""
try:
conda_cli(
*("env", "create"),
"--file=https://raw.githubusercontent.com/conda/conda/main/tests/env/support/simple.yml",
)
assert env_is_created("nlp")
finally:
# manual cleanup
conda_cli("remove", "--name=nlp", "--all", "--yes")
@pytest.mark.integration
def test_update(env1: str, conda_cli: CondaCLIFixture):
create_env(ENVIRONMENT_1)
conda_cli("env", "create")
create_env(ENVIRONMENT_2)
conda_cli("env", "update", "--name", env1)
stdout, _, _ = conda_cli("list", "--name", env1, "flask", "--json")
parsed = json.loads(stdout)
assert parsed
@pytest.mark.integration
def test_name(env1: str, conda_cli: CondaCLIFixture):
"""
# smoke test for gh-254
Test that --name can override the `name` key inside an environment.yml
"""
create_env(ENVIRONMENT_1)
conda_cli("env", "create", "--file", "environment.yml", "--name", env1, "--yes")
stdout, _, _ = conda_cli("info", "--json")
parsed = json.loads(stdout)
assert [env for env in parsed["envs"] if env.endswith(env1)]
@pytest.mark.integration
def test_create_valid_env_json_output(env1: str, conda_cli: CondaCLIFixture):
"""
Creates an environment from an environment.yml file with conda packages (no pip)
Check the json output
"""
create_env(ENVIRONMENT_1)
stdout, _, _ = conda_cli(
"env", "create", "--name", env1, "--quiet", "--json", "--yes"
)
output = json.loads(stdout)
assert output["success"] is True
assert len(output["actions"]["LINK"]) > 0
assert "PIP" not in output["actions"]
@pytest.mark.integration
def test_create_valid_env_with_conda_and_pip_json_output(
env1: str, conda_cli: CondaCLIFixture
):
"""
Creates an environment from an environment.yml file with conda and pip dependencies
Check the json output
"""
create_env(ENVIRONMENT_PYTHON_PIP_CLICK)
stdout, _, _ = conda_cli(
"env", "create", "--name", env1, "--quiet", "--json", "--yes"
)
output = json.loads(stdout)
assert len(output["actions"]["LINK"]) > 0
assert output["actions"]["PIP"][0].startswith("click")
@pytest.mark.integration
def test_update_env_json_output(env1: str, conda_cli: CondaCLIFixture):
"""
Update an environment by adding a conda package
Check the json output
"""
create_env(ENVIRONMENT_1)
conda_cli("env", "create", "--name", env1, "--json", "--yes")
create_env(ENVIRONMENT_2)
stdout, _, _ = conda_cli("env", "update", "--name", env1, "--quiet", "--json")
output = json.loads(stdout)
assert output["success"] is True
assert len(output["actions"]["LINK"]) > 0
assert "PIP" not in output["actions"]
@pytest.mark.integration
def test_update_env_only_pip_json_output(
env1: str, conda_cli: CondaCLIFixture, request
):
"""
Update an environment by adding only a pip package
Check the json output
"""
request.applymarker(
pytest.mark.xfail(
context.solver == "libmamba",
reason="Known issue: https://github.com/conda/conda-libmamba-solver/issues/320",
)
)
create_env(ENVIRONMENT_PYTHON_PIP_CLICK)
conda_cli("env", "create", "--name", env1, "--json", "--yes")
create_env(ENVIRONMENT_PYTHON_PIP_CLICK_ATTRS)
stdout, _, _ = conda_cli("env", "update", "--name", env1, "--quiet", "--json")
output = json.loads(stdout)
assert output["success"] is True
# No conda actions (FETCH/LINK), only pip
assert list(output["actions"].keys()) == ["PIP"]
# Only attrs installed
assert len(output["actions"]["PIP"]) == 1
assert output["actions"]["PIP"][0].startswith("attrs")
@pytest.mark.integration
def test_update_env_no_action_json_output(
env1: str, conda_cli: CondaCLIFixture, request
):
"""
Update an already up-to-date environment
Check the json output
"""
request.applymarker(
pytest.mark.xfail(
context.solver == "libmamba",
reason="Known issue: https://github.com/conda/conda-libmamba-solver/issues/320",
)
)
create_env(ENVIRONMENT_PYTHON_PIP_CLICK)
conda_cli("env", "create", "--name", env1, "--json", "--yes")
stdout, _, _ = conda_cli("env", "update", "--name", env1, "--quiet", "--json")
output = json.loads(stdout)
assert output["message"] == "All requested packages already installed."
@pytest.mark.integration
def test_remove_dry_run(env1: str, conda_cli: CondaCLIFixture):
# Test for GH-10231
create_env(ENVIRONMENT_1)
conda_cli("env", "create")
conda_cli("env", "remove", "--name", env1, "--dry-run")
assert env_is_created(env1)
@pytest.mark.integration
def test_set_unset_env_vars(env1: str, conda_cli: CondaCLIFixture):
create_env(ENVIRONMENT_1)
conda_cli("env", "create")
conda_cli(
*("env", "config", "vars", "set"),
*("--name", env1),
"DUDE=woah",
"SWEET=yaaa",
"API_KEY=AaBbCcDd===EeFf",
)
stdout, _, _ = conda_cli(
*("env", "config", "vars", "list"),
*("--name", env1),
"--json",
)
output_env_vars = json.loads(stdout)
assert output_env_vars == {
"DUDE": "woah",
"SWEET": "yaaa",
"API_KEY": "AaBbCcDd===EeFf",
}
conda_cli(
*("env", "config", "vars", "unset"),
*("--name", env1),
"DUDE",
"SWEET",
"API_KEY",
)
stdout, _, _ = conda_cli(
*("env", "config", "vars", "list"),
*("--name", env1),
"--json",
)
output_env_vars = json.loads(stdout)
assert output_env_vars == {}
@pytest.mark.integration
def test_set_unset_env_vars_env_no_exist(conda_cli: CondaCLIFixture):
with pytest.raises(EnvironmentLocationNotFound):
conda_cli(
*("env", "config", "vars", "set"),
*("--name", uuid4().hex),
"DUDE=woah",
"SWEET=yaaa",
"API_KEY=AaBbCcDd===EeFf",
)
@pytest.mark.integration
def test_pip_error_is_propagated(env1: str, conda_cli: CondaCLIFixture):
"""
Creates an environment from an environment.yml file with conda and incorrect pip dependencies
The output must clearly show pip error.
Check the json output
"""
create_env(ENVIRONMENT_PYTHON_PIP_NONEXISTING)
with pytest.raises(CondaEnvException, match="Pip failed"):
conda_cli("env", "create")
def env_is_created(env_name):
"""
Assert an environment is created
Args:
env_name: the environment name
Returns: True if created
False otherwise
"""
from os.path import basename
for prefix in list_all_known_prefixes():
name = ROOT_ENV_NAME if prefix == context.root_prefix else basename(prefix)
if name == env_name:
return True
return False
@pytest.mark.integration
def test_env_export(
env1: str, conda_cli: CondaCLIFixture, path_factory: PathFactoryFixture
):
"""Test conda env export."""
conda_cli("create", "--name", env1, "flask", "--yes")
assert env_is_created(env1)
stdout, _, _ = conda_cli("env", "export", "--name", env1)
env_yml = path_factory(suffix=".yml")
env_yml.write_text(stdout)
conda_cli("env", "remove", "--name", env1, "--yes")
assert not env_is_created(env1)
conda_cli("env", "create", "--file", env_yml, "--yes")
assert env_is_created(env1)
# regression test for #6220
stdout, stderr, _ = conda_cli("env", "export", "--name", env1, "--no-builds")
assert not stderr
env_description = yaml_safe_load(stdout)
assert len(env_description["dependencies"])
for spec_str in env_description["dependencies"]:
assert spec_str.count("=") == 1
conda_cli("env", "remove", "--name", env1, "--yes")
assert not env_is_created(env1)
@pytest.mark.integration
def test_env_export_with_variables(
env1: str, conda_cli: CondaCLIFixture, path_factory: PathFactoryFixture
):
"""Test conda env export."""
conda_cli("create", "--name", env1, "flask", "--yes")
assert env_is_created(env1)
conda_cli(
*("env", "config", "vars", "set"),
*("--name", env1),
"DUDE=woah",
"SWEET=yaaa",
)
stdout, _, _ = conda_cli("env", "export", "--name", env1)
env_yml = path_factory(suffix=".yml")
env_yml.write_text(stdout)
conda_cli("env", "remove", "--name", env1, "--yes")
assert not env_is_created(env1)
conda_cli("env", "create", "--file", env_yml, "--yes")
assert env_is_created(env1)
stdout, stderr, _ = conda_cli("env", "export", "--name", env1, "--no-builds")
assert not stderr
env_description = yaml_safe_load(stdout)
assert len(env_description["variables"])
assert env_description["variables"].keys()
conda_cli("env", "remove", "--name", env1, "--yes")
assert not env_is_created(env1)
@pytest.mark.integration
def test_env_export_json(env1: str, conda_cli: CondaCLIFixture):
"""Test conda env export."""
conda_cli("create", "--name", env1, "flask", "--yes")
assert env_is_created(env1)
stdout, _, _ = conda_cli("env", "export", "--name", env1, "--json")
conda_cli("env", "remove", "--name", env1, "--yes")
assert not env_is_created(env1)
# regression test for #6220
stdout, stderr, _ = conda_cli(
"env", "export", "--name", env1, "--no-builds", "--json"
)
assert not stderr
env_description = json.loads(stdout)
assert len(env_description["dependencies"])
for spec_str in env_description["dependencies"]:
assert spec_str.count("=") == 1
conda_cli("env", "remove", "--name", env1, "--yes")
assert not env_is_created(env1)
@pytest.mark.integration
def test_list(env1: str, conda_cli: CondaCLIFixture, path_factory: PathFactoryFixture):
"""Test conda list -e and conda create from txt."""
conda_cli("create", "--name", env1, "--yes")
assert env_is_created(env1)
stdout, _, _ = conda_cli("list", "--name", env1, "--export")
env_txt = path_factory(suffix=".txt")
env_txt.write_text(stdout)
conda_cli("env", "remove", "--name", env1, "--yes")
assert not env_is_created(env1)
conda_cli("create", "--name", env1, "--file", env_txt, "--yes")
assert env_is_created(env1)
stdout2, _, _ = conda_cli("list", "--name", env1, "--export")
assert stdout == stdout2
@pytest.mark.integration
def test_export_multi_channel(
env1: str, conda_cli: CondaCLIFixture, path_factory: PathFactoryFixture
):
"""Test conda env export."""
from conda.core.prefix_data import PrefixData
PrefixData._cache_.clear()
conda_cli("create", "--name", env1, "python", "--yes")
assert env_is_created(env1)
# install something from other channel not in config file
conda_cli(
"install",
*("--name", env1),
*("--channel", "conda-test"),
"test_timestamp_sort",
"--yes",
)
stdout, _, _ = conda_cli("env", "export", "--name", env1)
assert "conda-test" in stdout
stdout1, _, _ = conda_cli("list", "--name", env1, "--explicit")
env_yml = path_factory(suffix=".yml")
env_yml.write_text(stdout)
conda_cli("env", "remove", "--name", env1, "--yes")
assert not env_is_created(env1)
conda_cli("env", "create", "--file", env_yml, "--yes")
assert env_is_created(env1)
# check explicit that we have same file
stdout2, _, _ = conda_cli("list", "--name", env1, "--explicit")
assert stdout1 == stdout2
@pytest.mark.integration
def test_non_existent_file(conda_cli: CondaCLIFixture):
with pytest.raises(EnvironmentFileNotFound):
conda_cli("env", "create", "--file", "i_do_not_exist.yml", "--yes")
@pytest.mark.integration
def test_invalid_extensions(
conda_cli: CondaCLIFixture,
path_factory: PathFactoryFixture,
):
env_yml = path_factory(suffix=".ymla")
env_yml.touch()
with pytest.raises(EnvironmentFileExtensionNotValid):
conda_cli("env", "create", "--file", env_yml, "--yes")