Skip to content

Commit 6a450dc

Browse files
committed
Updated get_prompt to handle case where only a socket is found and host is None
1 parent 977d821 commit 6a450dc

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

mycli/main.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1193,10 +1193,15 @@ def get_prompt(self, string: str) -> str:
11931193
assert sqlexecute is not None
11941194
assert sqlexecute.server_info is not None
11951195
assert sqlexecute.server_info.species is not None
1196-
host = self.login_path if self.login_path and self.login_path_as_host else sqlexecute.host
1196+
if self.login_path and self.login_path_as_host:
1197+
prompt_host = self.login_path
1198+
elif sqlexecute.host is not None:
1199+
prompt_host = sqlexecute.host
1200+
else:
1201+
prompt_host = "localhost"
11971202
now = datetime.now()
11981203
string = string.replace("\\u", sqlexecute.user or "(none)")
1199-
string = string.replace("\\h", host or "(none)")
1204+
string = string.replace("\\h", prompt_host or "(none)")
12001205
string = string.replace("\\d", sqlexecute.dbname or "(none)")
12011206
string = string.replace("\\t", sqlexecute.server_info.species.name)
12021207
string = string.replace("\\n", "\n")

test/test_main.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
from mycli.main import MyCli, cli, thanks_picker
1313
from mycli.packages.special.main import COMMANDS as SPECIAL_COMMANDS
14-
from mycli.sqlexecute import ServerInfo
14+
from mycli.sqlexecute import ServerInfo, SQLExecute
1515
from test.utils import HOST, PASSWORD, PORT, USER, dbtest, run
1616

1717
test_dir = os.path.abspath(os.path.dirname(__file__))
@@ -37,6 +37,21 @@
3737
]
3838

3939

40+
@dbtest
41+
def test_prompt_no_host_only_socket(executor):
42+
mycli = MyCli()
43+
mycli.prompt_format = "\\t \\u@\\h:\\d> "
44+
mycli.sqlexecute = SQLExecute
45+
mycli.sqlexecute.server_info = ServerInfo.from_version_string("8.0.44-0ubuntu0.24.04.1")
46+
mycli.sqlexecute.host = None
47+
mycli.sqlexecute.socket = "/var/run/mysqld/mysqld.sock"
48+
mycli.sqlexecute.user = "root"
49+
mycli.sqlexecute.dbname = "mysql"
50+
mycli.sqlexecute.port = "3306"
51+
prompt = mycli.get_prompt(mycli.prompt_format)
52+
assert prompt == "MySQL root@localhost:mysql> "
53+
54+
4055
@dbtest
4156
def test_enable_show_warnings(executor):
4257
mycli = MyCli()

0 commit comments

Comments
 (0)