Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions keyring/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,15 @@ def __init__(self):
'service',
nargs="?",
)
try:
default_user = getpass.getuser()
except (OSError, KeyError, ModuleNotFoundError):
# on recent Python versions (>=3.13), `OSError` should suffice
default_user = None
self.parser.add_argument(
'username',
nargs="?",
default=default_user,
)
completion.install(self.parser)

Expand Down
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ type = [
# upstream
"pytest-mypy >= 1.0.1",

## workaround for python/mypy#20454
"mypy < 1.19; python_implementation == 'PyPy'",

# local
"pygobject-stubs",
"shtab", # Optional install for completion
Expand Down
14 changes: 14 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import getpass
import itertools
import os
import sys
from unittest import mock

Expand Down Expand Up @@ -42,6 +43,19 @@ def mocked_get_credential():
yield get_credential


def test_set_no_user(monkeypatch, mocked_set):
for name in ('LOGNAME', 'USER', 'LNAME', 'USERNAME'):
monkeypatch.delitem(os.environ, name, raising=False)
monkeypatch.setattr(os, 'getuid', lambda: -1, raising=False)
tool = cli.CommandLineTool()
tool.service = 'svc'
tool.username = 'usr'
monkeypatch.setattr(sys.stdin, 'isatty', lambda: True)
monkeypatch.setattr(getpass, 'getpass', PasswordEmitter('foo123'))
tool.do_set()
mocked_set.assert_called_once_with('svc', 'usr', 'foo123')


def test_set_interactive(monkeypatch, mocked_set):
tool = cli.CommandLineTool()
tool.service = 'svc'
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ deps =
diff-cover
commands =
pytest {posargs} --cov-report xml
diff-cover coverage.xml --compare-branch=origin/main --html-report diffcov.html
diff-cover coverage.xml --compare-branch=origin/main --format html:diffcov.html
diff-cover coverage.xml --compare-branch=origin/main --fail-under=100

[testenv:docs]
Expand Down