-
-
Notifications
You must be signed in to change notification settings - Fork 614
/
test_cli_compile.py
3839 lines (3226 loc) · 113 KB
/
test_cli_compile.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
from __future__ import annotations
import hashlib
import os
import pathlib
import shutil
import subprocess
import sys
from textwrap import dedent
from unittest import mock
from unittest.mock import MagicMock
import pytest
from pip._internal.req.constructors import install_req_from_line
from pip._internal.utils.hashes import FAVORITE_HASH
from pip._internal.utils.urls import path_to_url
from pip._vendor.packaging.version import Version
from piptools.build import ProjectMetadata
from piptools.scripts.compile import cli
from piptools.utils import (
COMPILE_EXCLUDE_OPTIONS,
get_pip_version_for_python_executable,
)
from .constants import MINIMAL_WHEELS_PATH, PACKAGES_PATH
legacy_resolver_only = pytest.mark.parametrize(
"current_resolver",
("legacy",),
indirect=("current_resolver",),
)
backtracking_resolver_only = pytest.mark.parametrize(
"current_resolver",
("backtracking",),
indirect=("current_resolver",),
)
@pytest.fixture(
autouse=True,
params=[
pytest.param("legacy", id="legacy resolver"),
pytest.param("backtracking", id="backtracking resolver"),
],
)
def current_resolver(request, monkeypatch):
# Hide --resolver option from pip-compile header, so that we don't have to
# inject it every time to tests outputs.
exclude_options = COMPILE_EXCLUDE_OPTIONS | {"--resolver"}
monkeypatch.setattr("piptools.utils.COMPILE_EXCLUDE_OPTIONS", exclude_options)
# Setup given resolver name
resolver_name = request.param
monkeypatch.setenv("PIP_TOOLS_RESOLVER", resolver_name)
return resolver_name
@pytest.fixture(autouse=True)
def _temp_dep_cache(tmpdir, monkeypatch):
monkeypatch.setenv("PIP_TOOLS_CACHE_DIR", str(tmpdir / "cache"))
def test_default_pip_conf_read(pip_with_index_conf, runner):
# preconditions
with open("requirements.in", "w"):
pass
out = runner.invoke(cli, ["-v"])
# check that we have our index-url as specified in pip.conf
assert "Using indexes:\n http://example.com" in out.stderr
assert "--index-url http://example.com" in out.stderr
def test_command_line_overrides_pip_conf(pip_with_index_conf, runner):
# preconditions
with open("requirements.in", "w"):
pass
out = runner.invoke(cli, ["-v", "-i", "http://override.com"])
# check that we have our index-url as specified in pip.conf
assert "Using indexes:\n http://override.com" in out.stderr
@pytest.mark.network
@pytest.mark.parametrize(
("install_requires", "expected_output"),
(
pytest.param("small-fake-a==0.1", "small-fake-a==0.1", id="regular"),
pytest.param(
"pip-tools @ https://github.com/jazzband/pip-tools/archive/7d86c8d3.zip",
"pip-tools @ https://github.com/jazzband/pip-tools/archive/7d86c8d3.zip",
id="zip URL",
),
pytest.param(
"pip-tools @ git+https://github.com/jazzband/pip-tools@7d86c8d3",
"pip-tools @ git+https://github.com/jazzband/pip-tools@7d86c8d3",
id="scm URL",
),
pytest.param(
"pip-tools @ https://files.pythonhosted.org/packages/06/96/"
"89872db07ae70770fba97205b0737c17ef013d0d1c790"
"899c16bb8bac419/pip_tools-3.6.1-py2.py3-none-any.whl",
"pip-tools @ https://files.pythonhosted.org/packages/06/96/"
"89872db07ae70770fba97205b0737c17ef013d0d1c790"
"899c16bb8bac419/pip_tools-3.6.1-py2.py3-none-any.whl",
id="wheel URL",
),
),
)
def test_command_line_setuptools_read(
runner, make_package, install_requires, expected_output
):
package_dir = make_package(
name="fake-setuptools-a",
install_requires=(install_requires,),
)
out = runner.invoke(
cli,
(
str(package_dir / "setup.py"),
"--find-links",
MINIMAL_WHEELS_PATH,
"--no-build-isolation",
),
)
assert out.exit_code == 0
# check that pip-compile generated a configuration file
output_file = package_dir / "requirements.txt"
assert output_file.exists()
# The package version must NOT be updated in the output file
assert expected_output in output_file.read_text().splitlines()
@pytest.mark.network
@pytest.mark.parametrize(
("options", "expected_output_file"),
(
# For the `pip-compile` output file should be "requirements.txt"
([], "requirements.txt"),
# For the `pip-compile --output-file=output.txt`
# output file should be "output.txt"
(["--output-file", "output.txt"], "output.txt"),
# For the `pip-compile setup.py` output file should be "requirements.txt"
(["setup.py"], "requirements.txt"),
# For the `pip-compile setup.py --output-file=output.txt`
# output file should be "output.txt"
(["setup.py", "--output-file", "output.txt"], "output.txt"),
),
)
def test_command_line_setuptools_output_file(runner, options, expected_output_file):
"""
Test the output files for setup.py as a requirement file.
"""
with open("setup.py", "w") as package:
package.write(
dedent(
"""\
from setuptools import setup
setup(install_requires=[])
"""
)
)
out = runner.invoke(cli, ["--no-build-isolation"] + options)
assert out.exit_code == 0
assert os.path.exists(expected_output_file)
@pytest.mark.network
def test_command_line_setuptools_nested_output_file(tmpdir, runner):
"""
Test the output file for setup.py in nested folder as a requirement file.
"""
proj_dir = tmpdir.mkdir("proj")
with open(str(proj_dir / "setup.py"), "w") as package:
package.write(
dedent(
"""\
from setuptools import setup
setup(install_requires=[])
"""
)
)
out = runner.invoke(cli, [str(proj_dir / "setup.py"), "--no-build-isolation"])
assert out.exit_code == 0
assert (proj_dir / "requirements.txt").exists()
@pytest.mark.network
def test_setuptools_preserves_environment_markers(
runner, make_package, make_wheel, make_pip_conf, tmpdir
):
make_pip_conf(
dedent(
"""\
[global]
disable-pip-version-check = True
"""
)
)
dists_dir = tmpdir / "dists"
foo_dir = make_package(name="foo", version="1.0")
make_wheel(foo_dir, dists_dir)
bar_dir = make_package(
name="bar", version="2.0", install_requires=['foo ; python_version >= "1"']
)
out = runner.invoke(
cli,
[
str(bar_dir / "setup.py"),
"--output-file",
"-",
"--no-header",
"--no-annotate",
"--no-emit-find-links",
"--no-build-isolation",
"--find-links",
str(dists_dir),
],
)
assert out.exit_code == 0, out.stderr
assert out.stdout == 'foo==1.0 ; python_version >= "1"\n'
def test_no_index_option(runner, tmp_path):
req_in = tmp_path / "requirements.in"
req_in.touch()
out = runner.invoke(cli, [req_in.as_posix(), "--no-index", "--verbose"])
assert out.exit_code == 0
assert "Ignoring indexes." in out.stderr
def test_find_links_option(runner):
with open("requirements.in", "w") as req_in:
req_in.write("-f ./libs3")
out = runner.invoke(cli, ["-v", "-f", "./libs1", "-f", "./libs2"])
# Check that find-links has been passed to pip
assert "Using links:\n ./libs1\n ./libs2\n ./libs3\n" in out.stderr
# Check that find-links has been written to a requirements.txt
with open("requirements.txt") as req_txt:
assert (
"--find-links ./libs1\n--find-links ./libs2\n--find-links ./libs3\n"
in req_txt.read()
)
def test_find_links_envvar(monkeypatch, runner):
with open("requirements.in", "w") as req_in:
req_in.write("-f ./libs3")
monkeypatch.setenv("PIP_FIND_LINKS", "./libs1 ./libs2")
out = runner.invoke(cli, ["-v"])
# Check that find-links has been passed to pip
assert "Using links:\n ./libs1\n ./libs2\n ./libs3\n" in out.stderr
# Check that find-links has been written to a requirements.txt
with open("requirements.txt") as req_txt:
assert (
"--find-links ./libs1\n--find-links ./libs2\n--find-links ./libs3\n"
in req_txt.read()
)
def test_extra_index_option(pip_with_index_conf, runner):
with open("requirements.in", "w"):
pass
out = runner.invoke(
cli,
[
"-v",
"--extra-index-url",
"http://extraindex1.com",
"--extra-index-url",
"http://extraindex2.com",
],
)
assert (
"Using indexes:\n"
" http://example.com\n"
" http://extraindex1.com\n"
" http://extraindex2.com" in out.stderr
)
assert (
"--index-url http://example.com\n"
"--extra-index-url http://extraindex1.com\n"
"--extra-index-url http://extraindex2.com" in out.stderr
)
def test_extra_index_envvar(monkeypatch, runner):
with open("requirements.in", "w"):
pass
monkeypatch.setenv("PIP_INDEX_URL", "http://example.com")
monkeypatch.setenv(
"PIP_EXTRA_INDEX_URL", "http://extraindex1.com http://extraindex2.com"
)
out = runner.invoke(cli, ["-v"])
assert (
"Using indexes:\n"
" http://example.com\n"
" http://extraindex1.com\n"
" http://extraindex2.com" in out.stderr
)
assert (
"--index-url http://example.com\n"
"--extra-index-url http://extraindex1.com\n"
"--extra-index-url http://extraindex2.com" in out.stderr
)
@pytest.mark.parametrize("option", ("--extra-index-url", "--find-links"))
def test_redacted_urls_in_verbose_output(runner, option):
"""
Test that URLs with sensitive data don't leak to the output.
"""
with open("requirements.in", "w"):
pass
out = runner.invoke(
cli,
[
"--no-header",
"--no-emit-index-url",
"--no-emit-find-links",
"--verbose",
option,
"http://username:[email protected]",
],
)
assert "http://username:****@example.com" in out.stderr
assert "password" not in out.stderr
def test_trusted_host_option(pip_conf, runner):
with open("requirements.in", "w"):
pass
out = runner.invoke(
cli, ["-v", "--trusted-host", "example.com", "--trusted-host", "example2.com"]
)
assert "--trusted-host example.com\n--trusted-host example2.com\n" in out.stderr
def test_trusted_host_envvar(monkeypatch, pip_conf, runner):
with open("requirements.in", "w"):
pass
monkeypatch.setenv("PIP_TRUSTED_HOST", "example.com example2.com")
out = runner.invoke(cli, ["-v"])
assert "--trusted-host example.com\n--trusted-host example2.com\n" in out.stderr
@pytest.mark.parametrize(
"options",
(
pytest.param(
["--trusted-host", "example.com", "--no-emit-trusted-host"],
id="trusted host",
),
pytest.param(
["--find-links", "wheels", "--no-emit-find-links"], id="find links"
),
pytest.param(
["--index-url", "https://index-url", "--no-emit-index-url"], id="index url"
),
),
)
def test_all_no_emit_options(runner, options):
with open("requirements.in", "w"):
pass
out = runner.invoke(cli, ["--output-file", "-", "--no-header", *options])
assert out.stdout.strip().splitlines() == []
@pytest.mark.parametrize(
("option", "expected_output"),
(
pytest.param(
"--emit-index-url", ["--index-url https://index-url"], id="index url"
),
pytest.param("--no-emit-index-url", [], id="no index"),
),
)
def test_emit_index_url_option(runner, option, expected_output):
with open("requirements.in", "w"):
pass
out = runner.invoke(
cli,
[
"--output-file",
"-",
"--quiet",
"--no-header",
"--index-url",
"https://index-url",
option,
],
)
assert out.stdout.strip().splitlines() == expected_output
@pytest.mark.network
def test_realistic_complex_sub_dependencies(runner, tmp_path):
wheels_dir = tmp_path / "wheels"
wheels_dir.mkdir()
# make a temporary wheel of a fake package
subprocess.run(
[
"pip",
"wheel",
"--no-deps",
"-w",
wheels_dir,
os.path.join(PACKAGES_PATH, "fake_with_deps", "."),
],
check=True,
)
with open("requirements.in", "w") as req_in:
req_in.write("fake_with_deps") # require fake package
out = runner.invoke(cli, ["-n", "--rebuild", "-f", wheels_dir.as_posix()])
assert out.exit_code == 0
def test_run_as_module_compile():
"""piptools can be run as ``python -m piptools ...``."""
result = subprocess.run(
[sys.executable, "-m", "piptools", "compile", "--help"],
stdout=subprocess.PIPE,
check=True,
)
# Should have run pip-compile successfully.
assert result.stdout.startswith(b"Usage:")
assert b"Compile requirements.txt from source files" in result.stdout
def test_editable_package(pip_conf, runner):
"""piptools can compile an editable"""
fake_package_dir = os.path.join(PACKAGES_PATH, "small_fake_with_deps")
fake_package_dir = path_to_url(fake_package_dir)
with open("requirements.in", "w") as req_in:
req_in.write("-e " + fake_package_dir) # require editable fake package
out = runner.invoke(cli, ["-n"])
assert out.exit_code == 0
assert fake_package_dir in out.stderr
assert "small-fake-a==0.1" in out.stderr
def test_editable_package_without_non_editable_duplicate(pip_conf, runner):
"""
piptools keeps editable requirement,
without also adding a duplicate "non-editable" requirement variation
"""
fake_package_dir = os.path.join(PACKAGES_PATH, "small_fake_a")
fake_package_dir = path_to_url(fake_package_dir)
with open("requirements.in", "w") as req_in:
# small_fake_with_unpinned_deps also requires small_fake_a
req_in.write(
"-e "
+ fake_package_dir
+ "\nsmall_fake_with_unpinned_deps" # require editable fake package
)
out = runner.invoke(cli, ["-n"])
assert out.exit_code == 0
assert fake_package_dir in out.stderr
# Shouldn't include a non-editable small-fake-a==<version>.
assert "small-fake-a==" not in out.stderr
@legacy_resolver_only
def test_editable_package_constraint_without_non_editable_duplicate(pip_conf, runner):
"""
piptools keeps editable constraint,
without also adding a duplicate "non-editable" requirement variation
"""
fake_package_dir = os.path.join(PACKAGES_PATH, "small_fake_a")
fake_package_dir = path_to_url(fake_package_dir)
with open("constraints.txt", "w") as constraints:
constraints.write("-e " + fake_package_dir) # require editable fake package
with open("requirements.in", "w") as req_in:
req_in.write(
"-c constraints.txt" # require editable fake package
"\nsmall_fake_with_unpinned_deps" # This one also requires small_fake_a
)
out = runner.invoke(cli, ["--output-file", "-", "--quiet"])
assert out.exit_code == 0
assert fake_package_dir in out.stdout
# Shouldn't include a non-editable small-fake-a==<version>.
assert "small-fake-a==" not in out.stdout
@legacy_resolver_only
@pytest.mark.parametrize("req_editable", ((True,), (False,)))
def test_editable_package_in_constraints(pip_conf, runner, req_editable):
"""
piptools can compile an editable that appears in both primary requirements
and constraints
"""
fake_package_dir = os.path.join(PACKAGES_PATH, "small_fake_with_deps")
fake_package_dir = path_to_url(fake_package_dir)
with open("constraints.txt", "w") as constraints_in:
constraints_in.write("-e " + fake_package_dir)
with open("requirements.in", "w") as req_in:
prefix = "-e " if req_editable else ""
req_in.write(prefix + fake_package_dir + "\n-c constraints.txt")
out = runner.invoke(cli, ["-n"])
assert out.exit_code == 0
assert fake_package_dir in out.stderr
assert "small-fake-a==0.1" in out.stderr
@pytest.mark.network
def test_editable_package_vcs(runner):
vcs_package = (
"git+https://github.com/jazzband/pip-tools@"
"f97e62ecb0d9b70965c8eff952c001d8e2722e94"
"#egg=pip-tools"
)
with open("requirements.in", "w") as req_in:
req_in.write("-e " + vcs_package)
out = runner.invoke(cli, ["-n", "--rebuild"])
assert out.exit_code == 0
assert vcs_package in out.stderr
assert "click" in out.stderr # dependency of pip-tools
@pytest.mark.network
def test_compile_cached_vcs_package(runner, venv):
"""
Test pip-compile doesn't write local paths for cached wheels of VCS packages.
Regression test for issue GH-1647.
"""
vcs_package = (
"typing-extensions @ git+https://github.com/python/typing_extensions@"
"9c0759a260fe126210a1e2026720000a3c40a919"
)
vcs_wheel_prefix = "typing_extensions-4.3.0-py3"
# Install and cache VCS package.
subprocess.run(
[os.fspath(venv / "python"), "-m" "pip", "install", vcs_package],
check=True,
)
assert (
vcs_wheel_prefix
in subprocess.run(
[
sys.executable,
"-m" "pip",
"cache",
"list",
"--format=abspath",
vcs_wheel_prefix,
],
check=True,
capture_output=True,
text=True,
).stdout
)
with open("requirements.in", "w") as req_in:
req_in.write(vcs_package)
out = runner.invoke(
cli,
[
"--output-file",
"-",
"--quiet",
"--no-header",
"--no-emit-options",
"--no-annotate",
"--no-build-isolation",
],
)
assert out.exit_code == 0, out
assert vcs_package == out.stdout.strip()
@legacy_resolver_only
def test_locally_available_editable_package_is_not_archived_in_cache_dir(
pip_conf, tmpdir, runner
):
"""
piptools will not create an archive for a locally available editable requirement
"""
cache_dir = tmpdir.mkdir("cache_dir")
fake_package_dir = os.path.join(PACKAGES_PATH, "small_fake_with_deps")
fake_package_dir = path_to_url(fake_package_dir)
with open("requirements.in", "w") as req_in:
req_in.write("-e " + fake_package_dir) # require editable fake package
out = runner.invoke(cli, ["-n", "--rebuild", "--cache-dir", str(cache_dir)])
assert out.exit_code == 0
assert fake_package_dir in out.stderr
assert "small-fake-a==0.1" in out.stderr
# we should not find any archived file in {cache_dir}/pkgs
assert not os.listdir(os.path.join(str(cache_dir), "pkgs"))
@pytest.mark.parametrize(
("line", "dependency"),
(
# use pip-tools version prior to its use of setuptools_scm,
# which is incompatible with https: install
pytest.param(
"https://github.com/jazzband/pip-tools/archive/"
"7d86c8d3ecd1faa6be11c7ddc6b29a30ffd1dae3.zip",
"\nclick==",
id="Zip URL",
),
pytest.param(
"git+https://github.com/jazzband/pip-tools@"
"7d86c8d3ecd1faa6be11c7ddc6b29a30ffd1dae3",
"\nclick==",
id="VCS URL",
),
pytest.param(
"https://files.pythonhosted.org/packages/06/96/"
"89872db07ae70770fba97205b0737c17ef013d0d1c790"
"899c16bb8bac419/pip_tools-3.6.1-py2.py3-none-any.whl",
"\nclick==",
id="Wheel URL",
),
pytest.param(
"pytest-django @ git+https://github.com/pytest-dev/pytest-django"
"@21492afc88a19d4ca01cd0ac392a5325b14f95c7"
"#egg=pytest-django",
"pytest-django @ git+https://github.com/pytest-dev/pytest-django"
"@21492afc88a19d4ca01cd0ac392a5325b14f95c7",
id="VCS with direct reference and egg",
),
),
)
@pytest.mark.parametrize("generate_hashes", ((True,), (False,)))
@pytest.mark.network
def test_url_package(runner, line, dependency, generate_hashes):
with open("requirements.in", "w") as req_in:
req_in.write(line)
out = runner.invoke(
cli,
["-n", "--rebuild", "--no-build-isolation"]
+ (["--generate-hashes"] if generate_hashes else []),
)
assert out.exit_code == 0
assert dependency in out.stderr
@pytest.mark.parametrize(
("line", "dependency", "rewritten_line"),
(
pytest.param(
path_to_url(
os.path.join(
MINIMAL_WHEELS_PATH, "small_fake_with_deps-0.1-py2.py3-none-any.whl"
)
),
"\nsmall-fake-a==0.1",
None,
id="Wheel URI",
),
pytest.param(
path_to_url(os.path.join(PACKAGES_PATH, "small_fake_with_deps")),
"\nsmall-fake-a==0.1",
None,
id="Local project URI",
),
pytest.param(
os.path.join(
MINIMAL_WHEELS_PATH, "small_fake_with_deps-0.1-py2.py3-none-any.whl"
),
"\nsmall-fake-a==0.1",
path_to_url(
os.path.join(
MINIMAL_WHEELS_PATH, "small_fake_with_deps-0.1-py2.py3-none-any.whl"
)
),
id="Bare path to file URI",
),
pytest.param(
os.path.join(
MINIMAL_WHEELS_PATH, "small_fake_with_deps-0.1-py2.py3-none-any.whl"
),
"\nsmall-fake-with-deps @ "
+ path_to_url(
os.path.join(
MINIMAL_WHEELS_PATH, "small_fake_with_deps-0.1-py2.py3-none-any.whl"
)
),
"\nsmall-fake-with-deps @ "
+ path_to_url(
os.path.join(
MINIMAL_WHEELS_PATH, "small_fake_with_deps-0.1-py2.py3-none-any.whl"
)
),
id="Local project with absolute URI",
),
pytest.param(
path_to_url(os.path.join(PACKAGES_PATH, "small_fake_with_subdir"))
+ "#subdirectory=subdir&egg=small-fake-a",
"small-fake-a @ "
+ path_to_url(os.path.join(PACKAGES_PATH, "small_fake_with_subdir"))
+ "#subdirectory=subdir",
"small-fake-a @ "
+ path_to_url(os.path.join(PACKAGES_PATH, "small_fake_with_subdir"))
+ "#subdirectory=subdir",
id="Local project with subdirectory",
),
),
)
@pytest.mark.parametrize("generate_hashes", ((True,), (False,)))
def test_local_file_uri_package(
pip_conf, runner, line, dependency, rewritten_line, generate_hashes
):
if rewritten_line is None:
rewritten_line = line
with open("requirements.in", "w") as req_in:
req_in.write(line)
out = runner.invoke(
cli, ["-n", "--rebuild"] + (["--generate-hashes"] if generate_hashes else [])
)
assert out.exit_code == 0
assert rewritten_line in out.stderr
assert dependency in out.stderr
def test_relative_file_uri_package(pip_conf, runner):
# Copy wheel into temp dir
shutil.copy(
os.path.join(
MINIMAL_WHEELS_PATH, "small_fake_with_deps-0.1-py2.py3-none-any.whl"
),
".",
)
with open("requirements.in", "w") as req_in:
req_in.write("file:small_fake_with_deps-0.1-py2.py3-none-any.whl")
out = runner.invoke(cli, ["-n", "--rebuild"])
assert out.exit_code == 0
assert "file:small_fake_with_deps-0.1-py2.py3-none-any.whl" in out.stderr
def test_direct_reference_with_extras(runner):
with open("requirements.in", "w") as req_in:
req_in.write(
"pip-tools[testing,coverage] @ git+https://github.com/jazzband/[email protected]"
)
out = runner.invoke(cli, ["-n", "--rebuild", "--no-build-isolation"])
assert out.exit_code == 0
assert (
"pip-tools[coverage,testing] @ git+https://github.com/jazzband/[email protected]"
in out.stderr
)
assert "pytest==" in out.stderr
assert "pytest-cov==" in out.stderr
def test_input_file_without_extension(pip_conf, runner):
"""
piptools can compile a file without an extension,
and add .txt as the default output file extension.
"""
with open("requirements", "w") as req_in:
req_in.write("small-fake-a==0.1")
out = runner.invoke(cli, ["requirements"])
assert out.exit_code == 0
assert "small-fake-a==0.1" in out.stderr
assert os.path.exists("requirements.txt")
def test_ignore_incompatible_existing_pins(pip_conf, runner):
"""
Successfully compile when existing output pins conflict with input.
"""
with open("requirements.txt", "w") as req_txt:
req_txt.write("small-fake-a==0.2\nsmall-fake-b==0.2")
with open("requirements.in", "w") as req_in:
req_in.write("small-fake-with-deps\nsmall-fake-b<0.2")
out = runner.invoke(cli, [])
assert out.exit_code == 0
def test_upgrade_packages_option(pip_conf, runner):
"""
piptools respects --upgrade-package/-P inline list.
"""
with open("requirements.in", "w") as req_in:
req_in.write("small-fake-a\nsmall-fake-b")
with open("requirements.txt", "w") as req_in:
req_in.write("small-fake-a==0.1\nsmall-fake-b==0.1")
out = runner.invoke(cli, ["--no-annotate", "-P", "small-fake-b"])
assert out.exit_code == 0
assert "small-fake-a==0.1" in out.stderr.splitlines()
assert "small-fake-b==0.3" in out.stderr.splitlines()
def test_upgrade_packages_option_irrelevant(pip_conf, runner):
"""
piptools ignores --upgrade-package/-P items not already constrained.
"""
with open("requirements.in", "w") as req_in:
req_in.write("small-fake-a")
with open("requirements.txt", "w") as req_in:
req_in.write("small-fake-a==0.1")
out = runner.invoke(cli, ["--no-annotate", "--upgrade-package", "small-fake-b"])
assert out.exit_code == 0
assert "small-fake-a==0.1" in out.stderr.splitlines()
assert "small-fake-b==0.3" not in out.stderr.splitlines()
def test_upgrade_packages_option_no_existing_file(pip_conf, runner):
"""
piptools respects --upgrade-package/-P inline list when the output file
doesn't exist.
"""
with open("requirements.in", "w") as req_in:
req_in.write("small-fake-a\nsmall-fake-b")
out = runner.invoke(cli, ["--no-annotate", "-P", "small-fake-b"])
assert out.exit_code == 0
assert "small-fake-a==0.2" in out.stderr.splitlines()
assert "small-fake-b==0.3" in out.stderr.splitlines()
assert (
"WARNING: the output file requirements.txt exists but is empty"
not in out.stderr
)
def test_upgrade_packages_empty_target_file_warning(pip_conf, runner):
"""
piptools warns the user if --upgrade-package/-P is specified and the
output file exists, but is empty.
"""
with open("requirements.in", "w") as req_in:
req_in.write("small-fake-a==0.2")
with open("requirements.txt", "w") as req_txt:
req_txt.write("")
out = runner.invoke(cli, ["--no-annotate", "-P", "small-fake-a"])
assert out.exit_code == 0
assert "small-fake-a==0.2" in out.stderr.splitlines()
assert "WARNING: the output file requirements.txt exists but is empty" in out.stderr
@pytest.mark.parametrize(
("current_package", "upgraded_package"),
(
pytest.param("small-fake-b==0.1", "small-fake-b==0.3", id="upgrade"),
pytest.param("small-fake-b==0.3", "small-fake-b==0.1", id="downgrade"),
),
)
def test_upgrade_packages_version_option(
pip_conf, runner, current_package, upgraded_package
):
"""
piptools respects --upgrade-package/-P inline list with specified versions.
"""
with open("requirements.in", "w") as req_in:
req_in.write("small-fake-a\nsmall-fake-b")
with open("requirements.txt", "w") as req_in:
req_in.write("small-fake-a==0.1\n" + current_package)
out = runner.invoke(cli, ["--no-annotate", "--upgrade-package", upgraded_package])
assert out.exit_code == 0
stderr_lines = out.stderr.splitlines()
assert "small-fake-a==0.1" in stderr_lines
assert upgraded_package in stderr_lines
def test_upgrade_packages_version_option_no_existing_file(pip_conf, runner):
"""
piptools respects --upgrade-package/-P inline list with specified versions.
"""
with open("requirements.in", "w") as req_in:
req_in.write("small-fake-a\nsmall-fake-b")
out = runner.invoke(cli, ["-P", "small-fake-b==0.2"])
assert out.exit_code == 0
assert "small-fake-a==0.2" in out.stderr
assert "small-fake-b==0.2" in out.stderr
@pytest.mark.parametrize(
"reqs_in",
(
pytest.param("small-fake-a\nsmall-fake-b", id="direct reqs"),
pytest.param("small-fake-with-unpinned-deps", id="parent req"),
),
)
def test_upgrade_packages_version_option_and_upgrade(pip_conf, runner, reqs_in):
"""
piptools respects --upgrade-package/-P inline list with specified versions
whilst also doing --upgrade.
"""
with open("requirements.in", "w") as req_in:
req_in.write(reqs_in)
with open("requirements.txt", "w") as req_in:
req_in.write("small-fake-a==0.1\nsmall-fake-b==0.1")
out = runner.invoke(cli, ["--upgrade", "-P", "small-fake-b==0.1"])
assert out.exit_code == 0
assert "small-fake-a==0.2" in out.stderr
assert "small-fake-b==0.1" in out.stderr
def test_upgrade_packages_version_option_and_upgrade_no_existing_file(pip_conf, runner):
"""
piptools respects --upgrade-package/-P inline list with specified versions
whilst also doing --upgrade and the output file doesn't exist.
"""
with open("requirements.in", "w") as req_in:
req_in.write("small-fake-a\nsmall-fake-b")
out = runner.invoke(cli, ["--upgrade", "-P", "small-fake-b==0.1"])
assert out.exit_code == 0
assert "small-fake-a==0.2" in out.stderr
assert "small-fake-b==0.1" in out.stderr
def test_upgrade_package_with_extra(runner, make_package, make_sdist, tmpdir):
"""
piptools ignores extras on --upgrade-package/-P items if already constrained.
"""
test_package_1 = make_package(
"test_package_1", version="0.1", extras_require={"more": "test_package_2"}
)
test_package_2 = make_package(
"test_package_2",
version="0.1",
)
dists_dir = tmpdir / "dists"
for pkg in (test_package_1, test_package_2):
make_sdist(pkg, dists_dir)
# Constrain our requirement with an extra
with open("requirements.in", "w") as req_in:
req_in.write("test-package-1[more]")
# Run update on test-package-1[more] -- this should be equivalent
# to running an update on test-package-1
out = runner.invoke(
cli,
[
"--output-file",
"-",