def test_explicit_multiple_argument(): command = MySecondCommand() tester = CommandTester(command) tester.execute("1 2 3") assert "1,2,3\n" == tester.io.fetch_output()
def test_add_constraint_dependencies(app, repo, installer): command = app.find("add") tester = CommandTester(command) cachy2 = get_package("cachy", "0.2.0") msgpack_dep = get_dependency("msgpack-python", ">=0.5 <0.6") cachy2.requires = [msgpack_dep] repo.add_package(get_package("cachy", "0.1.0")) repo.add_package(cachy2) repo.add_package(get_package("msgpack-python", "0.5.3")) tester.execute([("command", command.get_name()), ("name", ["cachy=0.2.0"])]) expected = """\ Updating dependencies Resolving dependencies... Package operations: 2 installs, 0 updates, 0 removals Writing lock file - Installing msgpack-python (0.5.3) - Installing cachy (0.2.0) """ assert tester.get_display(True) == expected assert len(installer.installs) == 2
def test_add_no_constraint(app, repo, installer): command = app.find("add") tester = CommandTester(command) repo.add_package(get_package("cachy", "0.1.0")) repo.add_package(get_package("cachy", "0.2.0")) tester.execute([("command", command.get_name()), ("name", ["cachy"])]) expected = """\ Using version ^0.2.0 for cachy Updating dependencies Resolving dependencies... Package operations: 1 install, 0 updates, 0 removals Writing lock file - Installing cachy (0.2.0) """ assert tester.get_display(True) == expected assert len(installer.installs) == 1 content = app.poetry.file.read()["tool"]["poetry"] assert "cachy" in content["dependencies"] assert content["dependencies"]["cachy"] == "^0.2.0"
def test_add_constraint(app, repo, installer): command = app.find("add") tester = CommandTester(command) repo.add_package(get_package("cachy", "0.1.0")) repo.add_package(get_package("cachy", "0.2.0")) tester.execute([("command", command.get_name()), ("name", ["cachy=0.1.0"])]) expected = """\ Updating dependencies Resolving dependencies... Package operations: 1 install, 0 updates, 0 removals Writing lock file - Installing cachy (0.1.0) """ assert tester.get_display(True) == expected assert len(installer.installs) == 1
def test_add_git_constraint_with_poetry(app, repo, installer): command = app.find("add") tester = CommandTester(command) repo.add_package(get_package("pendulum", "1.4.4")) tester.execute( [ ("command", command.get_name()), ("name", ["demo"]), ("--git", "https://github.com/demo/pyproject-demo.git"), ] ) expected = """\ Updating dependencies Resolving dependencies... Package operations: 2 installs, 0 updates, 0 removals Writing lock file - Installing pendulum (1.4.4) - Installing demo (0.1.2 9cf87a2) """ assert tester.get_display(True) == expected assert len(installer.installs) == 2
def test_show_outdated(app, poetry, installed, repo): command = app.find("show") tester = CommandTester(command) cachy_010 = get_package("cachy", "0.1.0") cachy_010.description = "Cachy package" cachy_020 = get_package("cachy", "0.2.0") cachy_020.description = "Cachy package" pendulum_200 = get_package("pendulum", "2.0.0") pendulum_200.description = "Pendulum package" installed.add_package(cachy_010) installed.add_package(pendulum_200) repo.add_package(cachy_010) repo.add_package(cachy_020) repo.add_package(pendulum_200) poetry.locker.mock_lock_data( { "package": [ { "name": "cachy", "version": "0.1.0", "description": "Cachy package", "category": "main", "optional": False, "platform": "*", "python-versions": "*", "checksum": [], }, { "name": "pendulum", "version": "2.0.0", "description": "Pendulum package", "category": "main", "optional": False, "platform": "*", "python-versions": "*", "checksum": [], }, ], "metadata": { "python-versions": "*", "platform": "*", "content-hash": "123456789", "hashes": {"cachy": [], "pendulum": []}, }, } ) tester.execute([("command", command.get_name()), ("--outdated", True)]) expected = """\ cachy 0.1.0 0.2.0 Cachy package """ assert tester.get_display(True) == expected
def test_execute_for_application_command(self): application = Application() tester = CommandTester(application.get('help')) tester.execute([('command_name', 'list')]) self.assertRegex( tester.get_display(), 'list \[--raw\] \[namespace\]' )
def test_overwrite(): command = MyCommand() tester = CommandTester(command) tester.execute("overwrite", decorated=True) expected = "Processing...{}Done! {}".format("\x08" * 13, "\x08" * 8) assert expected == tester.io.fetch_output()
def test_run_non_interactive(self): tester = CommandTester(TestCommand()) tester.execute([], {'interactive': False}) self.assertEqual( 'execute called\n', tester.get_display() )
def test_execute_for_command(self): command = HelpCommand() tester = CommandTester(command) command.set_command(ListCommand()) tester.execute([]) self.assertRegex( tester.get_display(), 'list \[--raw\] \[namespace\]' )
def test_execute_for_command_alias(self): command = HelpCommand() command.set_application(Application()) tester = CommandTester(command) tester.execute([('command_name', 'li')]) self.assertRegex( tester.get_display(), 'list \[--raw\] \[namespace\]' )
def test_as_text(self): command = TestCommand() command.set_application(Application()) tester = CommandTester(command) tester.execute([('command', command.get_name())]) self.assertEqual( self.open_fixture('command_astext.txt'), command.as_text() )
def test_about(app): command = app.find("check") tester = CommandTester(command) tester.execute([("command", command.get_name())]) expected = """\ All set! """ assert tester.get_display(True) == expected
def test_show_basic_with_not_installed_packages_decorated(app, poetry, installed): command = app.find("show") tester = CommandTester(command) cachy_010 = get_package("cachy", "0.1.0") cachy_010.description = "Cachy package" pendulum_200 = get_package("pendulum", "2.0.0") pendulum_200.description = "Pendulum package" installed.add_package(cachy_010) poetry.locker.mock_lock_data( { "package": [ { "name": "cachy", "version": "0.1.0", "description": "Cachy package", "category": "main", "optional": False, "platform": "*", "python-versions": "*", "checksum": [], }, { "name": "pendulum", "version": "2.0.0", "description": "Pendulum package", "category": "main", "optional": False, "platform": "*", "python-versions": "*", "checksum": [], }, ], "metadata": { "python-versions": "*", "platform": "*", "content-hash": "123456789", "hashes": {"cachy": [], "pendulum": []}, }, } ) tester.execute([("command", command.get_name())], {"decorated": True}) expected = """\ \033[32mcachy \033[0m \033[36m0.1.0\033[0m Cachy package \033[31mpendulum\033[0m \033[36m2.0.0\033[0m Pendulum package """ assert tester.get_display(True) == expected
def test_display_single_setting(app, config): command = app.find("config") command._config = Config(config.file) tester = CommandTester(command) tester.execute( [("command", command.get_name()), ("key", "settings.virtualenvs.create")] ) expected = """true """ assert tester.get_display(True) == expected
def test_list_displays_default_value_if_not_set(app, config): command = app.find("config") command._config = Config(config.file) tester = CommandTester(command) tester.execute([("command", command.get_name()), ("--list", True)]) expected = """settings.virtualenvs.create = true settings.virtualenvs.in-project = false settings.virtualenvs.path = "." repositories = {} """ assert tester.get_display(True) == expected
def test_about(app): command = app.find("about") tester = CommandTester(command) tester.execute([("command", command.get_name())]) expected = """\ Poetry - Package Management for Python Poetry is a dependency manager tracking local dependencies of your projects and libraries. See https://github.com/sdispater/poetry for more information. """ assert tester.get_display(True) == expected
def test_interactive_with_file_dependency(app, repo, mocker, poetry): repo.add_package(get_package("pendulum", "2.0.0")) repo.add_package(get_package("pytest", "3.6.0")) command = app.find("init") command._pool = poetry.pool mocker.patch("poetry.utils._compat.Path.open") p = mocker.patch("poetry.utils._compat.Path.cwd") p.return_value = Path(__file__).parent tester = CommandTester(command) inputs = [ "my-package", # Package name "1.2.3", # Version "This is a description", # Description "n", # Author "MIT", # License "~2.7 || ^3.6", # Python "", # Interactive packages "../../fixtures/distributions/demo-0.1.0-py2.py3-none-any.whl", # Search for package "", # Stop searching for packages "", # Interactive dev packages "pytest", # Search for package "0", "", "", "\n", # Generate ] tester.execute(inputs="\n".join(inputs)) expected = """\ [tool.poetry] name = "my-package" version = "1.2.3" description = "This is a description" authors = ["Your Name <*****@*****.**>"] license = "MIT" [tool.poetry.dependencies] python = "~2.7 || ^3.6" demo = {path = "../../fixtures/distributions/demo-0.1.0-py2.py3-none-any.whl"} [tool.poetry.dev-dependencies] pytest = "^3.6.0" """ assert expected in tester.io.fetch_output()
def test_get_prefers_explicitly_activated_non_existing_virtualenvs_over_env_var( app, tmp_dir, mocker): mocker.stopall() os.environ["VIRTUAL_ENV"] = "/environment/prefix" venv_name = EnvManager.generate_env_name("simple-project", str(app.poetry.file.parent)) current_python = sys.version_info[:3] python_minor = ".".join(str(v) for v in current_python[:2]) app.poetry.config.merge({"virtualenvs": {"path": str(tmp_dir)}}) mocker.patch( "poetry.utils.env.EnvManager._env", new_callable=mocker.PropertyMock, return_value=MockEnv( path=Path("/environment/prefix"), base=Path("/base/prefix"), version_info=current_python, is_venv=True, ), ) mocker.patch( "poetry.utils._compat.subprocess.check_output", side_effect=check_output_wrapper(Version(*current_python)), ) mocker.patch( "poetry.utils._compat.subprocess.Popen.communicate", side_effect=[("/prefix", None), ("/prefix", None), ("/prefix", None)], ) mocker.patch("poetry.utils.env.EnvManager.build_venv", side_effect=build_venv) command = app.find("env use") tester = CommandTester(command) tester.execute(python_minor) expected = """\ Creating virtualenv {} in {} Using virtualenv: {} """.format( "{}-py{}".format(venv_name, python_minor), tmp_dir, os.path.join(tmp_dir, "{}-py{}".format(venv_name, python_minor)), ) assert expected == tester.io.fetch_output()
def test_standardize_1(self): table = [["A", "B", "C"], [1, 2, 3], [4, 5, 6]] dialect = SimpleDialect(delimiter=";", quotechar="", escapechar="") tmpfname = self._build_file(table, dialect) application = build_application() command = application.find("standardize") tester = CommandTester(command) tester.execute(tmpfname) exp = "A,B,C\n1,2,3\n4,5,6" try: output = tester.io.fetch_output().strip() self.assertEqual(exp, output) finally: os.unlink(tmpfname)
def test_add_constraint_with_extras(app, repo, installer): command = app.find("add") tester = CommandTester(command) cachy2 = get_package("cachy", "0.2.0") cachy2.extras = {"msgpack": [get_dependency("msgpack-python")]} msgpack_dep = get_dependency("msgpack-python", ">=0.5 <0.6", optional=True) cachy2.requires = [msgpack_dep] repo.add_package(get_package("cachy", "0.1.0")) repo.add_package(cachy2) repo.add_package(get_package("msgpack-python", "0.5.3")) tester.execute( [ ("command", command.get_name()), ("name", ["cachy=0.2.0"]), ("--extras", ["msgpack"]), ] ) expected = """\ Updating dependencies Resolving dependencies... Package operations: 2 installs, 0 updates, 0 removals Writing lock file - Installing msgpack-python (0.5.3) - Installing cachy (0.2.0) """ assert tester.get_display(True) == expected assert len(installer.installs) == 2 content = app.poetry.file.read()["tool"]["poetry"] assert "cachy" in content["dependencies"] assert content["dependencies"]["cachy"] == { "version": "0.2.0", "extras": ["msgpack"], }
def test_activate_activates_non_existing_virtualenv_no_envs_file( app, tmp_dir, config, mocker): app.poetry._config = config if "VIRTUAL_ENV" in os.environ: del os.environ["VIRTUAL_ENV"] config.add_property("settings.virtualenvs.path", str(tmp_dir)) mocker.patch( "poetry.utils._compat.subprocess.check_output", side_effect=check_output_wrapper(), ) mocker.patch( "poetry.utils._compat.subprocess.Popen.communicate", side_effect=[("/prefix", None), ("/prefix", None)], ) m = mocker.patch("poetry.utils.env.EnvManager.build_venv", side_effect=build_venv) command = app.find("env use") tester = CommandTester(command) tester.execute("3.7") venv_name = EnvManager.generate_env_name("simple_project", str(app.poetry.file.parent)) m.assert_called_with(os.path.join(tmp_dir, "{}-py3.7".format(venv_name)), executable="python3.7") envs_file = TomlFile(Path(tmp_dir) / "envs.toml") assert envs_file.exists() envs = envs_file.read() assert envs[venv_name]["minor"] == "3.7" assert envs[venv_name]["patch"] == "3.7.1" expected = """\ Creating virtualenv {} in {} Using virtualenv: {} """.format( "{}-py3.7".format(venv_name), tmp_dir, os.path.join(tmp_dir, "{}-py3.7".format(venv_name)), ) assert expected == tester.io.fetch_output()
def test_detect_opts_2(self): table = [["A", "B", "C"], [1, 2, 3], [4, 5, 6]] dialect = SimpleDialect(delimiter=";", quotechar="", escapechar="") tmpfname = self._build_file(table, dialect) application = build_application() command = application.find("detect") tester = CommandTester(command) tester.execute(f"--num-chars 5 {tmpfname}") exp = "Detected: " + str(dialect) try: output = tester.io.fetch_output().strip() self.assertEqual(exp, output) finally: os.unlink(tmpfname)
def test_predefined_and_interactive_dependencies(app, repo, mocker, poetry): repo.add_package(get_package("pendulum", "2.0.0")) repo.add_package(get_package("pyramid", "1.10")) command = app.find("init") command._pool = poetry.pool mocker.patch("poetry.utils._compat.Path.open") p = mocker.patch("poetry.utils._compat.Path.cwd") p.return_value = Path(__file__) tester = CommandTester(command) inputs = [ "my-package", # Package name "1.2.3", # Version "This is a description", # Description "n", # Author "MIT", # License "~2.7 || ^3.6", # Python "", # Interactive packages "pyramid", # Search for package "0", # First option "", # Do not set constraint "", # Stop searching for packages "n", # Interactive dev packages "\n", # Generate ] tester.execute("--dependency pendulum", inputs="\n".join(inputs)) expected = """\ [tool.poetry] name = "my-package" version = "1.2.3" description = "This is a description" authors = ["Your Name <*****@*****.**>"] license = "MIT" [tool.poetry.dependencies] python = "~2.7 || ^3.6" """ output = tester.io.fetch_output() assert expected in output assert 'pendulum = "^2.0.0"' in output assert 'pyramid = "^1.10"' in output
def test_none_activated(app, tmp_dir): app.poetry.config.merge({"virtualenvs": {"path": str(tmp_dir)}}) venv_name = EnvManager.generate_env_name("simple-project", str(app.poetry.file.parent)) (Path(tmp_dir) / "{}-py3.7".format(venv_name)).mkdir() (Path(tmp_dir) / "{}-py3.6".format(venv_name)).mkdir() command = app.find("env list") tester = CommandTester(command) tester.execute() expected = """\ {}-py3.6 {}-py3.7 """.format(venv_name, venv_name) assert expected == tester.io.fetch_output()
def test_remove_by_name(app, tmp_dir): app.poetry.config.merge({"virtualenvs": {"path": str(tmp_dir)}}) venv_name = EnvManager.generate_env_name("simple_project", str(app.poetry.file.parent)) (Path(tmp_dir) / "{}-py3.7".format(venv_name)).mkdir() (Path(tmp_dir) / "{}-py3.6".format(venv_name)).mkdir() command = app.find("env remove") tester = CommandTester(command) tester.execute("{}-py3.6".format(venv_name)) assert not (Path(tmp_dir) / "{}-py3.6".format(venv_name)).exists() expected = "Deleted virtualenv: {}\n".format( (Path(tmp_dir) / "{}-py3.6".format(venv_name))) assert expected == tester.io.fetch_output()
def test_run_with_invalid_option(self): command = SomeCommand() tester = CommandTester(command) self.assertRaises( Exception, 'The "--bar" option does not exist.', tester.execute, [('--bar', True)] )
def test_add_git_constraint_with_poetry_bad_name(app, repo, installer): command = app.find("add") tester = CommandTester(command) repo.add_package(get_package("pendulum", "1.4.4")) with pytest.raises(RuntimeError) as e: tester.execute( [ ("command", command.get_name()), ("name", ["demox"]), ("--git", "https://github.com/demo/pyproject-demo.git"), ] ) expected = ( "The dependency name for demox does not match the actual package's name: demo" ) assert str(e.value) == expected assert len(installer.installs) == 0
def test_debug_resolve_git_dependency(app, repo): repo.add_package(get_package("pendulum", "2.0.3")) repo.add_package(get_package("cleo", "0.6.5")) command = app.find("debug resolve") tester = CommandTester(command) tester.execute("git+https://github.com/demo/demo.git") expected = """\ Resolving dependencies... Resolution results: pendulum 2.0.3 demo 0.1.2 """ assert expected == tester.io.fetch_output()
def test_add_constraint_with_extras(app, repo, installer): command = app.find('add') tester = CommandTester(command) cachy2 = get_package('cachy', '0.2.0') cachy2.extras = {'msgpack': [get_dependency('msgpack-python')]} msgpack_dep = get_dependency('msgpack-python', '>=0.5 <0.6', optional=True) cachy2.requires = [ msgpack_dep, ] repo.add_package(get_package('cachy', '0.1.0')) repo.add_package(cachy2) repo.add_package(get_package('msgpack-python', '0.5.3')) tester.execute([('command', command.get_name()), ('name', ['cachy=0.2.0']), ('--extras', ['msgpack'])]) expected = """\ Updating dependencies Resolving dependencies... Package operations: 2 installs, 0 updates, 0 removals Writing lock file - Installing msgpack-python (0.5.3) - Installing cachy (0.2.0) """ assert tester.get_display() == expected assert len(installer.installs) == 2 content = app.poetry.file.read(raw=True)['tool']['poetry'] assert 'cachy' in content['dependencies'] assert content['dependencies']['cachy'] == { 'version': '0.2.0', 'extras': ['msgpack'] }
def test_add_constraint_with_platform(app, repo, installer): platform = sys.platform command = app.find("add") tester = CommandTester(command) cachy2 = get_package("cachy", "0.2.0") repo.add_package(get_package("cachy", "0.1.0")) repo.add_package(cachy2) tester.execute( [ ("command", command.get_name()), ("name", ["cachy=0.2.0"]), ("--platform", platform), ] ) expected = """\ Updating dependencies Resolving dependencies... Package operations: 1 install, 0 updates, 0 removals Writing lock file - Installing cachy (0.2.0) """ assert tester.get_display(True) == expected assert len(installer.installs) == 1 content = app.poetry.file.read()["tool"]["poetry"] assert "cachy" in content["dependencies"] assert content["dependencies"]["cachy"] == { "version": "0.2.0", "platform": platform, }
def test_none_activated(app, tmp_dir, config): app.poetry._config = config config.add_property("settings.virtualenvs.path", str(tmp_dir)) venv_name = EnvManager.generate_env_name("simple_project", str(app.poetry.file.parent)) (Path(tmp_dir) / "{}-py3.7".format(venv_name)).mkdir() (Path(tmp_dir) / "{}-py3.6".format(venv_name)).mkdir() command = app.find("env list") tester = CommandTester(command) tester.execute() expected = """\ {}-py3.6 {}-py3.7 """.format(venv_name, venv_name) assert expected == tester.io.fetch_output()
def test_set_code(self): command = TestCommand() ret = command.set_code(lambda in_, out_: out_.writeln('from the code...')) self.assertEqual(ret, command) tester = CommandTester(command) tester.execute([]) self.assertEqual( 'interact called\nfrom the code...\n', tester.get_display() ) command = TestCommand() command.set_code(self.callable_method) tester = CommandTester(command) tester.execute([]) self.assertEqual( 'interact called\nfrom the code...\n', tester.get_display() )
def test_standardize_in_place_multi_noop(self): table = [["Å", "B", "C"], [1, 2, 3], [4, 5, 6]] dialects = ["excel", "excel", "excel"] tmpfnames = [self._build_file(table, D, newline="") for D in dialects] application = build_application() command = application.find("standardize") tester = CommandTester(command) retcode = tester.execute(f"-i {' '.join(tmpfnames)}") self.assertEqual(retcode, 0) exp = "\r\n".join([",".join(map(str, row)) for row in table]) + "\r\n" try: for tmpfname in tmpfnames: with open(tmpfname, "r", newline="") as fp: contents = fp.read() self.assertEqual(contents, exp) finally: any(map(os.unlink, tmpfnames))
def test_set_code(self): command = SomeCommand() ret = command.set_code(lambda c: c.line('from the code...')) self.assertEqual(ret, command) tester = CommandTester(command) tester.execute([]) self.assertEqual( 'interact called\nfrom the code...\n', tester.get_display() ) command = SomeCommand() command.set_code(self.callable_method) tester = CommandTester(command) tester.execute([]) self.assertEqual( 'interact called\nfrom the code...\n', tester.get_display() )
def test_standardize_3(self): table = [["A", "B", "C"], [1, 2, 3], [4, 5, 6]] dialect = SimpleDialect(delimiter=";", quotechar="", escapechar="") tmpfname = self._build_file(table, dialect) tmpfd, tmpoutname = tempfile.mkstemp(suffix=".csv") os.close(tmpfd) application = build_application() command = application.find("standardize") tester = CommandTester(command) tester.execute(f"-t {tmpfname}") exp = "A,1,4\nB,2,5\nC,3,6" try: output = tester.io.fetch_output().strip() self.assertEqual(exp, output) finally: os.unlink(tmpfname)
def test_remove_by_name(app, tmp_dir, config, mocker): app.poetry._config = config config.add_property("settings.virtualenvs.path", str(tmp_dir)) venv_name = EnvManager.generate_env_name("simple_project", str(app.poetry.file.parent)) (Path(tmp_dir) / "{}-py3.7".format(venv_name)).mkdir() (Path(tmp_dir) / "{}-py3.6".format(venv_name)).mkdir() command = app.find("env remove") tester = CommandTester(command) tester.execute("{}-py3.6".format(venv_name)) assert not (Path(tmp_dir) / "{}-py3.6".format(venv_name)).exists() expected = "Deleted virtualenv: {}\n".format( (Path(tmp_dir) / "{}-py3.6".format(venv_name))) assert expected == tester.io.fetch_output()
def test_standardize_1(self): table = [["A", "B", "C"], [1, 2, 3], [4, 5, 6]] dialect = SimpleDialect(delimiter=";", quotechar="", escapechar="") tmpfname = self._build_file(table, dialect) application = build_application() command = application.find("standardize") tester = CommandTester(command) tester.execute(tmpfname) # Excel format (i.e. RFC4180) *requires* CRLF crlf = "\r\n" exp = crlf.join(["A,B,C", "1,2,3", "4,5,6"]) # add line terminator of last row exp += crlf try: output = tester.io.fetch_output() self.assertEqual(exp, output) finally: os.unlink(tmpfname)
def test_get_prefers_explicitly_activated_virtualenvs_over_env_var( app, tmp_dir, mocker ): mocker.stopall() os.environ["VIRTUAL_ENV"] = "/environment/prefix" venv_name = EnvManager.generate_env_name( "simple-project", str(app.poetry.file.parent) ) current_python = sys.version_info[:3] python_minor = ".".join(str(v) for v in current_python[:2]) python_patch = ".".join(str(v) for v in current_python) app.poetry.config.merge({"virtualenvs": {"path": str(tmp_dir)}}) (Path(tmp_dir) / "{}-py{}".format(venv_name, python_minor)).mkdir() envs_file = TomlFile(Path(tmp_dir) / "envs.toml") doc = tomlkit.document() doc[venv_name] = {"minor": python_minor, "patch": python_patch} envs_file.write(doc) mocker.patch( "poetry.utils._compat.subprocess.check_output", side_effect=check_output_wrapper(Version(*current_python)), ) mocker.patch( "poetry.utils._compat.subprocess.Popen.communicate", side_effect=[("/prefix", None), ("/prefix", None), ("/prefix", None)], ) command = app.find("env use") tester = CommandTester(command) tester.execute(python_minor) expected = """\ Using virtualenv: {} """.format( os.path.join(tmp_dir, "{}-py{}".format(venv_name, python_minor)) ) assert expected == tester.io.fetch_output()
def test_predefined_dev_dependency(app, repo, mocker, poetry): repo.add_package(get_package("pytest", "3.6.0")) command = app.find("init") command._pool = poetry.pool mocker.patch("poetry.utils._compat.Path.open") p = mocker.patch("poetry.utils._compat.Path.cwd") p.return_value = Path(__file__) tester = CommandTester(command) inputs = [ "my-package", # Package name "1.2.3", # Version "This is a description", # Description "n", # Author "MIT", # License "~2.7 || ^3.6", # Python "n", # Interactive packages "n", # Interactive dev packages "\n", # Generate ] tester.execute("--dev-dependency pytest", inputs="\n".join(inputs)) expected = """\ [tool.poetry] name = "my-package" version = "1.2.3" description = "This is a description" authors = ["Your Name <*****@*****.**>"] license = "MIT" [tool.poetry.dependencies] python = "~2.7 || ^3.6" [tool.poetry.dev-dependencies] pytest = "^3.6.0" """ assert expected in tester.io.fetch_output()
def test_add_url_constraint_wheel_with_extras(app, repo, installer, mocker): command = app.find("add") tester = CommandTester(command) repo.add_package(get_package("pendulum", "1.4.4")) repo.add_package(get_package("cleo", "0.6.5")) repo.add_package(get_package("tomlkit", "0.5.5")) tester.execute( "https://python-poetry.org/distributions/demo-0.1.0-py2.py3-none-any.whl[foo,bar]" ) expected = """\ Updating dependencies Resolving dependencies... Writing lock file Package operations: 4 installs, 0 updates, 0 removals - Installing cleo (0.6.5) - Installing pendulum (1.4.4) - Installing tomlkit (0.5.5) - Installing demo (0.1.0 https://python-poetry.org/distributions/demo-0.1.0-py2.py3-none-any.whl) """ assert expected == tester.io.fetch_output() assert len(installer.installs) == 4 content = app.poetry.file.read()["tool"]["poetry"] assert "demo" in content["dependencies"] assert content["dependencies"]["demo"] == { "url": "https://python-poetry.org/distributions/demo-0.1.0-py2.py3-none-any.whl", "extras": ["foo", "bar"], }
def test_export_fails_on_invalid_format(app, repo): repo.add_package(get_package("foo", "1.0.0")) command = app.find("lock") tester = CommandTester(command) tester.execute() assert app.poetry.locker.lock.exists() command = app.find("export") tester = CommandTester(command) with pytest.raises(ValueError): tester.execute("--format invalid")
def test_add_git_constraint(app, repo, installer): command = app.find("add") tester = CommandTester(command) repo.add_package(get_package("pendulum", "1.4.4")) repo.add_package(get_package("cleo", "0.6.5")) tester.execute( [ ("command", command.get_name()), ("name", ["demo"]), ("--git", "https://github.com/demo/demo.git"), ] ) expected = """\ Updating dependencies Resolving dependencies... Package operations: 2 installs, 0 updates, 0 removals Writing lock file - Installing pendulum (1.4.4) - Installing demo (0.1.2 9cf87a2) """ assert tester.get_display(True) == expected assert len(installer.installs) == 2 content = app.poetry.file.read()["tool"]["poetry"] assert "demo" in content["dependencies"] assert content["dependencies"]["demo"] == { "git": "https://github.com/demo/demo.git" }
def test_show_outdated_with_only_up_to_date_packages(app, poetry, installed, repo): command = app.find("show") tester = CommandTester(command) cachy_020 = get_package("cachy", "0.2.0") cachy_020.description = "Cachy package" installed.add_package(cachy_020) repo.add_package(cachy_020) poetry.locker.mock_lock_data({ "package": [ { "name": "cachy", "version": "0.2.0", "description": "Cachy package", "category": "main", "optional": False, "platform": "*", "python-versions": "*", "checksum": [], }, ], "metadata": { "python-versions": "*", "platform": "*", "content-hash": "123456789", "hashes": { "cachy": [] }, }, }) tester.execute("--outdated") expected = "" assert expected == tester.io.fetch_output()
def test_standardize_2(self): table = [["A", "B", "C"], [1, 2, 3], [4, 5, 6]] dialect = SimpleDialect(delimiter=";", quotechar="", escapechar="") tmpfname = self._build_file(table, dialect) tmpfd, tmpoutname = tempfile.mkstemp(suffix=".csv") os.close(tmpfd) application = build_application() command = application.find("standardize") tester = CommandTester(command) tester.execute(f"-o {tmpoutname} {tmpfname}") exp = "A,B,C\n1,2,3\n4,5,6\n" with open(tmpoutname, "r") as fp: output = fp.read() try: self.assertEqual(exp, output) finally: os.unlink(tmpfname) os.unlink(tmpoutname)
def test_debug_resolve_git_dependency(app, repo): repo.add_package(get_package("pendulum", "2.0.3")) repo.add_package(get_package("cleo", "0.6.5")) command = app.find("debug:resolve") tester = CommandTester(command) tester.execute([ ("command", command.get_name()), ("package", ["git+https://github.com/demo/demo.git"]), ]) expected = """\ Resolving dependencies... Resolution results: - pendulum (2.0.3) - demo (0.1.2) """ assert tester.get_display(True) == expected
def test_add_file_constraint_sdist(app, repo, installer): command = app.find("add") tester = CommandTester(command) repo.add_package(get_package("pendulum", "1.4.4")) tester.execute( [ ("command", command.get_name()), ("name", ["demo"]), ("--path", "../distributions/demo-0.1.0.tar.gz"), ] ) expected = """\ Updating dependencies Resolving dependencies... Package operations: 2 installs, 0 updates, 0 removals Writing lock file - Installing pendulum (1.4.4) - Installing demo (0.1.0 ../distributions/demo-0.1.0.tar.gz) """ assert tester.get_display(True) == expected assert len(installer.installs) == 2 content = app.poetry.file.read()["tool"]["poetry"] assert "demo" in content["dependencies"] assert content["dependencies"]["demo"] == { "path": "../distributions/demo-0.1.0.tar.gz" }
def test_add_file_constraint_sdist(app, repo, installer): command = app.find("add") tester = CommandTester(command) repo.add_package(get_package("pendulum", "1.4.4")) tester.execute( [ ("command", command.get_name()), ("name", ["demo"]), ("--path", "../distributions/demo-0.1.0.tar.gz"), ] ) expected = """\ Updating dependencies Resolving dependencies... Package operations: 2 installs, 0 updates, 0 removals Writing lock file - Installing pendulum (1.4.4) - Installing demo (0.1.0) """ assert tester.get_display(True) == expected assert len(installer.installs) == 2 content = app.poetry.file.read()["tool"]["poetry"] assert "demo" in content["dependencies"] assert content["dependencies"]["demo"] == { "path": "../distributions/demo-0.1.0.tar.gz" }
def test_interactive_with_dependencies(app, repo, mocker, poetry): repo.add_package(get_package("pendulum", "2.0.0")) repo.add_package(get_package("pytest", "3.6.0")) command = app.find("init") command._pool = poetry.pool mocker.patch("poetry.utils._compat.Path.open") p = mocker.patch("poetry.utils._compat.Path.cwd") p.return_value = Path(__file__).parent tester = CommandTester(command) tester.set_inputs( [ "my-package", # Package name "1.2.3", # Version "This is a description", # Description "n", # Author "MIT", # License "~2.7 || ^3.6", # Python "", # Interactive packages "pendulum", # Search for package "0", # First option "", # Do not set constraint "", # Stop searching for packages "", # Interactive dev packages "pytest", # Search for package "0", "", "", "\n", # Generate ] ) tester.execute([("command", command.name)]) output = tester.get_display(True) expected = """\ [tool.poetry] name = "my-package" version = "1.2.3" description = "This is a description" authors = ["Your Name <*****@*****.**>"] license = "MIT" [tool.poetry.dependencies] python = "~2.7 || ^3.6" pendulum = "^2.0" [tool.poetry.dev-dependencies] pytest = "^3.6" """ assert expected in output
def test_basic_interactive(app, mocker, poetry): command = app.find("init") command._pool = poetry.pool mocker.patch("poetry.utils._compat.Path.open") p = mocker.patch("poetry.utils._compat.Path.cwd") p.return_value = Path(__file__) tester = CommandTester(command) tester.set_inputs( [ "my-package", # Package name "1.2.3", # Version "This is a description", # Description "n", # Author "MIT", # License "~2.7 || ^3.6", # Python "n", # Interactive packages "n", # Interactive dev packages "\n", # Generate ] ) tester.execute([("command", command.name)]) output = tester.get_display(True) expected = """\ [tool.poetry] name = "my-package" version = "1.2.3" description = "This is a description" authors = ["Your Name <*****@*****.**>"] license = "MIT" [tool.poetry.dependencies] python = "~2.7 || ^3.6" [tool.poetry.dev-dependencies] """ assert expected in output