From 7f5676e2b8f360de0dcb181736c482a1d73e76a8 Mon Sep 17 00:00:00 2001 From: Frost Ming Date: Thu, 6 Dec 2018 14:14:29 +0800 Subject: [PATCH 1/5] add news --- news/3362.trivial.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 news/3362.trivial.rst diff --git a/news/3362.trivial.rst b/news/3362.trivial.rst new file mode 100644 index 00000000..2216b071 --- /dev/null +++ b/news/3362.trivial.rst @@ -0,0 +1 @@ +The inline tables won't be rewritten now. From 27f83a8631220e6c799e80597484a0439c0271e1 Mon Sep 17 00:00:00 2001 From: Frost Ming Date: Thu, 6 Dec 2018 15:54:18 +0800 Subject: [PATCH 2/5] preserve inline spaces in toml --- pipenv/utils.py | 34 +++++++++++++++++++++---------- tests/integration/test_project.py | 7 ++++++- 2 files changed, 29 insertions(+), 12 deletions(-) diff --git a/pipenv/utils.py b/pipenv/utils.py index 9f03465c..b0920764 100644 --- a/pipenv/utils.py +++ b/pipenv/utils.py @@ -83,19 +83,31 @@ def cleanup_toml(tml): def convert_toml_outline_tables(parsed): """Converts all outline tables to inline tables.""" - if isinstance(parsed, tomlkit.container.Container): - empty_inline_table = tomlkit.inline_table - else: - empty_inline_table = toml.TomlDecoder().get_empty_inline_table + def convert_tomlkit_table(section): + for key, value in section._body: + if not key: + continue + if hasattr(value, "keys") and not isinstance(value, tomlkit.items.InlineTable): + table = tomlkit.inline_table() + table.update(value.value) + section[key.key] = table + + def convert_toml_table(section): + for package, value in section.items(): + if hasattr(value, "keys") and not isinstance(toml.decoder.InlineTableDict): + table = toml.TomlDecoder().get_empty_inline_table() + table.update(value) + section[package] = table + for section in ("packages", "dev-packages"): table_data = parsed.get(section, {}) - for package, value in table_data.items(): - if hasattr(value, "keys") and not isinstance( - value, (tomlkit.items.InlineTable, toml.decoder.InlineTableDict) - ): - table = empty_inline_table() - table.update(value) - table_data[package] = table + if not table_data: + continue + if isinstance(parsed, tomlkit.container.Container): + convert_tomlkit_table(table_data) + else: + convert_toml_table(table_data) + return parsed diff --git a/tests/integration/test_project.py b/tests/integration/test_project.py index f9a2ba12..bec98d59 100644 --- a/tests/integration/test_project.py +++ b/tests/integration/test_project.py @@ -154,16 +154,21 @@ def test_rewrite_outline_table(PipenvInstance, pypi): with PipenvInstance(pypi=pypi, chdir=True) as p: with open(p.pipfile_path, 'w') as f: contents = """ +[packages] +six = {version = "*", editable = true} + [packages.requests] version = "*" """.strip() f.write(contents) - c = p.pipenv('install click') + c = p.pipenv("install -e click") assert c.return_code == 0 with open(p.pipfile_path) as f: contents = f.read() assert "[packages.requests]" not in contents + assert 'six = {version = "*", editable = true}' in contents assert 'requests = {version = "*"}' in contents + assert 'click = {' in contents @pytest.mark.install From 560d4ff5aa86eeedfb8bf48c4c59259745d7bf58 Mon Sep 17 00:00:00 2001 From: Frost Ming Date: Thu, 6 Dec 2018 16:00:33 +0800 Subject: [PATCH 3/5] fix a typo --- pipenv/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pipenv/utils.py b/pipenv/utils.py index b0920764..67f3e9a6 100644 --- a/pipenv/utils.py +++ b/pipenv/utils.py @@ -94,7 +94,7 @@ def convert_toml_outline_tables(parsed): def convert_toml_table(section): for package, value in section.items(): - if hasattr(value, "keys") and not isinstance(toml.decoder.InlineTableDict): + if hasattr(value, "keys") and not isinstance(value, toml.decoder.InlineTableDict): table = toml.TomlDecoder().get_empty_inline_table() table.update(value) section[package] = table From 8efaea620e3f7dfbcaa424263b713c4ccf929bb1 Mon Sep 17 00:00:00 2001 From: Frost Ming Date: Thu, 6 Dec 2018 16:02:37 +0800 Subject: [PATCH 4/5] Raise condition evaluation --- pipenv/utils.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pipenv/utils.py b/pipenv/utils.py index 67f3e9a6..08847e59 100644 --- a/pipenv/utils.py +++ b/pipenv/utils.py @@ -99,11 +99,12 @@ def convert_toml_outline_tables(parsed): table.update(value) section[package] = table + is_tomlkit_parsed = isinstance(parsed, tomlkit.container.Container) for section in ("packages", "dev-packages"): table_data = parsed.get(section, {}) if not table_data: continue - if isinstance(parsed, tomlkit.container.Container): + if is_tomlkit_parsed: convert_tomlkit_table(table_data) else: convert_toml_table(table_data) From aadb24b9df3f39b6b56263cf4c45e205cc3d2e6f Mon Sep 17 00:00:00 2001 From: Frost Ming Date: Fri, 7 Dec 2018 09:00:04 +0800 Subject: [PATCH 5/5] Move to install test suite --- tests/integration/test_install_basic.py | 22 ++++++++++++++++++++++ tests/integration/test_project.py | 24 +----------------------- 2 files changed, 23 insertions(+), 23 deletions(-) diff --git a/tests/integration/test_install_basic.py b/tests/integration/test_install_basic.py index 317351a0..f858f8ac 100644 --- a/tests/integration/test_install_basic.py +++ b/tests/integration/test_install_basic.py @@ -446,3 +446,25 @@ def test_install_package_with_dots(PipenvInstance, pypi): c = p.pipenv("install backports.html") assert c.ok assert "backports.html" in p.pipfile["packages"] + + +@pytest.mark.install +def test_rewrite_outline_table(PipenvInstance, pypi): + with PipenvInstance(pypi=pypi, chdir=True) as p: + with open(p.pipfile_path, 'w') as f: + contents = """ +[packages] +six = {version = "*", editable = true} + +[packages.requests] +version = "*" + """.strip() + f.write(contents) + c = p.pipenv("install -e click") + assert c.return_code == 0 + with open(p.pipfile_path) as f: + contents = f.read() + assert "[packages.requests]" not in contents + assert 'six = {version = "*", editable = true}' in contents + assert 'requests = {version = "*"}' in contents + assert 'click = {' in contents diff --git a/tests/integration/test_project.py b/tests/integration/test_project.py index bec98d59..6a4a858a 100644 --- a/tests/integration/test_project.py +++ b/tests/integration/test_project.py @@ -148,29 +148,6 @@ six = {{version = "*", index = "pypi"}} assert c.return_code == 0 -@pytest.mark.install -@pytest.mark.project -def test_rewrite_outline_table(PipenvInstance, pypi): - with PipenvInstance(pypi=pypi, chdir=True) as p: - with open(p.pipfile_path, 'w') as f: - contents = """ -[packages] -six = {version = "*", editable = true} - -[packages.requests] -version = "*" - """.strip() - f.write(contents) - c = p.pipenv("install -e click") - assert c.return_code == 0 - with open(p.pipfile_path) as f: - contents = f.read() - assert "[packages.requests]" not in contents - assert 'six = {version = "*", editable = true}' in contents - assert 'requests = {version = "*"}' in contents - assert 'click = {' in contents - - @pytest.mark.install @pytest.mark.project def test_include_editable_packages(PipenvInstance, pypi, testsroot, pathlib_tmpdir): @@ -190,6 +167,7 @@ def test_include_editable_packages(PipenvInstance, pypi, testsroot, pathlib_tmpd @pytest.mark.project +@pytest.mark.virtualenv def test_run_in_virtualenv(PipenvInstance, pypi, virtualenv): with PipenvInstance(chdir=True, pypi=pypi) as p: os.environ.pop("PIPENV_IGNORE_VIRTUALENVS", None)