33import pytest
44
55from poetry .utils ._compat import Path
6+ from poetry .utils ._compat import decode
67from tests .helpers import get_package
78
89
10+ @pytest .fixture
11+ def source_dir (tmp_path ): # type: (Path) -> Path
12+ yield Path (tmp_path .as_posix ())
13+
14+
915@pytest .fixture (autouse = True )
10- def patches (mocker , mocked_open_files ):
11- mocked_open_files .append ("pyproject.toml" )
16+ def patches (mocker , source_dir ):
1217 patch = mocker .patch ("poetry.utils._compat.Path.cwd" )
13- patch .return_value = Path ( __file__ ). parent
18+ patch .return_value = source_dir
1419
1520
1621@pytest .fixture
1722def tester (command_tester_factory ):
1823 return command_tester_factory ("init" )
1924
2025
21- def test_basic_interactive (tester ):
22- inputs = [
23- "my-package" , # Package name
24- "1.2.3" , # Version
25- "This is a description" , # Description
26- "n" , # Author
27- "MIT" , # License
28- "~2.7 || ^3.6" , # Python
29- "n" , # Interactive packages
30- "n" , # Interactive dev packages
31- "\n " , # Generate
32- ]
33- tester .execute (inputs = "\n " .join (inputs ))
26+ @pytest .fixture
27+ def init_basic_inputs ():
28+ return "\n " .join (
29+ [
30+ "my-package" , # Package name
31+ "1.2.3" , # Version
32+ "This is a description" , # Description
33+ "n" , # Author
34+ "MIT" , # License
35+ "~2.7 || ^3.6" , # Python
36+ "n" , # Interactive packages
37+ "n" , # Interactive dev packages
38+ "\n " , # Generate
39+ ]
40+ )
3441
35- expected = """\
42+
43+ @pytest .fixture ()
44+ def init_basic_toml ():
45+ return """\
3646 [tool.poetry]
3747name = "my-package"
3848version = "1.2.3"
@@ -46,7 +56,10 @@ def test_basic_interactive(tester):
4656[tool.poetry.dev-dependencies]
4757"""
4858
49- assert expected in tester .io .fetch_output ()
59+
60+ def test_basic_interactive (tester , init_basic_inputs , init_basic_toml ):
61+ tester .execute (inputs = init_basic_inputs )
62+ assert init_basic_toml in tester .io .fetch_output ()
5063
5164
5265def test_interactive_with_dependencies (tester , repo ):
@@ -556,3 +569,36 @@ def test_predefined_and_interactive_dev_dependencies(tester, repo):
556569 assert expected in output
557570 assert 'pytest-requests = "^0.2.0"' in output
558571 assert 'pytest = "^3.6.0"' in output
572+
573+
574+ def test_init_existing_pyproject_simple (
575+ tester , source_dir , init_basic_inputs , init_basic_toml
576+ ):
577+ pyproject_file = source_dir / "pyproject.toml"
578+ existing_section = """
579+ [tool.black]
580+ line-length = 88
581+ """
582+ pyproject_file .write_text (decode (existing_section ))
583+ tester .execute (inputs = init_basic_inputs )
584+ assert (
585+ "{}\n {}" .format (existing_section , init_basic_toml ) in pyproject_file .read_text ()
586+ )
587+
588+
589+ def test_init_existing_pyproject_with_build_system_fails (
590+ tester , source_dir , init_basic_inputs
591+ ):
592+ pyproject_file = source_dir / "pyproject.toml"
593+ existing_section = """
594+ [build-system]
595+ requires = ["setuptools >= 40.6.0", "wheel"]
596+ build-backend = "setuptools.build_meta"
597+ """
598+ pyproject_file .write_text (decode (existing_section ))
599+ tester .execute (inputs = init_basic_inputs )
600+ assert (
601+ tester .io .fetch_output ().strip ()
602+ == "A pyproject.toml file with a defined build-system already exists."
603+ )
604+ assert "{}" .format (existing_section ) in pyproject_file .read_text ()
0 commit comments