@@ -823,6 +823,59 @@ def test_projects_use(tmp_dir_cwd: Path, default_credentials: Path) -> None:
823823 }
824824
825825
826+ def test_projects_use_without_project_name (tmp_dir_cwd : Path , default_credentials : Path ) -> None :
827+ with ExitStack () as stack :
828+ stack .enter_context (patch ('logfire._internal.config.LogfireCredentials._get_user_token' , return_value = '' ))
829+ console = stack .enter_context (patch ('logfire._internal.cli.Console' ))
830+ prompt_mock = stack .enter_context (patch ('rich.prompt.Prompt.ask' , side_effect = ['1' ]))
831+
832+ m = requests_mock .Mocker ()
833+ stack .enter_context (m )
834+ m .get (
835+ 'https://logfire-api.pydantic.dev/v1/projects/' ,
836+ json = [
837+ {'organization_name' : 'fake_org' , 'project_name' : 'myproject' },
838+ {'organization_name' : 'fake_org' , 'project_name' : 'otherproject' },
839+ ],
840+ )
841+ create_project_response = {
842+ 'json' : {
843+ 'project_name' : 'myproject' ,
844+ 'token' : 'fake_token' ,
845+ 'project_url' : 'fake_project_url' ,
846+ }
847+ }
848+ m .post (
849+ 'https://logfire-api.pydantic.dev/v1/organizations/fake_org/projects/myproject/write-tokens/' ,
850+ [create_project_response ],
851+ )
852+
853+ main (['projects' , 'use' ])
854+
855+ assert prompt_mock .mock_calls == [
856+ call (
857+ (
858+ 'Please select one of the following projects by number:\n '
859+ '1. fake_org/myproject\n '
860+ '2. fake_org/otherproject\n '
861+ ),
862+ choices = ['1' , '2' ],
863+ default = '1' ,
864+ )
865+ ]
866+
867+ console_calls = [re .sub (r'^call(\(\).)?' , '' , str (call )) for call in console .mock_calls ]
868+ assert console_calls == [
869+ IsStr (regex = r'^\(file=.*' ),
870+ "print('Project configured successfully. You will be able to view it at: fake_project_url')" ,
871+ ]
872+
873+ assert json .loads ((tmp_dir_cwd / '.logfire/logfire_credentials.json' ).read_text ()) == {
874+ ** create_project_response ['json' ],
875+ 'logfire_api_url' : 'https://logfire-api.pydantic.dev' ,
876+ }
877+
878+
826879def test_projects_use_multiple (tmp_dir_cwd : Path , default_credentials : Path ) -> None :
827880 with ExitStack () as stack :
828881 stack .enter_context (patch ('logfire._internal.config.LogfireCredentials._get_user_token' , return_value = '' ))
0 commit comments