Skip to content

Commit

Permalink
tests: Add coverage for add and config commands
Browse files Browse the repository at this point in the history
  • Loading branch information
Juniper-vdg committed Mar 28, 2021
1 parent 34d66ba commit 1a25310
Show file tree
Hide file tree
Showing 2 changed files with 182 additions and 0 deletions.
26 changes: 26 additions & 0 deletions tests/console/commands/test_add.py
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,32 @@ def test_add_constraint_with_extras_option(app, repo, tester):
}


def test_add_constraint_with_multiple_extras_variadic_fails(app, repo, tester):
cachy2 = get_package("cachy", "0.2.0")

cachy2.extras = {
"msgpack": [get_dependency("msgpack-python")],
"toml": [get_dependency("tomlkit")],
}
msgpack_dep = get_dependency("msgpack-python", ">=0.5 <0.6", optional=True)
cachy2.requires = [msgpack_dep]

toml_dep = get_dependency("tomlkit", ">=0.5 <0.6", optional=True)
cachy2.requires = [toml_dep]

repo.add_package(get_package("cachy", "0.1.0"))
repo.add_package(cachy2)
repo.add_package(get_package("msgpack-python", "0.5.3"))
repo.add_package(get_package("tomlkit", "0.5.5"))

with pytest.raises(ValueError) as e:
tester.execute("cachy=0.2.0 --extras msgpack toml")

expected = "You can only specify one package when using the --extras option"

assert expected == str(e.value)


def test_add_url_constraint_wheel(app, repo, tester, mocker):
p = mocker.patch("pathlib.Path.cwd")
p.return_value = Path(__file__) / ".."
Expand Down
156 changes: 156 additions & 0 deletions tests/console/commands/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,40 @@ def test_set_pypi_token(tester, auth_config_source):
assert "mytoken" == auth_config_source.config["pypi-token"]["pypi"]


def test_unset_pypi_token(tester, auth_config_source):
tester.execute("pypi-token.pypi mytoken")
tester.execute("--unset pypi-token.pypi")

assert "pypi" not in auth_config_source.config["pypi-token"]


def test_set_pypi_token_multiple_error(tester):
with pytest.raises(ValueError) as err:
tester.execute("pypi-token.pypi mytoken other")

assert "Expected only one argument (token), got 2" == str(err.value)


def test_set_http_basic(tester, auth_config_source):
tester.execute("http-basic.pypi my_username my_password")
expected_credentials = {"username": "my_username", "password": "my_password"}
assert expected_credentials == auth_config_source.config["http-basic"]["pypi"]


def test_unset_http_basic(tester, auth_config_source):
tester.execute("http-basic.pypi my_username my_password")
tester.execute("--unset http-basic.pypi")

assert "pypi" not in auth_config_source.config["http-basic"]


def test_set_http_basic_multiple_error(tester):
with pytest.raises(ValueError) as err:
tester.execute("http-basic.pypi my_username my_password other")

assert "Expected one or two arguments (username, password), got 3" == str(err.value)


def test_set_client_cert(tester, auth_config_source, mocker):
mocker.spy(ConfigSource, "__init__")

Expand All @@ -136,6 +170,24 @@ def test_set_cert(tester, auth_config_source, mocker):
assert "path/to/ca.pem" == auth_config_source.config["certificates"]["foo"]["cert"]


def test_unset_cert(tester, auth_config_source, mocker):
mocker.spy(ConfigSource, "__init__")

tester.execute("certificates.foo.cert path/to/ca.pem")
tester.execute("--unset certificates.foo.cert")

assert "cert" not in auth_config_source.config["certificates"]["foo"]


def test_set_cert_multiple_error(tester, auth_config_source, mocker):
mocker.spy(ConfigSource, "__init__")

with pytest.raises(ValueError) as err:
tester.execute("certificates.foo.cert path/to/ca.pem other")

assert "You must pass exactly 1 value" == str(err.value)


def test_config_installer_parallel(tester, command_tester_factory):
tester.execute("--local installer.parallel")
assert tester.io.fetch_output().strip() == "true"
Expand All @@ -154,3 +206,107 @@ def test_config_installer_parallel(tester, command_tester_factory):
"install"
)._command._installer._executor._max_workers
assert workers == 1


def test_cannot_set_and_unset_simultaneously(tester):
with pytest.raises(RuntimeError) as err:
tester.execute("--unset foo bar")

assert "You can not combine a setting value with --unset" == str(err.value)


def test_cannot_get_missing_setting(tester):
with pytest.raises(ValueError) as err:
tester.execute("missing_config_value")

assert "There is no missing_config_value setting." == str(err.value)


def test_config_add_repo(tester, config):
tester.execute("repositories.foo https://bar.com")

assert "foo" in config.config_source.config["repositories"]
assert (
"https://bar.com" == config.config_source.config["repositories"]["foo"]["url"]
)


def test_config_get_repo(tester):
tester.execute("repositories.foo https://bar.com")
tester.execute("repositories.foo.url")

expected = """https://bar.com
"""

assert expected == tester.io.fetch_output()


def test_config_get_repo_multiple(tester):
tester.execute("repositories.foo https://bar.com")
tester.execute("repositories.baz https://qux.com")
tester.execute("repositories")

expected = """{'foo': {'url': 'https://bar.com'}, 'baz': {'url': 'https://qux.com'}}
"""

assert expected == tester.io.fetch_output()


def test_config_remove_repo(tester, config):
tester.execute("repositories.foo https://bar.com")
tester.execute("--unset repositories.foo")

assert {} == config.config_source.config["repositories"]


def test_config_cannot_add_repo_with_multiple_urls(tester):
with pytest.raises(ValueError) as err:
tester.execute("repositories.foo https://bar.com https://baz.com")

assert (
"You must pass the url. Example: poetry config repositories.foo https://bar.com"
== str(err.value)
)


def test_config_repos_cannot_remove_entire_section(tester):
with pytest.raises(ValueError) as err:
tester.execute("repo https://bar.com")

assert "You cannot remove the [repositories] section" == str(err.value)


def test_config_repos_cannot_get_undefined(tester):
with pytest.raises(ValueError) as err:
tester.execute("repositories.missing_repo")

assert "There is no missing_repo repository defined" == str(err.value)


def test_config_repos_cannot_unset_missing(tester):
with pytest.raises(ValueError) as err:
tester.execute("--unset repo.missing_repo")

assert "There is no missing_repo repository defined" == str(err.value)


def test_list_repos(tester, config, config_cache_dir):
tester.execute("repositories.foo https://bar.com")
tester.execute("--list")

expected = """cache-dir = {cache}
experimental.new-installer = true
installer.parallel = true
repositories.foo.url = "https://bar.com"
virtualenvs.create = true
virtualenvs.in-project = null
virtualenvs.options.always-copy = false
virtualenvs.options.system-site-packages = false
virtualenvs.path = {path} # {virtualenvs}
""".format(
cache=json.dumps(str(config_cache_dir)),
path=json.dumps(os.path.join("{cache-dir}", "virtualenvs")),
virtualenvs=str(config_cache_dir / "virtualenvs"),
)

assert expected == tester.io.fetch_output()

0 comments on commit 1a25310

Please sign in to comment.