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
|
import os
import sys
import py
import pytest
import tox
import tox.config
from tox.hookspecs import hookimpl
from tox.interpreters import NoInterpreterInfo
from tox.venv import CreationConfig
from tox.venv import getdigest
from tox.venv import tox_testenv_create
from tox.venv import tox_testenv_install_deps
from tox.venv import VirtualEnv
# def test_global_virtualenv(capfd):
# v = VirtualEnv()
# assert v.list()
# out, err = capfd.readouterr()
# assert not out
# assert not err
#
def test_getdigest(tmpdir):
assert getdigest(tmpdir) == "0" * 32
def test_getsupportedinterpreter(monkeypatch, newconfig, mocksession):
config = newconfig([], """
[testenv:python]
basepython=%s
""" % sys.executable)
venv = VirtualEnv(config.envconfigs['python'], session=mocksession)
interp = venv.getsupportedinterpreter()
# realpath needed for debian symlinks
assert py.path.local(interp).realpath() == py.path.local(sys.executable).realpath()
monkeypatch.setattr(sys, 'platform', "win32")
monkeypatch.setattr(venv.envconfig, 'basepython', 'jython')
pytest.raises(tox.exception.UnsupportedInterpreter, venv.getsupportedinterpreter)
monkeypatch.undo()
monkeypatch.setattr(venv.envconfig, "envname", "py1")
monkeypatch.setattr(venv.envconfig, 'basepython', 'notexistingpython')
pytest.raises(tox.exception.InterpreterNotFound, venv.getsupportedinterpreter)
monkeypatch.undo()
# check that we properly report when no version_info is present
info = NoInterpreterInfo(name=venv.name)
info.executable = "something"
monkeypatch.setattr(config.interpreters, "get_info", lambda *args, **kw: info)
pytest.raises(tox.exception.InvocationError, venv.getsupportedinterpreter)
def test_create(monkeypatch, mocksession, newconfig):
config = newconfig([], """
[testenv:py123]
""")
envconfig = config.envconfigs['py123']
venv = VirtualEnv(envconfig, session=mocksession)
assert venv.path == envconfig.envdir
assert not venv.path.check()
action = mocksession.newaction(venv, "getenv")
tox_testenv_create(action=action, venv=venv)
pcalls = mocksession._pcalls
assert len(pcalls) >= 1
args = pcalls[0].args
assert "virtualenv" == str(args[2])
if sys.platform != "win32":
# realpath is needed for stuff like the debian symlinks
assert py.path.local(sys.executable).realpath() == py.path.local(args[0]).realpath()
# assert Envconfig.toxworkdir in args
assert venv.getcommandpath("easy_install", cwd=py.path.local())
interp = venv._getliveconfig().python
assert interp == venv.envconfig.python_info.executable
assert venv.path_config.check(exists=False)
@pytest.mark.skipif("sys.platform == 'win32'")
def test_commandpath_venv_precedence(tmpdir, monkeypatch,
mocksession, newconfig):
config = newconfig([], """
[testenv:py123]
""")
envconfig = config.envconfigs['py123']
venv = VirtualEnv(envconfig, session=mocksession)
tmpdir.ensure("easy_install")
monkeypatch.setenv("PATH", str(tmpdir), prepend=os.pathsep)
envconfig.envbindir.ensure("easy_install")
p = venv.getcommandpath("easy_install")
assert py.path.local(p).relto(envconfig.envbindir), p
def test_create_sitepackages(monkeypatch, mocksession, newconfig):
config = newconfig([], """
[testenv:site]
sitepackages=True
[testenv:nosite]
sitepackages=False
""")
envconfig = config.envconfigs['site']
venv = VirtualEnv(envconfig, session=mocksession)
action = mocksession.newaction(venv, "getenv")
tox_testenv_create(action=action, venv=venv)
pcalls = mocksession._pcalls
assert len(pcalls) >= 1
args = pcalls[0].args
assert "--system-site-packages" in map(str, args)
mocksession._clearmocks()
envconfig = config.envconfigs['nosite']
venv = VirtualEnv(envconfig, session=mocksession)
action = mocksession.newaction(venv, "getenv")
tox_testenv_create(action=action, venv=venv)
pcalls = mocksession._pcalls
assert len(pcalls) >= 1
args = pcalls[0].args
assert "--system-site-packages" not in map(str, args)
assert "--no-site-packages" not in map(str, args)
def test_install_deps_wildcard(newmocksession):
mocksession = newmocksession([], """
[tox]
distshare = {toxworkdir}/distshare
[testenv:py123]
deps=
{distshare}/dep1-*
""")
venv = mocksession.getenv("py123")
action = mocksession.newaction(venv, "getenv")
tox_testenv_create(action=action, venv=venv)
pcalls = mocksession._pcalls
assert len(pcalls) == 1
distshare = venv.session.config.distshare
distshare.ensure("dep1-1.0.zip")
distshare.ensure("dep1-1.1.zip")
tox_testenv_install_deps(action=action, venv=venv)
assert len(pcalls) == 2
args = pcalls[-1].args
assert pcalls[-1].cwd == venv.envconfig.config.toxinidir
assert "pip" in str(args[0])
assert args[1] == "install"
args = [arg for arg in args if str(arg).endswith("dep1-1.1.zip")]
assert len(args) == 1
def test_install_deps_indexserver(newmocksession):
mocksession = newmocksession([], """
[tox]
indexserver =
abc = ABC
abc2 = ABC
[testenv:py123]
deps=
dep1
:abc:dep2
:abc2:dep3
""")
venv = mocksession.getenv('py123')
action = mocksession.newaction(venv, "getenv")
tox_testenv_create(action=action, venv=venv)
pcalls = mocksession._pcalls
assert len(pcalls) == 1
pcalls[:] = []
tox_testenv_install_deps(action=action, venv=venv)
# two different index servers, two calls
assert len(pcalls) == 3
args = " ".join(pcalls[0].args)
assert "-i " not in args
assert "dep1" in args
args = " ".join(pcalls[1].args)
assert "-i ABC" in args
assert "dep2" in args
args = " ".join(pcalls[2].args)
assert "-i ABC" in args
assert "dep3" in args
def test_install_deps_pre(newmocksession):
mocksession = newmocksession([], """
[testenv]
pip_pre=true
deps=
dep1
""")
venv = mocksession.getenv('python')
action = mocksession.newaction(venv, "getenv")
tox_testenv_create(action=action, venv=venv)
pcalls = mocksession._pcalls
assert len(pcalls) == 1
pcalls[:] = []
tox_testenv_install_deps(action=action, venv=venv)
assert len(pcalls) == 1
args = " ".join(pcalls[0].args)
assert "--pre " in args
assert "dep1" in args
def test_installpkg_indexserver(newmocksession, tmpdir):
mocksession = newmocksession([], """
[tox]
indexserver =
default = ABC
""")
venv = mocksession.getenv('python')
pcalls = mocksession._pcalls
p = tmpdir.ensure("distfile.tar.gz")
mocksession.installpkg(venv, p)
# two different index servers, two calls
assert len(pcalls) == 1
args = " ".join(pcalls[0].args)
assert "-i ABC" in args
def test_install_recreate(newmocksession, tmpdir):
pkg = tmpdir.ensure("package.tar.gz")
mocksession = newmocksession(['--recreate'], """
[testenv]
deps=xyz
""")
venv = mocksession.getenv('python')
action = mocksession.newaction(venv, "update")
venv.update(action)
mocksession.installpkg(venv, pkg)
mocksession.report.expect("verbosity0", "*create*")
venv.update(action)
mocksession.report.expect("verbosity0", "*recreate*")
def test_install_sdist_extras(newmocksession):
mocksession = newmocksession([], """
[testenv]
extras = testing
development
""")
venv = mocksession.getenv('python')
action = mocksession.newaction(venv, "getenv")
tox_testenv_create(action=action, venv=venv)
pcalls = mocksession._pcalls
assert len(pcalls) == 1
pcalls[:] = []
venv.installpkg('distfile.tar.gz', action=action)
assert 'distfile.tar.gz[testing,development]' in pcalls[-1].args
def test_develop_extras(newmocksession, tmpdir):
mocksession = newmocksession([], """
[testenv]
extras = testing
development
""")
venv = mocksession.getenv('python')
action = mocksession.newaction(venv, "getenv")
tox_testenv_create(action=action, venv=venv)
pcalls = mocksession._pcalls
assert len(pcalls) == 1
pcalls[:] = []
venv.developpkg(tmpdir, action=action)
expected = "%s[testing,development]" % tmpdir.strpath
assert expected in pcalls[-1].args
def test_env_variables_added_to_needs_reinstall(tmpdir, mocksession, newconfig, monkeypatch):
tmpdir.ensure("setup.py")
monkeypatch.setenv("TEMP_PASS_VAR", "123")
monkeypatch.setenv("TEMP_NOPASS_VAR", "456")
config = newconfig([], """
[testenv:python]
passenv = temp_pass_var
setenv =
CUSTOM_VAR = 789
""")
venv = VirtualEnv(config.envconfigs['python'], session=mocksession)
action = mocksession.newaction(venv, "hello")
venv._needs_reinstall(tmpdir, action)
pcalls = mocksession._pcalls
assert len(pcalls) == 2
env = pcalls[0].env
# should have access to setenv vars
assert 'CUSTOM_VAR' in env
assert env['CUSTOM_VAR'] == '789'
# should have access to passenv vars
assert 'TEMP_PASS_VAR' in env
assert env['TEMP_PASS_VAR'] == "123"
# should also have access to full invocation environment,
# for backward compatibility, and to match behavior of venv.run_install_command()
assert 'TEMP_NOPASS_VAR' in env
assert env["TEMP_NOPASS_VAR"] == "456"
def test_test_hashseed_is_in_output(newmocksession):
original_make_hashseed = tox.config.make_hashseed
tox.config.make_hashseed = lambda: '123456789'
try:
mocksession = newmocksession([], '''
[testenv]
''')
finally:
tox.config.make_hashseed = original_make_hashseed
venv = mocksession.getenv('python')
action = mocksession.newaction(venv, "update")
venv.update(action)
venv.test()
mocksession.report.expect("verbosity0", "python runtests: PYTHONHASHSEED='123456789'")
def test_test_runtests_action_command_is_in_output(newmocksession):
mocksession = newmocksession([], '''
[testenv]
commands = echo foo bar
''')
venv = mocksession.getenv('python')
action = mocksession.newaction(venv, "update")
venv.update(action)
venv.test()
mocksession.report.expect("verbosity0", "*runtests*commands?0? | echo foo bar")
def test_install_error(newmocksession, monkeypatch):
mocksession = newmocksession(['--recreate'], """
[testenv]
deps=xyz
commands=
qwelkqw
""")
venv = mocksession.getenv('python')
venv.test()
mocksession.report.expect("error", "*not find*qwelkqw*")
assert venv.status == "commands failed"
def test_install_command_not_installed(newmocksession, monkeypatch):
mocksession = newmocksession(['--recreate'], """
[testenv]
commands=
pytest
""")
venv = mocksession.getenv('python')
venv.test()
mocksession.report.expect("warning", "*test command found but not*")
assert venv.status == 0
def test_install_command_whitelisted(newmocksession, monkeypatch):
mocksession = newmocksession(['--recreate'], """
[testenv]
whitelist_externals = pytest
xy*
commands=
pytest
xyz
""")
venv = mocksession.getenv('python')
venv.test()
mocksession.report.expect("warning", "*test command found but not*",
invert=True)
assert venv.status == "commands failed"
@pytest.mark.skipif("not sys.platform.startswith('linux')")
def test_install_command_not_installed_bash(newmocksession):
mocksession = newmocksession(['--recreate'], """
[testenv]
commands=
bash
""")
venv = mocksession.getenv('python')
venv.test()
mocksession.report.expect("warning", "*test command found but not*")
def test_install_python3(tmpdir, newmocksession):
if not py.path.local.sysfind('python3'):
pytest.skip("needs python3")
mocksession = newmocksession([], """
[testenv:py123]
basepython=python3
deps=
dep1
dep2
""")
venv = mocksession.getenv('py123')
action = mocksession.newaction(venv, "getenv")
tox_testenv_create(action=action, venv=venv)
pcalls = mocksession._pcalls
assert len(pcalls) == 1
args = pcalls[0].args
assert str(args[2]) == 'virtualenv'
pcalls[:] = []
action = mocksession.newaction(venv, "hello")
venv._install(["hello"], action=action)
assert len(pcalls) == 1
args = pcalls[0].args
assert "pip" in args[0]
for _ in args:
assert "--download-cache" not in args, args
class TestCreationConfig:
def test_basic(self, newconfig, mocksession, tmpdir):
config = newconfig([], "")
envconfig = config.envconfigs['python']
venv = VirtualEnv(envconfig, session=mocksession)
cconfig = venv._getliveconfig()
assert cconfig.matches(cconfig)
path = tmpdir.join("configdump")
cconfig.writeconfig(path)
newconfig = CreationConfig.readconfig(path)
assert newconfig.matches(cconfig)
assert cconfig.matches(newconfig)
def test_matchingdependencies(self, newconfig, mocksession):
config = newconfig([], """
[testenv]
deps=abc
""")
envconfig = config.envconfigs['python']
venv = VirtualEnv(envconfig, session=mocksession)
cconfig = venv._getliveconfig()
config = newconfig([], """
[testenv]
deps=xyz
""")
envconfig = config.envconfigs['python']
venv = VirtualEnv(envconfig, session=mocksession)
otherconfig = venv._getliveconfig()
assert not cconfig.matches(otherconfig)
def test_matchingdependencies_file(self, newconfig, mocksession):
config = newconfig([], """
[tox]
distshare={toxworkdir}/distshare
[testenv]
deps=abc
{distshare}/xyz.zip
""")
xyz = config.distshare.join("xyz.zip")
xyz.ensure()
envconfig = config.envconfigs['python']
venv = VirtualEnv(envconfig, session=mocksession)
cconfig = venv._getliveconfig()
assert cconfig.matches(cconfig)
xyz.write("hello")
newconfig = venv._getliveconfig()
assert not cconfig.matches(newconfig)
def test_matchingdependencies_latest(self, newconfig, mocksession):
config = newconfig([], """
[tox]
distshare={toxworkdir}/distshare
[testenv]
deps={distshare}/xyz-*
""")
config.distshare.ensure("xyz-1.2.0.zip")
xyz2 = config.distshare.ensure("xyz-1.2.1.zip")
envconfig = config.envconfigs['python']
venv = VirtualEnv(envconfig, session=mocksession)
cconfig = venv._getliveconfig()
md5, path = cconfig.deps[0]
assert path == xyz2
assert md5 == path.computehash()
def test_python_recreation(self, tmpdir, newconfig, mocksession):
pkg = tmpdir.ensure("package.tar.gz")
config = newconfig([], "")
envconfig = config.envconfigs['python']
venv = VirtualEnv(envconfig, session=mocksession)
cconfig = venv._getliveconfig()
action = mocksession.newaction(venv, "update")
venv.update(action)
assert not venv.path_config.check()
mocksession.installpkg(venv, pkg)
assert venv.path_config.check()
assert mocksession._pcalls
args1 = map(str, mocksession._pcalls[0].args)
assert 'virtualenv' in " ".join(args1)
mocksession.report.expect("*", "*create*")
# modify config and check that recreation happens
mocksession._clearmocks()
action = mocksession.newaction(venv, "update")
venv.update(action)
mocksession.report.expect("*", "*reusing*")
mocksession._clearmocks()
action = mocksession.newaction(venv, "update")
cconfig.python = py.path.local("balla")
cconfig.writeconfig(venv.path_config)
venv.update(action)
mocksession.report.expect("verbosity0", "*recreate*")
def test_dep_recreation(self, newconfig, mocksession):
config = newconfig([], "")
envconfig = config.envconfigs['python']
venv = VirtualEnv(envconfig, session=mocksession)
action = mocksession.newaction(venv, "update")
venv.update(action)
cconfig = venv._getliveconfig()
cconfig.deps[:] = [("1" * 32, "xyz.zip")]
cconfig.writeconfig(venv.path_config)
mocksession._clearmocks()
action = mocksession.newaction(venv, "update")
venv.update(action)
mocksession.report.expect("*", "*recreate*")
def test_develop_recreation(self, newconfig, mocksession):
config = newconfig([], "")
envconfig = config.envconfigs['python']
venv = VirtualEnv(envconfig, session=mocksession)
action = mocksession.newaction(venv, "update")
venv.update(action)
cconfig = venv._getliveconfig()
cconfig.usedevelop = True
cconfig.writeconfig(venv.path_config)
mocksession._clearmocks()
action = mocksession.newaction(venv, "update")
venv.update(action)
mocksession.report.expect("verbosity0", "*recreate*")
class TestVenvTest:
def test_envbindir_path(self, newmocksession, monkeypatch):
monkeypatch.setenv("PIP_RESPECT_VIRTUALENV", "1")
mocksession = newmocksession([], """
[testenv:python]
commands=abc
""")
venv = mocksession.getenv("python")
action = mocksession.newaction(venv, "getenv")
monkeypatch.setenv("PATH", "xyz")
sysfind_calls = []
monkeypatch.setattr("py.path.local.sysfind", classmethod(
lambda *args, **kwargs: sysfind_calls.append(kwargs) or 0 / 0))
with pytest.raises(ZeroDivisionError):
venv._install(list('123'), action=action)
assert sysfind_calls.pop()["paths"] == [venv.envconfig.envbindir]
with pytest.raises(ZeroDivisionError):
venv.test(action)
assert sysfind_calls.pop()["paths"] == [venv.envconfig.envbindir]
with pytest.raises(ZeroDivisionError):
venv.run_install_command(['qwe'], action=action)
assert sysfind_calls.pop()["paths"] == [venv.envconfig.envbindir]
monkeypatch.setenv("PIP_RESPECT_VIRTUALENV", "1")
monkeypatch.setenv("PIP_REQUIRE_VIRTUALENV", "1")
monkeypatch.setenv("__PYVENV_LAUNCHER__", "1")
with pytest.raises(ZeroDivisionError):
venv.run_install_command(['qwe'], action=action)
assert 'PIP_RESPECT_VIRTUALENV' not in os.environ
assert 'PIP_REQUIRE_VIRTUALENV' not in os.environ
assert '__PYVENV_LAUNCHER__' not in os.environ
def test_pythonpath_usage(self, newmocksession, monkeypatch):
monkeypatch.setenv("PYTHONPATH", "/my/awesome/library")
mocksession = newmocksession([], """
[testenv:python]
commands=abc
""")
venv = mocksession.getenv("python")
action = mocksession.newaction(venv, "getenv")
venv.run_install_command(['qwe'], action=action)
assert 'PYTHONPATH' not in os.environ
mocksession.report.expect("warning", "*Discarding $PYTHONPATH from environment*")
pcalls = mocksession._pcalls
assert len(pcalls) == 1
assert 'PYTHONPATH' not in pcalls[0].env
# passenv = PYTHONPATH allows PYTHONPATH to stay in environment
monkeypatch.setenv("PYTHONPATH", "/my/awesome/library")
mocksession = newmocksession([], """
[testenv:python]
commands=abc
passenv = PYTHONPATH
""")
venv = mocksession.getenv("python")
action = mocksession.newaction(venv, "getenv")
venv.run_install_command(['qwe'], action=action)
assert 'PYTHONPATH' in os.environ
mocksession.report.not_expect("warning", "*Discarding $PYTHONPATH from environment*")
pcalls = mocksession._pcalls
assert len(pcalls) == 2
assert pcalls[1].env['PYTHONPATH'] == '/my/awesome/library'
# FIXME this test fails when run in isolation - find what this depends on
# AssertionError: found warning('*Discarding $PYTHONPATH [...]
def test_env_variables_added_to_pcall(tmpdir, mocksession, newconfig, monkeypatch):
pkg = tmpdir.ensure("package.tar.gz")
monkeypatch.setenv("X123", "123")
monkeypatch.setenv("YY", "456")
config = newconfig([], """
[testenv:python]
commands=python -V
passenv = x123
setenv =
ENV_VAR = value
PYTHONPATH = value
""")
mocksession._clearmocks()
venv = VirtualEnv(config.envconfigs['python'], session=mocksession)
mocksession.installpkg(venv, pkg)
venv.test()
pcalls = mocksession._pcalls
assert len(pcalls) == 2
for x in pcalls:
env = x.env
assert env is not None
assert 'ENV_VAR' in env
assert env['ENV_VAR'] == 'value'
assert env['VIRTUAL_ENV'] == str(venv.path)
assert env['X123'] == "123"
assert 'PYTHONPATH' in env
assert env['PYTHONPATH'] == 'value'
# all env variables are passed for installation
assert pcalls[0].env["YY"] == "456"
assert "YY" not in pcalls[1].env
assert {"ENV_VAR", "VIRTUAL_ENV", "PYTHONHASHSEED", "X123", "PATH"} \
.issubset(pcalls[1].env)
# setenv does not trigger PYTHONPATH warnings
mocksession.report.not_expect("warning", "*Discarding $PYTHONPATH from environment*")
# for e in os.environ:
# assert e in env
def test_installpkg_no_upgrade(tmpdir, newmocksession):
pkg = tmpdir.ensure("package.tar.gz")
mocksession = newmocksession([], "")
venv = mocksession.getenv('python')
venv.just_created = True
venv.envconfig.envdir.ensure(dir=1)
mocksession.installpkg(venv, pkg)
pcalls = mocksession._pcalls
assert len(pcalls) == 1
assert '-U' not in pcalls[0].args
def test_installpkg_upgrade(newmocksession, tmpdir):
pkg = tmpdir.ensure("package.tar.gz")
mocksession = newmocksession([], "")
venv = mocksession.getenv('python')
assert not hasattr(venv, 'just_created')
mocksession.installpkg(venv, pkg)
pcalls = mocksession._pcalls
assert len(pcalls) == 1
index = pcalls[0].args.index(str(pkg))
assert index >= 0
assert '-U' in pcalls[0].args[:index]
assert '--no-deps' in pcalls[0].args[:index]
def test_run_install_command(newmocksession):
mocksession = newmocksession([], "")
venv = mocksession.getenv('python')
venv.just_created = True
venv.envconfig.envdir.ensure(dir=1)
action = mocksession.newaction(venv, "hello")
venv.run_install_command(packages=["whatever"], action=action)
pcalls = mocksession._pcalls
assert len(pcalls) == 1
assert 'pip' in pcalls[0].args[0]
assert 'install' in pcalls[0].args
env = pcalls[0].env
assert env is not None
def test_run_custom_install_command(newmocksession):
mocksession = newmocksession([], """
[testenv]
install_command=easy_install {opts} {packages}
""")
venv = mocksession.getenv('python')
venv.just_created = True
venv.envconfig.envdir.ensure(dir=1)
action = mocksession.newaction(venv, "hello")
venv.run_install_command(packages=["whatever"], action=action)
pcalls = mocksession._pcalls
assert len(pcalls) == 1
assert 'easy_install' in pcalls[0].args[0]
assert pcalls[0].args[1:] == ['whatever']
def test_command_relative_issue36(newmocksession, tmpdir, monkeypatch):
mocksession = newmocksession([], """
[testenv]
""")
x = tmpdir.ensure("x")
venv = mocksession.getenv("python")
x2 = venv.getcommandpath("./x", cwd=tmpdir)
assert x == x2
mocksession.report.not_expect("warning", "*test command found but not*")
x3 = venv.getcommandpath("/bin/bash", cwd=tmpdir)
assert x3 == "/bin/bash"
mocksession.report.not_expect("warning", "*test command found but not*")
monkeypatch.setenv("PATH", str(tmpdir))
x4 = venv.getcommandpath("x", cwd=tmpdir)
assert x4.endswith(os.sep + 'x')
mocksession.report.expect("warning", "*test command found but not*")
def test_ignore_outcome_failing_cmd(newmocksession):
mocksession = newmocksession([], """
[testenv]
commands=testenv_fail
ignore_outcome=True
""")
venv = mocksession.getenv('python')
venv.test()
assert venv.status == "ignored failed command"
mocksession.report.expect("warning", "*command failed but result from "
"testenv is ignored*")
def test_tox_testenv_create(newmocksession):
log = []
class Plugin:
@hookimpl
def tox_testenv_create(self, action, venv):
log.append(1)
@hookimpl
def tox_testenv_install_deps(self, action, venv):
log.append(2)
mocksession = newmocksession([], """
[testenv]
commands=testenv_fail
ignore_outcome=True
""", plugins=[Plugin()])
venv = mocksession.getenv('python')
venv.update(action=mocksession.newaction(venv, "getenv"))
assert log == [1, 2]
def test_tox_testenv_pre_post(newmocksession):
log = []
class Plugin:
@hookimpl
def tox_runtest_pre(self, venv):
log.append('started')
@hookimpl
def tox_runtest_post(self, venv):
log.append('finished')
mocksession = newmocksession([], """
[testenv]
commands=testenv_fail
""", plugins=[Plugin()])
venv = mocksession.getenv('python')
venv.status = None
assert log == []
mocksession.runtestenv(venv)
assert log == ['started', 'finished']
|