pytester: factor out testdir._env_run_update
This commit is contained in:
		
							parent
							
								
									f013a5e8c1
								
							
						
					
					
						commit
						ace3a02cd4
					
				|  | @ -508,6 +508,10 @@ class Testdir(object): | ||||||
|         # Discard outer pytest options. |         # Discard outer pytest options. | ||||||
|         mp.delenv("PYTEST_ADDOPTS", raising=False) |         mp.delenv("PYTEST_ADDOPTS", raising=False) | ||||||
| 
 | 
 | ||||||
|  |         # Environment (updates) for inner runs. | ||||||
|  |         tmphome = str(self.tmpdir) | ||||||
|  |         self._env_run_update = {"HOME": tmphome, "USERPROFILE": tmphome} | ||||||
|  | 
 | ||||||
|     def __repr__(self): |     def __repr__(self): | ||||||
|         return "<Testdir %r>" % (self.tmpdir,) |         return "<Testdir %r>" % (self.tmpdir,) | ||||||
| 
 | 
 | ||||||
|  | @ -804,8 +808,8 @@ class Testdir(object): | ||||||
|         try: |         try: | ||||||
|             # Do not load user config (during runs only). |             # Do not load user config (during runs only). | ||||||
|             mp_run = MonkeyPatch() |             mp_run = MonkeyPatch() | ||||||
|             mp_run.setenv("HOME", str(self.tmpdir)) |             for k, v in self._env_run_update.items(): | ||||||
|             mp_run.setenv("USERPROFILE", str(self.tmpdir)) |                 mp_run.setenv(k, v) | ||||||
|             finalizers.append(mp_run.undo) |             finalizers.append(mp_run.undo) | ||||||
| 
 | 
 | ||||||
|             # When running pytest inline any plugins active in the main test |             # When running pytest inline any plugins active in the main test | ||||||
|  | @ -1045,9 +1049,7 @@ class Testdir(object): | ||||||
|         env["PYTHONPATH"] = os.pathsep.join( |         env["PYTHONPATH"] = os.pathsep.join( | ||||||
|             filter(None, [os.getcwd(), env.get("PYTHONPATH", "")]) |             filter(None, [os.getcwd(), env.get("PYTHONPATH", "")]) | ||||||
|         ) |         ) | ||||||
|         # Do not load user config. |         env.update(self._env_run_update) | ||||||
|         env["HOME"] = str(self.tmpdir) |  | ||||||
|         env["USERPROFILE"] = env["HOME"] |  | ||||||
|         kw["env"] = env |         kw["env"] = env | ||||||
| 
 | 
 | ||||||
|         if stdin is Testdir.CLOSE_STDIN: |         if stdin is Testdir.CLOSE_STDIN: | ||||||
|  | @ -1236,8 +1238,7 @@ class Testdir(object): | ||||||
| 
 | 
 | ||||||
|         # Do not load user config. |         # Do not load user config. | ||||||
|         env = os.environ.copy() |         env = os.environ.copy() | ||||||
|         env["HOME"] = str(self.tmpdir) |         env.update(self._env_run_update) | ||||||
|         env["USERPROFILE"] = env["HOME"] |  | ||||||
| 
 | 
 | ||||||
|         child = pexpect.spawn(cmd, logfile=logfile, env=env) |         child = pexpect.spawn(cmd, logfile=logfile, env=env) | ||||||
|         self.request.addfinalizer(logfile.close) |         self.request.addfinalizer(logfile.close) | ||||||
|  |  | ||||||
|  | @ -569,12 +569,15 @@ def test_spawn_uses_tmphome(testdir): | ||||||
|     # Does use HOME only during run. |     # Does use HOME only during run. | ||||||
|     assert os.environ.get("HOME") != tmphome |     assert os.environ.get("HOME") != tmphome | ||||||
| 
 | 
 | ||||||
|  |     testdir._env_run_update["CUSTOMENV"] = "42" | ||||||
|  | 
 | ||||||
|     p1 = testdir.makepyfile( |     p1 = testdir.makepyfile( | ||||||
|         """ |         """ | ||||||
|         import os |         import os | ||||||
| 
 | 
 | ||||||
|         def test(): |         def test(): | ||||||
|             assert os.environ["HOME"] == {tmphome!r} |             assert os.environ["HOME"] == {tmphome!r} | ||||||
|  |             assert os.environ["CUSTOMENV"] == "42" | ||||||
|         """.format( |         """.format( | ||||||
|             tmphome=tmphome |             tmphome=tmphome | ||||||
|         ) |         ) | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue