From f663c1b4d5a180ef15cfeb304670f7943184dc29 Mon Sep 17 00:00:00 2001 From: Petr Viktorin Date: Wed, 4 Nov 2020 15:23:37 +0100 Subject: [PATCH] Tests: Manipulate INI config with ConfigParser --- tests/fixtures/tox.ini | 1 - tests/test_integration.py | 30 ++++++++++++++++++++++++------ 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/tests/fixtures/tox.ini b/tests/fixtures/tox.ini index 7a48efe..87b16cd 100644 --- a/tests/fixtures/tox.ini +++ b/tests/fixtures/tox.ini @@ -10,4 +10,3 @@ extras = full commands = python -c 'import os, sys; print(os.path.realpath(sys.exec_prefix), "is the exec_prefix")' -# note: some tests assume [testenv] is the last section diff --git a/tests/test_integration.py b/tests/test_integration.py index f991507..b9d590e 100644 --- a/tests/test_integration.py +++ b/tests/test_integration.py @@ -7,6 +7,8 @@ import subprocess import sys import textwrap import warnings +import configparser +import contextlib from packaging import version @@ -51,6 +53,24 @@ def print_extras_stdout_arg(request): return request.param +@contextlib.contextmanager +def modify_config(tox_ini_path): + """Context manager that allows modifying the given Tox config file + + A statement like:: + + with prepare_config(projdir) as config: + + will make `config` a ConfigParser instance that is saved at the end + of the `with` block. + """ + config = configparser.ConfigParser() + config.read(tox_ini_path) + yield config + with open(tox_ini_path, 'w') as tox_ini_file: + config.write(tox_ini_file) + + def tox(*args, quiet=True, **kwargs): kwargs.setdefault("encoding", "utf-8") kwargs.setdefault("stdout", subprocess.PIPE) @@ -552,9 +572,8 @@ def test_noquiet_installed_packages(flag): @pytest.mark.parametrize("flag", ["--print-deps-to=-", "--print-extras-to=-", "--current-env"]) @pytest.mark.parametrize("usedevelop", [True, False]) def test_self_is_not_installed(projdir, flag, usedevelop): - tox_ini = projdir / "tox.ini" - with open(tox_ini, 'a') as tox_ini_file: - print(f"usedevelop={usedevelop}", file=tox_ini_file) + with modify_config(projdir / 'tox.ini') as config: + config['testenv']['usedevelop'] = str(usedevelop) result = tox("-e", NATIVE_TOXENV, flag, quiet=False) assert 'test==0.0.0' not in result.stdout assert 'test @ file://' not in result.stdout @@ -562,9 +581,8 @@ def test_self_is_not_installed(projdir, flag, usedevelop): @pytest.mark.parametrize("usedevelop", [True, False]) def test_self_is_installed_with_regular_tox(projdir, usedevelop): - tox_ini = projdir / "tox.ini" - with open(tox_ini, 'a') as tox_ini_file: - print(f"usedevelop={usedevelop}", file=tox_ini_file) + with modify_config(projdir / 'tox.ini') as config: + config['testenv']['usedevelop'] = str(usedevelop) result = tox("-e", NATIVE_TOXENV, quiet=False) assert ('test==0.0.0' in result.stdout or 'test @ file://' in result.stdout)