Skip to content

Commit

Permalink
Drop rich from logfire projects list (#750)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kludex authored Dec 30, 2024
1 parent 2379a05 commit 1fd9642
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 19 deletions.
14 changes: 5 additions & 9 deletions logfire/_internal/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import requests
from opentelemetry import trace
from rich.console import Console
from rich.table import Table

from logfire.exceptions import LogfireConfigError
from logfire.propagate import ContextCarrier, get_context
Expand Down Expand Up @@ -244,18 +243,15 @@ def parse_auth(args: argparse.Namespace) -> None:
def parse_list_projects(args: argparse.Namespace) -> None:
"""List user projects."""
logfire_url = args.logfire_url
console = Console(file=sys.stderr)
projects = LogfireCredentials.get_user_projects(session=args._session, logfire_api_url=logfire_url)
if projects:
table = Table()
table.add_column('Organization')
table.add_column('Project')
sys.stderr.write(f' {"Organization":<18} | Project\n')
sys.stderr.write(f'{"-" * 20}|{"-" * 18}\n')
for project in projects:
table.add_row(project['organization_name'], project['project_name'])
console.print(table)
sys.stderr.write(f' {project["organization_name"]:<18} | {project["project_name"]}\n')
else:
console.print(
'No projects found for the current user. You can create a new project with `logfire projects new`'
sys.stderr.write(
'No projects found for the current user. You can create a new project with `logfire projects new`\n'
)


Expand Down
25 changes: 15 additions & 10 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,10 +381,9 @@ def test_projects_help(capsys: pytest.CaptureFixture[str]) -> None:
assert capsys.readouterr().out.splitlines()[0] == 'usage: logfire projects [-h] {list,new,use} ...'


def test_projects_list(default_credentials: Path) -> None:
def test_projects_list(default_credentials: Path, capsys: pytest.CaptureFixture[str]) -> None:
with ExitStack() as stack:
stack.enter_context(patch('logfire._internal.config.LogfireCredentials._get_user_token', return_value=''))
table_add_row = stack.enter_context(patch('logfire._internal.cli.Table.add_row'))

m = requests_mock.Mocker()
stack.enter_context(m)
Expand All @@ -395,25 +394,31 @@ def test_projects_list(default_credentials: Path) -> None:

main(['projects', 'list'])

assert "call('test-org', 'test-pr')" == str(table_add_row.mock_calls[0])
output = capsys.readouterr().err
assert output.splitlines() == snapshot(
[
' Organization | Project',
'--------------------|------------------',
' test-org | test-pr',
]
)


def test_projects_list_no_project(default_credentials: Path) -> None:
def test_projects_list_no_project(default_credentials: Path, capsys: pytest.CaptureFixture[str]) -> None:
with ExitStack() as stack:
stack.enter_context(patch('logfire._internal.config.LogfireCredentials._get_user_token', return_value=''))
console = stack.enter_context(patch('logfire._internal.cli.Console'))

m = requests_mock.Mocker()
stack.enter_context(m)
m.get('https://logfire-api.pydantic.dev/v1/projects/', json=[])

main(['projects', 'list'])

console_calls = [re.sub(r'^call(\(\).)?', '', str(call)) for call in console.mock_calls]
assert console_calls == [
IsStr(regex=r'^\(file=.*'),
"print('No projects found for the current user. You can create a new project with `logfire projects new`')",
]
output = capsys.readouterr().err
assert (
output
== 'No projects found for the current user. You can create a new project with `logfire projects new`\n'
)


def test_projects_new_with_project_name_and_org(tmp_dir_cwd: Path, default_credentials: Path) -> None:
Expand Down

0 comments on commit 1fd9642

Please sign in to comment.