diff --git a/logfire/_internal/cli.py b/logfire/_internal/cli.py index 048e25474..4287d071c 100644 --- a/logfire/_internal/cli.py +++ b/logfire/_internal/cli.py @@ -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 @@ -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' ) diff --git a/tests/test_cli.py b/tests/test_cli.py index 0effa0344..bacdad585 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -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) @@ -395,13 +394,19 @@ 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) @@ -409,11 +414,11 @@ def test_projects_list_no_project(default_credentials: Path) -> None: 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: